fast-json-stringify 5.15.0 → 5.16.0
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 +3 -2
- package/lib/standalone.js +7 -4
- package/package.json +1 -1
- package/test/sanitize7.test.js +68 -0
- package/test/standalone-mode.test.js +103 -8
package/index.js
CHANGED
|
@@ -349,7 +349,8 @@ function buildInnerObject (context, location) {
|
|
|
349
349
|
|
|
350
350
|
for (const key of requiredProperties) {
|
|
351
351
|
if (!propertiesKeys.includes(key)) {
|
|
352
|
-
|
|
352
|
+
const sanitizedKey = JSON.stringify(key)
|
|
353
|
+
code += `if (obj[${sanitizedKey}] === undefined) throw new Error('${sanitizedKey.replace(/'/g, '\\\'')} is required!')\n`
|
|
353
354
|
}
|
|
354
355
|
}
|
|
355
356
|
|
|
@@ -387,7 +388,7 @@ function buildInnerObject (context, location) {
|
|
|
387
388
|
`
|
|
388
389
|
} else if (isRequired) {
|
|
389
390
|
code += ` else {
|
|
390
|
-
throw new Error('${sanitizedKey} is required!')
|
|
391
|
+
throw new Error('${sanitizedKey.replace(/'/g, '\\\'')} is required!')
|
|
391
392
|
}
|
|
392
393
|
`
|
|
393
394
|
} else {
|
package/lib/standalone.js
CHANGED
|
@@ -3,19 +3,22 @@
|
|
|
3
3
|
function buildStandaloneCode (contextFunc, context, serializer, validator) {
|
|
4
4
|
let ajvDependencyCode = ''
|
|
5
5
|
if (context.validatorSchemasIds.size > 0) {
|
|
6
|
+
ajvDependencyCode += 'const Validator = require(\'fast-json-stringify/lib/validator\')\n'
|
|
6
7
|
ajvDependencyCode += `const validatorState = ${JSON.stringify(validator.getState())}\n`
|
|
7
8
|
ajvDependencyCode += 'const validator = Validator.restoreFromState(validatorState)\n'
|
|
8
9
|
} else {
|
|
9
10
|
ajvDependencyCode += 'const validator = null\n'
|
|
10
11
|
}
|
|
11
12
|
|
|
13
|
+
// Don't need to keep external schemas once compiled
|
|
14
|
+
// validatorState will hold external schemas if it needs them
|
|
15
|
+
const { schema, ...serializerState } = serializer.getState()
|
|
16
|
+
|
|
12
17
|
return `
|
|
13
18
|
'use strict'
|
|
14
|
-
const { dependencies } = require('fast-json-stringify/lib/standalone')
|
|
15
|
-
|
|
16
|
-
const { Serializer, Validator } = dependencies
|
|
17
19
|
|
|
18
|
-
const
|
|
20
|
+
const Serializer = require('fast-json-stringify/lib/serializer')
|
|
21
|
+
const serializerState = ${JSON.stringify(serializerState)}
|
|
19
22
|
const serializer = Serializer.restoreFromState(serializerState)
|
|
20
23
|
|
|
21
24
|
${ajvDependencyCode}
|
package/package.json
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tap').test
|
|
4
|
+
const build = require('..')
|
|
5
|
+
|
|
6
|
+
test('required property containing single quote, contains property', (t) => {
|
|
7
|
+
t.plan(1)
|
|
8
|
+
|
|
9
|
+
const stringify = build({
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
'\'': { type: 'string' }
|
|
13
|
+
},
|
|
14
|
+
required: [
|
|
15
|
+
'\''
|
|
16
|
+
]
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
t.throws(() => stringify({}), new Error('"\'" is required!'))
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('required property containing double quote, contains property', (t) => {
|
|
23
|
+
t.plan(1)
|
|
24
|
+
|
|
25
|
+
const stringify = build({
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: {
|
|
28
|
+
'"': { type: 'string' }
|
|
29
|
+
},
|
|
30
|
+
required: [
|
|
31
|
+
'"'
|
|
32
|
+
]
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
t.throws(() => stringify({}), new Error('""" is required!'))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('required property containing single quote, does not contain property', (t) => {
|
|
39
|
+
t.plan(1)
|
|
40
|
+
|
|
41
|
+
const stringify = build({
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
a: { type: 'string' }
|
|
45
|
+
},
|
|
46
|
+
required: [
|
|
47
|
+
'\''
|
|
48
|
+
]
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
t.throws(() => stringify({}), new Error('"\'" is required!'))
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('required property containing double quote, does not contain property', (t) => {
|
|
55
|
+
t.plan(1)
|
|
56
|
+
|
|
57
|
+
const stringify = build({
|
|
58
|
+
type: 'object',
|
|
59
|
+
properties: {
|
|
60
|
+
a: { type: 'string' }
|
|
61
|
+
},
|
|
62
|
+
required: [
|
|
63
|
+
'"'
|
|
64
|
+
]
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
t.throws(() => stringify({}), new Error('""" is required!'))
|
|
68
|
+
})
|
|
@@ -26,14 +26,14 @@ test('activate standalone mode', async (t) => {
|
|
|
26
26
|
t.type(code, 'string')
|
|
27
27
|
t.equal(code.indexOf('ajv'), -1)
|
|
28
28
|
|
|
29
|
-
const
|
|
29
|
+
const destination = path.resolve(tmpDir, 'standalone.js')
|
|
30
30
|
|
|
31
31
|
t.teardown(async () => {
|
|
32
|
-
await fs.promises.rm(
|
|
32
|
+
await fs.promises.rm(destination, { force: true })
|
|
33
33
|
})
|
|
34
34
|
|
|
35
|
-
await fs.promises.writeFile(
|
|
36
|
-
const standalone = require(
|
|
35
|
+
await fs.promises.writeFile(destination, code)
|
|
36
|
+
const standalone = require(destination)
|
|
37
37
|
t.same(standalone({ firstName: 'Foo', surname: 'bar' }), JSON.stringify({ firstName: 'Foo' }), 'surname evicted')
|
|
38
38
|
})
|
|
39
39
|
|
|
@@ -89,14 +89,14 @@ test('test ajv schema', async (t) => {
|
|
|
89
89
|
t.type(code, 'string')
|
|
90
90
|
t.equal(code.indexOf('ajv') > 0, true)
|
|
91
91
|
|
|
92
|
-
const
|
|
92
|
+
const destination = path.resolve(tmpDir, 'standalone2.js')
|
|
93
93
|
|
|
94
94
|
t.teardown(async () => {
|
|
95
|
-
await fs.promises.rm(
|
|
95
|
+
await fs.promises.rm(destination, { force: true })
|
|
96
96
|
})
|
|
97
97
|
|
|
98
|
-
await fs.promises.writeFile(
|
|
99
|
-
const standalone = require(
|
|
98
|
+
await fs.promises.writeFile(destination, code)
|
|
99
|
+
const standalone = require(destination)
|
|
100
100
|
t.same(standalone({
|
|
101
101
|
kind: 'foobar',
|
|
102
102
|
foo: 'FOO',
|
|
@@ -119,3 +119,98 @@ test('test ajv schema', async (t) => {
|
|
|
119
119
|
}]
|
|
120
120
|
}))
|
|
121
121
|
})
|
|
122
|
+
|
|
123
|
+
test('no need to keep external schemas once compiled', async (t) => {
|
|
124
|
+
t.plan(1)
|
|
125
|
+
const externalSchema = {
|
|
126
|
+
first: {
|
|
127
|
+
definitions: {
|
|
128
|
+
id1: {
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: {
|
|
131
|
+
id1: {
|
|
132
|
+
type: 'integer'
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const code = fjs({
|
|
140
|
+
$ref: 'first#/definitions/id1'
|
|
141
|
+
}, {
|
|
142
|
+
mode: 'standalone',
|
|
143
|
+
schema: externalSchema
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
const destination = path.resolve(tmpDir, 'standalone3.js')
|
|
147
|
+
|
|
148
|
+
t.teardown(async () => {
|
|
149
|
+
await fs.promises.rm(destination, { force: true })
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
await fs.promises.writeFile(destination, code)
|
|
153
|
+
const standalone = require(destination)
|
|
154
|
+
|
|
155
|
+
t.same(standalone({ id1: 5 }), JSON.stringify({ id1: 5 }), 'serialization works with external schemas')
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('no need to keep external schemas once compiled - with oneOf validator', async (t) => {
|
|
159
|
+
t.plan(2)
|
|
160
|
+
|
|
161
|
+
const externalSchema = {
|
|
162
|
+
ext: {
|
|
163
|
+
definitions: {
|
|
164
|
+
oBaz: {
|
|
165
|
+
type: 'object',
|
|
166
|
+
properties: {
|
|
167
|
+
baz: { type: 'number' }
|
|
168
|
+
},
|
|
169
|
+
required: ['baz']
|
|
170
|
+
},
|
|
171
|
+
oBar: {
|
|
172
|
+
type: 'object',
|
|
173
|
+
properties: {
|
|
174
|
+
bar: { type: 'string' }
|
|
175
|
+
},
|
|
176
|
+
required: ['bar']
|
|
177
|
+
},
|
|
178
|
+
other: {
|
|
179
|
+
type: 'string',
|
|
180
|
+
const: 'other'
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const schema = {
|
|
187
|
+
title: 'object with oneOf property value containing refs to external schema',
|
|
188
|
+
type: 'object',
|
|
189
|
+
properties: {
|
|
190
|
+
oneOfSchema: {
|
|
191
|
+
oneOf: [
|
|
192
|
+
{ $ref: 'ext#/definitions/oBaz' },
|
|
193
|
+
{ $ref: 'ext#/definitions/oBar' }
|
|
194
|
+
]
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
required: ['oneOfSchema']
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const code = fjs(schema, {
|
|
201
|
+
mode: 'standalone',
|
|
202
|
+
schema: externalSchema
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
const destination = path.resolve(tmpDir, 'standalone-oneOf-ref.js')
|
|
206
|
+
|
|
207
|
+
t.teardown(async () => {
|
|
208
|
+
await fs.promises.rm(destination, { force: true })
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
await fs.promises.writeFile(destination, code)
|
|
212
|
+
const stringify = require(destination)
|
|
213
|
+
|
|
214
|
+
t.equal(stringify({ oneOfSchema: { baz: 5 } }), '{"oneOfSchema":{"baz":5}}')
|
|
215
|
+
t.equal(stringify({ oneOfSchema: { bar: 'foo' } }), '{"oneOfSchema":{"bar":"foo"}}')
|
|
216
|
+
})
|