From da359f08e5b1b0a223cb5078c73f09bbccd86320 Mon Sep 17 00:00:00 2001 From: frykten Date: Thu, 14 Oct 2021 15:18:36 +0200 Subject: [PATCH 1/4] Check isEmpty for validators using ember-validators --- tests/unit/validators/confirmation-test.js | 4 +++- tests/unit/validators/exclusion-test.js | 4 +++- tests/unit/validators/format-test.js | 4 +++- tests/unit/validators/inclusion-test.js | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/unit/validators/confirmation-test.js b/tests/unit/validators/confirmation-test.js index 9e880fc..333c547 100644 --- a/tests/unit/validators/confirmation-test.js +++ b/tests/unit/validators/confirmation-test.js @@ -70,7 +70,9 @@ module('Unit | Validator | confirmation', function () { let options = { allowBlank: true, on: 'foo' }; let validator = validateConfirmation(options); - assert.true(validator(key, ''), 'Empty string is accepted'); + assert.true(validator(key, null), 'null is accepted'); + assert.true(validator(key, undefined), 'undefined is accepted'); + assert.true(validator(key, ''), 'empty string is accepted'); }); }); diff --git a/tests/unit/validators/exclusion-test.js b/tests/unit/validators/exclusion-test.js index d1dabab..230b87b 100644 --- a/tests/unit/validators/exclusion-test.js +++ b/tests/unit/validators/exclusion-test.js @@ -76,6 +76,8 @@ module('Unit | Validator | exclusion', function () { let options = { allowBlank: true }; let validator = validateExclusion(options); - assert.true(validator(key, ''), 'Empty string is accepted'); + assert.true(validator(key, null), 'null is accepted'); + assert.true(validator(key, undefined), 'undefined is accepted'); + assert.true(validator(key, ''), 'empty string is accepted'); }); }); diff --git a/tests/unit/validators/format-test.js b/tests/unit/validators/format-test.js index a09e834..bb5120b 100644 --- a/tests/unit/validators/format-test.js +++ b/tests/unit/validators/format-test.js @@ -16,7 +16,9 @@ module('Unit | Validator | format', function () { let options = { allowBlank: true }; let validator = validateFormat(options); - assert.true(validator(key, '')); + assert.true(validator(key, null), 'null is accepted'); + assert.true(validator(key, undefined), 'undefined is accepted'); + assert.true(validator(key, ''), 'empty string is accepted'); }); test('it accepts a `type` option', function (assert) { diff --git a/tests/unit/validators/inclusion-test.js b/tests/unit/validators/inclusion-test.js index 796c304..69c9e9b 100644 --- a/tests/unit/validators/inclusion-test.js +++ b/tests/unit/validators/inclusion-test.js @@ -82,6 +82,8 @@ module('Unit | Validator | inclusion', function () { let options = { allowBlank: true }; let validator = validateInclusion(options); - assert.true(validator(key, ''), 'Empty string is accepted'); + assert.true(validator(key, null), 'null is accepted'); + assert.true(validator(key, undefined), 'undefined is accepted'); + assert.true(validator(key, ''), 'empty string is accepted'); }); }); From e4849728a49534d318e422d0fdf23f85423de879 Mon Sep 17 00:00:00 2001 From: frykten Date: Thu, 14 Oct 2021 15:21:00 +0200 Subject: [PATCH 2/4] Change date's allowBlank to isEmpty --- addon/validators/date.js | 4 +++- tests/unit/validators/date-test.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/addon/validators/date.js b/addon/validators/date.js index e8c843e..7f55c93 100644 --- a/addon/validators/date.js +++ b/addon/validators/date.js @@ -1,3 +1,5 @@ +import { isEmpty } from '@ember/utils'; + import buildMessage from 'ember-changeset-validations/utils/validation-errors'; import withDefaults from 'ember-changeset-validations/utils/with-defaults'; import toDate from 'ember-changeset-validations/utils/to-date'; @@ -19,7 +21,7 @@ export default function validateDate(options = {}) { let { before, onOrBefore, after, onOrAfter, message } = options; let type = 'date'; - if (allowBlank && (typeof value === 'undefined' || value === null)) { + if (allowBlank && isEmpty(value)) { return true; } diff --git a/tests/unit/validators/date-test.js b/tests/unit/validators/date-test.js index accfcc8..8077714 100644 --- a/tests/unit/validators/date-test.js +++ b/tests/unit/validators/date-test.js @@ -10,7 +10,8 @@ module('Unit | Validator | date', function () { assert.true(validator(key, null), 'null is allowed'); assert.true(validator(key, undefined), 'undefined is allowed'); - assert.true(validator(key, 123), 'number value is is allowed'); + assert.true(validator(key, ''), 'empty string is allowed'); + assert.true(validator(key, 123), 'number value is allowed'); assert.equal( validator(key, '1992-03-30'), From 0d287982cb5360a734270a089006f222821a8d7d Mon Sep 17 00:00:00 2001 From: frykten Date: Thu, 14 Oct 2021 15:21:40 +0200 Subject: [PATCH 3/4] Remove unuseful forcing of allowNone on the number validator --- addon/validators/number.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/addon/validators/number.js b/addon/validators/number.js index 6893050..23b9628 100644 --- a/addon/validators/number.js +++ b/addon/validators/number.js @@ -5,10 +5,6 @@ import evValidateNumber from 'ember-validators/number'; export default function validateNumber(options = {}) { options = withDefaults(options, { allowString: true, allowNone: false }); - if (options.allowBlank) { - options.allowNone = true; - } - return (key, value) => { let result = evValidateNumber(value, options, null, key); return result === true ? true : buildMessage(key, result); From 2db32f31c032e6baf3772e4262e9e7b8af933a8e Mon Sep 17 00:00:00 2001 From: frykten Date: Thu, 14 Oct 2021 15:25:20 +0200 Subject: [PATCH 4/4] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c1cf515..8942cb1 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,7 @@ This API accepts valid Date objects or a Date in milliseconds since Jan 1 1970, ```js { + propertyName: validateDate({ allowBlank: true }) // can be blank propertyName: validateDate({ before: new Date('3000-01-01') }), // must be before 1st Jan. 3000 propertyName: validateDate({ onOrBefore: Date.parse(new Date('3000-01-01')) }), // must be not after 1st Jan. 3000 propertyName: validateDate({ after: new Date('3000-01-01') }), // must be after 1st Jan. 3000