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

[bug]: don't mutate options to validator #119

Merged
merged 4 commits into from
Feb 9, 2022
Merged
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
59 changes: 37 additions & 22 deletions addon/date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { set } from '@ember/object';
import validationError from 'ember-validators/utils/validation-error';

/**
Expand Down Expand Up @@ -45,42 +44,58 @@ export default function validateDate(value, options) {
}

if (before) {
before = parseAsDate(before, format, locale);

if (!isBefore(date, before)) {
set(options, 'before', parseDateError(before, errorFormat, locale));
return validationError('before', value, options);
const beforeCompare = parseAsDate(before, format, locale);

if (!isBefore(date, beforeCompare)) {
return validationError(
'before',
value,
Object.assign({}, options, {
before: parseDateError(beforeCompare, errorFormat, locale),
})
);
}
}

if (onOrBefore) {
onOrBefore = parseAsDate(onOrBefore, format, locale);
const onOrBeforeCompare = parseAsDate(onOrBefore, format, locale);

if (!isSameOrBefore(date, onOrBefore)) {
set(
options,
if (!isSameOrBefore(date, onOrBeforeCompare)) {
return validationError(
'onOrBefore',
parseDateError(onOrBefore, errorFormat, locale)
value,
Object.assign({}, options, {
onOrBefore: parseDateError(onOrBeforeCompare, errorFormat, locale),
})
);
return validationError('onOrBefore', value, options);
}
}

if (after) {
after = parseAsDate(after, format, locale);

if (!isAfter(date, after)) {
set(options, 'after', parseDateError(after, errorFormat, locale));
return validationError('after', value, options);
const afterCompare = parseAsDate(after, format, locale);

if (!isAfter(date, afterCompare)) {
return validationError(
'after',
value,
Object.assign({}, options, {
after: parseDateError(afterCompare, errorFormat, locale),
})
);
}
}

if (onOrAfter) {
onOrAfter = parseAsDate(onOrAfter, format, locale);

if (!isSameOrAfter(date, onOrAfter)) {
set(options, 'onOrAfter', parseDateError(onOrAfter, errorFormat, locale));
return validationError('onOrAfter', value, options);
const onOrAfterCompare = parseAsDate(onOrAfter, format, locale);

if (!isSameOrAfter(date, onOrAfterCompare)) {
return validationError(
'onOrAfter',
value,
Object.assign({}, options, {
onOrAfter: parseDateError(onOrAfterCompare, errorFormat, locale),
})
);
}
}

Expand Down
20 changes: 12 additions & 8 deletions addon/format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isEmpty, isNone } from '@ember/utils';
import { assert } from '@ember/debug';
import { set } from '@ember/object';

import Ember from 'ember';
import validationError from 'ember-validators/utils/validation-error';
Expand Down Expand Up @@ -44,27 +43,32 @@ export default function validateFormat(value, options, model, attribute) {
!isEmpty(Object.keys(options))
);

let regexTest = regex;

if (allowBlank && isEmpty(value)) {
return true;
}

if (type && !regex && regularExpressions[type]) {
regex = regularExpressions[type];
regexTest = regularExpressions[type];
}

if (type === 'email') {
if (regex === regularExpressions.email) {
regex = formatEmailRegex(options);
if (regexTest === regularExpressions.email) {
regexTest = formatEmailRegex(options);
}

set(options, 'regex', regex);
Object.assign({}, options, { regex: regexTest });
}

if (
!canInvoke(value, 'match') ||
(regex && isEmpty(value.match(regex)) !== inverse)
(regexTest && isEmpty(value.match(regexTest)) !== inverse)
) {
return validationError(type || 'invalid', value, options);
return validationError(
type || 'invalid',
value,
Object.assign({}, options, { regex: regexTest })
);
}

return true;
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/validators/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,40 @@ test('after now or on', function (assert) {
assert.true(processResult(result));
});

test("we don't mutate any option", function (assert) {
const dateRef = new Date('1/1/2015');
const onOrAfterOptions = {
onOrAfter: dateRef,
};
const afterOptions = {
after: dateRef,
};
const beforeOptions = {
before: dateRef,
};
const onOrBeforeOptions = {
onOrBefore: dateRef,
};

validate('1/1/2020', afterOptions);
validate('1/1/2020', onOrAfterOptions);
validate('1/1/2010', beforeOptions);
validate('1/1/2010', onOrBeforeOptions);

assert.strictEqual(afterOptions.after, dateRef, 'after option mutated');
assert.strictEqual(
onOrAfterOptions.onOrAfter,
dateRef,
'onOrAfter option mutated'
);
assert.strictEqual(beforeOptions.before, dateRef, 'before option mutated');
assert.strictEqual(
onOrBeforeOptions.onOrBefore,
dateRef,
'onOrBefore option mutated'
);
});

skip('after or on precision', function (assert) {
let precisions = ['second', 'minute', 'hour', 'day', 'month', 'year'];

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/validators/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,16 @@ test('custom with g flag', function (assert) {
result = validate('bar', options);
assert.strictEqual(processResult(result), 'This field is invalid');
});

test("we don't mutate any option", function (assert) {
const type = 'email';

const options = {
type,
regex: 'hello!',
};

validate('not an email', options);

assert.strictEqual(options.regex, 'hello!', 'regex option mutated');
});