use of ninja.validation.Validator in project ninja by ninjaframework.
the class ControllerMethodInvoker method validateArgumentWithExtractor.
private static ArgumentExtractor<?> validateArgumentWithExtractor(MethodParameter methodParameter, Annotation[] annotations, Injector injector, ArgumentExtractor<?> extractor) {
// We have validators that get applied before parsing, and validators
// that get applied after parsing.
List<Validator<?>> preParseValidators = new ArrayList<>();
List<Validator<?>> postParseValidators = new ArrayList<>();
Class<?> boxedParamType = methodParameter.parameterClass;
if (methodParameter.parameterClass.isPrimitive()) {
boxedParamType = box(methodParameter.parameterClass);
}
// Now we have an extractor, lets apply validators that are able to validate
for (Annotation annotation : annotations) {
WithValidator withValidator = annotation.annotationType().getAnnotation(WithValidator.class);
if (withValidator != null) {
Validator<?> validator = instantiateComponent(withValidator.value(), annotation, methodParameter.parameterClass, injector);
// If the validator can validate the extractors type, then it's a pre parse validator
if (validator.getValidatedType().isAssignableFrom(extractor.getExtractedType())) {
preParseValidators.add(validator);
// If it can validate the parameter type, it's a post parse validator
} else if (validator.getValidatedType().isAssignableFrom(boxedParamType)) {
postParseValidators.add(validator);
// Otherwise, we can't validate with this validator
} else {
throw new RoutingException("Validator for field " + extractor.getFieldName() + " validates type " + validator.getValidatedType() + ", which doesn't match extracted type " + extractor.getExtractedType() + " or parameter type " + methodParameter.parameterClass);
}
}
}
// If we have pre parse validators, wrap our extractor in them
if (!preParseValidators.isEmpty()) {
extractor = new ValidatingArgumentExtractor(extractor, preParseValidators);
}
// String, and we can lookup a parser to parse it into the param type
if (!boxedParamType.isAssignableFrom(extractor.getExtractedType())) {
if (extractor.getFieldName() != null) {
if (String.class.isAssignableFrom(extractor.getExtractedType())) {
// Look up a parser for a single-valued parameter
ParamParser<?> parser = injector.getInstance(ParamParsers.class).getParamParser(methodParameter.parameterClass);
if (parser == null) {
throw new RoutingException("Can't find parameter parser for type " + extractor.getExtractedType() + " on field " + extractor.getFieldName());
} else {
extractor = new ParsingArgumentExtractor(extractor, parser);
}
} else if (String[].class.isAssignableFrom(extractor.getExtractedType())) {
// Look up a parser for a multi-valued parameter
ArrayParamParser<?> parser = injector.getInstance(ParamParsers.class).getArrayParser(methodParameter.parameterClass);
if (parser == null) {
throw new RoutingException("Can't find parameter array parser for type " + extractor.getExtractedType() + " on field " + extractor.getFieldName());
} else {
extractor = new ParsingArrayExtractor(extractor, parser);
}
} else {
throw new RoutingException("Extracted type " + extractor.getExtractedType() + " for field " + extractor.getFieldName() + " doesn't match parameter type " + methodParameter.parameterClass);
}
}
}
// If we have any post parse validators, wrap our extractor in them
if (!postParseValidators.isEmpty()) {
extractor = new ValidatingArgumentExtractor(extractor, postParseValidators);
}
if (methodParameter.isOptional) {
extractor = new OptionalArgumentExtractor(extractor);
}
return extractor;
}
Aggregations