node-web-mvc
Version:
node spring mvc
34 lines (33 loc) • 1.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const UnexpectedTypeException_1 = __importDefault(require("../../errors/UnexpectedTypeException"));
const Target_1 = __importDefault(require("../../servlets/annotations/Target"));
const ElementType_1 = __importDefault(require("../../servlets/annotations/annotation/ElementType"));
const Constraints_1 = __importDefault(require("./Constraints"));
class Pattern extends Constraints_1.default {
constructor() {
super(...arguments);
this.message = '{validation.constraints.Pattern.message}';
}
isValid(value, context) {
if (!context.currentTyper.isType(String)) {
throw new UnexpectedTypeException_1.default(context);
}
if (value === null || value === undefined) {
return true;
}
return this.regexp.test(value);
}
}
/**
* 通过正则表达式验证配置的元素值
*
* 支持类型:
* - `String`
*
* `null` 或者 `undefined` 不做验证
*/
exports.default = (0, Target_1.default)([ElementType_1.default.PROPERTY, ElementType_1.default.PARAMETER])(Pattern);