@ticatec/web-bean-validator
Version:
A TypeScript/JavaScript library for rule-based entity validation with boundary checking for strings, numbers, dates, enums, objects, and arrays.
35 lines (34 loc) • 1.25 kB
JavaScript
import BaseValidator from "./BaseValidator";
import i18nRes from "../i18nRes";
export default class StringValidator extends BaseValidator {
constructor(field, options = null) {
super(field, options);
this.minLen = options === null || options === void 0 ? void 0 : options.minLen;
this.format = options === null || options === void 0 ? void 0 : options.format;
}
/**
* 同时检查字符串是否为空
* @param value
* @protected
*/
checkNullValue(value) {
return super.checkNullValue(value) || value.length == 0;
}
checkType(value) {
return typeof value == 'string' ? value.toString().trim() : null;
}
checkField(value, result) {
if (this.minLen != null && value.length < this.minLen) {
result.setError(this.field, this.formatErrorMessage(i18nRes.validation.stringShortage, {
field: this.name,
length: value.length
}));
return false;
}
if (this.format != null && this.format.regex != null && value.match(this.format.regex) == null) {
result.setError(this.field, this.format.message);
return false;
}
return true;
}
}