use of graphql.execution.ResultPath in project graphql-java-extended-validation by graphql-java.
the class TargetedValidationRules method walkObjectArg.
private List<GraphQLError> walkObjectArg(ValidationRule rule, ValidationEnvironment validationEnvironment, GraphQLInputObjectType argumentType, Map<String, Object> objectMap) {
List<GraphQLError> errors = new ArrayList<>();
// run them in a stable order
List<GraphQLInputObjectField> fieldDefinitions = Util.sort(argumentType.getFieldDefinitions(), GraphQLInputObjectField::getName);
for (GraphQLInputObjectField inputField : fieldDefinitions) {
GraphQLInputType fieldType = inputField.getType();
Object validatedValue = objectMap.getOrDefault(inputField.getName(), GraphQLInputObjectField.getInputFieldDefaultValue(inputField));
if (validatedValue == null) {
continue;
}
ResultPath newPath = validationEnvironment.getValidatedPath().segment(inputField.getName());
ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder.validatedPath(newPath).validatedValue(validatedValue).validatedType(fieldType).directives(inputField.getDirectives()).validatedElement(INPUT_OBJECT_FIELD));
List<GraphQLError> ruleErrors = runValidationImpl(rule, newValidationEnvironment, fieldType, validatedValue);
errors.addAll(ruleErrors);
}
return errors;
}
use of graphql.execution.ResultPath in project graphql-java-extended-validation by graphql-java.
the class TargetedValidationRules method runValidationRules.
/**
* Runs the contained rules that match the currently executing field named by the {@link graphql.schema.DataFetchingEnvironment}
*
* @param env the field being executed
* @param interpolator the message interpolator to use
* @param defaultLocale the default locale in play
*
* @return a list of zero or more input data validation errors
*/
public List<GraphQLError> runValidationRules(DataFetchingEnvironment env, MessageInterpolator interpolator, Locale defaultLocale) {
defaultLocale = LocaleUtil.determineLocale(env, defaultLocale);
List<GraphQLError> errors = new ArrayList<>();
GraphQLObjectType fieldContainer = env.getExecutionStepInfo().getObjectType();
GraphQLFieldDefinition fieldDefinition = env.getFieldDefinition();
ResultPath fieldPath = env.getExecutionStepInfo().getPath();
//
// run the field specific rules
ValidationCoordinates fieldCoords = ValidationCoordinates.newCoordinates(fieldContainer, fieldDefinition);
List<ValidationRule> rules = rulesMap.getOrDefault(fieldCoords, Collections.emptyList());
if (!rules.isEmpty()) {
ValidationEnvironment ruleEnvironment = ValidationEnvironment.newValidationEnvironment().dataFetchingEnvironment(env).messageInterpolator(interpolator).locale(defaultLocale).validatedElement(FIELD).validatedPath(fieldPath).build();
for (ValidationRule rule : rules) {
List<GraphQLError> ruleErrors = rule.runValidation(ruleEnvironment);
errors.addAll(ruleErrors);
}
}
//
// run the argument specific rules next
List<GraphQLArgument> sortedArgs = Util.sort(fieldDefinition.getArguments(), GraphQLArgument::getName);
for (GraphQLArgument fieldArg : sortedArgs) {
ValidationCoordinates argCoords = ValidationCoordinates.newCoordinates(fieldContainer, fieldDefinition, fieldArg);
rules = rulesMap.getOrDefault(argCoords, Collections.emptyList());
if (rules.isEmpty()) {
continue;
}
Object argValue = env.getArgument(fieldArg.getName());
GraphQLInputType inputType = fieldArg.getType();
ValidationEnvironment ruleEnvironment = ValidationEnvironment.newValidationEnvironment().dataFetchingEnvironment(env).argument(fieldArg).validatedElement(ARGUMENT).validatedType(inputType).validatedValue(argValue).validatedPath(fieldPath.segment(fieldArg.getName())).directives(fieldArg.getDirectives()).messageInterpolator(interpolator).locale(defaultLocale).build();
for (ValidationRule rule : rules) {
List<GraphQLError> ruleErrors = runValidationImpl(rule, ruleEnvironment, inputType, argValue);
errors.addAll(ruleErrors);
}
}
return errors;
}
Aggregations