Skip to content

Commit 56c99ea

Browse files
committed
Split SchemaValidator rules into SchemaValidationRule classes
SchemaValidator keeps the single loop over types and directive definitions and calls the hooks of a SchemaValidationRule per rule class. Each validateSchema call creates its own rule instances, so per-validation state stays in final fields. InputValueValidator holds the value coercion shared by DefaultValuesAreValid and AppliedDirectiveArgumentsAreValid.
1 parent 1bdb449 commit 56c99ea

15 files changed

Lines changed: 1245 additions & 678 deletions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package graphql.schema.validation;
2+
3+
import graphql.Internal;
4+
import graphql.schema.GraphQLAppliedDirective;
5+
import graphql.schema.GraphQLAppliedDirectiveArgument;
6+
import graphql.schema.GraphQLArgument;
7+
import graphql.schema.GraphQLDirective;
8+
import graphql.schema.GraphQLDirectiveContainer;
9+
import graphql.schema.GraphQLInputType;
10+
import graphql.schema.InputValueWithState;
11+
import org.jspecify.annotations.NullMarked;
12+
13+
import static graphql.schema.GraphQLTypeUtil.isNonNull;
14+
import static java.lang.String.format;
15+
16+
/**
17+
* Schema validation rule ensuring the argument values of applied directives are valid for their types.
18+
*/
19+
@Internal
20+
@NullMarked
21+
public class AppliedDirectiveArgumentsAreValid implements SchemaValidationRule {
22+
23+
private final InputValueValidator inputValueValidator;
24+
private final SchemaValidationErrorCollector errors;
25+
26+
public AppliedDirectiveArgumentsAreValid(InputValueValidator inputValueValidator, SchemaValidationErrorCollector errors) {
27+
this.inputValueValidator = inputValueValidator;
28+
this.errors = errors;
29+
}
30+
31+
@Override
32+
public void checkDirectiveContainer(GraphQLDirectiveContainer container) {
33+
for (GraphQLDirective directive : container.getDirectives()) {
34+
for (GraphQLArgument argument : directive.getArguments()) {
35+
checkArgument(directive.getName(), argument.getName(), argument.getArgumentValue(), argument.getType());
36+
}
37+
}
38+
for (GraphQLAppliedDirective directive : container.getAppliedDirectives()) {
39+
for (GraphQLAppliedDirectiveArgument argument : directive.getArguments()) {
40+
checkArgument(directive.getName(), argument.getName(), argument.getArgumentValue(), argument.getType());
41+
}
42+
}
43+
}
44+
45+
private void checkArgument(String directiveName, String argumentName, InputValueWithState argumentValue, GraphQLInputType argumentType) {
46+
boolean invalid = !inputValueValidator.isValidValue(argumentValue, argumentType)
47+
|| (argumentValue.isNotSet() && isNonNull(argumentType));
48+
if (invalid) {
49+
errors.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidAppliedDirectiveArgument,
50+
format("Invalid argument '%s' for applied directive of name '%s'", argumentName, directiveName)));
51+
}
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package graphql.schema.validation;
2+
3+
import graphql.Internal;
4+
import graphql.schema.GraphQLDirective;
5+
import graphql.schema.GraphQLDirectiveContainer;
6+
import graphql.schema.GraphQLSchema;
7+
import org.jspecify.annotations.NullMarked;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import static java.lang.String.format;
13+
14+
/**
15+
* Schema validation rule ensuring applied directives are defined in the schema and that non repeatable
16+
* directives are applied at most once.
17+
*/
18+
@Internal
19+
@NullMarked
20+
public class AppliedDirectivesAreValid implements SchemaValidationRule {
21+
22+
private final GraphQLSchema schema;
23+
private final SchemaValidationErrorCollector errors;
24+
25+
public AppliedDirectivesAreValid(GraphQLSchema schema, SchemaValidationErrorCollector errors) {
26+
this.schema = schema;
27+
this.errors = errors;
28+
}
29+
30+
@Override
31+
public void checkDirectiveContainer(GraphQLDirectiveContainer container) {
32+
Map<String, List<GraphQLDirective>> directivesByName = container.getAllDirectivesByName();
33+
if (directivesByName.isEmpty()) {
34+
return;
35+
}
36+
for (Map.Entry<String, List<GraphQLDirective>> entry : directivesByName.entrySet()) {
37+
GraphQLDirective directiveDef = schema.getDirective(entry.getKey());
38+
if (directiveDef == null) {
39+
addError(SchemaValidationErrorType.InvalidAppliedDirective, format("A definition for directive '%s' could not be found", entry.getKey()));
40+
} else if (directiveDef.isNonRepeatable() && entry.getValue().size() > 1) {
41+
addError(SchemaValidationErrorType.InvalidAppliedDirective,
42+
format("The directive '%s' on the '%s' called '%s' is a non repeatable directive but has been applied %d times",
43+
directiveDef.getName(), container.getClass().getSimpleName(), container.getName(), entry.getValue().size()));
44+
}
45+
}
46+
}
47+
48+
private void addError(SchemaValidationErrorType errorType, String message) {
49+
errors.addError(new SchemaValidationError(errorType, message));
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package graphql.schema.validation;
2+
3+
import graphql.Internal;
4+
import graphql.schema.GraphQLArgument;
5+
import graphql.schema.GraphQLInputObjectField;
6+
import graphql.schema.GraphQLInputObjectType;
7+
import graphql.schema.GraphQLInputType;
8+
import graphql.schema.GraphQLSchemaElement;
9+
import graphql.schema.InputValueWithState;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
import static graphql.schema.GraphQLTypeUtil.simplePrint;
13+
import static java.lang.String.format;
14+
15+
/**
16+
* Schema validation rule ensuring argument and input field default values are valid for their types.
17+
*/
18+
@Internal
19+
@NullMarked
20+
public class DefaultValuesAreValid implements SchemaValidationRule {
21+
22+
private final InputValueValidator inputValueValidator;
23+
private final SchemaValidationErrorCollector errors;
24+
25+
public DefaultValuesAreValid(InputValueValidator inputValueValidator, SchemaValidationErrorCollector errors) {
26+
this.inputValueValidator = inputValueValidator;
27+
this.errors = errors;
28+
}
29+
30+
@Override
31+
public void checkInputObjectField(GraphQLInputObjectType inputObjectType, GraphQLInputObjectField inputObjectField) {
32+
if (inputObjectField.hasSetDefaultValue()) {
33+
checkDefaultValue(inputObjectField.getInputFieldDefaultValue(), inputObjectField.getType());
34+
}
35+
}
36+
37+
@Override
38+
public void checkArgument(GraphQLSchemaElement fieldOrDirective, GraphQLArgument argument) {
39+
if (argument.hasSetDefaultValue()) {
40+
checkDefaultValue(argument.getArgumentDefaultValue(), argument.getType());
41+
}
42+
}
43+
44+
private void checkDefaultValue(InputValueWithState defaultValue, GraphQLInputType type) {
45+
if (inputValueValidator.isValidValue(defaultValue, type)) {
46+
return;
47+
}
48+
errors.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidDefaultValue,
49+
format("Invalid default value %s for type %s", defaultValue.getValue(), simplePrint(type))));
50+
}
51+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package graphql.schema.validation;
2+
3+
import graphql.Directives;
4+
import graphql.Internal;
5+
import graphql.schema.GraphQLArgument;
6+
import graphql.schema.GraphQLDirective;
7+
import graphql.schema.GraphQLFieldDefinition;
8+
import graphql.schema.GraphQLInputObjectField;
9+
import graphql.schema.GraphQLInputObjectType;
10+
import graphql.schema.GraphQLSchemaElement;
11+
import org.jspecify.annotations.NullMarked;
12+
13+
import static graphql.schema.GraphQLTypeUtil.isNonNull;
14+
import static java.lang.String.format;
15+
16+
/**
17+
* Spec: the @deprecated directive must not appear on required (non-null without a default) arguments
18+
* or input object field definitions.
19+
*/
20+
@Internal
21+
@NullMarked
22+
public class DeprecatedInputObjectAndArgumentsAreValid implements SchemaValidationRule {
23+
24+
private static final String DEPRECATED_DIRECTIVE_NAME = Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName();
25+
26+
private final SchemaValidationErrorCollector errors;
27+
28+
public DeprecatedInputObjectAndArgumentsAreValid(SchemaValidationErrorCollector errors) {
29+
this.errors = errors;
30+
}
31+
32+
@Override
33+
public void checkInputObjectField(GraphQLInputObjectType type, GraphQLInputObjectField inputObjectField) {
34+
if (!isNonNull(inputObjectField.getType()) || inputObjectField.hasSetDefaultValue()) {
35+
return;
36+
}
37+
if (inputObjectField.getAppliedDirective(DEPRECATED_DIRECTIVE_NAME) == null) {
38+
return;
39+
}
40+
addError(SchemaValidationErrorType.RequiredInputFieldCannotBeDeprecated,
41+
format("Required input field '%s.%s' cannot be deprecated.", type.getName(), inputObjectField.getName()));
42+
}
43+
44+
@Override
45+
public void checkArgument(GraphQLSchemaElement fieldOrDirective, GraphQLArgument argument) {
46+
if (!argument.isDeprecated() || !isNonNull(argument.getType()) || argument.hasSetDefaultValue()) {
47+
return;
48+
}
49+
if (fieldOrDirective instanceof GraphQLFieldDefinition) {
50+
addError(SchemaValidationErrorType.RequiredFieldArgumentCannotBeDeprecated,
51+
format("Required argument '%s' on field '%s' cannot be deprecated.", argument.getName(), ((GraphQLFieldDefinition) fieldOrDirective).getName()));
52+
} else if (fieldOrDirective instanceof GraphQLDirective) {
53+
addError(SchemaValidationErrorType.RequiredDirectiveArgumentCannotBeDeprecated,
54+
format("Required argument '%s' on directive '%s' cannot be deprecated.", argument.getName(), ((GraphQLDirective) fieldOrDirective).getName()));
55+
}
56+
}
57+
58+
private void addError(SchemaValidationErrorType errorType, String message) {
59+
errors.addError(new SchemaValidationError(errorType, message));
60+
}
61+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package graphql.schema.validation;
2+
3+
import graphql.Internal;
4+
import graphql.schema.GraphQLArgument;
5+
import graphql.schema.GraphQLFieldDefinition;
6+
import graphql.schema.GraphQLFieldsContainer;
7+
import graphql.schema.GraphQLInputObjectField;
8+
import graphql.schema.GraphQLInputObjectType;
9+
import graphql.schema.GraphQLInputType;
10+
import graphql.schema.GraphQLModifiedType;
11+
import graphql.schema.GraphQLOutputType;
12+
import graphql.schema.GraphQLType;
13+
import org.jspecify.annotations.NullMarked;
14+
15+
import static graphql.schema.GraphQLTypeUtil.simplePrint;
16+
import static java.lang.String.format;
17+
18+
/**
19+
* Schema validation rule ensuring output types are only used in output positions and input types only in input positions.
20+
*/
21+
@Internal
22+
@NullMarked
23+
public class InputAndOutputTypesUsedAppropriately implements SchemaValidationRule {
24+
25+
private final SchemaValidationErrorCollector errors;
26+
27+
public InputAndOutputTypesUsedAppropriately(SchemaValidationErrorCollector errors) {
28+
this.errors = errors;
29+
}
30+
31+
@Override
32+
public void checkFieldDefinition(GraphQLFieldsContainer container, GraphQLFieldDefinition fieldDefinition) {
33+
for (GraphQLArgument argument : fieldDefinition.getArguments()) {
34+
if (hasMisusedType(argument.getType(), true)) {
35+
String path = container.getName() + "." + fieldDefinition.getName() + "." + argument.getName();
36+
addTypeContextErrors(argument.getType(), true, path);
37+
}
38+
}
39+
if (hasMisusedType(fieldDefinition.getType(), false)) {
40+
String path = container.getName() + "." + fieldDefinition.getName();
41+
addTypeContextErrors(fieldDefinition.getType(), false, path);
42+
}
43+
}
44+
45+
@Override
46+
public void checkInputObjectField(GraphQLInputObjectType type, GraphQLInputObjectField inputObjectField) {
47+
if (hasMisusedType(inputObjectField.getType(), true)) {
48+
String path = type.getName() + "." + inputObjectField.getName();
49+
addTypeContextErrors(inputObjectField.getType(), true, path);
50+
}
51+
}
52+
53+
private boolean hasMisusedType(GraphQLType type, boolean inputContext) {
54+
GraphQLType current = type;
55+
while (true) {
56+
if (isMisused(current, inputContext)) {
57+
return true;
58+
}
59+
if (!(current instanceof GraphQLModifiedType)) {
60+
return false;
61+
}
62+
current = ((GraphQLModifiedType) current).getWrappedType();
63+
}
64+
}
65+
66+
private void addTypeContextErrors(GraphQLType type, boolean inputContext, String path) {
67+
GraphQLType current = type;
68+
while (true) {
69+
if (isMisused(current, inputContext)) {
70+
addTypeContextError(current, inputContext, path);
71+
}
72+
if (!(current instanceof GraphQLModifiedType)) {
73+
return;
74+
}
75+
current = ((GraphQLModifiedType) current).getWrappedType();
76+
}
77+
}
78+
79+
private void addTypeContextError(GraphQLType misusedType, boolean inputContext, String path) {
80+
if (inputContext) {
81+
addError(SchemaValidationErrorType.OutputTypeUsedInInputTypeContext,
82+
format("The output type '%s' has been used in an input type context : '%s'", simplePrint(misusedType), path));
83+
} else {
84+
addError(SchemaValidationErrorType.InputTypeUsedInOutputTypeContext,
85+
format("The input type '%s' has been used in a output type context : '%s'", simplePrint(misusedType), path));
86+
}
87+
}
88+
89+
private boolean isMisused(GraphQLType type, boolean inputContext) {
90+
return inputContext ? !(type instanceof GraphQLInputType) : !(type instanceof GraphQLOutputType);
91+
}
92+
93+
private void addError(SchemaValidationErrorType errorType, String message) {
94+
errors.addError(new SchemaValidationError(errorType, message));
95+
}
96+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package graphql.schema.validation;
2+
3+
import graphql.GraphQLContext;
4+
import graphql.Internal;
5+
import graphql.execution.NonNullableValueCoercedAsNullException;
6+
import graphql.execution.ValuesResolver;
7+
import graphql.language.Value;
8+
import graphql.schema.CoercingParseValueException;
9+
import graphql.schema.GraphQLInputType;
10+
import graphql.schema.GraphQLSchema;
11+
import graphql.schema.InputValueWithState;
12+
import graphql.validation.ValidationUtil;
13+
import org.jspecify.annotations.NullMarked;
14+
import org.jspecify.annotations.Nullable;
15+
16+
import java.util.Locale;
17+
18+
/**
19+
* Checks that default values and applied directive argument values are valid for their input types.
20+
*/
21+
@Internal
22+
@NullMarked
23+
public class InputValueValidator {
24+
25+
private final ValidationUtil validationUtil = new ValidationUtil();
26+
private final GraphQLContext graphQLContext = GraphQLContext.getDefault();
27+
private final GraphQLSchema schema;
28+
private final SchemaValidationErrorCollector errors;
29+
30+
public InputValueValidator(GraphQLSchema schema, SchemaValidationErrorCollector errors) {
31+
this.schema = schema;
32+
this.errors = errors;
33+
}
34+
35+
public boolean isValidValue(InputValueWithState value, GraphQLInputType type) {
36+
if (value.isLiteral()) {
37+
return validationUtil.isValidLiteralValue((Value<?>) value.getValue(), type, schema, graphQLContext, Locale.getDefault());
38+
}
39+
// coercing external values expands input field defaults, which never terminates once defaults form a cycle
40+
if (!value.isExternal() || errors.containsValidationError(SchemaValidationErrorType.DefaultValueCircularRef)) {
41+
return true;
42+
}
43+
return isValidExternalValue(value.getValue(), type);
44+
}
45+
46+
private boolean isValidExternalValue(@Nullable Object externalValue, GraphQLInputType type) {
47+
try {
48+
ValuesResolver.externalValueToInternalValue(schema.getCodeRegistry().getFieldVisibility(), externalValue, type, graphQLContext, Locale.getDefault());
49+
return true;
50+
} catch (CoercingParseValueException | NonNullableValueCoercedAsNullException e) {
51+
return false;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)