Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmonize blank option #318

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion addon/validators/date.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}

Expand Down
4 changes: 0 additions & 4 deletions addon/validators/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines -8 to -10
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this one as it is not useful: isEmpty is basically isNone but with a larger scope, hence allowBlank has the same scope as allowNone but larger 🤷 (cf.)


return (key, value) => {
let result = evValidateNumber(value, options, null, key);
return result === true ? true : buildMessage(key, result);
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/validators/confirmation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/validators/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/validators/exclusion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
4 changes: 3 additions & 1 deletion tests/unit/validators/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/validators/inclusion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});