UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

32 lines (31 loc) 1.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); exports.VALIDATOR_MAX_METADATA_KEY = Symbol('validation:validator:isMax'); function isMax(max, errorMessage) { return (target, propertyKey, descriptor) => { const original = descriptor.value; Reflect.defineMetadata(exports.VALIDATOR_MAX_METADATA_KEY, null, target, propertyKey); Reflect.defineMetadata(index_1.VALIDATOR_METADATA_KEY, new MaxValidator(max, errorMessage), target, propertyKey); }; } exports.isMax = isMax; class MaxValidator extends index_1.ValidatorBase { constructor(max, errorMessage = 'The value of ${DisplayName} must smaller than min: ${Max} .') { super('Max', errorMessage); this.max = max; } get Max() { return this.max; } validate(context) { if (context.Value === undefined || context.Value === null) { return Promise.resolve(true); } if (context.Value > this.max) { return Promise.reject(new index_1.ValidatorException(this.Name, context.Target, context.PropertyName, this.formatErrorMessage(context))); } return Promise.resolve(true); } } exports.MaxValidator = MaxValidator;