Skip to content

Commit

Permalink
fix: fill the target rule params for message generators closes #3077
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Dec 9, 2020
1 parent a7fe71e commit f5e1bd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/vee-validate/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ async function _test(field: FieldValidationContext, value: any, rule: { name: st
field: field.name,
value,
form: field.formData,
rule,
rule: {
...rule,
params,
},
};

const result = await validator(value, params, ctx);
Expand Down
22 changes: 21 additions & 1 deletion packages/vee-validate/tests/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { validate, defineRule } from '@/vee-validate';
import { validate, defineRule, configure } from '@/vee-validate';
import { numeric } from '@/rules';
import { getConfig } from '../src/config';

test('allows empty rules for the string format', async () => {
defineRule('numeric', numeric);
Expand All @@ -21,3 +22,22 @@ test('handles targets expressed in objects', async () => {
result = await validate('test', { confirmed: { target: '@other' } }, { values: { other: 'test' } });
expect(result.errors).toHaveLength(0);
});

// #3077
test('target params are filled in the params in message context', async () => {
defineRule('lessThan', (value, params: any) => Number(value) < Number(params[0]));
const { generateMessage: original } = getConfig();
configure({
generateMessage: context => {
const params = context.rule?.params as any;
return `This value must be less than ${params[0]}`;
},
});

const result = await validate(2, 'lessThan:@other', { values: { other: 1 } });
expect(result.errors).toContain(`This value must be less than 1`);

configure({
generateMessage: original,
});
});

0 comments on commit f5e1bd3

Please sign in to comment.