fast-json-stringify 3.0.3 → 4.0.0
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/.github/workflows/benchmark.yml +9 -4
- package/.github/workflows/ci.yml +3 -33
- package/README.md +65 -11
- package/benchmark/bench-cmp-branch.js +114 -0
- package/{bench.js → benchmark/bench-cmp-lib.js} +70 -8
- package/benchmark/bench-thread.js +21 -0
- package/benchmark/bench.js +251 -0
- package/example.js +1 -4
- package/index.d.ts +4 -0
- package/index.js +540 -585
- package/package.json +13 -6
- package/test/additionalProperties.test.js +23 -19
- package/test/allof.test.js +56 -0
- package/test/anyof.test.js +88 -9
- package/test/array.test.js +126 -24
- package/test/basic.test.js +8 -8
- package/test/date.test.js +62 -35
- package/test/debug-mode.test.js +1 -1
- package/test/if-then-else.test.js +2 -2
- package/test/nestedObjects.test.js +31 -0
- package/test/nullable.test.js +343 -15
- package/test/oneof.test.js +9 -22
- package/test/patternProperties.test.js +17 -6
- package/test/recursion.test.js +65 -0
- package/test/required.test.js +1 -1
- package/test/sanitize3.test.js +9 -11
- package/test/typesArray.test.js +40 -3
package/test/oneof.test.js
CHANGED
|
@@ -104,11 +104,7 @@ test('object with field of type string and coercion disable ', (t) => {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
const stringify = build(schema)
|
|
107
|
-
|
|
108
|
-
const value = stringify({
|
|
109
|
-
str: 1
|
|
110
|
-
})
|
|
111
|
-
t.equal(value, '{"str":null}')
|
|
107
|
+
t.throws(() => stringify({ str: 1 }))
|
|
112
108
|
})
|
|
113
109
|
|
|
114
110
|
test('object with field of type string and coercion enable ', (t) => {
|
|
@@ -403,7 +399,7 @@ test('oneOf object with field of type string with format or null', (t) => {
|
|
|
403
399
|
})
|
|
404
400
|
|
|
405
401
|
test('one array item match oneOf types', (t) => {
|
|
406
|
-
t.plan(
|
|
402
|
+
t.plan(3)
|
|
407
403
|
|
|
408
404
|
const schema = {
|
|
409
405
|
type: 'object',
|
|
@@ -429,15 +425,13 @@ test('one array item match oneOf types', (t) => {
|
|
|
429
425
|
|
|
430
426
|
const stringify = build(schema)
|
|
431
427
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
})
|
|
435
|
-
|
|
436
|
-
t.equal('{"data":["foo"]}', responseWithMappedType)
|
|
428
|
+
t.equal(stringify({ data: ['foo'] }), '{"data":["foo"]}')
|
|
429
|
+
t.equal(stringify({ data: [1] }), '{"data":[1]}')
|
|
430
|
+
t.throws(() => stringify({ data: [false, 'foo'] }))
|
|
437
431
|
})
|
|
438
432
|
|
|
439
433
|
test('some array items match oneOf types', (t) => {
|
|
440
|
-
t.plan(
|
|
434
|
+
t.plan(2)
|
|
441
435
|
|
|
442
436
|
const schema = {
|
|
443
437
|
type: 'object',
|
|
@@ -463,11 +457,8 @@ test('some array items match oneOf types', (t) => {
|
|
|
463
457
|
|
|
464
458
|
const stringify = build(schema)
|
|
465
459
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
})
|
|
469
|
-
|
|
470
|
-
t.equal('{"data":["foo",5]}', responseWithMappedTypes)
|
|
460
|
+
t.equal(stringify({ data: ['foo', 5] }), '{"data":["foo",5]}')
|
|
461
|
+
t.throws(() => stringify({ data: [false, 'foo', true, 5] }))
|
|
471
462
|
})
|
|
472
463
|
|
|
473
464
|
test('all array items does not match oneOf types', (t) => {
|
|
@@ -497,9 +488,5 @@ test('all array items does not match oneOf types', (t) => {
|
|
|
497
488
|
|
|
498
489
|
const stringify = build(schema)
|
|
499
490
|
|
|
500
|
-
|
|
501
|
-
data: [null, false, true, undefined, [], {}]
|
|
502
|
-
})
|
|
503
|
-
|
|
504
|
-
t.equal('{"data":[]}', emptyResponse)
|
|
491
|
+
t.throws(() => stringify({ data: [null, false, true, undefined, [], {}] }))
|
|
505
492
|
})
|
|
@@ -63,7 +63,7 @@ test('patternProperties - string coerce', (t) => {
|
|
|
63
63
|
})
|
|
64
64
|
|
|
65
65
|
test('patternProperties - number coerce', (t) => {
|
|
66
|
-
t.plan(
|
|
66
|
+
t.plan(2)
|
|
67
67
|
const stringify = build({
|
|
68
68
|
title: 'check number coerce',
|
|
69
69
|
type: 'object',
|
|
@@ -75,8 +75,16 @@ test('patternProperties - number coerce', (t) => {
|
|
|
75
75
|
}
|
|
76
76
|
})
|
|
77
77
|
|
|
78
|
-
const
|
|
79
|
-
t.equal(stringify(
|
|
78
|
+
const coercibleValues = { foo: true, ofoo: '42' }
|
|
79
|
+
t.equal(stringify(coercibleValues), '{"foo":1,"ofoo":42}')
|
|
80
|
+
|
|
81
|
+
const incoercibleValues = { xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
|
|
82
|
+
try {
|
|
83
|
+
stringify(incoercibleValues)
|
|
84
|
+
t.fail('should throw an error')
|
|
85
|
+
} catch (err) {
|
|
86
|
+
t.ok(err)
|
|
87
|
+
}
|
|
80
88
|
})
|
|
81
89
|
|
|
82
90
|
test('patternProperties - boolean coerce', (t) => {
|
|
@@ -119,7 +127,7 @@ test('patternProperties - object coerce', (t) => {
|
|
|
119
127
|
})
|
|
120
128
|
|
|
121
129
|
test('patternProperties - array coerce', (t) => {
|
|
122
|
-
t.plan(
|
|
130
|
+
t.plan(2)
|
|
123
131
|
const stringify = build({
|
|
124
132
|
title: 'check array coerce',
|
|
125
133
|
type: 'object',
|
|
@@ -134,6 +142,9 @@ test('patternProperties - array coerce', (t) => {
|
|
|
134
142
|
}
|
|
135
143
|
})
|
|
136
144
|
|
|
137
|
-
const
|
|
138
|
-
t.equal(stringify(
|
|
145
|
+
const coercibleValues = { arrfoo: [1, 2] }
|
|
146
|
+
t.equal(stringify(coercibleValues), '{"arrfoo":["1","2"]}')
|
|
147
|
+
|
|
148
|
+
const incoercibleValues = { foo: 'true', ofoo: 0, objfoo: { tyrion: 'lannister' } }
|
|
149
|
+
t.throws(() => stringify(incoercibleValues))
|
|
139
150
|
})
|
package/test/recursion.test.js
CHANGED
|
@@ -178,3 +178,68 @@ test('can stringify recursive references in object types (issue #365)', t => {
|
|
|
178
178
|
const value = stringify(data)
|
|
179
179
|
t.equal(value, '{"category":{"parent":{"parent":{"parent":{"parent":{}}}}}}')
|
|
180
180
|
})
|
|
181
|
+
|
|
182
|
+
test('can stringify recursive inline $id references (issue #410)', t => {
|
|
183
|
+
t.plan(1)
|
|
184
|
+
const schema = {
|
|
185
|
+
$id: 'Node',
|
|
186
|
+
type: 'object',
|
|
187
|
+
properties: {
|
|
188
|
+
id: {
|
|
189
|
+
type: 'string'
|
|
190
|
+
},
|
|
191
|
+
nodes: {
|
|
192
|
+
type: 'array',
|
|
193
|
+
items: {
|
|
194
|
+
$ref: 'Node'
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
required: [
|
|
199
|
+
'id',
|
|
200
|
+
'nodes'
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const stringify = build(schema)
|
|
205
|
+
const data = {
|
|
206
|
+
id: '0',
|
|
207
|
+
nodes: [
|
|
208
|
+
{
|
|
209
|
+
id: '1',
|
|
210
|
+
nodes: [{
|
|
211
|
+
id: '2',
|
|
212
|
+
nodes: [
|
|
213
|
+
{ id: '3', nodes: [] },
|
|
214
|
+
{ id: '4', nodes: [] },
|
|
215
|
+
{ id: '5', nodes: [] }
|
|
216
|
+
]
|
|
217
|
+
}]
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
id: '6',
|
|
221
|
+
nodes: [{
|
|
222
|
+
id: '7',
|
|
223
|
+
nodes: [
|
|
224
|
+
{ id: '8', nodes: [] },
|
|
225
|
+
{ id: '9', nodes: [] },
|
|
226
|
+
{ id: '10', nodes: [] }
|
|
227
|
+
]
|
|
228
|
+
}]
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
id: '11',
|
|
232
|
+
nodes: [{
|
|
233
|
+
id: '12',
|
|
234
|
+
nodes: [
|
|
235
|
+
{ id: '13', nodes: [] },
|
|
236
|
+
{ id: '14', nodes: [] },
|
|
237
|
+
{ id: '15', nodes: [] }
|
|
238
|
+
]
|
|
239
|
+
}]
|
|
240
|
+
}
|
|
241
|
+
]
|
|
242
|
+
}
|
|
243
|
+
const value = stringify(data)
|
|
244
|
+
t.equal(value, '{"id":"0","nodes":[{"id":"1","nodes":[{"id":"2","nodes":[{"id":"3","nodes":[]},{"id":"4","nodes":[]},{"id":"5","nodes":[]}]}]},{"id":"6","nodes":[{"id":"7","nodes":[{"id":"8","nodes":[]},{"id":"9","nodes":[]},{"id":"10","nodes":[]}]}]},{"id":"11","nodes":[{"id":"12","nodes":[{"id":"13","nodes":[]},{"id":"14","nodes":[]},{"id":"15","nodes":[]}]}]}]}')
|
|
245
|
+
})
|
package/test/required.test.js
CHANGED
package/test/sanitize3.test.js
CHANGED
|
@@ -3,15 +3,13 @@
|
|
|
3
3
|
const t = require('tap')
|
|
4
4
|
const build = require('..')
|
|
5
5
|
|
|
6
|
-
const stringify = build({
|
|
7
|
-
$defs: {
|
|
8
|
-
type: 'foooo"bar'
|
|
9
|
-
},
|
|
10
|
-
patternProperties: {
|
|
11
|
-
x: { $ref: '#/$defs' }
|
|
12
|
-
}
|
|
13
|
-
})
|
|
14
|
-
|
|
15
6
|
t.throws(() => {
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
build({
|
|
8
|
+
$defs: {
|
|
9
|
+
type: 'foooo"bar'
|
|
10
|
+
},
|
|
11
|
+
patternProperties: {
|
|
12
|
+
x: { $ref: '#/$defs' }
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
}, 'foooo"bar unsupported')
|
package/test/typesArray.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const test = require('tap').test
|
|
4
|
-
const moment = require('moment')
|
|
5
4
|
const build = require('..')
|
|
6
5
|
|
|
7
6
|
test('possibly nullable integer primitive alternative', (t) => {
|
|
@@ -88,6 +87,27 @@ test('possibly nullable number primitive alternative with null value', (t) => {
|
|
|
88
87
|
t.equal(value, '{"data":0}')
|
|
89
88
|
})
|
|
90
89
|
|
|
90
|
+
test('possibly nullable number primitive alternative with null value', (t) => {
|
|
91
|
+
t.plan(1)
|
|
92
|
+
|
|
93
|
+
const schema = {
|
|
94
|
+
title: 'simple object with multi-type nullable primitive',
|
|
95
|
+
type: 'object',
|
|
96
|
+
properties: {
|
|
97
|
+
data: {
|
|
98
|
+
type: ['boolean']
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const stringify = build(schema)
|
|
104
|
+
|
|
105
|
+
const value = stringify({
|
|
106
|
+
data: null
|
|
107
|
+
})
|
|
108
|
+
t.equal(value, '{"data":false}')
|
|
109
|
+
})
|
|
110
|
+
|
|
91
111
|
test('nullable integer primitive', (t) => {
|
|
92
112
|
t.plan(1)
|
|
93
113
|
|
|
@@ -361,7 +381,7 @@ test('string type array can handle dates', (t) => {
|
|
|
361
381
|
const stringify = build(schema)
|
|
362
382
|
const value = stringify({
|
|
363
383
|
date: new Date('2018-04-20T07:52:31.017Z'),
|
|
364
|
-
dateObject:
|
|
384
|
+
dateObject: new Date('2018-04-21T07:52:31.017Z')
|
|
365
385
|
})
|
|
366
386
|
t.equal(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
|
|
367
387
|
})
|
|
@@ -433,5 +453,22 @@ test('should throw an error when type is array and object is null', (t) => {
|
|
|
433
453
|
}
|
|
434
454
|
|
|
435
455
|
const stringify = build(schema)
|
|
436
|
-
t.throws(() => stringify({ arr: null }), new TypeError('
|
|
456
|
+
t.throws(() => stringify({ arr: null }), new TypeError('The value \'null\' does not match schema definition.'))
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
test('throw an error if none of types matches', (t) => {
|
|
460
|
+
t.plan(1)
|
|
461
|
+
|
|
462
|
+
const schema = {
|
|
463
|
+
title: 'simple object with multi-type nullable primitive',
|
|
464
|
+
type: 'object',
|
|
465
|
+
properties: {
|
|
466
|
+
data: {
|
|
467
|
+
type: ['number', 'boolean']
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const stringify = build(schema)
|
|
473
|
+
t.throws(() => stringify({ data: 'string' }), 'The value "string" does not match schema definition.')
|
|
437
474
|
})
|