From 16e65b2b5a0bdcaa7e99549563a7a89e3bb92e2c Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 3 Oct 2015 00:23:36 +0200 Subject: [PATCH] Validate also arrays --- src/path-observer.js | 14 ++++++++++++-- src/validation-property.js | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/path-observer.js b/src/path-observer.js index b25866b6..684460d4 100644 --- a/src/path-observer.js +++ b/src/path-observer.js @@ -36,7 +36,12 @@ export class PathObserver { let subscription; let currentValue; if (!observer) { - observer = this.observerLocator.getObserver(currentSubject, currentPath); + if (Array.isArray(currentSubject[currentPath])) { + observer = this.observerLocator.getArrayObserver(currentSubject[currentPath]); + } + else { + observer = this.observerLocator.getObserver(currentSubject, currentPath); + } this.observers.push(observer); subscription = observer.subscribe((newValue, oldValue) => { this.observeParts(observer.propertyName); @@ -67,7 +72,12 @@ export class PathObserver { getObserver() { if (this.path.length === 1) { this.subject[this.path[0]]; //binding issue with @bindable properties, see: https://github.com/aurelia/binding/issues/89 - return this.observerLocator.getObserver(this.subject, this.path[0]); + if (Array.isArray(this.subject[this.path[0]])) { + observer = this.observerLocator.getArrayObserver(this.subject[this.path[0]]); + } + else { + observer = this.observerLocator.getObserver(this.subject, this.path[0]); + } } return this; } diff --git a/src/validation-property.js b/src/validation-property.js index fd2519ca..5a1e79cf 100644 --- a/src/validation-property.js +++ b/src/validation-property.js @@ -59,11 +59,29 @@ export class ValidationProperty { */ validate(newValue, shouldBeDirty, forceExecution) { if ((!this.propertyResult.isDirty && shouldBeDirty) || this.latestValue !== newValue || forceExecution) { - this.latestValue = newValue; + this.latestValue = Array.isArray(newValue) ? newValue.slice(0) : newValue; return this.config.locale().then((locale) => { return this.collectionOfValidationRules.validate(newValue, locale) .then((validationResponse) => { - if (this.latestValue === validationResponse.latestValue) { + let equal = (function equal(var1, var2) { + if (typeof var1 === 'object' && var2) { + if (Object.keys(var1).length == Object.keys(var2).length) { + for (let i in var1) { + if (var1.hasOwnProperty(i)) { + return equal(var1[i], var2[i]); + } + } + return true; // the object/array must be empty + } + else { + return false; + } + } + else { + return var1 == var2; + } + })(this.latestValue, validationResponse.latestValue); + if (equal) { this.propertyResult.setValidity(validationResponse, shouldBeDirty); } return validationResponse.isValid;