fast-json-stringify 7.2.0 → 7.2.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.
@@ -19,6 +19,6 @@ jobs:
19
19
  permissions:
20
20
  id-token: write # required for npm provenance via OIDC
21
21
  contents: write # required to push the release commit and the tag
22
- uses: fastify/workflows/.github/workflows/reusable-release.yml@b1a11bce713bc857e52b2231fec3c89071ad3135
22
+ uses: fastify/workflows/.github/workflows/reusable-release.yml@731d0cca9a6a5b9586dad6b0326d49001ab9216f
23
23
  with:
24
24
  semver: ${{ inputs.semver }}
package/index.js CHANGED
@@ -1161,7 +1161,7 @@ function buildArray (context, location, input) {
1161
1161
  const tmpRes = buildValue(context, itemLocation, value)
1162
1162
  functionCode += `
1163
1163
  if (${i} < arrayLength) {
1164
- if (${buildArrayTypeCondition(item.type, value)}) {
1164
+ if (${buildTupleItemCondition(item, value)}) {
1165
1165
  if (${i}) {
1166
1166
  json += JSON_STR_COMMA
1167
1167
  }
@@ -1255,7 +1255,7 @@ function buildArray (context, location, input) {
1255
1255
  const tmpRes = buildValue(context, itemLocation, value)
1256
1256
  inlinedCode += `
1257
1257
  if (${i} < arrayLength_${objVar}) {
1258
- if (${buildArrayTypeCondition(item.type, value)}) {
1258
+ if (${buildTupleItemCondition(item, value)}) {
1259
1259
  !addComma_${localUid} && (addComma_${localUid} = true) || (json += JSON_STR_COMMA)
1260
1260
  ${tmpRes}
1261
1261
  } else {
@@ -1342,6 +1342,13 @@ function buildArrayTypeCondition (type, accessor) {
1342
1342
  return condition
1343
1343
  }
1344
1344
 
1345
+ // A nullable tuple item accepts null on top of its declared type(s);
1346
+ // buildValue() then serializes it as `null`.
1347
+ function buildTupleItemCondition (item, accessor) {
1348
+ const condition = buildArrayTypeCondition(item.type, accessor)
1349
+ return item.nullable === true ? `${accessor} === null || ${condition}` : condition
1350
+ }
1351
+
1345
1352
  function generateFuncName (context) {
1346
1353
  return 'anonymous' + context.functionsCounter++
1347
1354
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "7.2.0",
3
+ "version": "7.2.1",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -411,6 +411,35 @@ test('array items is a schema and additionalItems is false', (t) => {
411
411
  t.assert.equal(validate({ foo: ['foo', 'bar'] }), true)
412
412
  })
413
413
 
414
+ test('array items is a list of schema with nullable items', (t) => {
415
+ t.plan(6)
416
+
417
+ const items = [
418
+ { type: 'integer', nullable: true },
419
+ { type: 'number', nullable: true },
420
+ { type: 'boolean', nullable: true },
421
+ { type: 'object', nullable: true, properties: { a: { type: 'string' } } },
422
+ { type: 'array', nullable: true, items: { type: 'string' } },
423
+ { type: 'string', nullable: true }
424
+ ]
425
+ const input = [null, null, null, null, null, null]
426
+ const expected = '[null,null,null,null,null,null]'
427
+
428
+ const inlined = build({ type: 'array', items })
429
+ t.assert.equal(inlined(input), expected)
430
+ t.assert.equal(inlined([1, 1.5, true, { a: 'x' }, ['y'], 'z']), '[1,1.5,true,{"a":"x"},["y"],"z"]')
431
+
432
+ const withId = build({ $id: 'nullable-tuple', type: 'array', items })
433
+ t.assert.equal(withId(input), expected)
434
+ t.assert.equal(withId([1, 1.5, true, { a: 'x' }, ['y'], 'z']), '[1,1.5,true,{"a":"x"},["y"],"z"]')
435
+
436
+ const notNullable = build({ type: 'array', items: [{ type: 'integer' }] })
437
+ t.assert.throws(() => notNullable([null]), /Item at 0 does not match schema definition/)
438
+
439
+ const ajv = new Ajv({ allErrors: true, strict: false })
440
+ t.assert.equal(ajv.validate({ type: 'array', items }, JSON.parse(inlined(input))), true)
441
+ })
442
+
414
443
  test('array items is a list of schema and additionalItems is a schema', (t) => {
415
444
  t.plan(1)
416
445