Skip to content

Commit 8aab377

Browse files
committed
validation: Simplify 'ValuesOfCorrectType' rule (#2158)
1 parent e0e8b0e commit 8aab377

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

src/validation/rules/ValuesOfCorrectType.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import suggestionList from '../../jsutils/suggestionList';
1010

1111
import { GraphQLError } from '../../error/GraphQLError';
1212

13-
import { type ValueNode } from '../../language/ast';
13+
import { Kind } from '../../language/kinds';
1414
import { print } from '../../language/printer';
15+
import { type ValueNode } from '../../language/ast';
1516
import { type ASTVisitor } from '../../language/visitor';
1617

1718
import {
@@ -76,27 +77,19 @@ export function unknownFieldMessage(
7677
*/
7778
export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor {
7879
return {
79-
NullValue(node) {
80-
const type = context.getInputType();
81-
if (isNonNullType(type)) {
82-
context.reportError(
83-
new GraphQLError(badValueMessage(inspect(type), print(node)), node),
84-
);
85-
}
86-
},
8780
ListValue(node) {
8881
// Note: TypeInfo will traverse into a list's item type, so look to the
8982
// parent input type to check if it is a list.
9083
const type = getNullableType(context.getParentInputType());
9184
if (!isListType(type)) {
92-
isValidScalar(context, node);
85+
isValidValueNode(context, node);
9386
return false; // Don't traverse further.
9487
}
9588
},
9689
ObjectValue(node) {
9790
const type = getNamedType(context.getInputType());
9891
if (!isInputObjectType(type)) {
99-
isValidScalar(context, node);
92+
isValidValueNode(context, node);
10093
return false; // Don't traverse further.
10194
}
10295
// Ensure every required field exists.
@@ -130,35 +123,27 @@ export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor {
130123
);
131124
}
132125
},
133-
EnumValue(node) {
134-
const type = getNamedType(context.getInputType());
135-
if (!isEnumType(type)) {
136-
isValidScalar(context, node);
137-
} else if (!type.getValue(node.value)) {
126+
NullValue(node) {
127+
const type = context.getInputType();
128+
if (isNonNullType(type)) {
138129
context.reportError(
139-
new GraphQLError(
140-
badEnumValueMessage(
141-
type.name,
142-
print(node),
143-
enumTypeSuggestion(type, node),
144-
),
145-
node,
146-
),
130+
new GraphQLError(badValueMessage(inspect(type), print(node)), node),
147131
);
148132
}
149133
},
150-
IntValue: node => isValidScalar(context, node),
151-
FloatValue: node => isValidScalar(context, node),
152-
StringValue: node => isValidScalar(context, node),
153-
BooleanValue: node => isValidScalar(context, node),
134+
EnumValue: node => isValidValueNode(context, node),
135+
IntValue: node => isValidValueNode(context, node),
136+
FloatValue: node => isValidValueNode(context, node),
137+
StringValue: node => isValidValueNode(context, node),
138+
BooleanValue: node => isValidValueNode(context, node),
154139
};
155140
}
156141

157142
/**
158143
* Any value literal may be a valid representation of a Scalar, depending on
159144
* that scalar type.
160145
*/
161-
function isValidScalar(context: ValidationContext, node: ValueNode): void {
146+
function isValidValueNode(context: ValidationContext, node: ValueNode): void {
162147
// Report any error at the full type expected by the location.
163148
const locationType = context.getInputType();
164149
if (!locationType) {
@@ -167,15 +152,29 @@ function isValidScalar(context: ValidationContext, node: ValueNode): void {
167152

168153
const type = getNamedType(locationType);
169154

155+
if (isEnumType(type)) {
156+
if (node.kind !== Kind.ENUM || !type.getValue(node.value)) {
157+
context.reportError(
158+
new GraphQLError(
159+
badEnumValueMessage(
160+
type.name,
161+
print(node),
162+
enumTypeSuggestion(type, node),
163+
),
164+
node,
165+
),
166+
);
167+
}
168+
return;
169+
}
170+
170171
if (!isScalarType(type)) {
171-
const message = isEnumType(type)
172-
? badEnumValueMessage(
173-
inspect(locationType),
174-
print(node),
175-
enumTypeSuggestion(type, node),
176-
)
177-
: badValueMessage(inspect(locationType), print(node));
178-
context.reportError(new GraphQLError(message, node));
172+
context.reportError(
173+
new GraphQLError(
174+
badValueMessage(inspect(locationType), print(node)),
175+
node,
176+
),
177+
);
179178
return;
180179
}
181180

0 commit comments

Comments
 (0)