Skip to content

Commit

Permalink
fix: allow number and boolean values in validation message "$value" t…
Browse files Browse the repository at this point in the history
…okens (#1467)
  • Loading branch information
kffl authored Dec 15, 2022
1 parent 2e360ee commit d325cc7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/validation/ValidationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ValidationUtils {
messageString &&
validationArguments.value !== undefined &&
validationArguments.value !== null &&
typeof validationArguments.value === 'string'
['string', 'boolean', 'number'].includes(typeof validationArguments.value)
)
messageString = messageString.replace(/\$value/g, validationArguments.value);
if (messageString) messageString = messageString.replace(/\$property/g, validationArguments.property);
Expand Down
36 changes: 35 additions & 1 deletion test/functional/validation-options.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
Contains,
Equals,
IsDefined,
Matches,
Max,
MinLength,
IsArray,
Validate,
Expand Down Expand Up @@ -58,7 +60,7 @@ describe('message', () => {
});
});

it('$value token should be replaced in a custom message', () => {
it('$value token should be replaced in a custom message with a string', () => {
class MyClass {
@MinLength(2, {
message: args => {
Expand All @@ -78,6 +80,38 @@ describe('message', () => {
});
});

it('$value token should be replaced in a custom message with a number', () => {
class MyClass {
@Max(100, { message: 'Maximum value is $constraint1, but actual is $value' })
val: number = 50;
}

const model = new MyClass();
model.val = 101;
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].constraints).toEqual({
max: 'Maximum value is 100, but actual is 101',
});
});
});

it('$value token should be replaced in a custom message with a boolean', () => {
class MyClass {
@Equals(true, { message: 'Value must be $constraint1, but actual is $value' })
val: boolean = false;
}

const model = new MyClass();
model.val = false;
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].constraints).toEqual({
equals: 'Value must be true, but actual is false',
});
});
});

it('$constraint1 token should be replaced in a custom message', () => {
class MyClass {
@Contains('hello', {
Expand Down

0 comments on commit d325cc7

Please sign in to comment.