fast-json-stringify 1.17.0 → 1.20.1
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/.dependabot/config.yml +8 -0
- package/.github/workflows/ci.yml +18 -0
- package/README.md +58 -7
- package/example.js +11 -0
- package/index.js +110 -14
- package/package.json +3 -10
- package/test/array.test.js +26 -0
- package/test/date.test.js +381 -1
- package/test/debug-mode.test.js +65 -0
- package/test/oneof.test.js +399 -0
- package/test/typesArray.test.js +6 -3
- package/.travis.yml +0 -14
- package/azure-pipelines.yml +0 -35
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const build = require('..')
|
|
5
|
+
|
|
6
|
+
test('object with multiple types field', (t) => {
|
|
7
|
+
t.plan(2)
|
|
8
|
+
|
|
9
|
+
const schema = {
|
|
10
|
+
title: 'object with multiple types field',
|
|
11
|
+
type: 'object',
|
|
12
|
+
properties: {
|
|
13
|
+
str: {
|
|
14
|
+
oneOf: [{
|
|
15
|
+
type: 'string'
|
|
16
|
+
}, {
|
|
17
|
+
type: 'boolean'
|
|
18
|
+
}]
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const stringify = build(schema)
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
const value = stringify({
|
|
26
|
+
str: 'string'
|
|
27
|
+
})
|
|
28
|
+
t.is(value, '{"str":"string"}')
|
|
29
|
+
} catch (e) {
|
|
30
|
+
t.fail(e.message)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const value = stringify({
|
|
35
|
+
str: true
|
|
36
|
+
})
|
|
37
|
+
t.is(value, '{"str":true}')
|
|
38
|
+
} catch (e) {
|
|
39
|
+
t.fail()
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('object with field of type object or null', (t) => {
|
|
44
|
+
t.plan(2)
|
|
45
|
+
|
|
46
|
+
const schema = {
|
|
47
|
+
title: 'object with field of type object or null',
|
|
48
|
+
type: 'object',
|
|
49
|
+
properties: {
|
|
50
|
+
prop: {
|
|
51
|
+
oneOf: [{
|
|
52
|
+
type: 'object',
|
|
53
|
+
properties: {
|
|
54
|
+
str: {
|
|
55
|
+
type: 'string'
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}, {
|
|
59
|
+
type: 'null'
|
|
60
|
+
}]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const stringify = build(schema)
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const value = stringify({
|
|
68
|
+
prop: null
|
|
69
|
+
})
|
|
70
|
+
t.is(value, '{"prop":null}')
|
|
71
|
+
} catch (e) {
|
|
72
|
+
t.fail()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const value = stringify({
|
|
77
|
+
prop: {
|
|
78
|
+
str: 'string',
|
|
79
|
+
remove: 'this'
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
t.is(value, '{"prop":{"str":"string"}}')
|
|
83
|
+
} catch (e) {
|
|
84
|
+
t.fail()
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('object with field of type object or array', (t) => {
|
|
89
|
+
t.plan(2)
|
|
90
|
+
|
|
91
|
+
const schema = {
|
|
92
|
+
title: 'object with field of type object or array',
|
|
93
|
+
type: 'object',
|
|
94
|
+
properties: {
|
|
95
|
+
prop: {
|
|
96
|
+
oneOf: [{
|
|
97
|
+
type: 'object',
|
|
98
|
+
properties: {},
|
|
99
|
+
additionalProperties: true
|
|
100
|
+
}, {
|
|
101
|
+
type: 'array',
|
|
102
|
+
items: {
|
|
103
|
+
type: 'string'
|
|
104
|
+
}
|
|
105
|
+
}]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const stringify = build(schema)
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
const value = stringify({
|
|
113
|
+
prop: {
|
|
114
|
+
str: 'string'
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
t.is(value, '{"prop":{"str":"string"}}')
|
|
118
|
+
} catch (e) {
|
|
119
|
+
t.fail()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const value = stringify({
|
|
124
|
+
prop: ['string']
|
|
125
|
+
})
|
|
126
|
+
t.is(value, '{"prop":["string"]}')
|
|
127
|
+
} catch (e) {
|
|
128
|
+
t.fail()
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('object with field of type string and coercion disable ', (t) => {
|
|
133
|
+
t.plan(1)
|
|
134
|
+
|
|
135
|
+
const schema = {
|
|
136
|
+
title: 'object with field of type string',
|
|
137
|
+
type: 'object',
|
|
138
|
+
properties: {
|
|
139
|
+
str: {
|
|
140
|
+
oneOf: [{
|
|
141
|
+
type: 'string'
|
|
142
|
+
}]
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const stringify = build(schema)
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
const value = stringify({
|
|
150
|
+
str: 1
|
|
151
|
+
})
|
|
152
|
+
t.is(value, '{"str":null}')
|
|
153
|
+
} catch (e) {
|
|
154
|
+
t.fail()
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('object with field of type string and coercion enable ', (t) => {
|
|
159
|
+
t.plan(1)
|
|
160
|
+
|
|
161
|
+
const schema = {
|
|
162
|
+
title: 'object with field of type string',
|
|
163
|
+
type: 'object',
|
|
164
|
+
properties: {
|
|
165
|
+
str: {
|
|
166
|
+
oneOf: [{
|
|
167
|
+
type: 'string'
|
|
168
|
+
}]
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const options = {
|
|
174
|
+
ajv: {
|
|
175
|
+
coerceTypes: true
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const stringify = build(schema, options)
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
const value = stringify({
|
|
182
|
+
str: 1
|
|
183
|
+
})
|
|
184
|
+
t.is(value, '{"str":"1"}')
|
|
185
|
+
} catch (e) {
|
|
186
|
+
t.fail()
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('object with field with type union of multiple objects', (t) => {
|
|
191
|
+
t.plan(2)
|
|
192
|
+
|
|
193
|
+
const schema = {
|
|
194
|
+
title: 'object with oneOf property value containing objects',
|
|
195
|
+
type: 'object',
|
|
196
|
+
properties: {
|
|
197
|
+
oneOfSchema: {
|
|
198
|
+
oneOf: [
|
|
199
|
+
{
|
|
200
|
+
type: 'object',
|
|
201
|
+
properties: {
|
|
202
|
+
baz: { type: 'number' }
|
|
203
|
+
},
|
|
204
|
+
required: ['baz']
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
type: 'object',
|
|
208
|
+
properties: {
|
|
209
|
+
bar: { type: 'string' }
|
|
210
|
+
},
|
|
211
|
+
required: ['bar']
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
required: ['oneOfSchema']
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const stringify = build(schema)
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
const value = stringify({ oneOfSchema: { baz: 5 } })
|
|
223
|
+
t.is(value, '{"oneOfSchema":{"baz":5}}')
|
|
224
|
+
} catch (e) {
|
|
225
|
+
t.fail()
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
const value = stringify({ oneOfSchema: { bar: 'foo' } })
|
|
230
|
+
t.is(value, '{"oneOfSchema":{"bar":"foo"}}')
|
|
231
|
+
} catch (e) {
|
|
232
|
+
t.fail()
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('null value in schema', (t) => {
|
|
237
|
+
t.plan(0)
|
|
238
|
+
|
|
239
|
+
const schema = {
|
|
240
|
+
title: 'schema with null child',
|
|
241
|
+
type: 'string',
|
|
242
|
+
nullable: true,
|
|
243
|
+
enum: [null]
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
build(schema)
|
|
248
|
+
} catch (e) {
|
|
249
|
+
t.fail()
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('oneOf and $ref together', (t) => {
|
|
254
|
+
t.plan(2)
|
|
255
|
+
|
|
256
|
+
const schema = {
|
|
257
|
+
type: 'object',
|
|
258
|
+
properties: {
|
|
259
|
+
cs: {
|
|
260
|
+
oneOf: [
|
|
261
|
+
{
|
|
262
|
+
$ref: '#/definitions/Option'
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
type: 'boolean'
|
|
266
|
+
}
|
|
267
|
+
]
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
definitions: {
|
|
271
|
+
Option: {
|
|
272
|
+
type: 'string'
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const stringify = build(schema)
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
const value = stringify({
|
|
281
|
+
cs: 'franco'
|
|
282
|
+
})
|
|
283
|
+
t.is(value, '{"cs":"franco"}')
|
|
284
|
+
} catch (e) {
|
|
285
|
+
t.fail()
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
const value = stringify({
|
|
290
|
+
cs: true
|
|
291
|
+
})
|
|
292
|
+
t.is(value, '{"cs":true}')
|
|
293
|
+
} catch (e) {
|
|
294
|
+
t.fail()
|
|
295
|
+
}
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
test('oneOf and $ref: 2 levels are fine', (t) => {
|
|
299
|
+
t.plan(1)
|
|
300
|
+
|
|
301
|
+
const schema = {
|
|
302
|
+
type: 'object',
|
|
303
|
+
properties: {
|
|
304
|
+
cs: {
|
|
305
|
+
oneOf: [
|
|
306
|
+
{
|
|
307
|
+
$ref: '#/definitions/Option'
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
type: 'boolean'
|
|
311
|
+
}
|
|
312
|
+
]
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
definitions: {
|
|
316
|
+
Option: {
|
|
317
|
+
oneOf: [
|
|
318
|
+
{
|
|
319
|
+
type: 'number'
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
type: 'boolean'
|
|
323
|
+
}
|
|
324
|
+
]
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const stringify = build(schema)
|
|
330
|
+
try {
|
|
331
|
+
const value = stringify({
|
|
332
|
+
cs: 3
|
|
333
|
+
})
|
|
334
|
+
t.is(value, '{"cs":3}')
|
|
335
|
+
} catch (e) {
|
|
336
|
+
t.fail()
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
test('oneOf and $ref: multiple levels should throw at build.', (t) => {
|
|
341
|
+
t.plan(3)
|
|
342
|
+
|
|
343
|
+
const schema = {
|
|
344
|
+
type: 'object',
|
|
345
|
+
properties: {
|
|
346
|
+
cs: {
|
|
347
|
+
oneOf: [
|
|
348
|
+
{
|
|
349
|
+
$ref: '#/definitions/Option'
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
type: 'boolean'
|
|
353
|
+
}
|
|
354
|
+
]
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
definitions: {
|
|
358
|
+
Option: {
|
|
359
|
+
oneOf: [
|
|
360
|
+
{
|
|
361
|
+
$ref: '#/definitions/Option2'
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
type: 'string'
|
|
365
|
+
}
|
|
366
|
+
]
|
|
367
|
+
},
|
|
368
|
+
Option2: {
|
|
369
|
+
type: 'number'
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const stringify = build(schema)
|
|
375
|
+
try {
|
|
376
|
+
const value = stringify({
|
|
377
|
+
cs: 3
|
|
378
|
+
})
|
|
379
|
+
t.is(value, '{"cs":3}')
|
|
380
|
+
} catch (e) {
|
|
381
|
+
t.fail(e)
|
|
382
|
+
}
|
|
383
|
+
try {
|
|
384
|
+
const value = stringify({
|
|
385
|
+
cs: true
|
|
386
|
+
})
|
|
387
|
+
t.is(value, '{"cs":true}')
|
|
388
|
+
} catch (e) {
|
|
389
|
+
t.fail(e)
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
const value = stringify({
|
|
393
|
+
cs: 'pippo'
|
|
394
|
+
})
|
|
395
|
+
t.is(value, '{"cs":"pippo"}')
|
|
396
|
+
} catch (e) {
|
|
397
|
+
t.fail(e)
|
|
398
|
+
}
|
|
399
|
+
})
|
package/test/typesArray.test.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const test = require('tap').test
|
|
4
|
+
const moment = require('moment')
|
|
4
5
|
const build = require('..')
|
|
5
6
|
|
|
6
7
|
test('possibly nullable integer primitive alternative', (t) => {
|
|
@@ -423,15 +424,17 @@ test('string type array can handle dates', (t) => {
|
|
|
423
424
|
const schema = {
|
|
424
425
|
type: 'object',
|
|
425
426
|
properties: {
|
|
426
|
-
date: { type: ['string'] }
|
|
427
|
+
date: { type: ['string'] },
|
|
428
|
+
dateObject: { type: ['string'], format: 'date-time' }
|
|
427
429
|
}
|
|
428
430
|
}
|
|
429
431
|
const stringify = build(schema)
|
|
430
432
|
try {
|
|
431
433
|
const value = stringify({
|
|
432
|
-
date: new Date('2018-04-20T07:52:31.017Z')
|
|
434
|
+
date: new Date('2018-04-20T07:52:31.017Z'),
|
|
435
|
+
dateObject: moment('2018-04-21T07:52:31.017Z')
|
|
433
436
|
})
|
|
434
|
-
t.is(value, '{"date":"2018-04-20T07:52:31.017Z"}')
|
|
437
|
+
t.is(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
|
|
435
438
|
} catch (e) {
|
|
436
439
|
t.fail()
|
|
437
440
|
}
|
package/.travis.yml
DELETED
package/azure-pipelines.yml
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
pool:
|
|
2
|
-
vmImage: 'ubuntu-16.04'
|
|
3
|
-
|
|
4
|
-
strategy:
|
|
5
|
-
matrix:
|
|
6
|
-
node_6_x:
|
|
7
|
-
node_version: 6.x
|
|
8
|
-
node_8_x:
|
|
9
|
-
node_version: 8.x
|
|
10
|
-
node_10_x:
|
|
11
|
-
node_version: 10.x
|
|
12
|
-
node_11_x:
|
|
13
|
-
node_version: 11.x
|
|
14
|
-
|
|
15
|
-
steps:
|
|
16
|
-
- task: NodeTool@0
|
|
17
|
-
inputs:
|
|
18
|
-
versionSpec: $(node_version)
|
|
19
|
-
displayName: Install Node.js
|
|
20
|
-
|
|
21
|
-
- bash: npm install
|
|
22
|
-
displayName: Install dependencies
|
|
23
|
-
|
|
24
|
-
- bash: npm run test:lint
|
|
25
|
-
displayName: Linter
|
|
26
|
-
|
|
27
|
-
- bash: npm run test:report && npm run test:reporter
|
|
28
|
-
displayName: Test
|
|
29
|
-
|
|
30
|
-
- task: PublishTestResults@2
|
|
31
|
-
displayName: "Publish Test Results"
|
|
32
|
-
condition: succeededOrFailed()
|
|
33
|
-
inputs:
|
|
34
|
-
testResultsFiles: "**test/junit-testresults.xml"
|
|
35
|
-
testRunTitle: TestRun $(node_version)
|