fast-json-stringify 5.15.0 → 5.15.1
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/package.json +1 -1
- package/test/sanitize7.test.js +68 -0
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/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
|
+
})
|