Skip to content

Commit

Permalink
fix: Change rule errors to use object path
Browse files Browse the repository at this point in the history
BREAKING CHANGE: If you have code that rely on matching the string of
the validation error. You need to replace all with `/` with `.`.
  • Loading branch information
relekang committed May 23, 2016
1 parent 1c980ad commit 710e122
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
16 changes: 8 additions & 8 deletions src/rules/__tests__/ageByDate_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ test('rules.ageByDate should return null for valid values', t => {
t.is(ageByDate('field', two, { exact: 2 }), null);
});

test('rules.ageByDate should return "ageByDate/min" when age is below min', t => {
t.is(ageByDate('field', moment().format('YYYY-MM-DD'), { min: 18 }), 'ageByDate/min');
test('rules.ageByDate should return "ageByDate.min" when age is below min', t => {
t.is(ageByDate('field', moment().format('YYYY-MM-DD'), { min: 18 }), 'ageByDate.min');
});

test('rules.ageByDate should return "ageByDate/max" when age is above max', t => {
test('rules.ageByDate should return "ageByDate.max" when age is above max', t => {
t.is(
ageByDate('field', moment().subtract(5, 'years').format('YYYY-MM-DD'), { max: 2 }),
'ageByDate/max'
'ageByDate.max'
);
});

test('rules.ageByDate should return "ageByDate/min" when age is below exact', t => {
t.is(ageByDate('field', moment().format('YYYY-MM-DD'), { exact: 18 }), 'ageByDate/min');
test('rules.ageByDate should return "ageByDate.min" when age is below exact', t => {
t.is(ageByDate('field', moment().format('YYYY-MM-DD'), { exact: 18 }), 'ageByDate.min');
});

test('rules.ageByDate should return "ageByDate/max" when age is above exact', t => {
test('rules.ageByDate should return "ageByDate.max" when age is above exact', t => {
t.is(
ageByDate('field', moment().subtract(5, 'years').format('YYYY-MM-DD'), { exact: 2 }),
'ageByDate/max'
'ageByDate.max'
);
});
24 changes: 12 additions & 12 deletions src/rules/__tests__/date_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ test('rules.date should return null for valid formats when format is specified',
t.is(date('field', '2015', { format: 'YYYY' }), null);
});

test('rules.date should return "date/format" for invalid formats', t => {
t.is(date('field', '2015-r05-05'), 'date/format');
t.is(date('field', '2015-02-29'), 'date/format');
t.is(date('field', '2016-02-31'), 'date/format');
test('rules.date should return "date.format" for invalid formats', t => {
t.is(date('field', '2015-r05-05'), 'date.format');
t.is(date('field', '2015-02-29'), 'date.format');
t.is(date('field', '2016-02-31'), 'date.format');
});

test('rules.date should return "date/format" for invalid formats when format is specified', t => {
t.is(date('field', '2015-05-05T10:00', { format: 'YYYY-MM-DD' }), 'date/format');
test('rules.date should return "date.format" for invalid formats when format is specified', t => {
t.is(date('field', '2015-05-05T10:00', { format: 'YYYY-MM-DD' }), 'date.format');
});

test('rules.date should return "date/past" when date is not in past', t => {
test('rules.date should return "date.past" when date is not in past', t => {
const inFuture = moment(new Date()).add(1, 'days');
t.is(date('field', inFuture, { past: true }), 'date/past');
t.is(date('field', moment(), { past: true }), 'date/past');
t.is(date('field', inFuture, { past: true }), 'date.past');
t.is(date('field', moment(), { past: true }), 'date.past');
});

test('rules.date should return "date/future" when date is not in future', t => {
test('rules.date should return "date.future" when date is not in future', t => {
const inPast = moment(new Date()).subtract(1, 'days');
t.is(date('field', inPast, { future: true }), 'date/future');
t.is(date('field', moment(), { future: true }), 'date/future');
t.is(date('field', inPast, { future: true }), 'date.future');
t.is(date('field', moment(), { future: true }), 'date.future');
});
16 changes: 8 additions & 8 deletions src/rules/__tests__/length_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ test('rules.length should return null accepted values', t => {
t.is(length('field', undefined, { min: 1, max: 1 }), null);
});

test('rules.length should return "length/min" when value is to short', t => {
t.is(length('field', '00', { min: 3 }), 'length/min');
test('rules.length should return "length.min" when value is to short', t => {
t.is(length('field', '00', { min: 3 }), 'length.min');
});

test('rules.length should return "length/max" when value is to long', t => {
t.is(length('field', '00', { max: 1 }), 'length/max');
test('rules.length should return "length.max" when value is to long', t => {
t.is(length('field', '00', { max: 1 }), 'length.max');
});

test('rules.length should return "length/max" when value is longer than exact', t => {
t.is(length('field', '00', { exact: 1 }), 'length/max');
test('rules.length should return "length.max" when value is longer than exact', t => {
t.is(length('field', '00', { exact: 1 }), 'length.max');
});


test('rules.length should return "length/min" when value is longer than exact', t => {
t.is(length('field', '', { exact: 1 }), 'length/min');
test('rules.length should return "length.min" when value is longer than exact', t => {
t.is(length('field', '', { exact: 1 }), 'length.min');
});
22 changes: 11 additions & 11 deletions src/rules/__tests__/numeric_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ test('numeric should return null for integer values when integerOnly is true', t
});

test(
'numeric should return "numeric/integerOnly" for non-integer values when integerOnly is true',
'numeric should return "numeric.integerOnly" for non-integer values when integerOnly is true',
t => {
t.is(numeric('field', 42.2, { integerOnly: true }), 'numeric/integerOnly');
t.is(numeric('field', '42.3', { integerOnly: true }), 'numeric/integerOnly');
t.is(numeric('field', 42.2, { integerOnly: true }), 'numeric.integerOnly');
t.is(numeric('field', '42.3', { integerOnly: true }), 'numeric.integerOnly');
}
);

Expand Down Expand Up @@ -56,28 +56,28 @@ test('numeric should validate max value', t => {
t.is(numeric('field', 41, { max: 42 }), null);
t.is(numeric('field', 42, { max: 42 }), null);
t.is(numeric('field', 41.9, { max: 42 }), null);
t.is(numeric('field', 43, { max: 42 }), 'numeric/max');
t.is(numeric('field', 42.1, { max: 42 }), 'numeric/max');
t.is(numeric('field', '43', { max: 42 }), 'numeric/max');
t.is(numeric('field', 43, { max: 42 }), 'numeric.max');
t.is(numeric('field', 42.1, { max: 42 }), 'numeric.max');
t.is(numeric('field', '43', { max: 42 }), 'numeric.max');
});

test('numeric should validate min value', t => {
t.is(numeric('field', 0, { min: 0 }), null);
t.is(numeric('field', 43, { min: 42 }), null);
t.is(numeric('field', 42, { min: 42 }), null);
t.is(numeric('field', 42.1, { min: 42 }), null);
t.is(numeric('field', 41.9, { min: 42 }), 'numeric/min');
t.is(numeric('field', 41, { min: 42 }), 'numeric/min');
t.is(numeric('field', '41', { min: 42 }), 'numeric/min');
t.is(numeric('field', 41.9, { min: 42 }), 'numeric.min');
t.is(numeric('field', 41, { min: 42 }), 'numeric.min');
t.is(numeric('field', '41', { min: 42 }), 'numeric.min');
});

test('numeric should validate max value when set by other field', t => {
t.is(numeric('field', 0, { max: { field: 'a' }, values: { a: 1 } }), null);
t.is(numeric('field', 2, { max: { field: 'a' }, values: { a: 1 } }), 'numeric/max/field/a');
t.is(numeric('field', 2, { max: { field: 'a' }, values: { a: 1 } }), 'numeric.max.field');
});


test('numeric should validate min value when set by other field', t => {
t.is(numeric('field', 2, { min: { field: 'a' }, values: { a: 1 } }), null);
t.is(numeric('field', 0, { min: { field: 'a' }, values: { a: 1 } }), 'numeric/min/field/a');
t.is(numeric('field', 0, { min: { field: 'a' }, values: { a: 1 } }), 'numeric.min.field');
});
4 changes: 2 additions & 2 deletions src/rules/ageByDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export function ageByDate(field, value, options) {
}

if (!isNil(min) && Math.abs(moment(value).diff(moment(), 'years')) < min) {
return 'ageByDate/min';
return 'ageByDate.min';
}

if (!isNil(max) && Math.abs(moment(value).diff(moment(), 'years')) > max) {
return 'ageByDate/max';
return 'ageByDate.max';
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions src/rules/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import moment from 'moment';

export function date(field, value, options) {
if (!moment(value, get(options, 'format'), true).isValid()) {
return 'date/format';
return 'date.format';
}

if (get(options, 'past') && moment(value).diff(moment(), 'days') >= 0) {
return 'date/past';
return 'date.past';
}

if (get(options, 'future') && moment(value).diff(moment(), 'days') <= 0) {
return 'date/future';
return 'date.future';
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/rules/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export function length(field, value, options) {
}

if (!isNil(min) && value.length < min) {
return 'length/min';
return 'length.min';
}

if (!isNil(max) && value.length > max) {
return 'length/max';
return 'length.max';
}

return null;
Expand Down
10 changes: 5 additions & 5 deletions src/rules/numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ export function numeric(field, value, options) {
}

if (get(options, 'integerOnly') && !isInt(number.toString())) {
return 'numeric/integerOnly';
return 'numeric.integerOnly';
}

const min = get(options, 'min');
if (isObject(min)) {
if (min.field && evaluateMin(number, get(options, `values.${min.field}`))) {
return `numeric/min/field/${min.field}`;
return 'numeric.min.field';
}
} else {
if (evaluateMin(number, min)) {
return 'numeric/min';
return 'numeric.min';
}
}

const max = get(options, 'max');
if (isObject(max)) {
if (max.field && evaluateMax(number, get(options, `values.${max.field}`))) {
return `numeric/max/field/${max.field}`;
return 'numeric.max.field';
}
} else {
if (evaluateMax(number, max)) {
return 'numeric/max';
return 'numeric.max';
}
}

Expand Down

0 comments on commit 710e122

Please sign in to comment.