11import { jest } from '@jest/globals'
2- import Ajv from 'ajv'
32
3+ import { getJsonValidator , validateJson } from '#src/tests/lib/validate-json-schema.js'
44import readJsonFile from '#src/frame/lib/read-json-file.js'
55import { schemaValidator , previewsValidator , upcomingChangesValidator } from '../lib/validator.js'
66import { formatAjvErrors } from '#src/tests/helpers/schemas.js'
@@ -11,15 +11,8 @@ const allVersionValues = Object.values(allVersions)
1111const graphqlVersions = allVersionValues . map ( ( v ) => v . openApiVersionName )
1212const graphqlTypes = readJsonFile ( './src/graphql/lib/types.json' ) . map ( ( t ) => t . kind )
1313
14- const ajv = new Ajv ( { allErrors : true , allowUnionTypes : true } )
15- const previewsValidate = ajv . compile ( previewsValidator )
16- const upcomingChangesValidate = ajv . compile ( upcomingChangesValidator )
17- // setup ajv validator functions for each graphql type (e.g. queries, mutations,
18- // etc.)
19- const schemaValidatorFunctions = { }
20- graphqlTypes . forEach ( ( type ) => {
21- schemaValidatorFunctions [ type ] = ajv . compile ( schemaValidator [ type ] )
22- } )
14+ const previewsValidate = getJsonValidator ( previewsValidator )
15+ const upcomingChangesValidate = getJsonValidator ( upcomingChangesValidator )
2316
2417describe ( 'graphql json files' , ( ) => {
2518 jest . setTimeout ( 3 * 60 * 1000 )
@@ -38,16 +31,16 @@ describe('graphql json files', () => {
3831 if ( typeObjsTested . has ( key ) ) return
3932 typeObjsTested . add ( key )
4033
41- const valid = schemaValidatorFunctions [ type ] ( typeObj )
42- let errors
34+ const { isValid, errors } = validateJson ( schemaValidator [ type ] , typeObj )
4335
44- if ( ! valid ) {
45- errors = `kind: ${ typeObj . kind } , name: ${ typeObj . name } : ${ formatAjvErrors (
46- schemaValidatorFunctions [ type ] . errors ,
36+ let formattedErrors = errors
37+ if ( ! isValid ) {
38+ formattedErrors = `kind: ${ typeObj . kind } , name: ${ typeObj . name } : ${ formatAjvErrors (
39+ errors ,
4740 ) } `
4841 }
4942
50- expect ( valid , errors ) . toBe ( true )
43+ expect ( isValid , formattedErrors ) . toBe ( true )
5144 } )
5245 } )
5346 } )
@@ -57,14 +50,14 @@ describe('graphql json files', () => {
5750 graphqlVersions . forEach ( ( version ) => {
5851 const previews = readJsonFile ( `${ GRAPHQL_DATA_DIR } /${ version } /previews.json` )
5952 previews . forEach ( ( preview ) => {
60- const valid = previewsValidate ( preview )
53+ const isValid = previewsValidate ( preview )
6154 let errors
6255
63- if ( ! valid ) {
56+ if ( ! isValid ) {
6457 errors = formatAjvErrors ( previewsValidate . errors )
6558 }
6659
67- expect ( valid , errors ) . toBe ( true )
60+ expect ( isValid , errors ) . toBe ( true )
6861 } )
6962 } )
7063 } )
@@ -75,14 +68,14 @@ describe('graphql json files', () => {
7568 for ( const changes of Object . values ( upcomingChanges ) ) {
7669 // each object value is an array of changes
7770 changes . forEach ( ( changeObj ) => {
78- const valid = upcomingChangesValidate ( changeObj )
71+ const isValid = upcomingChangesValidate ( changeObj )
7972 let errors
8073
81- if ( ! valid ) {
74+ if ( ! isValid ) {
8275 errors = formatAjvErrors ( upcomingChangesValidate . errors )
8376 }
8477
85- expect ( valid , errors ) . toBe ( true )
78+ expect ( isValid , errors ) . toBe ( true )
8679 } )
8780 }
8881 } )
0 commit comments