3
3
const test = require ( 'tap' ) . test
4
4
const validator = require ( 'is-my-json-valid' )
5
5
const build = require ( '..' )
6
+ const Ajv = require ( 'ajv' )
6
7
7
8
test ( 'error on invalid largeArrayMechanism' , ( t ) => {
8
9
t . plan ( 1 )
@@ -265,7 +266,7 @@ test('array items is a list of schema and additionalItems is true, just the desc
265
266
t . equal ( result , '{"foo":["foo"]}' )
266
267
} )
267
268
268
- test ( 'array items is a list of schema and additionalItems is false' , ( t ) => {
269
+ test ( 'array items is a list of schema and additionalItems is false /1 ' , ( t ) => {
269
270
t . plan ( 1 )
270
271
271
272
const schema = {
@@ -274,9 +275,7 @@ test('array items is a list of schema and additionalItems is false', (t) => {
274
275
foo : {
275
276
type : 'array' ,
276
277
items : [
277
- {
278
- type : 'string'
279
- }
278
+ { type : 'string' }
280
279
] ,
281
280
additionalItems : false
282
281
}
@@ -285,17 +284,55 @@ test('array items is a list of schema and additionalItems is false', (t) => {
285
284
286
285
const stringify = build ( schema )
287
286
288
- try {
289
- stringify ( {
290
- foo : [
291
- 'foo' ,
292
- 'bar'
293
- ]
294
- } )
295
- t . fail ( )
296
- } catch ( error ) {
297
- t . ok ( / d o e s n o t m a t c h s c h e m a d e f i n i t i o n ./ . test ( error . message ) )
287
+ t . throws ( ( ) => stringify ( { foo : [ 'foo' , 'bar' ] } ) , new Error ( 'Item at 1 does not match schema definition.' ) )
288
+ } )
289
+
290
+ test ( 'array items is a list of schema and additionalItems is false /2' , ( t ) => {
291
+ t . plan ( 3 )
292
+
293
+ const schema = {
294
+ type : 'object' ,
295
+ properties : {
296
+ foo : {
297
+ type : 'array' ,
298
+ items : [
299
+ { type : 'string' } ,
300
+ { type : 'string' }
301
+ ] ,
302
+ additionalItems : false
303
+ }
304
+ }
305
+ }
306
+
307
+ const stringify = build ( schema )
308
+
309
+ t . throws ( ( ) => stringify ( { foo : [ 1 , 'bar' ] } ) , new Error ( 'Item at 0 does not match schema definition.' ) )
310
+ t . throws ( ( ) => stringify ( { foo : [ 'foo' , 1 ] } ) , new Error ( 'Item at 1 does not match schema definition.' ) )
311
+ t . throws ( ( ) => stringify ( { foo : [ 'foo' , 'bar' , 'baz' ] } ) , new Error ( 'Item at 2 does not match schema definition.' ) )
312
+ } )
313
+
314
+ test ( 'array items is a schema and additionalItems is false' , ( t ) => {
315
+ t . plan ( 2 )
316
+
317
+ const schema = {
318
+ type : 'object' ,
319
+ properties : {
320
+ foo : {
321
+ type : 'array' ,
322
+ items : { type : 'string' } ,
323
+ additionalItems : false
324
+ }
325
+ }
298
326
}
327
+
328
+ const stringify = build ( schema )
329
+
330
+ // ajv ignores additionalItems if items is not an Array
331
+ const ajv = new Ajv ( { allErrors : true , strict : false } )
332
+
333
+ const validate = ajv . compile ( schema )
334
+ t . same ( stringify ( { foo : [ 'foo' , 'bar' ] } ) , '{"foo":["foo","bar"]}' )
335
+ t . equal ( validate ( { foo : [ 'foo' , 'bar' ] } ) , true )
299
336
} )
300
337
301
338
// https://github.com/fastify/fast-json-stringify/issues/279
0 commit comments