This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLStandardSchema_test.ts
More file actions
113 lines (97 loc) · 3.86 KB
/
GraphQLStandardSchema_test.ts
File metadata and controls
113 lines (97 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { findIssues, graphQLResponseSchema } from './GraphQLStandardSchema.ts'
import { assertEquals, assertRejects } from 'jsr:@std/assert'
import type { StandardSchemaV1 } from './StandardSchemaV1.ts'
// Helper function from https://standardschema.dev/ to validate an input
export async function standardValidate<T extends StandardSchemaV1>(
schema: T,
input: StandardSchemaV1.InferInput<T>,
): Promise<StandardSchemaV1.InferOutput<T>> {
let result = schema['~standard'].validate(input)
if (result instanceof Promise) result = await result
// if the `issues` field exists, the validation failed
if (result.issues) {
throw new Error(JSON.stringify(result.issues, null, 2))
}
return result.value
}
Deno.test('GraphQLStandardSchema should work as expected when no options are provided', async () => {
const schema = graphQLResponseSchema()
assertEquals(schema['~standard'].vendor, 'dreamit')
assertEquals(schema['~standard'].version, 1)
// Case: Value is a string and parseStringToObject is false
await assertRejects(
async () => {
await standardValidate(schema, 'string')
},
Error,
'Provided value is a string and \\"parseStringToObject\\" option is disabled',
)
// Case: Value is an object but undefined
await assertRejects(
async () => {
await standardValidate(schema, {})
},
Error,
'GraphQL response should contain at least one of data or error field, but both are missing.',
)
// Case: Data is set and valid
assertEquals(
await standardValidate(schema, { data: { message: 'OK' } }),
{ data: { message: 'OK' } },
)
})
Deno.test('GraphQLStandardSchema should work as expected when parseStringToObject is enabled', async () => {
const schema = graphQLResponseSchema({ parseStringToObject: true })
// Case: Value is a string and parseStringToObject is false
await assertRejects(
async () => {
await standardValidate(schema, 'string')
},
Error,
'String value could not be parsed to object. Error is SyntaxError: Unexpected token \'s\', \\"string\\" is not valid JSON"',
)
})
Deno.test('findIssues should find expected issues', () => {
// Case: Both data and errors missing
assertEquals(
findIssues({}).at(0)?.message,
'GraphQL response should contain at least one of data or error field, but both are missing.',
)
// Case: Errors is set but empty
assertEquals(
findIssues({ errors: [] }).at(0)
?.message,
'GraphQL response contains empty "errors" field but should at least have one error entry.',
)
// Case: Data is set but not an object
assertEquals(
findIssues({ data: 'string' }).at(0)
?.message,
'GraphQL response contains "data" field of type "string" but "data" should be an "object".',
)
// Case: Data is set but not an object
assertEquals(
findIssues({ errors: 'string' }).at(0)
?.message,
'GraphQL response contains "errors" field of type "string" but "errors" should be an "array".',
)
// Case: Extensions is set but not an object
assertEquals(
findIssues({ data: {}, extensions: 'string' }).at(0)
?.message,
'GraphQL response contains "extensions" field of type "string" but "extensions" should be an "object".',
)
// Case: Additional field myCustomField is set and allowAdditionalFieldsInResponse is false
assertEquals(
findIssues({ data: {}, myCustomField: 'string' }).at(0)
?.message,
'GraphQL response should contain only "data", "errors" or "extensions" fields but has the following fields: "myCustomField".',
)
// Case: Additional field myCustomField is set and allowAdditionalFieldsInResponse is true
assertEquals(
findIssues({ data: {}, myCustomField: 'string' }, {
allowAdditionalFieldsInResponse: true,
}).length,
0,
)
})