fast-json-stringify
Advanced tools
Comparing version 1.10.0 to 1.11.0
27
index.js
@@ -427,5 +427,21 @@ 'use strict' | ||
function idFinder (schema, searchedId) { | ||
let objSchema | ||
const explore = (schema, searchedId) => { | ||
Object.keys(schema || {}).forEach((key, i, a) => { | ||
if (key === '$id' && schema[key] === searchedId) { | ||
objSchema = schema | ||
} else if (objSchema === undefined && typeof schema[key] === 'object') { | ||
explore(schema[key], searchedId) | ||
} | ||
}) | ||
} | ||
explore(schema, searchedId) | ||
return objSchema | ||
} | ||
function refFinder (ref, schema, externalSchema) { | ||
// Split file from walk | ||
ref = ref.split('#') | ||
// If external file | ||
@@ -435,8 +451,15 @@ if (ref[0]) { | ||
} | ||
var code = 'return schema' | ||
// If it has a path | ||
if (ref[1]) { | ||
// ref[1] could contain a JSON pointer - ex: /definitions/num | ||
// or plan name fragment id without suffix # - ex: customId | ||
var walk = ref[1].split('/') | ||
for (var i = 1; i < walk.length; i++) { | ||
code += `['${walk[i]}']` | ||
if (walk.length === 1) { | ||
return idFinder(schema, `#${ref[1]}`) | ||
} else { | ||
for (var i = 1; i < walk.length; i++) { | ||
code += `['${walk[i]}']` | ||
} | ||
} | ||
@@ -443,0 +466,0 @@ } |
{ | ||
"name": "fast-json-stringify", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -36,9 +36,9 @@ "main": "index.js", | ||
"standard": "^12.0.1", | ||
"tap": "^12.0.0", | ||
"tap": "^12.5.1", | ||
"uglify-es": "^3.3.9" | ||
}, | ||
"dependencies": { | ||
"ajv": "^6.5.4", | ||
"ajv": "^6.8.1", | ||
"deepmerge": "^3.0.0" | ||
} | ||
} |
@@ -230,3 +230,4 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/fastify/fast-json-stringify.svg?branch=master)](https://travis-ci.org/fastify/fast-json-stringify) | ||
If *additionalProperties* is not present or is set to `false`, every property that is not explicitly listed in the *properties* and *patternProperties* objects,will be ignored, as described in <a href="#missingFields">Missing fields</a>. | ||
If *additionalProperties* is not present or is set to `false`, every property that is not explicitly listed in the *properties* and *patternProperties* objects,will be ignored, as described in <a href="#missingFields">Missing fields</a>. | ||
Missing fields are ignored to avoid having to rewrite objects before serializing. However, other schema rules would throw in similar situations. | ||
If *additionalProperties* is set to `true`, it will be used by `JSON.stringify` to stringify the additional properties. If you want to achieve maximum performance, we strongly encourage you to use a fixed schema where possible. | ||
@@ -233,0 +234,0 @@ Example: |
@@ -392,1 +392,96 @@ 'use strict' | ||
}) | ||
test('ref internal - plain name fragment', (t) => { | ||
t.plan(2) | ||
const schema = { | ||
title: 'object with $ref', | ||
definitions: { | ||
def: { | ||
$id: '#uri', | ||
type: 'object', | ||
properties: { | ||
str: { | ||
type: 'string' | ||
} | ||
}, | ||
required: ['str'] | ||
} | ||
}, | ||
type: 'object', | ||
properties: { | ||
obj: { | ||
$ref: '#uri' | ||
} | ||
} | ||
} | ||
const object = { | ||
obj: { | ||
str: 'test' | ||
} | ||
} | ||
const stringify = build(schema) | ||
const output = stringify(object) | ||
try { | ||
JSON.parse(output) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
t.equal(output, '{"obj":{"str":"test"}}') | ||
}) | ||
test('ref internal - multiple $ref format', (t) => { | ||
t.plan(2) | ||
const schema = { | ||
type: 'object', | ||
definitions: { | ||
one: { | ||
type: 'string', | ||
definitions: { | ||
two: { | ||
$id: '#twos', | ||
type: 'string' | ||
} | ||
} | ||
} | ||
}, | ||
properties: { | ||
zero: { | ||
$id: '#three', | ||
type: 'string' | ||
}, | ||
a: { $ref: '#/definitions/one' }, | ||
b: { $ref: '#three' }, | ||
c: { $ref: '#/properties/zero' }, | ||
d: { $ref: '#twos' }, | ||
e: { $ref: '#/definitions/one/definitions/two' } | ||
} | ||
} | ||
const object = { | ||
zero: 'test', | ||
a: 'test', | ||
b: 'test', | ||
c: 'test', | ||
d: 'test', | ||
e: 'test' | ||
} | ||
const stringify = build(schema) | ||
const output = stringify(object) | ||
try { | ||
JSON.parse(output) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
t.equal(output, '{"zero":"test","a":"test","b":"test","c":"test","d":"test","e":"test"}') | ||
}) |
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
114367
4363
505
Updatedajv@^6.8.1