fast-json-stringify
Advanced tools
Comparing version 2.7.1 to 2.7.2
39
index.js
@@ -231,2 +231,6 @@ 'use strict' | ||
function getTestSerializer (format) { | ||
return stringSerializerMap[format] | ||
} | ||
function $pad2Zeros (num) { | ||
@@ -284,7 +288,8 @@ const s = '00' + num | ||
function $asDatetime (date) { | ||
function $asDatetime (date, skipQuotes) { | ||
const quotes = skipQuotes === true ? '' : '"' | ||
if (date instanceof Date) { | ||
return '"' + date.toISOString() + '"' | ||
return quotes + date.toISOString() + quotes | ||
} else if (date && typeof date.toISOString === 'function') { | ||
return '"' + date.toISOString() + '"' | ||
return quotes + date.toISOString() + quotes | ||
} else { | ||
@@ -295,3 +300,4 @@ return $asString(date) | ||
function $asDate (date) { | ||
function $asDate (date, skipQuotes) { | ||
const quotes = skipQuotes === true ? '' : '"' | ||
if (date instanceof Date) { | ||
@@ -301,5 +307,5 @@ const year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(date) | ||
const day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date) | ||
return '"' + year + '-' + month + '-' + day + '"' | ||
return quotes + year + '-' + month + '-' + day + quotes | ||
} else if (date && typeof date.format === 'function') { | ||
return '"' + date.format('YYYY-MM-DD') + '"' | ||
return quotes + date.format('YYYY-MM-DD') + quotes | ||
} else { | ||
@@ -310,3 +316,4 @@ return $asString(date) | ||
function $asTime (date) { | ||
function $asTime (date, skipQuotes) { | ||
const quotes = skipQuotes === true ? '' : '"' | ||
if (date instanceof Date) { | ||
@@ -316,5 +323,5 @@ const hour = new Intl.DateTimeFormat('en', { hour: 'numeric', hour12: false }).format(date) | ||
const second = new Intl.DateTimeFormat('en', { second: 'numeric' }).format(date) | ||
return '"' + $pad2Zeros(hour) + ':' + $pad2Zeros(minute) + ':' + $pad2Zeros(second) + '"' | ||
return quotes + $pad2Zeros(hour) + ':' + $pad2Zeros(minute) + ':' + $pad2Zeros(second) + quotes | ||
} else if (date && typeof date.format === 'function') { | ||
return '"' + date.format('HH:mm:ss') + '"' | ||
return quotes + date.format('HH:mm:ss') + quotes | ||
} else { | ||
@@ -1186,2 +1193,7 @@ return $asString(date) | ||
const nestedResult = nested(laterCode, name, key, location, subKey !== '' ? subKey : 'i' + index, isArray) | ||
// We need a test serializer as the String serializer will not work with | ||
// date/time ajv validations | ||
// see: https://github.com/fastify/fast-json-stringify/issues/325 | ||
const testSerializer = getTestSerializer(location.schema.format) | ||
const testValue = testSerializer !== undefined ? `${testSerializer}(obj${accessor}, true)` : `obj${accessor}` | ||
@@ -1196,3 +1208,3 @@ // Since we are only passing the relevant schema to ajv.validate, it needs to be full dereferenced | ||
code += ` | ||
${index === 0 ? 'if' : 'else if'}(ajv.validate(${JSON.stringify(location.schema)}, obj${accessor})) | ||
${index === 0 ? 'if' : 'else if'}(ajv.validate(${JSON.stringify(location.schema)}, ${testValue})) | ||
${nestedResult.code} | ||
@@ -1210,6 +1222,7 @@ ` | ||
const nestedResult = nested(laterCode, name, key, location, subKey !== '' ? subKey : 'i' + index, isArray) | ||
// see comment on anyOf about derefencing the schema before calling ajv.validate | ||
const testSerializer = getTestSerializer(location.schema.format) | ||
const testValue = testSerializer !== undefined ? `${testSerializer}(obj${accessor}, true)` : `obj${accessor}` | ||
// see comment on anyOf about dereferencing the schema before calling ajv.validate | ||
code += ` | ||
${index === 0 ? 'if' : 'else if'}(ajv.validate(${JSON.stringify(location.schema)}, obj${accessor})) | ||
${index === 0 ? 'if' : 'else if'}(ajv.validate(${JSON.stringify(location.schema)}, ${testValue})) | ||
${nestedResult.code} | ||
@@ -1216,0 +1229,0 @@ ` |
{ | ||
"name": "fast-json-stringify", | ||
"version": "2.7.1", | ||
"version": "2.7.2", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -450,1 +450,25 @@ 'use strict' | ||
}) | ||
test('anyOf object with field of type string with format or null', (t) => { | ||
t.plan(1) | ||
const toStringify = new Date() | ||
const withOneOfSchema = { | ||
type: 'object', | ||
properties: { | ||
prop: { | ||
anyOf: [{ | ||
type: 'string', | ||
format: 'date-time' | ||
}, { | ||
type: 'null' | ||
}] | ||
} | ||
} | ||
} | ||
const withOneOfStringify = build(withOneOfSchema) | ||
t.equal(withOneOfStringify({ | ||
prop: toStringify | ||
}), `{"prop":"${toStringify.toISOString()}"}`) | ||
}) |
@@ -378,1 +378,27 @@ 'use strict' | ||
}) | ||
test('oneOf object with field of type string with format or null', (t) => { | ||
t.plan(1) | ||
const toStringify = new Date() | ||
const withOneOfSchema = { | ||
type: 'object', | ||
properties: { | ||
prop: { | ||
oneOf: [{ | ||
type: 'string', | ||
format: 'date-time' | ||
}, { | ||
type: 'null' | ||
}] | ||
} | ||
} | ||
} | ||
const withOneOfStringify = build(withOneOfSchema) | ||
t.equal(withOneOfStringify({ | ||
prop: toStringify | ||
}), `{"prop":"${toStringify.toISOString()}"}`) | ||
}) |
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
331447
9994