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