1
1
'use strict'
2
2
3
- const tape = require ( 'tape' )
4
3
const fs = require ( 'fs' )
5
4
const path = require ( 'path' )
6
- const { validator, parser } = require ( '../' )
7
5
const schemas = require ( './util/schemas' )
6
+ const { processTest } = require ( './util/json-schema-test' )
8
7
9
8
// these tests require lax mode
10
9
const unsafe = new Set ( [
@@ -89,7 +88,7 @@ function processTestDir(schemaDir, main, subdir = '') {
89
88
const sub = path . join ( subdir , file ) // relative to schemaDir
90
89
if ( shouldIngore ( sub ) ) continue
91
90
if ( file . endsWith ( '.json' ) ) {
92
- const content = fs . readFileSync ( path . join ( dir , file ) )
91
+ const content = fs . readFileSync ( path . join ( dir , file ) , 'utf-8' )
93
92
processTest ( main , sub , JSON . parse ( content ) , shouldIngore , requiresLax )
94
93
} else {
95
94
// assume it's a dir and let it fail otherwise
@@ -98,57 +97,6 @@ function processTestDir(schemaDir, main, subdir = '') {
98
97
}
99
98
}
100
99
101
- const schemaVersions = new Map (
102
- Object . entries ( {
103
- 'draft2019-09' : 'http://json-schema.org/draft/2019-09/schema#' ,
104
- draft7 : 'http://json-schema.org/draft-07/schema#' ,
105
- draft6 : 'http://json-schema.org/draft-06/schema#' ,
106
- draft4 : 'http://json-schema.org/draft-04/schema#' ,
107
- draft3 : 'http://json-schema.org/draft-03/schema#' ,
108
- } )
109
- )
110
-
111
- function processTest ( main , id , file , shouldIngore , requiresLax ) {
112
- for ( const block of file ) {
113
- if ( shouldIngore ( `${ id } /${ block . description } ` ) ) continue
114
- tape ( `json-schema-test-suite ${ main } /${ id } /${ block . description } ` , ( t ) => {
115
- try {
116
- const mode = requiresLax ( `${ id } /${ block . description } ` ) ? 'lax' : 'default'
117
- const $schemaDefault = schemaVersions . get ( main )
118
- const extraFormats = main === 'draft3' // needs old formats
119
- const blockSchemas = [
120
- ...( Object . hasOwnProperty . call ( block , 'schema' ) ? [ block . schema ] : [ ] ) ,
121
- ...( block . schemas || [ ] ) ,
122
- ]
123
- for ( const schema of blockSchemas ) {
124
- for ( const [ includeErrors , allErrors ] of [ [ false , false ] , [ true , false ] , [ true , true ] ] ) {
125
- // ajv sometimes specifies just the schema id as "schema"
126
- const wrapped = typeof schema === 'string' ? { $ref : schema } : schema
127
- const opts = { schemas, mode, $schemaDefault, extraFormats, includeErrors, allErrors }
128
- const validate = validator ( wrapped , opts )
129
- const parse = parser ( wrapped , opts )
130
- for ( const test of block . tests ) {
131
- if ( shouldIngore ( `${ id } /${ block . description } /${ test . description } ` ) ) continue
132
- t . same ( validate ( test . data ) , test . valid , test . description )
133
- t . same ( parse ( JSON . stringify ( test . data ) ) . valid , test . valid , test . description )
134
- }
135
- if ( mode === 'lax' ) {
136
- t . throws (
137
- ( ) => validator ( wrapped , { ...opts , mode : 'default' } ) ,
138
- 'Throws without lax mode'
139
- )
140
- }
141
- }
142
- }
143
- } catch ( e ) {
144
- t . fail ( e )
145
- } finally {
146
- t . end ( )
147
- }
148
- } )
149
- }
150
- }
151
-
152
100
/** JSON Schema Test Suite tests **/
153
101
const testsDir = 'JSON-Schema-Test-Suite/tests'
154
102
processTestDir ( testsDir , 'draft4' )
0 commit comments