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_MIN_METADATA_KEY = Symbol('validation:validator:isMin'); function isMin(min, errorMessage) { return (target, propertyKey, descriptor) => { const original = descriptor.value; Reflect.defineMetadata(exports.VALIDATOR_MIN_METADATA_KEY, null, target, propertyKey); Reflect.defineMetadata(index_1.VALIDATOR_METADATA_KEY, new MinValidator(min, errorMessage), target, propertyKey); }; } exports.isMin = isMin; class MinValidator extends index_1.ValidatorBase { constructor(min, errorMessage = 'The value of ${DisplayName} must greater than min: ${Min} .') { super('Min', errorMessage); this.min = min; } get Min() { return this.min; } validate(context) { if (context.Value === undefined || context.Value === null) { return Promise.resolve(true); } if (context.Value < this.min) { return Promise.reject(new index_1.ValidatorException(this.Name, context.Target, context.PropertyName, this.formatErrorMessage(context))); } return Promise.resolve(true); } } exports.MinValidator = MinValidator;