MCPcopy
hub / github.com/graphql/graphiql / validateValue

Function validateValue

packages/codemirror-graphql/src/variables/lint.ts:112–217  ·  view source on GitHub ↗
(
  type?: GraphQLType,
  valueAST?: ParseValueOutput,
)

Source from the content-addressed store, hash-verified

110
111// Returns a list of validation errors in the form Array<[Node, String]>.
112function validateValue(
113 type?: GraphQLType,
114 valueAST?: ParseValueOutput,
115): any[][] {
116 // TODO: Can't figure out the right type.
117 if (!type || !valueAST) {
118 return [];
119 }
120
121 // Validate non-nullable values.
122 if (type instanceof GraphQLNonNull) {
123 if (valueAST.kind === 'Null') {
124 return [[valueAST, `Type "${type}" is non-nullable and cannot be null.`]];
125 }
126 return validateValue(type.ofType, valueAST);
127 }
128
129 if (valueAST.kind === 'Null') {
130 return [];
131 }
132
133 // Validate lists of values, accepting a non-list as a list of one.
134 if (type instanceof GraphQLList) {
135 const itemType = type.ofType;
136 if (valueAST.kind === 'Array') {
137 const values = (valueAST as ParseArrayOutput).values || [];
138 return mapCat(values, item => validateValue(itemType, item));
139 }
140 return validateValue(itemType, valueAST);
141 }
142
143 // Validate input objects.
144 if (type instanceof GraphQLInputObjectType) {
145 if (valueAST.kind !== 'Object') {
146 return [[valueAST, `Type "${type}" must be an Object.`]];
147 }
148
149 // Validate each field in the input object.
150 const providedFields = Object.create(null);
151 const fieldErrors: any[][] = mapCat(
152 (valueAST as ParseObjectOutput).members,
153 member => {
154 // TODO: Can't figure out the right type here
155 const fieldName = member?.key?.value;
156 providedFields[fieldName] = true;
157 const inputField = type.getFields()[fieldName];
158 if (!inputField) {
159 return [
160 [
161 member.key,
162 `Type "${type}" does not have a field "${fieldName}".`,
163 ],
164 ];
165 }
166 const fieldType = inputField ? inputField.type : undefined;
167 return validateValue(fieldType, member.value);
168 },
169 );

Callers 1

validateVariablesFunction · 0.85

Calls 3

mapCatFunction · 0.85
isNullishFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected