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
package/test/date.test.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const test = require('tap').test
|
|
4
|
+
const moment = require('moment')
|
|
4
5
|
const validator = require('is-my-json-valid')
|
|
5
6
|
const build = require('..')
|
|
6
7
|
|
|
7
|
-
test('render a
|
|
8
|
+
test('render a date in a string as JSON', (t) => {
|
|
8
9
|
t.plan(2)
|
|
9
10
|
|
|
10
11
|
const schema = {
|
|
@@ -20,3 +21,382 @@ test('render a a date in a string as JSON', (t) => {
|
|
|
20
21
|
t.equal(output, JSON.stringify(toStringify))
|
|
21
22
|
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
22
23
|
})
|
|
24
|
+
|
|
25
|
+
test('render a date in a string when format is date-format as ISOString', (t) => {
|
|
26
|
+
t.plan(2)
|
|
27
|
+
|
|
28
|
+
const schema = {
|
|
29
|
+
title: 'a date in a string',
|
|
30
|
+
type: 'string',
|
|
31
|
+
format: 'date-time'
|
|
32
|
+
}
|
|
33
|
+
const toStringify = new Date()
|
|
34
|
+
|
|
35
|
+
const validate = validator(schema)
|
|
36
|
+
const stringify = build(schema)
|
|
37
|
+
const output = stringify(toStringify)
|
|
38
|
+
|
|
39
|
+
t.equal(output, JSON.stringify(toStringify))
|
|
40
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('render a date in a string when format is date as YYYY-MM-DD', (t) => {
|
|
44
|
+
t.plan(2)
|
|
45
|
+
|
|
46
|
+
const schema = {
|
|
47
|
+
title: 'a date in a string',
|
|
48
|
+
type: 'string',
|
|
49
|
+
format: 'date'
|
|
50
|
+
}
|
|
51
|
+
const toStringify = new Date()
|
|
52
|
+
|
|
53
|
+
const validate = validator(schema)
|
|
54
|
+
const stringify = build(schema)
|
|
55
|
+
const output = stringify(toStringify)
|
|
56
|
+
|
|
57
|
+
t.equal(output, `"${moment(toStringify).format('YYYY-MM-DD')}"`)
|
|
58
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('verify padding for rendered date in a string when format is date', (t) => {
|
|
62
|
+
t.plan(2)
|
|
63
|
+
|
|
64
|
+
const schema = {
|
|
65
|
+
title: 'a date in a string',
|
|
66
|
+
type: 'string',
|
|
67
|
+
format: 'date'
|
|
68
|
+
}
|
|
69
|
+
const toStringify = new Date(2020, 0, 1, 0, 0, 0, 0)
|
|
70
|
+
|
|
71
|
+
const validate = validator(schema)
|
|
72
|
+
const stringify = build(schema)
|
|
73
|
+
const output = stringify(toStringify)
|
|
74
|
+
|
|
75
|
+
t.equal(output, `"${moment(toStringify).format('YYYY-MM-DD')}"`)
|
|
76
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('render a date in a string when format is time as HH:mm:ss', (t) => {
|
|
80
|
+
t.plan(2)
|
|
81
|
+
|
|
82
|
+
const schema = {
|
|
83
|
+
title: 'a date in a string',
|
|
84
|
+
type: 'string',
|
|
85
|
+
format: 'time'
|
|
86
|
+
}
|
|
87
|
+
const toStringify = new Date()
|
|
88
|
+
|
|
89
|
+
const validate = validator(schema)
|
|
90
|
+
const stringify = build(schema)
|
|
91
|
+
const output = stringify(toStringify)
|
|
92
|
+
|
|
93
|
+
validate(JSON.parse(output))
|
|
94
|
+
|
|
95
|
+
t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
|
|
96
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('verify padding for rendered date in a string when format is time', (t) => {
|
|
100
|
+
t.plan(2)
|
|
101
|
+
|
|
102
|
+
const schema = {
|
|
103
|
+
title: 'a date in a string',
|
|
104
|
+
type: 'string',
|
|
105
|
+
format: 'time'
|
|
106
|
+
}
|
|
107
|
+
const toStringify = new Date(2020, 0, 1, 1, 1, 1, 1)
|
|
108
|
+
|
|
109
|
+
const validate = validator(schema)
|
|
110
|
+
const stringify = build(schema)
|
|
111
|
+
const output = stringify(toStringify)
|
|
112
|
+
|
|
113
|
+
validate(JSON.parse(output))
|
|
114
|
+
|
|
115
|
+
t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
|
|
116
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('render a moment.js instance in a string when format is date-time as ISOString', (t) => {
|
|
120
|
+
t.plan(2)
|
|
121
|
+
|
|
122
|
+
const schema = {
|
|
123
|
+
title: 'a moment.js object in a string',
|
|
124
|
+
type: 'string',
|
|
125
|
+
format: 'date-time'
|
|
126
|
+
}
|
|
127
|
+
const toStringify = moment()
|
|
128
|
+
|
|
129
|
+
const validate = validator(schema)
|
|
130
|
+
const stringify = build(schema)
|
|
131
|
+
const output = stringify(toStringify)
|
|
132
|
+
|
|
133
|
+
t.equal(output, JSON.stringify(toStringify))
|
|
134
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('render a moment.js instance in a string when format is date as YYYY-MM-DD', (t) => {
|
|
138
|
+
t.plan(2)
|
|
139
|
+
|
|
140
|
+
const schema = {
|
|
141
|
+
title: 'a moment.js object in a string',
|
|
142
|
+
type: 'string',
|
|
143
|
+
format: 'date'
|
|
144
|
+
}
|
|
145
|
+
const toStringify = moment()
|
|
146
|
+
|
|
147
|
+
const validate = validator(schema)
|
|
148
|
+
const stringify = build(schema)
|
|
149
|
+
const output = stringify(toStringify)
|
|
150
|
+
|
|
151
|
+
t.equal(output, `"${toStringify.format('YYYY-MM-DD')}"`)
|
|
152
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('render a moment.js instance in a string when format is time as HH:mm:ss', (t) => {
|
|
156
|
+
t.plan(2)
|
|
157
|
+
|
|
158
|
+
const schema = {
|
|
159
|
+
title: 'a moment.js object in a string',
|
|
160
|
+
type: 'string',
|
|
161
|
+
format: 'time'
|
|
162
|
+
}
|
|
163
|
+
const toStringify = moment()
|
|
164
|
+
|
|
165
|
+
const validate = validator(schema)
|
|
166
|
+
const stringify = build(schema)
|
|
167
|
+
const output = stringify(toStringify)
|
|
168
|
+
|
|
169
|
+
t.equal(output, `"${toStringify.format('HH:mm:ss')}"`)
|
|
170
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('render a nested object in a string when type is date-format as ISOString', (t) => {
|
|
174
|
+
t.plan(2)
|
|
175
|
+
|
|
176
|
+
const schema = {
|
|
177
|
+
title: 'an object in a string',
|
|
178
|
+
type: 'object',
|
|
179
|
+
properties: {
|
|
180
|
+
date: {
|
|
181
|
+
type: 'string',
|
|
182
|
+
format: 'date-time'
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const toStringify = { date: moment() }
|
|
187
|
+
|
|
188
|
+
const validate = validator(schema)
|
|
189
|
+
const stringify = build(schema)
|
|
190
|
+
const output = stringify(toStringify)
|
|
191
|
+
|
|
192
|
+
t.equal(output, JSON.stringify(toStringify))
|
|
193
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('serializing null value', t => {
|
|
197
|
+
const input = { updatedAt: null }
|
|
198
|
+
|
|
199
|
+
function createSchema (properties) {
|
|
200
|
+
return {
|
|
201
|
+
title: 'an object in a string',
|
|
202
|
+
type: 'object',
|
|
203
|
+
properties
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function serialize (schema, input) {
|
|
208
|
+
const validate = validator(schema)
|
|
209
|
+
const stringify = build(schema)
|
|
210
|
+
const output = stringify(input)
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
validate,
|
|
214
|
+
output
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
t.plan(3)
|
|
219
|
+
|
|
220
|
+
t.test('type::string', t => {
|
|
221
|
+
t.plan(3)
|
|
222
|
+
|
|
223
|
+
t.test('format::date-time', t => {
|
|
224
|
+
t.plan(2)
|
|
225
|
+
|
|
226
|
+
const prop = {
|
|
227
|
+
updatedAt: {
|
|
228
|
+
type: 'string',
|
|
229
|
+
format: 'date-time'
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const {
|
|
234
|
+
output,
|
|
235
|
+
validate
|
|
236
|
+
} = serialize(createSchema(prop), input)
|
|
237
|
+
|
|
238
|
+
t.equal(output, '{"updatedAt":""}')
|
|
239
|
+
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date-time format')
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
t.test('format::date', t => {
|
|
243
|
+
t.plan(2)
|
|
244
|
+
|
|
245
|
+
const prop = {
|
|
246
|
+
updatedAt: {
|
|
247
|
+
type: 'string',
|
|
248
|
+
format: 'date'
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const {
|
|
253
|
+
output,
|
|
254
|
+
validate
|
|
255
|
+
} = serialize(createSchema(prop), input)
|
|
256
|
+
|
|
257
|
+
t.equal(output, '{"updatedAt":""}')
|
|
258
|
+
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date format')
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
t.test('format::time', t => {
|
|
262
|
+
t.plan(2)
|
|
263
|
+
|
|
264
|
+
const prop = {
|
|
265
|
+
updatedAt: {
|
|
266
|
+
type: 'string',
|
|
267
|
+
format: 'time'
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const {
|
|
272
|
+
output,
|
|
273
|
+
validate
|
|
274
|
+
} = serialize(createSchema(prop), input)
|
|
275
|
+
|
|
276
|
+
t.equal(output, '{"updatedAt":""}')
|
|
277
|
+
t.notOk(validate(JSON.parse(output)), 'an empty string is not a time format')
|
|
278
|
+
})
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
t.test('type::array', t => {
|
|
282
|
+
t.plan(3)
|
|
283
|
+
|
|
284
|
+
t.test('format::date-time', t => {
|
|
285
|
+
t.plan(2)
|
|
286
|
+
|
|
287
|
+
const prop = {
|
|
288
|
+
updatedAt: {
|
|
289
|
+
type: ['string'],
|
|
290
|
+
format: 'date-time'
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const {
|
|
295
|
+
output,
|
|
296
|
+
validate
|
|
297
|
+
} = serialize(createSchema(prop), input)
|
|
298
|
+
|
|
299
|
+
t.equal(output, '{"updatedAt":""}')
|
|
300
|
+
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date-time format')
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
t.test('format::date', t => {
|
|
304
|
+
t.plan(2)
|
|
305
|
+
|
|
306
|
+
const prop = {
|
|
307
|
+
updatedAt: {
|
|
308
|
+
type: ['string'],
|
|
309
|
+
format: 'date'
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const {
|
|
314
|
+
output,
|
|
315
|
+
validate
|
|
316
|
+
} = serialize(createSchema(prop), input)
|
|
317
|
+
|
|
318
|
+
t.equal(output, '{"updatedAt":""}')
|
|
319
|
+
t.notOk(validate(JSON.parse(output)), 'an empty string is not a date format')
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
t.test('format::time', t => {
|
|
323
|
+
t.plan(2)
|
|
324
|
+
|
|
325
|
+
const prop = {
|
|
326
|
+
updatedAt: {
|
|
327
|
+
type: ['string'],
|
|
328
|
+
format: 'time'
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const {
|
|
333
|
+
output,
|
|
334
|
+
validate
|
|
335
|
+
} = serialize(createSchema(prop), input)
|
|
336
|
+
|
|
337
|
+
t.equal(output, '{"updatedAt":""}')
|
|
338
|
+
t.notOk(validate(JSON.parse(output)), 'an empty string is not a time format')
|
|
339
|
+
})
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
t.test('type::array::nullable', t => {
|
|
343
|
+
t.plan(3)
|
|
344
|
+
|
|
345
|
+
t.test('format::date-time', t => {
|
|
346
|
+
t.plan(2)
|
|
347
|
+
|
|
348
|
+
const prop = {
|
|
349
|
+
updatedAt: {
|
|
350
|
+
type: ['string', 'null'],
|
|
351
|
+
format: 'date-time'
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const {
|
|
356
|
+
output,
|
|
357
|
+
validate
|
|
358
|
+
} = serialize(createSchema(prop), input)
|
|
359
|
+
|
|
360
|
+
t.equal(output, '{"updatedAt":null}')
|
|
361
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
t.test('format::date', t => {
|
|
365
|
+
t.plan(2)
|
|
366
|
+
|
|
367
|
+
const prop = {
|
|
368
|
+
updatedAt: {
|
|
369
|
+
type: ['string', 'null'],
|
|
370
|
+
format: 'date'
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const {
|
|
375
|
+
output,
|
|
376
|
+
validate
|
|
377
|
+
} = serialize(createSchema(prop), input)
|
|
378
|
+
|
|
379
|
+
t.equal(output, '{"updatedAt":null}')
|
|
380
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
t.test('format::time', t => {
|
|
384
|
+
t.plan(2)
|
|
385
|
+
|
|
386
|
+
const prop = {
|
|
387
|
+
updatedAt: {
|
|
388
|
+
type: ['string', 'null'],
|
|
389
|
+
format: 'time'
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const {
|
|
394
|
+
output,
|
|
395
|
+
validate
|
|
396
|
+
} = serialize(createSchema(prop), input)
|
|
397
|
+
|
|
398
|
+
t.equal(output, '{"updatedAt":null}')
|
|
399
|
+
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
400
|
+
})
|
|
401
|
+
})
|
|
402
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tap').test
|
|
4
|
+
const fjs = require('..')
|
|
5
|
+
|
|
6
|
+
function build (opts) {
|
|
7
|
+
return fjs({
|
|
8
|
+
title: 'default string',
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
firstName: {
|
|
12
|
+
type: 'string'
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
required: ['firstName']
|
|
16
|
+
}, opts)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('activate debug mode', t => {
|
|
20
|
+
t.plan(2)
|
|
21
|
+
const debugMode = build({ debugMode: true })
|
|
22
|
+
t.type(debugMode, Array)
|
|
23
|
+
t.like(debugMode.toString.toString(), 'join', 'to string override')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('activate debug mode truthy', t => {
|
|
27
|
+
t.plan(2)
|
|
28
|
+
const debugMode = build({ debugMode: 'yes' })
|
|
29
|
+
t.type(debugMode, Array)
|
|
30
|
+
t.like(debugMode.toString.toString(), 'join', 'to string override')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('to string auto-consistent', t => {
|
|
34
|
+
t.plan(2)
|
|
35
|
+
const debugMode = build({ debugMode: 1 })
|
|
36
|
+
t.type(debugMode, Array)
|
|
37
|
+
|
|
38
|
+
const str = debugMode.toString()
|
|
39
|
+
const compiled = fjs.restore(str)
|
|
40
|
+
const tobe = JSON.stringify({ firstName: 'Foo' })
|
|
41
|
+
t.deepEquals(compiled({ firstName: 'Foo', surname: 'bar' }), tobe, 'surname evicted')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('to string auto-consistent with ajv', t => {
|
|
45
|
+
t.plan(2)
|
|
46
|
+
const debugMode = fjs({
|
|
47
|
+
title: 'object with multiple types field',
|
|
48
|
+
type: 'object',
|
|
49
|
+
properties: {
|
|
50
|
+
str: {
|
|
51
|
+
anyOf: [{
|
|
52
|
+
type: 'string'
|
|
53
|
+
}, {
|
|
54
|
+
type: 'boolean'
|
|
55
|
+
}]
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}, { debugMode: 1 })
|
|
59
|
+
t.type(debugMode, Array)
|
|
60
|
+
|
|
61
|
+
const str = debugMode.toString()
|
|
62
|
+
const compiled = fjs.restore(str)
|
|
63
|
+
const tobe = JSON.stringify({ str: 'Foo' })
|
|
64
|
+
t.deepEquals(compiled({ str: 'Foo', void: 'me' }), tobe)
|
|
65
|
+
})
|