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