Skip to content

Commit a2ee09f

Browse files
committed
fix: use original $value in error message override
1 parent ade7f56 commit a2ee09f

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/standard-validator-wrap.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export default function (validator, opts = {}) {
2929
(result.isValid === false && stopValidationChainIfFail);
3030

3131
const overrideMessage = (!result.isValid && messageEvaluator) ?
32-
messageEvaluator(scopeVariation(_scope, {$errors: result.errors})) :
32+
messageEvaluator(scopeVariation(_scope, {
33+
// use original scope $value, not overrided value
34+
$value: scope.overrideContext.$value,
35+
// pass original errors
36+
$errors: result.errors
37+
})) :
3338
null;
3439

3540
if (forceBreak || overrideMessage) {

test/standard-validator-wrap.spec.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ test('standardValidatorWrap: overrides error message', t => {
3232

3333
test('standardValidatorWrap: overrides error message with string interoperation', t => {
3434
const s = createSimpleScope({});
35-
t.deepEqual(w(() => "foo", {message: "bar ${$errors.join(',')}"})(s), {isValid: false, errors: ['bar foo']});
35+
s.overrideContext.$value = 'test';
36+
t.deepEqual(w({
37+
$patchScope: scope => scopeVariation(scope, {$value: 'lorem'}),
38+
$validator: () => "foo"
39+
}, {message: "${$value} bar ${$errors.join(',')}"})(s), {isValid: false, errors: ['test bar foo']});
3640
t.end();
3741
});
3842

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

60-
const good = createSimpleScope({$value: 'good'});
61-
const bad = createSimpleScope({$value: 'bad'});
64+
const good = createSimpleScope({});
65+
good.overrideContext.$value = 'good';
66+
const bad = createSimpleScope({});
67+
bad.overrideContext.$value = 'bad';
6268
t.deepEqual(w(validator)(good), {isValid: true});
63-
t.deepEqual(w(validator, {message: 'length ${$value} < min ${$min}'})(good), {isValid: true});
69+
t.deepEqual(w(validator, {message: 'length of ${$value} is less than ${$min}'})(good), {isValid: true});
6470
t.deepEqual(w(validator)(bad), {isValid: false, errors: ['foo']});
65-
t.deepEqual(w(validator, {message: '${$errors.join(",")}: length ${$value} < min ${$min}'})(bad), {isValid: false, errors: ['foo: length 3 < min 4']});
71+
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']});
6672
t.end();
6773
});

test/validation.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ test('Validation: wraps errors', t => {
270270
test('Validation: can add default helper', t => {
271271
v.addHelper('sum', (a, b) => a + b);
272272
let rules = {
273-
a: {validate: 'number', value: 'sum($value, b)', greaterThan: 10, message: "sum(${$value}) is not more than 10"}
273+
a: {validate: 'number', value: 'sum($value, b)', greaterThan: 10, message: "sum(${sum($value, b)}) is not more than 10"}
274274
};
275275

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

0 commit comments

Comments
 (0)