node-web-mvc
Version:
node spring mvc
43 lines (42 loc) • 1.5 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 Max extends Constraints_1.default {
constructor() {
super(...arguments);
this.message = '{validation.constraints.Max.message}';
}
getSize(value, context) {
const typer = context.currentTyper;
if (typer.isType(String)) {
return value.length;
}
else if (typer.isType(Number)) {
return value;
}
else {
throw new UnexpectedTypeException_1.default(context);
}
}
isValid(content, context) {
const maxValue = this.value;
const value = this.getSize(content, context);
return value <= maxValue;
}
}
/**
* 验证标注元素的值必须小于等于配置的最大值
*
* 支持的数据类型:
* - `Number`
* - `String`
*
* `null` 或者 `undefined` 则不做验证
*/
exports.default = (0, Target_1.default)([ElementType_1.default.PROPERTY, ElementType_1.default.PARAMETER])(Max);