ts-api-core
Version:
Nodejs api framework core
45 lines • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PickValidator = void 0;
const abstract_validator_1 = require("../abstract.validator");
/**
* 枚举值参数校验类
* @param pick?: any[] 枚举值数组
* @param defaultValue?: any | undefined 默认值
* @param must?: boolean 是否必须, 默认 true
* @param message?: string 错误提示消息
*/
class PickValidator extends abstract_validator_1.AbstractValidator {
constructor(pick, defaultValue, must = true, message) {
var _a;
super();
this.pick = pick;
this.must = must;
this.defaultValue = defaultValue;
this.setDefaultMessage(message !== null && message !== void 0 ? message : "可选值:" + ((_a = this.pick) !== null && _a !== void 0 ? _a : []).toString());
}
valid(param, doUpdateValue) {
if ((param === null || param === undefined || Number.isNaN(param))
&& this.must === false) {
return true;
}
if (this.pick !== undefined) {
for (const i of this.pick) {
// eslint-disable-next-line eqeqeq
if (param == i) {
doUpdateValue(i);
return true;
}
}
}
return false;
}
getDefaultValue() {
return this.defaultValue;
}
isMust() {
return this.must === true;
}
}
exports.PickValidator = PickValidator;
//# sourceMappingURL=pick.validator.js.map