node-web-mvc
Version:
node spring mvc
82 lines (81 loc) • 3.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const MethodArgumentNotValidException_1 = __importDefault(require("../errors/MethodArgumentNotValidException"));
const Javascript_1 = __importDefault(require("../interface/Javascript"));
const AnnotationIndexer_1 = __importDefault(require("../servlets/annotations/annotation/AnnotationIndexer"));
const ValidationContext_1 = __importDefault(require("./ValidationContext"));
const ValidationMessage_1 = __importDefault(require("./ValidationMessage"));
const Constraints_1 = __importDefault(require("./annotation/Constraints"));
const Valid_1 = __importDefault(require("./annotation/Valid"));
const Validated_1 = __importDefault(require("./annotation/Validated"));
const validationMessage = new ValidationMessage_1.default();
class DataValidator {
/**
* 获取配置在当前参数作用于上的验证注解
* @param parameter
* @returns
*/
getValidateAnnotation(parameter) {
var _a;
const annotations = parameter.getParameterAnnotations();
return (_a = annotations.find(annotation => {
return AnnotationIndexer_1.default.isAnnotationTypeOf(annotation, Valid_1.default) || AnnotationIndexer_1.default.isAnnotationTypeOf(annotation, Validated_1.default);
})) === null || _a === void 0 ? void 0 : _a.nativeAnnotation;
}
determineValidationHints(anno) {
const value = anno.value || Constraints_1.default.EMPTY_GROUP;
return value instanceof Array ? value : [value].filter(Boolean);
}
async validate(model, parameter) {
if (model === null || model === undefined)
return;
// 获取参数上的校验注解
const validAnnotation = this.getValidateAnnotation(parameter);
if (!validAnnotation) {
// 如果没有配置,则直接跳过校验
return;
}
// 查找分组规则
const groups = this.determineValidationHints(validAnnotation);
return this.validateByAnnotation(model, groups, parameter, parameter.parameterType, '');
}
async validateByAnnotation(model, groups, parameter, targetType, paths) {
const constraints = Constraints_1.default.getConstraints(targetType, groups);
if (constraints.length < 1) {
// 如果没有配置任何规则
return null;
}
const context = new ValidationContext_1.default(constraints);
const childValidations = Valid_1.default.getChildValidations(targetType);
// 按照约束校验
for (const anno of constraints) {
const value = model[anno.name];
const constraint = anno.nativeAnnotation;
context.current = anno;
context.currentField = anno.name;
context.currentTyper = Javascript_1.default.createTyper(anno.dataType.clazz);
const isValid = await constraint.isValid(value, context);
const currentPaths = paths ? `${paths}.${anno.name}` : anno.name;
if (!isValid) {
const message = validationMessage.format(constraint);
throw new MethodArgumentNotValidException_1.default(parameter, message, currentPaths);
}
}
// 验证子对象
for (const anno of childValidations) {
const value = model[anno.name];
if (value === null || value === undefined) {
return;
}
const currentPaths = paths ? `${paths}.${anno.name}` : anno.name;
if (anno) {
// 如果配置了校验,则嵌套校验
await this.validateByAnnotation(value, groups, parameter, anno.dataType.clazz, currentPaths);
}
}
}
}
exports.default = DataValidator;