fast-json-stringify 2.5.1 → 2.5.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 +6 -2
- package/package.json +1 -1
- package/test/side-effect.test.js +34 -0
package/index.js
CHANGED
|
@@ -957,7 +957,7 @@ function buildObject (location, code, name) {
|
|
|
957
957
|
}
|
|
958
958
|
|
|
959
959
|
function buildArray (location, code, name, key = null) {
|
|
960
|
-
|
|
960
|
+
let schema = location.schema
|
|
961
961
|
code += `
|
|
962
962
|
function ${name} (obj) {
|
|
963
963
|
`
|
|
@@ -979,6 +979,10 @@ function buildArray (location, code, name, key = null) {
|
|
|
979
979
|
}
|
|
980
980
|
|
|
981
981
|
if (schema.items.$ref) {
|
|
982
|
+
if (!schema[fjsCloned]) {
|
|
983
|
+
schema = clone(location.schema)
|
|
984
|
+
schema[fjsCloned] = true
|
|
985
|
+
}
|
|
982
986
|
location = refFinder(schema.items.$ref, location)
|
|
983
987
|
schema.items = location.schema
|
|
984
988
|
}
|
|
@@ -1215,7 +1219,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1215
1219
|
`
|
|
1216
1220
|
} else if ('const' in schema) {
|
|
1217
1221
|
code += `
|
|
1218
|
-
if(ajv.validate(${
|
|
1222
|
+
if(ajv.validate(${JSON.stringify(schema)}, obj${accessor}))
|
|
1219
1223
|
json += '${JSON.stringify(schema.const)}'
|
|
1220
1224
|
else
|
|
1221
1225
|
throw new Error(\`Item $\{JSON.stringify(obj${accessor})} does not match schema definition.\`)
|
package/package.json
CHANGED
package/test/side-effect.test.js
CHANGED
|
@@ -121,3 +121,37 @@ test('multiple $ref tree', t => {
|
|
|
121
121
|
t.is(value, '{"people":{"name":"hello","age":42}}')
|
|
122
122
|
t.deepEqual(schema, clonedSchema)
|
|
123
123
|
})
|
|
124
|
+
|
|
125
|
+
test('must not mutate items $ref', t => {
|
|
126
|
+
t.plan(2)
|
|
127
|
+
|
|
128
|
+
const referenceSchema = {
|
|
129
|
+
$id: 'ShowSchema',
|
|
130
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
131
|
+
type: 'object',
|
|
132
|
+
properties: {
|
|
133
|
+
name: {
|
|
134
|
+
type: 'string'
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const schema = {
|
|
140
|
+
$id: 'ListSchema',
|
|
141
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
142
|
+
type: 'array',
|
|
143
|
+
items: {
|
|
144
|
+
$ref: 'ShowSchema#'
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const clonedSchema = clone(schema)
|
|
148
|
+
const stringify = build(schema, {
|
|
149
|
+
schema: {
|
|
150
|
+
[referenceSchema.$id]: referenceSchema
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
const value = stringify([{ name: 'foo' }])
|
|
155
|
+
t.is(value, '[{"name":"foo"}]')
|
|
156
|
+
t.deepEqual(schema, clonedSchema)
|
|
157
|
+
})
|