diff --git a/src/validation/ValidationUtils.ts b/src/validation/ValidationUtils.ts index 7408031627..c67e8bcf1c 100644 --- a/src/validation/ValidationUtils.ts +++ b/src/validation/ValidationUtils.ts @@ -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); diff --git a/test/functional/validation-options.spec.ts b/test/functional/validation-options.spec.ts index 99db80aa76..515325a652 100644 --- a/test/functional/validation-options.spec.ts +++ b/test/functional/validation-options.spec.ts @@ -1,7 +1,9 @@ import { Contains, + Equals, IsDefined, Matches, + Max, MinLength, IsArray, Validate, @@ -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 => { @@ -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', {