Skip to content

Commit c7f677b

Browse files
leebyronyaacovCR
authored andcommitted
Input Value Validation
Factors out input validation to reusable functions: * Introduces `validateInputLiteral` by extracting this behavior from `ValuesOfCorrectTypeRule`. * Introduces `validateInputValue` by extracting this behavior from `coerceInputValue` * Simplifies `coerceInputValue` to return early on validation error * Unifies error reporting between `validateInputValue` and `validateInputLiteral`, causing some error message strings to change, but error data (eg locations) are preserved. These two parallel functions will be used to validate default values in #3049 Potentially breaking if you rely on the existing behavior of `coerceInputValue` to call a callback function, as the call signature has changed. GraphQL behavior should not change, though error messages are now slightly different.
1 parent 3ea9f70 commit c7f677b

14 files changed

+1472
-661
lines changed

src/execution/__tests__/nonnull-test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ describe('Execute: handles non-nullable types', () => {
647647
errors: [
648648
{
649649
message:
650-
'Argument Query.withNonNullArg(cannotBeNull:) of non-null type String! must not be null.',
650+
'Argument Query.withNonNullArg(cannotBeNull:) has invalid value: Expected value of non-null type String! not to be null.',
651651
locations: [{ line: 3, column: 42 }],
652652
path: ['withNonNullArg'],
653653
},
@@ -677,7 +677,7 @@ describe('Execute: handles non-nullable types', () => {
677677
errors: [
678678
{
679679
message:
680-
'Argument Query.withNonNullArg(cannotBeNull:) of required type String! was provided the variable "$testVar" which was not provided a runtime value.',
680+
'Argument Query.withNonNullArg(cannotBeNull:) has invalid value: Expected variable "$testVar" provided to type String! to provide a runtime value.',
681681
locations: [{ line: 3, column: 42 }],
682682
path: ['withNonNullArg'],
683683
},
@@ -705,7 +705,7 @@ describe('Execute: handles non-nullable types', () => {
705705
errors: [
706706
{
707707
message:
708-
'Argument Query.withNonNullArg(cannotBeNull:) of non-null type String! must not be null.',
708+
'Argument Query.withNonNullArg(cannotBeNull:) has invalid value: Expected variable "$testVar" provided to non-null type String! not to be null.',
709709
locations: [{ line: 3, column: 43 }],
710710
path: ['withNonNullArg'],
711711
},

src/execution/__tests__/subscribe-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ describe('Subscription Initialization Phase', () => {
523523
errors: [
524524
{
525525
message:
526-
'Variable "$arg" got invalid value "meow"; Int cannot represent non-integer value: "meow"',
526+
'Variable "$arg" has invalid value: Int cannot represent non-integer value: "meow"',
527527
locations: [{ line: 2, column: 21 }],
528528
},
529529
],

src/execution/__tests__/variables-test.ts

+20-19
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('Execute: Handles inputs', () => {
226226
errors: [
227227
{
228228
message:
229-
'Argument TestType.fieldWithObjectInput(input:) of type TestInputObject has invalid value ["foo", "bar", "baz"].',
229+
'Argument TestType.fieldWithObjectInput(input:) has invalid value: Expected value of type TestInputObject to be an object, found: ["foo", "bar", "baz"].',
230230
path: ['fieldWithObjectInput'],
231231
locations: [{ line: 3, column: 41 }],
232232
},
@@ -262,9 +262,10 @@ describe('Execute: Handles inputs', () => {
262262
errors: [
263263
{
264264
message:
265-
'Argument TestType.fieldWithObjectInput(input:) of type TestInputObject has invalid value { c: "foo", e: "bar" }.',
265+
'Argument TestType.fieldWithObjectInput(input:) has invalid value at .e: FaultyScalarErrorMessage',
266266
path: ['fieldWithObjectInput'],
267267
locations: [{ line: 3, column: 41 }],
268+
extensions: { code: 'FaultyScalarErrorExtensionCode' },
268269
},
269270
],
270271
});
@@ -418,7 +419,7 @@ describe('Execute: Handles inputs', () => {
418419
errors: [
419420
{
420421
message:
421-
'Variable "$input" got invalid value "SerializedValue" at "input.e"; FaultyScalarErrorMessage',
422+
'Variable "$input" has invalid value at .e: FaultyScalarErrorMessage',
422423
locations: [{ line: 2, column: 16 }],
423424
extensions: { code: 'FaultyScalarErrorExtensionCode' },
424425
},
@@ -434,7 +435,7 @@ describe('Execute: Handles inputs', () => {
434435
errors: [
435436
{
436437
message:
437-
'Variable "$input" got invalid value null at "input.c"; Expected non-nullable type "String!" not to be null.',
438+
'Variable "$input" has invalid value at .c: Expected value of non-null type String! not to be null.',
438439
locations: [{ line: 2, column: 16 }],
439440
},
440441
],
@@ -448,7 +449,7 @@ describe('Execute: Handles inputs', () => {
448449
errors: [
449450
{
450451
message:
451-
'Variable "$input" got invalid value "foo bar"; Expected type "TestInputObject" to be an object.',
452+
'Variable "$input" has invalid value: Expected value of type TestInputObject to be an object, found: "foo bar".',
452453
locations: [{ line: 2, column: 16 }],
453454
},
454455
],
@@ -462,7 +463,7 @@ describe('Execute: Handles inputs', () => {
462463
errors: [
463464
{
464465
message:
465-
'Variable "$input" got invalid value { a: "foo", b: "bar" }; Field "c" of required type "String!" was not provided.',
466+
'Variable "$input" has invalid value: Expected value of type TestInputObject to include required field "c", found: { a: "foo", b: "bar" }.',
466467
locations: [{ line: 2, column: 16 }],
467468
},
468469
],
@@ -481,12 +482,12 @@ describe('Execute: Handles inputs', () => {
481482
errors: [
482483
{
483484
message:
484-
'Variable "$input" got invalid value { a: "foo" } at "input.na"; Field "c" of required type "String!" was not provided.',
485+
'Variable "$input" has invalid value at .na: Expected value of type TestInputObject to include required field "c", found: { a: "foo" }.',
485486
locations: [{ line: 2, column: 18 }],
486487
},
487488
{
488489
message:
489-
'Variable "$input" got invalid value { na: { a: "foo" } }; Field "nb" of required type "String!" was not provided.',
490+
'Variable "$input" has invalid value: Expected value of type TestNestedInputObject to include required field "nb", found: { na: { a: "foo" } }.',
490491
locations: [{ line: 2, column: 18 }],
491492
},
492493
],
@@ -503,7 +504,7 @@ describe('Execute: Handles inputs', () => {
503504
errors: [
504505
{
505506
message:
506-
'Variable "$input" got invalid value { a: "foo", b: "bar", c: "baz", extra: "dog" }; Field "extra" is not defined by type "TestInputObject".',
507+
'Variable "$input" has invalid value: Expected value of type TestInputObject not to include unknown field "extra", found: { a: "foo", b: "bar", c: "baz", extra: "dog" }.',
507508
locations: [{ line: 2, column: 16 }],
508509
},
509510
],
@@ -678,7 +679,7 @@ describe('Execute: Handles inputs', () => {
678679
errors: [
679680
{
680681
message:
681-
'Variable "$value" of required type String! was not provided.',
682+
'Variable "$value" has invalid value: Expected a value of non-null type String! to be provided.',
682683
locations: [{ line: 2, column: 16 }],
683684
},
684685
],
@@ -697,7 +698,7 @@ describe('Execute: Handles inputs', () => {
697698
errors: [
698699
{
699700
message:
700-
'Variable "$value" of non-null type String! must not be null.',
701+
'Variable "$value" has invalid value: Expected value of non-null type String! not to be null.',
701702
locations: [{ line: 2, column: 16 }],
702703
},
703704
],
@@ -763,7 +764,7 @@ describe('Execute: Handles inputs', () => {
763764
errors: [
764765
{
765766
message:
766-
'Variable "$value" got invalid value [1, 2, 3]; String cannot represent a non string value: [1, 2, 3]',
767+
'Variable "$value" has invalid value: String cannot represent a non string value: [1, 2, 3]',
767768
locations: [{ line: 2, column: 16 }],
768769
},
769770
],
@@ -791,7 +792,7 @@ describe('Execute: Handles inputs', () => {
791792
errors: [
792793
{
793794
message:
794-
'Argument TestType.fieldWithNonNullableStringInput(input:) of required type String! was provided the variable "$foo" which was not provided a runtime value.',
795+
'Argument TestType.fieldWithNonNullableStringInput(input:) has invalid value: Expected variable "$foo" provided to type String! to provide a runtime value.',
795796
locations: [{ line: 3, column: 50 }],
796797
path: ['fieldWithNonNullableStringInput'],
797798
},
@@ -846,7 +847,7 @@ describe('Execute: Handles inputs', () => {
846847
errors: [
847848
{
848849
message:
849-
'Variable "$input" of non-null type [String]! must not be null.',
850+
'Variable "$input" has invalid value: Expected value of non-null type [String]! not to be null.',
850851
locations: [{ line: 2, column: 16 }],
851852
},
852853
],
@@ -909,7 +910,7 @@ describe('Execute: Handles inputs', () => {
909910
errors: [
910911
{
911912
message:
912-
'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.',
913+
'Variable "$input" has invalid value at [1]: Expected value of non-null type String! not to be null.',
913914
locations: [{ line: 2, column: 16 }],
914915
},
915916
],
@@ -928,7 +929,7 @@ describe('Execute: Handles inputs', () => {
928929
errors: [
929930
{
930931
message:
931-
'Variable "$input" of non-null type [String!]! must not be null.',
932+
'Variable "$input" has invalid value: Expected value of non-null type [String!]! not to be null.',
932933
locations: [{ line: 2, column: 16 }],
933934
},
934935
],
@@ -958,7 +959,7 @@ describe('Execute: Handles inputs', () => {
958959
errors: [
959960
{
960961
message:
961-
'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.',
962+
'Variable "$input" has invalid value at [1]: Expected value of non-null type String! not to be null.',
962963
locations: [{ line: 2, column: 16 }],
963964
},
964965
],
@@ -1043,7 +1044,7 @@ describe('Execute: Handles inputs', () => {
10431044
errors: [
10441045
{
10451046
message:
1046-
'Argument TestType.fieldWithDefaultArgumentValue(input:) of type String has invalid value WRONG_TYPE.',
1047+
'Argument TestType.fieldWithDefaultArgumentValue(input:) has invalid value: String cannot represent a non string value: WRONG_TYPE',
10471048
locations: [{ line: 3, column: 48 }],
10481049
path: ['fieldWithDefaultArgumentValue'],
10491050
},
@@ -1083,7 +1084,7 @@ describe('Execute: Handles inputs', () => {
10831084

10841085
function invalidValueError(value: number, index: number) {
10851086
return {
1086-
message: `Variable "$input" got invalid value ${value} at "input[${index}]"; String cannot represent a non string value: ${value}`,
1087+
message: `Variable "$input" has invalid value at [${index}]: String cannot represent a non string value: ${value}`,
10871088
locations: [{ line: 2, column: 14 }],
10881089
};
10891090
}

0 commit comments

Comments
 (0)