fast-json-stringify 7.3.0 → 7.3.2

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/index.js CHANGED
@@ -1392,7 +1392,7 @@ function generateFuncName (context) {
1392
1392
 
1393
1393
  function buildMultiTypeSerializer (context, location, input) {
1394
1394
  const schema = location.schema
1395
- const types = schema.type.sort(t1 => t1 === 'null' ? -1 : 1)
1395
+ const types = [...schema.type].sort(t1 => t1 === 'null' ? -1 : 1)
1396
1396
 
1397
1397
  let code = ''
1398
1398
 
package/lib/projection.js CHANGED
@@ -306,7 +306,7 @@ const projectionHelpers = `
306
306
  if (v === null) return ''
307
307
  if (v instanceof Date) return v.toISOString()
308
308
  if (typeof v === 'string') return v
309
- return v.toString()
309
+ throw new Error('The value "' + v + '" cannot be converted to a date-time.')
310
310
  }
311
311
  const asDateValue = (v) => { const s = serializer.asDate(v); return s.slice(1, -1) }
312
312
  const asTimeValue = (v) => { const s = serializer.asTime(v); return s.slice(1, -1) }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "7.3.0",
3
+ "version": "7.3.2",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -66,6 +66,10 @@ const inputs = {
66
66
  'Date in a plain string': { id: 1, name: new Date(0), tags: [new Date(0)] },
67
67
  'RegExp in a plain string': { id: 1, name: /a"b/, tags: [/c/g] },
68
68
  'date-time given as string': { id: 1, createdAt: '2020-01-01T00:00:00.000Z' },
69
+ 'date-time given as a number throws': { id: 1, createdAt: 1700000000000 },
70
+ 'date-time given as an object throws': { id: 1, createdAt: { toString () { return 'custom' } } },
71
+ 'date given as a number throws': { id: 1, birthday: 1700000000000 },
72
+ 'time given as a number throws': { id: 1, at: 1700000000000 },
69
73
  'toJSON on root': { id: 1, toJSON () { return { id: 7, name: 'json' } } },
70
74
  'toJSON on nested object': { id: 1, address: { toJSON () { return { city: 'Roma' } } } },
71
75
  'toJSON on array item': { id: 1, friends: [{ toJSON () { return { id: 9 } } }] },
@@ -4,6 +4,63 @@ const { test } = require('node:test')
4
4
  const clone = require('rfdc/default')
5
5
  const build = require('..')
6
6
 
7
+ test('multi-type schemas should not change the input schema', t => {
8
+ t.plan(5)
9
+
10
+ const schema = {
11
+ type: ['object', 'null'],
12
+ properties: {
13
+ name: { type: ['string', 'null'] },
14
+ values: { type: 'array', items: { type: ['number', 'null'] } }
15
+ }
16
+ }
17
+ const clonedSchema = clone(schema)
18
+ const stringify = build(schema)
19
+
20
+ t.assert.deepStrictEqual(schema, clonedSchema)
21
+ t.assert.equal(stringify({ name: 'hello', values: [42, null] }), '{"name":"hello","values":[42,null]}')
22
+ t.assert.equal(stringify(null), 'null')
23
+
24
+ const stringifyAgain = build(schema)
25
+ t.assert.equal(stringifyAgain({ name: null, values: [] }), '{"name":null,"values":[]}')
26
+ t.assert.deepStrictEqual(schema, clonedSchema)
27
+ })
28
+
29
+ test('multi-type schemas should not change an external reference schema', t => {
30
+ t.plan(3)
31
+
32
+ const referenceSchema = {
33
+ $id: 'externalSchema',
34
+ type: ['string', 'null']
35
+ }
36
+ const clonedSchema = clone(referenceSchema)
37
+ const stringify = build({
38
+ type: 'object',
39
+ properties: { name: { $ref: 'externalSchema' } }
40
+ }, {
41
+ schema: { externalSchema: referenceSchema }
42
+ })
43
+
44
+ t.assert.equal(stringify({ name: 'hello' }), '{"name":"hello"}')
45
+ t.assert.equal(stringify({ name: null }), '{"name":null}')
46
+ t.assert.deepStrictEqual(referenceSchema, clonedSchema)
47
+ })
48
+
49
+ test('multi-type schemas should support frozen type arrays', t => {
50
+ t.plan(3)
51
+
52
+ const stringify = build({
53
+ type: Object.freeze(['object', 'null']),
54
+ properties: {
55
+ name: { type: Object.freeze(['string', 'null']) }
56
+ }
57
+ })
58
+
59
+ t.assert.equal(stringify({ name: 'hello' }), '{"name":"hello"}')
60
+ t.assert.equal(stringify({ name: null }), '{"name":null}')
61
+ t.assert.equal(stringify(null), 'null')
62
+ })
63
+
7
64
  test('oneOf with $ref should not change the input schema', t => {
8
65
  t.plan(2)
9
66