fast-json-stringify
Advanced tools
Comparing version 5.9.1 to 5.10.0
25
index.js
@@ -7,3 +7,2 @@ 'use strict' | ||
const clone = require('rfdc')({ proto: true }) | ||
const { randomUUID } = require('crypto') | ||
const { RefResolver } = require('json-schema-ref-resolver') | ||
@@ -35,2 +34,4 @@ | ||
let schemaIdCounter = 0 | ||
function isValidSchema (schema, name) { | ||
@@ -91,3 +92,3 @@ if (!validate(schema)) { | ||
refResolver: new RefResolver(), | ||
rootSchemaId: schema.$id || randomUUID(), | ||
rootSchemaId: schema.$id || `__fjs_root_${schemaIdCounter++}`, | ||
validatorSchemasIds: new Set() | ||
@@ -506,3 +507,3 @@ } | ||
mergedSchema.$id = `merged_${randomUUID()}` | ||
mergedSchema.$id = `__fjs_merged_${schemaIdCounter++}` | ||
context.refResolver.addSchema(mergedSchema) | ||
@@ -566,2 +567,3 @@ location.addMergedSchema(mergedSchema, mergedSchema.$id) | ||
const nullable = schema.nullable === true | ||
functionCode += ` | ||
@@ -571,2 +573,4 @@ // ${schemaRef} | ||
const obj = ${toJSON('input')} | ||
${!nullable ? 'if (obj === null) return \'{}\'' : ''} | ||
${buildInnerObject(context, location)} | ||
@@ -609,3 +613,5 @@ } | ||
const nullable = schema.nullable === true | ||
functionCode += ` | ||
${!nullable ? 'if (obj === null) return \'[]\'' : ''} | ||
if (!Array.isArray(obj)) { | ||
@@ -690,3 +696,11 @@ throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`) | ||
case 'string': | ||
condition = `typeof obj${accessor} === 'string'` | ||
condition = `typeof obj${accessor} === 'string' || | ||
obj${accessor} === null || | ||
obj${accessor} instanceof Date || | ||
obj${accessor} instanceof RegExp || | ||
( | ||
typeof obj${accessor} === "object" && | ||
typeof obj${accessor}.toString === "function" && | ||
obj${accessor}.toString !== Object.prototype.toString | ||
)` | ||
break | ||
@@ -751,4 +765,3 @@ case 'integer': | ||
typeof ${input}.toString === "function" && | ||
${input}.toString !== Object.prototype.toString && | ||
!(${input} instanceof Date) | ||
${input}.toString !== Object.prototype.toString | ||
) | ||
@@ -755,0 +768,0 @@ ) |
{ | ||
"name": "fast-json-stringify", | ||
"version": "5.9.1", | ||
"version": "5.10.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -19,3 +19,6 @@ "main": "index.js", | ||
}, | ||
"precommit": ["lint", "test"], | ||
"precommit": [ | ||
"lint", | ||
"test" | ||
], | ||
"repository": { | ||
@@ -39,3 +42,3 @@ "type": "git", | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@sinclair/typebox": "^0.31.1", | ||
"@sinclair/typebox": "^0.32.3", | ||
"benchmark": "^2.1.4", | ||
@@ -42,0 +45,0 @@ "cli-select": "^1.1.2", |
@@ -593,2 +593,4 @@ # fast-json-stringify | ||
- `boolean` -> `false` | ||
- `object` -> `{}` | ||
- `array` -> `[]` | ||
@@ -595,0 +597,0 @@ <a name="largearrays"></a> |
@@ -34,3 +34,3 @@ 'use strict' | ||
t.same(JSON.parse(output), toStringify) | ||
t.same(JSON.parse(output), JSON.parse(JSON.stringify(toStringify))) | ||
t.equal(output, JSON.stringify(toStringify)) | ||
@@ -42,2 +42,26 @@ t.ok(validate(JSON.parse(output)), 'valid schema') | ||
buildTest({ | ||
title: 'dates tuple', | ||
type: 'object', | ||
properties: { | ||
dates: { | ||
type: 'array', | ||
minItems: 2, | ||
maxItems: 2, | ||
items: [ | ||
{ | ||
type: 'string', | ||
format: 'date-time' | ||
}, | ||
{ | ||
type: 'string', | ||
format: 'date-time' | ||
} | ||
] | ||
} | ||
} | ||
}, { | ||
dates: [new Date(1), new Date(2)] | ||
}) | ||
buildTest({ | ||
title: 'string array', | ||
@@ -44,0 +68,0 @@ type: 'object', |
@@ -130,3 +130,3 @@ 'use strict' | ||
test('throw an error on non nullable null sub-object', (t) => { | ||
test('on non nullable null sub-object it should coerce to {}', (t) => { | ||
t.plan(1) | ||
@@ -152,6 +152,8 @@ | ||
} | ||
t.throws(() => { stringify(object) }) | ||
const result = stringify(object) | ||
t.equal(result, JSON.stringify({ product: {} })) | ||
}) | ||
test('throw an error on non nullable null object', (t) => { | ||
test('on non nullable null object it should coerce to {}', (t) => { | ||
t.plan(1) | ||
@@ -175,3 +177,30 @@ | ||
}) | ||
t.throws(() => { stringify(null) }) | ||
const result = stringify(null) | ||
t.equal(result, '{}') | ||
}) | ||
test('on non-nullable null object it should skip rendering, skipping required fields checks', (t) => { | ||
t.plan(1) | ||
const stringify = build({ | ||
title: 'simple object', | ||
nullable: false, | ||
type: 'object', | ||
properties: { | ||
product: { | ||
nullable: false, | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
}, | ||
required: ['product'] | ||
}) | ||
const result = stringify(null) | ||
t.equal(result, '{}') | ||
}) |
@@ -470,3 +470,3 @@ 'use strict' | ||
test('should throw an error when type is array and object is null', (t) => { | ||
test('should not throw an error when type is array and object is null, it should instead coerce to []', (t) => { | ||
t.plan(1) | ||
@@ -486,3 +486,4 @@ const schema = { | ||
const stringify = build(schema) | ||
t.throws(() => stringify({ arr: null }), new TypeError('The value of \'#/properties/arr\' does not match schema definition.')) | ||
const result = stringify({ arr: null }) | ||
t.equal(result, JSON.stringify({ arr: [] })) | ||
}) | ||
@@ -489,0 +490,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
355697
13281
722