fast-json-stringify 1.15.0 → 1.15.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bench.js +82 -11
- package/build-schema-validator.js +1 -1
- package/example.js +1 -1
- package/index.js +60 -22
- package/package.json +12 -5
- package/test/additionalProperties.test.js +5 -5
- package/test/anyof.test.js +18 -1
- package/test/array.test.js +58 -58
- package/test/basic.test.js +87 -87
- package/test/defaults.test.js +142 -142
- package/test/if-then-else.test.js +93 -93
- package/test/inferType.test.js +27 -27
- package/test/json-schema-test-suite/util.js +1 -1
- package/test/nullable.test.js +19 -19
- package/test/patternProperties.test.js +2 -2
- package/test/ref.test.js +282 -0
- package/test/sanitize.test.js +142 -0
package/test/ref.test.js
CHANGED
|
@@ -434,6 +434,206 @@ test('ref internal - plain name fragment', (t) => {
|
|
|
434
434
|
t.equal(output, '{"obj":{"str":"test"}}')
|
|
435
435
|
})
|
|
436
436
|
|
|
437
|
+
test('ref external - plain name fragment', (t) => {
|
|
438
|
+
t.plan(2)
|
|
439
|
+
|
|
440
|
+
const externalSchema = {
|
|
441
|
+
first: {
|
|
442
|
+
$id: '#first-schema',
|
|
443
|
+
type: 'object',
|
|
444
|
+
properties: {
|
|
445
|
+
str: {
|
|
446
|
+
type: 'string'
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
second: {
|
|
451
|
+
definitions: {
|
|
452
|
+
second: {
|
|
453
|
+
$id: '#second-schema',
|
|
454
|
+
type: 'object',
|
|
455
|
+
properties: {
|
|
456
|
+
int: {
|
|
457
|
+
type: 'integer'
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const schema = {
|
|
466
|
+
title: 'object with $ref to external plain name fragment',
|
|
467
|
+
type: 'object',
|
|
468
|
+
properties: {
|
|
469
|
+
first: {
|
|
470
|
+
$ref: '#first-schema'
|
|
471
|
+
},
|
|
472
|
+
second: {
|
|
473
|
+
$ref: '#second-schema'
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const object = {
|
|
479
|
+
first: {
|
|
480
|
+
str: 'test'
|
|
481
|
+
},
|
|
482
|
+
second: {
|
|
483
|
+
int: 42
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const stringify = build(schema, { schema: externalSchema })
|
|
488
|
+
const output = stringify(object)
|
|
489
|
+
|
|
490
|
+
try {
|
|
491
|
+
JSON.parse(output)
|
|
492
|
+
t.pass()
|
|
493
|
+
} catch (e) {
|
|
494
|
+
t.fail()
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
t.equal(output, '{"first":{"str":"test"},"second":{"int":42}}')
|
|
498
|
+
})
|
|
499
|
+
|
|
500
|
+
test('ref external - duplicate plain name fragment', (t) => {
|
|
501
|
+
t.plan(2)
|
|
502
|
+
|
|
503
|
+
const externalSchema = {
|
|
504
|
+
external: {
|
|
505
|
+
$id: '#duplicateSchema',
|
|
506
|
+
type: 'object',
|
|
507
|
+
properties: {
|
|
508
|
+
prop: {
|
|
509
|
+
type: 'boolean'
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
other: {
|
|
514
|
+
$id: '#otherSchema',
|
|
515
|
+
type: 'object',
|
|
516
|
+
properties: {
|
|
517
|
+
prop: {
|
|
518
|
+
type: 'integer'
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const schema = {
|
|
525
|
+
title: 'object with $ref to plain name fragment',
|
|
526
|
+
type: 'object',
|
|
527
|
+
definitions: {
|
|
528
|
+
duplicate: {
|
|
529
|
+
$id: '#duplicateSchema',
|
|
530
|
+
type: 'object',
|
|
531
|
+
properties: {
|
|
532
|
+
prop: {
|
|
533
|
+
type: 'string'
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
properties: {
|
|
539
|
+
local: {
|
|
540
|
+
$ref: '#duplicateSchema'
|
|
541
|
+
},
|
|
542
|
+
external: {
|
|
543
|
+
$ref: 'external#duplicateSchema'
|
|
544
|
+
},
|
|
545
|
+
other: {
|
|
546
|
+
$ref: '#otherSchema'
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const object = {
|
|
552
|
+
local: {
|
|
553
|
+
prop: 'test'
|
|
554
|
+
},
|
|
555
|
+
external: {
|
|
556
|
+
prop: true
|
|
557
|
+
},
|
|
558
|
+
other: {
|
|
559
|
+
prop: 42
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const stringify = build(schema, { schema: externalSchema })
|
|
564
|
+
const output = stringify(object)
|
|
565
|
+
|
|
566
|
+
try {
|
|
567
|
+
JSON.parse(output)
|
|
568
|
+
t.pass()
|
|
569
|
+
} catch (e) {
|
|
570
|
+
t.fail()
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
t.equal(output, '{"local":{"prop":"test"},"external":{"prop":true},"other":{"prop":42}}')
|
|
574
|
+
})
|
|
575
|
+
|
|
576
|
+
test('ref external - explicit external plain name fragment must not fallback to other external schemas', (t) => {
|
|
577
|
+
t.plan(1)
|
|
578
|
+
|
|
579
|
+
const externalSchema = {
|
|
580
|
+
first: {
|
|
581
|
+
$id: '#target',
|
|
582
|
+
type: 'object',
|
|
583
|
+
properties: {
|
|
584
|
+
prop: {
|
|
585
|
+
type: 'string'
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
},
|
|
589
|
+
second: {
|
|
590
|
+
$id: '#wrong',
|
|
591
|
+
type: 'object',
|
|
592
|
+
properties: {
|
|
593
|
+
prop: {
|
|
594
|
+
type: 'integer'
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
const schema = {
|
|
601
|
+
title: 'object with $ref to plain name fragment',
|
|
602
|
+
type: 'object',
|
|
603
|
+
definitions: {
|
|
604
|
+
third: {
|
|
605
|
+
$id: '#wrong',
|
|
606
|
+
type: 'object',
|
|
607
|
+
properties: {
|
|
608
|
+
prop: {
|
|
609
|
+
type: 'boolean'
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
},
|
|
614
|
+
properties: {
|
|
615
|
+
target: {
|
|
616
|
+
$ref: 'first#wrong'
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const object = {
|
|
622
|
+
target: {
|
|
623
|
+
prop: 'test'
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
try {
|
|
628
|
+
const stringify = build(schema, { schema: externalSchema })
|
|
629
|
+
const output = stringify(object)
|
|
630
|
+
JSON.parse(output)
|
|
631
|
+
t.fail()
|
|
632
|
+
} catch (e) {
|
|
633
|
+
t.pass()
|
|
634
|
+
}
|
|
635
|
+
})
|
|
636
|
+
|
|
437
637
|
test('ref internal - multiple $ref format', (t) => {
|
|
438
638
|
t.plan(2)
|
|
439
639
|
|
|
@@ -484,3 +684,85 @@ test('ref internal - multiple $ref format', (t) => {
|
|
|
484
684
|
|
|
485
685
|
t.equal(output, '{"zero":"test","a":"test","b":"test","c":"test","d":"test","e":"test"}')
|
|
486
686
|
})
|
|
687
|
+
|
|
688
|
+
test('ref in root external', (t) => {
|
|
689
|
+
t.plan(2)
|
|
690
|
+
|
|
691
|
+
const externalSchema = {
|
|
692
|
+
numbers: {
|
|
693
|
+
$id: 'numbers',
|
|
694
|
+
definitions: {
|
|
695
|
+
num: {
|
|
696
|
+
type: 'object',
|
|
697
|
+
properties: {
|
|
698
|
+
int: {
|
|
699
|
+
type: 'integer'
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const schema = {
|
|
708
|
+
title: 'object with $ref in root schema',
|
|
709
|
+
type: 'object',
|
|
710
|
+
$ref: 'numbers#/definitions/num'
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
const object = { int: 42 }
|
|
714
|
+
const stringify = build(schema, { schema: externalSchema })
|
|
715
|
+
const output = stringify(object)
|
|
716
|
+
|
|
717
|
+
try {
|
|
718
|
+
JSON.parse(output)
|
|
719
|
+
t.pass()
|
|
720
|
+
} catch (e) {
|
|
721
|
+
t.fail()
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
t.equal(output, '{"int":42}')
|
|
725
|
+
})
|
|
726
|
+
|
|
727
|
+
test('ref in root external multiple times', (t) => {
|
|
728
|
+
t.plan(2)
|
|
729
|
+
|
|
730
|
+
const externalSchema = {
|
|
731
|
+
numbers: {
|
|
732
|
+
$id: 'numbers',
|
|
733
|
+
$ref: 'subnumbers#/definitions/num'
|
|
734
|
+
},
|
|
735
|
+
subnumbers: {
|
|
736
|
+
$id: 'subnumbers',
|
|
737
|
+
definitions: {
|
|
738
|
+
num: {
|
|
739
|
+
type: 'object',
|
|
740
|
+
properties: {
|
|
741
|
+
int: {
|
|
742
|
+
type: 'integer'
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
const schema = {
|
|
751
|
+
title: 'object with $ref in root schema',
|
|
752
|
+
type: 'object',
|
|
753
|
+
$ref: 'numbers#/definitions/num'
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
const object = { int: 42 }
|
|
757
|
+
const stringify = build(schema, { schema: externalSchema })
|
|
758
|
+
const output = stringify(object)
|
|
759
|
+
|
|
760
|
+
try {
|
|
761
|
+
JSON.parse(output)
|
|
762
|
+
t.pass()
|
|
763
|
+
} catch (e) {
|
|
764
|
+
t.fail()
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
t.equal(output, '{"int":42}')
|
|
768
|
+
})
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const build = require('..')
|
|
5
|
+
|
|
6
|
+
const stringify = build({
|
|
7
|
+
title: 'Example Schema',
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
firstName: {
|
|
11
|
+
type: 'string'
|
|
12
|
+
},
|
|
13
|
+
lastName: {
|
|
14
|
+
type: 'string'
|
|
15
|
+
},
|
|
16
|
+
age: {
|
|
17
|
+
description: 'Age in years"',
|
|
18
|
+
type: 'integer'
|
|
19
|
+
},
|
|
20
|
+
[(() => `phra'&& process.exit(1) ||'phra`)()]: {},
|
|
21
|
+
now: {
|
|
22
|
+
type: 'string'
|
|
23
|
+
},
|
|
24
|
+
reg: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
default: 'a\'&& process.exit(1) ||\''
|
|
27
|
+
},
|
|
28
|
+
obj: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
bool: {
|
|
32
|
+
type: 'boolean'
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
'"\'w00t': {
|
|
37
|
+
type: 'string',
|
|
38
|
+
default: '"\'w00t'
|
|
39
|
+
},
|
|
40
|
+
arr: {
|
|
41
|
+
type: 'array',
|
|
42
|
+
items: {
|
|
43
|
+
type: 'object',
|
|
44
|
+
properties: {
|
|
45
|
+
'phra\' && process.exit(1)//': {
|
|
46
|
+
type: 'number'
|
|
47
|
+
},
|
|
48
|
+
str: {
|
|
49
|
+
type: 'string'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
required: ['now'],
|
|
56
|
+
patternProperties: {
|
|
57
|
+
'.*foo$': {
|
|
58
|
+
type: 'string'
|
|
59
|
+
},
|
|
60
|
+
test: {
|
|
61
|
+
type: 'number'
|
|
62
|
+
},
|
|
63
|
+
'phra\'/ && process.exit(1) && /\'': {
|
|
64
|
+
type: 'number'
|
|
65
|
+
},
|
|
66
|
+
'"\'w00t.*////': {
|
|
67
|
+
type: 'number'
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
additionalProperties: {
|
|
71
|
+
type: 'string'
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const obj = {
|
|
76
|
+
firstName: 'Matteo',
|
|
77
|
+
lastName: 'Collina',
|
|
78
|
+
age: 32,
|
|
79
|
+
now: new Date(),
|
|
80
|
+
foo: 'hello"',
|
|
81
|
+
bar: "world'",
|
|
82
|
+
'fuzz"': 42,
|
|
83
|
+
"me'": 42,
|
|
84
|
+
numfoo: 42,
|
|
85
|
+
test: 42,
|
|
86
|
+
strtest: '23',
|
|
87
|
+
arr: [{ 'phra\' && process.exit(1)//': 42 }],
|
|
88
|
+
obj: { bool: true },
|
|
89
|
+
notmatch: 'valar morghulis',
|
|
90
|
+
notmatchobj: { a: true },
|
|
91
|
+
notmatchnum: 42
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// pass if it does not crash
|
|
95
|
+
const json = stringify(obj)
|
|
96
|
+
JSON.parse(json)
|
|
97
|
+
|
|
98
|
+
const stringify2 = build({
|
|
99
|
+
title: 'Example Schema',
|
|
100
|
+
type: 'object',
|
|
101
|
+
patternProperties: {
|
|
102
|
+
'"\'w00t.*////': {
|
|
103
|
+
type: 'number'
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
t.deepEqual(JSON.parse(stringify2({
|
|
109
|
+
'"\'phra////': 42,
|
|
110
|
+
asd: 42
|
|
111
|
+
})), {
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
const stringify3 = build({
|
|
115
|
+
title: 'Example Schema',
|
|
116
|
+
type: 'object',
|
|
117
|
+
properties: {
|
|
118
|
+
"\"phra\\'&&(console.log(42))//||'phra": {}
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
// this verifies the escaping
|
|
123
|
+
JSON.parse(stringify3({
|
|
124
|
+
'"phra\'&&(console.log(42))//||\'phra': 42
|
|
125
|
+
}))
|
|
126
|
+
|
|
127
|
+
const stringify4 = build({
|
|
128
|
+
title: 'Example Schema',
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: {
|
|
131
|
+
'"\\\\\\\\\'w00t': {
|
|
132
|
+
type: 'string',
|
|
133
|
+
default: '"\'w00t'
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
t.deepEqual(JSON.parse(stringify4({})), {
|
|
139
|
+
'"\\\\\\\\\'w00t': '"\'w00t'
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
t.pass('no crashes')
|