From 35ce89c63a103acbd35d029efbb3aea8fc9e144d Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Tue, 19 Sep 2017 02:22:59 +0200 Subject: [PATCH 1/3] adding `isTruelyInvalid` property --- addon/-private/internal-result-object.js | 1 + addon/-private/result.js | 7 +++++++ addon/validations/factory.js | 1 + addon/validations/result-collection.js | 16 ++++++++++++++++ addon/validators/dependent.js | 2 +- .../validations/factory-general-test.js | 12 ++++++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/addon/-private/internal-result-object.js b/addon/-private/internal-result-object.js index e5a73161..54092ce4 100644 --- a/addon/-private/internal-result-object.js +++ b/addon/-private/internal-result-object.js @@ -50,6 +50,7 @@ export default Ember.Object.extend({ isInvalid: not('isValid'), isNotValidating: not('isValidating'), isTruelyValid: and('isNotValidating', 'isValid'), + isTruelyInvalid: and('isNotValidating', 'isInvalid'), isAsync: computed('_promise', function() { return isPromise(get(this, '_promise')); diff --git a/addon/-private/result.js b/addon/-private/result.js index 1c641f50..312cd32e 100644 --- a/addon/-private/result.js +++ b/addon/-private/result.js @@ -111,6 +111,13 @@ const Result = Ember.Object.extend({ */ isTruelyValid: readOnly('_result.isTruelyValid'), + /** + * @property isTruelyInvalid + * @readOnly + * @type {Boolean} + */ + isTruelyInvalid: readOnly('_result.isTruelyInvalid'), + /** * @property isAsync * @readOnly diff --git a/addon/validations/factory.js b/addon/validations/factory.js index 26a5380c..795ea925 100644 --- a/addon/validations/factory.js +++ b/addon/validations/factory.js @@ -518,6 +518,7 @@ function createTopLevelPropsMixin(validatableAttrs) { 'isNotValidating', 'isInvalid', 'isTruelyValid', + 'isTruelyInvalid', 'hasWarnings', 'messages', 'message', diff --git a/addon/validations/result-collection.js b/addon/validations/result-collection.js index a6f8c84b..e256b428 100644 --- a/addon/validations/result-collection.js +++ b/addon/validations/result-collection.js @@ -110,6 +110,22 @@ export default Ember.ArrayProxy.extend({ */ isTruelyValid: isEvery('content', 'isTruelyValid', true, true).readOnly(), + /** + * Will be true only if isValid is `false` and isValidating is `false` + * + * ```javascript + * // Examples + * get(user, 'validations.isTruelyInvalid') + * get(user, 'validations.attrs.username.isTruelyInvalid') + * ``` + * + * @property isTruelyInvalid + * @default true + * @readOnly + * @type {Boolean} + */ + isTruelyInvalid: computed.not('isTruelyValid').readOnly(), + /** * Will be true is the attribute in question is not `null` or `undefined`. If the object being * validated is an Ember Data Model and you have a `defaultValue` specified, then it will use that for comparison. diff --git a/addon/validators/dependent.js b/addon/validators/dependent.js index e8fed191..e5cad93f 100755 --- a/addon/validators/dependent.js +++ b/addon/validators/dependent.js @@ -60,7 +60,7 @@ const Dependent = Base.extend({ let dependentValidations = getWithDefault(options, 'on', A()).map((dependent) => get(model, `validations.attrs.${dependent}`)); - if (!isEmpty(dependentValidations.filter((v) => !get(v, 'isTruelyValid')))) { + if (!isEmpty(dependentValidations.filter((v) => get(v, 'isTruelyInvalid')))) { return this.createErrorMessage('invalid', value, options); } diff --git a/tests/integration/validations/factory-general-test.js b/tests/integration/validations/factory-general-test.js index 10f7a81e..e3a93fb7 100644 --- a/tests/integration/validations/factory-general-test.js +++ b/tests/integration/validations/factory-general-test.js @@ -39,6 +39,7 @@ test('basic sync validation – invalid', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.firstName.isValid'), false); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -60,6 +61,7 @@ test('basic sync validation – invalid', function(assert) { assert.equal(object.get('validations.isValid'), true, 'isValid was expected to be TRUE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be TRUE'); assert.equal(object.get('validations.isTruelyValid'), true, 'isTruelyValid was expected to be TRUE'); + assert.equal(object.get('validations.isTruelyInvalid'), false, 'isTruelyInvalid was expected to be FALSE'); assert.equal(object.get('validations.attrs.firstName.isValid'), true); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -81,6 +83,7 @@ test('basic sync validation - valid', function(assert) { assert.equal(object.get('validations.isValid'), true, 'isValid was expected to be TRUE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), true, 'isTruelyValid was expected to be TRUE'); + assert.equal(object.get('validations.isTruelyInvalid'), false, 'isTruelyInvalid was expected to be FALSE'); assert.equal(object.get('validations.attrs.firstName.isValid'), true); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -99,6 +102,7 @@ test('basic sync validation - 50% invalid', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.firstName.isValid'), true); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -134,6 +138,7 @@ test('basic sync validation - API - #validation', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.firstName.isValid'), true); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -172,6 +177,7 @@ test('basic sync validation - API - #validationSync', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.firstName.isValid'), true); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -193,6 +199,7 @@ test('basic sync validation returns null', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.firstName.isValid'), false); assert.equal(object.get('validations.attrs.firstName.isValidating'), false); @@ -321,6 +328,7 @@ test('debounced validations', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be TRUE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.lastName.isValid'), false); assert.equal(object.get('validations.attrs.lastName.isValidating'), false); @@ -390,6 +398,7 @@ test('debounced validations should cleanup on object destroy', function(assert) assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be TRUE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.lastName.isValid'), false); assert.equal(object.get('validations.attrs.lastName.isValidating'), false); @@ -426,6 +435,7 @@ test('disabled validations - simple', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be TRUE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.lastName.isValid'), true); assert.equal(object.get('validations.attrs.firstName.isValid'), false); @@ -450,6 +460,7 @@ test('disabled validations - cp with dependent key', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be TRUE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.lastName.isValid'), false); assert.equal(object.get('validations.attrs.firstName.isValid'), true); @@ -926,6 +937,7 @@ test('options CP changes trigger attribute revalidation', function(assert) { assert.equal(object.get('validations.isValid'), false, 'isValid was expected to be FALSE'); assert.equal(object.get('validations.isValidating'), false, 'isValidating was expected to be FALSE'); assert.equal(object.get('validations.isTruelyValid'), false, 'isTruelyValid was expected to be FALSE'); + assert.equal(object.get('validations.isTruelyInvalid'), true, 'isTruelyInvalid was expected to be TRUE'); assert.equal(object.get('validations.attrs.firstName.isValid'), false); assert.equal(object.get('validations.attrs.firstName.message'), 'First Name is too short (minimum is 6 characters)'); From c767cb8be4a60b5ba0483355407164bc0239940d Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Tue, 19 Sep 2017 21:13:25 +0200 Subject: [PATCH 2/3] adding requested changes --- addon/validations/result-collection.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addon/validations/result-collection.js b/addon/validations/result-collection.js index e256b428..1358ab1f 100644 --- a/addon/validations/result-collection.js +++ b/addon/validations/result-collection.js @@ -120,11 +120,11 @@ export default Ember.ArrayProxy.extend({ * ``` * * @property isTruelyInvalid - * @default true + * @default false * @readOnly * @type {Boolean} */ - isTruelyInvalid: computed.not('isTruelyValid').readOnly(), + isTruelyInvalid: isAny('content', 'isTruelyInvalid', true, false).readOnly() /** * Will be true is the attribute in question is not `null` or `undefined`. If the object being From ca692aed5ebc982e8e235c92f9ace8a7af45cad8 Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Tue, 19 Sep 2017 21:16:13 +0200 Subject: [PATCH 3/3] Update result-collection.js --- addon/validations/result-collection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/validations/result-collection.js b/addon/validations/result-collection.js index 1358ab1f..0c832da9 100644 --- a/addon/validations/result-collection.js +++ b/addon/validations/result-collection.js @@ -124,7 +124,7 @@ export default Ember.ArrayProxy.extend({ * @readOnly * @type {Boolean} */ - isTruelyInvalid: isAny('content', 'isTruelyInvalid', true, false).readOnly() + isTruelyInvalid: isAny('content', 'isTruelyInvalid', true, false).readOnly(), /** * Will be true is the attribute in question is not `null` or `undefined`. If the object being