UNPKG

nowjs-core

Version:

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

43 lines (42 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); exports.VALIDATOR_PATTERN_METADATA_KEY = Symbol('validation:validator:isPattern'); function isPattern(pattern, errorMessage) { return (target, propertyKey, descriptor) => { const original = descriptor.value; Reflect.defineMetadata(exports.VALIDATOR_PATTERN_METADATA_KEY, null, target, propertyKey); Reflect.defineMetadata(index_1.VALIDATOR_METADATA_KEY, new PatternValidator(pattern, errorMessage), target, propertyKey); }; } exports.isPattern = isPattern; class PatternValidatorBase extends index_1.ValidatorBase { constructor(name, pattern, errorMessage = 'The value of ${DisplayName} must have pattern : ${Pattern} .') { super(name, errorMessage); this.pattern = pattern; } setPattern(pattern) { this.pattern = pattern; } get Pattern() { return this.pattern; } validate(context) { if (context.Value === undefined || context.Value === null || typeof context.Value !== 'string') { return Promise.resolve(true); } if (!this.pattern.test(context.Value)) { return Promise.reject(new index_1.ValidatorException(this.Name, context.Target, context.PropertyName, this.formatErrorMessage(context))); } else { return Promise.resolve(true); } } } exports.PatternValidatorBase = PatternValidatorBase; class PatternValidator extends PatternValidatorBase { constructor(pattern, errorMessage = 'The value of ${DisplayName} must have pattern : ${Pattern} .') { super('Pattern', pattern, errorMessage); } } exports.PatternValidator = PatternValidator;