Skip to content

Commit

Permalink
fix: use original $value in error message override
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed May 17, 2018
1 parent ade7f56 commit a2ee09f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/standard-validator-wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export default function (validator, opts = {}) {
(result.isValid === false && stopValidationChainIfFail);

const overrideMessage = (!result.isValid && messageEvaluator) ?
messageEvaluator(scopeVariation(_scope, {$errors: result.errors})) :
messageEvaluator(scopeVariation(_scope, {
// use original scope $value, not overrided value
$value: scope.overrideContext.$value,
// pass original errors
$errors: result.errors
})) :
null;

if (forceBreak || overrideMessage) {
Expand Down
16 changes: 11 additions & 5 deletions test/standard-validator-wrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ test('standardValidatorWrap: overrides error message', t => {

test('standardValidatorWrap: overrides error message with string interoperation', t => {
const s = createSimpleScope({});
t.deepEqual(w(() => "foo", {message: "bar ${$errors.join(',')}"})(s), {isValid: false, errors: ['bar foo']});
s.overrideContext.$value = 'test';
t.deepEqual(w({
$patchScope: scope => scopeVariation(scope, {$value: 'lorem'}),
$validator: () => "foo"
}, {message: "${$value} bar ${$errors.join(',')}"})(s), {isValid: false, errors: ['test bar foo']});
t.end();
});

Expand All @@ -57,11 +61,13 @@ test('standardValidatorWrap: patches scope', t => {
}
};

const good = createSimpleScope({$value: 'good'});
const bad = createSimpleScope({$value: 'bad'});
const good = createSimpleScope({});
good.overrideContext.$value = 'good';
const bad = createSimpleScope({});
bad.overrideContext.$value = 'bad';
t.deepEqual(w(validator)(good), {isValid: true});
t.deepEqual(w(validator, {message: 'length ${$value} < min ${$min}'})(good), {isValid: true});
t.deepEqual(w(validator, {message: 'length of ${$value} is less than ${$min}'})(good), {isValid: true});
t.deepEqual(w(validator)(bad), {isValid: false, errors: ['foo']});
t.deepEqual(w(validator, {message: '${$errors.join(",")}: length ${$value} < min ${$min}'})(bad), {isValid: false, errors: ['foo: length 3 < min 4']});
t.deepEqual(w(validator, {message: '${$errors.join(",")}: length of ${$value} is less than ${$min}'})(bad), {isValid: false, errors: ['foo: length of bad is less than 4']});
t.end();
});
2 changes: 1 addition & 1 deletion test/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ test('Validation: wraps errors', t => {
test('Validation: can add default helper', t => {
v.addHelper('sum', (a, b) => a + b);
let rules = {
a: {validate: 'number', value: 'sum($value, b)', greaterThan: 10, message: "sum(${$value}) is not more than 10"}
a: {validate: 'number', value: 'sum($value, b)', greaterThan: 10, message: "sum(${sum($value, b)}) is not more than 10"}
};

t.deepEqual(v.validate({a: 2, b: 3}, rules), {
Expand Down

0 comments on commit a2ee09f

Please sign in to comment.