@@ -318,8 +318,8 @@ test('missing values', (t) => {
318
318
} )
319
319
320
320
test ( 'patternProperties' , ( t ) => {
321
- t . plan ( 7 )
322
- let stringify = build ( {
321
+ t . plan ( 1 )
322
+ const stringify = build ( {
323
323
title : 'patternProperties' ,
324
324
type : 'object' ,
325
325
properties : {
@@ -336,8 +336,11 @@ test('patternProperties', (t) => {
336
336
337
337
let obj = { str : 'test' , foo : 42 , ofoo : true , foof : 'string' , objfoo : { a : true } , notMe : false }
338
338
t . equal ( '{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}' , stringify ( obj ) )
339
+ } )
339
340
340
- stringify = build ( {
341
+ test ( 'patternProperties should not change properties' , ( t ) => {
342
+ t . plan ( 1 )
343
+ const stringify = build ( {
341
344
title : 'patternProperties should not change properties' ,
342
345
type : 'object' ,
343
346
properties : {
@@ -352,10 +355,13 @@ test('patternProperties', (t) => {
352
355
}
353
356
} )
354
357
355
- obj = { foo : '42' , ofoo : 42 }
358
+ const obj = { foo : '42' , ofoo : 42 }
356
359
t . equal ( '{"ofoo":42,"foo":"42"}' , stringify ( obj ) )
360
+ } )
357
361
358
- stringify = build ( {
362
+ test ( 'patternProperties - string coerce' , ( t ) => {
363
+ t . plan ( 1 )
364
+ const stringify = build ( {
359
365
title : 'check string coerce' ,
360
366
type : 'object' ,
361
367
properties : { } ,
@@ -366,10 +372,13 @@ test('patternProperties', (t) => {
366
372
}
367
373
} )
368
374
369
- obj = { foo : true , ofoo : 42 , arrfoo : [ 'array' , 'test' ] , objfoo : { a : 'world' } }
375
+ const obj = { foo : true , ofoo : 42 , arrfoo : [ 'array' , 'test' ] , objfoo : { a : 'world' } }
370
376
t . equal ( '{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}' , stringify ( obj ) )
377
+ } )
371
378
372
- stringify = build ( {
379
+ test ( 'patternProperties - number coerce' , ( t ) => {
380
+ t . plan ( 1 )
381
+ const stringify = build ( {
373
382
title : 'check number coerce' ,
374
383
type : 'object' ,
375
384
properties : { } ,
@@ -380,10 +389,13 @@ test('patternProperties', (t) => {
380
389
}
381
390
} )
382
391
383
- obj = { foo : true , ofoo : '42' , xfoo : 'string' , arrfoo : [ 1 , 2 ] , objfoo : { num : 42 } }
392
+ const obj = { foo : true , ofoo : '42' , xfoo : 'string' , arrfoo : [ 1 , 2 ] , objfoo : { num : 42 } }
384
393
t . equal ( '{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}' , stringify ( obj ) )
394
+ } )
385
395
386
- stringify = build ( {
396
+ test ( 'patternProperties - boolean coerce' , ( t ) => {
397
+ t . plan ( 1 )
398
+ const stringify = build ( {
387
399
title : 'check boolean coerce' ,
388
400
type : 'object' ,
389
401
properties : { } ,
@@ -394,10 +406,13 @@ test('patternProperties', (t) => {
394
406
}
395
407
} )
396
408
397
- obj = { foo : 'true' , ofoo : 0 , arrfoo : [ 1 , 2 ] , objfoo : { a : true } }
409
+ const obj = { foo : 'true' , ofoo : 0 , arrfoo : [ 1 , 2 ] , objfoo : { a : true } }
398
410
t . equal ( '{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}' , stringify ( obj ) )
411
+ } )
399
412
400
- stringify = build ( {
413
+ test ( 'patternProperties - object coerce' , ( t ) => {
414
+ t . plan ( 1 )
415
+ const stringify = build ( {
401
416
title : 'check object coerce' ,
402
417
type : 'object' ,
403
418
properties : { } ,
@@ -408,10 +423,13 @@ test('patternProperties', (t) => {
408
423
}
409
424
} )
410
425
411
- obj = { foo : true , ofoo : '42' , arrfoo : [ 1 , 2 ] , objfoo : { answer : 42 } }
426
+ const obj = { foo : true , ofoo : '42' , arrfoo : [ 1 , 2 ] , objfoo : { answer : 42 } }
412
427
t . equal ( '{"foo":{},"ofoo":{},"arrfoo":{},"objfoo":{}}' , stringify ( obj ) )
428
+ } )
413
429
414
- stringify = build ( {
430
+ test ( 'patternProperties - array coerce' , ( t ) => {
431
+ t . plan ( 1 )
432
+ const stringify = build ( {
415
433
title : 'check array coerce' ,
416
434
type : 'object' ,
417
435
properties : { } ,
@@ -422,6 +440,28 @@ test('patternProperties', (t) => {
422
440
}
423
441
} )
424
442
425
- obj = { foo : 'true' , ofoo : 0 , arrfoo : [ 1 , 2 ] , objfoo : { tyrion : 'lannister' } }
443
+ const obj = { foo : 'true' , ofoo : 0 , arrfoo : [ 1 , 2 ] , objfoo : { tyrion : 'lannister' } }
426
444
t . equal ( '{"foo":[],"ofoo":[],"arrfoo":[],"objfoo":[]}' , stringify ( obj ) )
427
445
} )
446
+
447
+ test ( 'patternProperties - throw on unknown type' , ( t ) => {
448
+ t . plan ( 1 )
449
+ const stringify = build ( {
450
+ title : 'check array coerce' ,
451
+ type : 'object' ,
452
+ properties : { } ,
453
+ patternProperties : {
454
+ foo : {
455
+ type : 'strangetype'
456
+ }
457
+ }
458
+ } )
459
+
460
+ const obj = { foo : 'true' , ofoo : 0 , arrfoo : [ 1 , 2 ] , objfoo : { tyrion : 'lannister' } }
461
+ try {
462
+ stringify ( obj )
463
+ t . fail ( )
464
+ } catch ( e ) {
465
+ t . pass ( )
466
+ }
467
+ } )
0 commit comments