diff --git a/src/validation/reporter.spec.ts b/src/validation/reporter.spec.ts index 1334b6f..377d9bc 100644 --- a/src/validation/reporter.spec.ts +++ b/src/validation/reporter.spec.ts @@ -290,6 +290,60 @@ describe('Reporter', () => { expect(errors).toBeNull(); expect(result).toEqual({ t1: 1234 }); }); + + it('should report an error when number is greater than max rule', () => { + interface S { + s1: number; + } + interface T { + t1: number; + } + + const VALUE = 10; + const MAX = 5; + const schema = createSchema({ + t1: { + fn: value => value.s1, + validation: Validation.number().max(MAX) + } + }); + const result = morphism(schema, { s1: VALUE }); + const error = new ValidationError({ targetProperty: 't1', expect: `value to be less or equal than ${MAX}`, value: VALUE }); + const message = defaultFormatter(error); + const errors = reporter.report(result); + expect(errors).not.toBeNull(); + if (errors) { + expect(errors.length).toEqual(1); + expect(errors[0]).toBe(message); + } + }); + + it('should report an error when number is less than min rule', () => { + interface S { + s1: number; + } + interface T { + t1: number; + } + + const VALUE = 2; + const MIN = 5; + const schema = createSchema({ + t1: { + fn: value => value.s1, + validation: Validation.number().min(MIN) + } + }); + const result = morphism(schema, { s1: VALUE }); + const error = new ValidationError({ targetProperty: 't1', expect: `value to be greater or equal than ${MIN}`, value: VALUE }); + const message = defaultFormatter(error); + const errors = reporter.report(result); + expect(errors).not.toBeNull(); + if (errors) { + expect(errors.length).toEqual(1); + expect(errors[0]).toBe(message); + } + }); }); describe('boolean', () => { diff --git a/src/validation/validators/NumberValidator.ts b/src/validation/validators/NumberValidator.ts index ce180d3..d53ff6f 100644 --- a/src/validation/validators/NumberValidator.ts +++ b/src/validation/validators/NumberValidator.ts @@ -15,4 +15,31 @@ export class NumberValidator extends BaseValidator { } }); } + + min(val: number) { + this.addRule({ + name: 'min', + expect: `value to be greater or equal than ${val}`, + test: function(value) { + if (value < val) { + throw new ValidatorError({ value, expect: this.expect }); + } + return value; + } + }); + return this; + } + max(val: number) { + this.addRule({ + name: 'max', + expect: `value to be less or equal than ${val}`, + test: function(value) { + if (value > val) { + throw new ValidatorError({ value, expect: this.expect }); + } + return value; + } + }); + return this; + } }