fast-json-stringify 1.15.0 → 1.15.4
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/bench.js +82 -11
- package/build-schema-validator.js +1 -1
- package/example.js +1 -1
- package/index.js +60 -22
- package/package.json +12 -5
- package/test/additionalProperties.test.js +5 -5
- package/test/anyof.test.js +18 -1
- package/test/array.test.js +58 -58
- package/test/basic.test.js +87 -87
- package/test/defaults.test.js +142 -142
- package/test/if-then-else.test.js +93 -93
- package/test/inferType.test.js +27 -27
- package/test/json-schema-test-suite/util.js +1 -1
- package/test/nullable.test.js +19 -19
- package/test/patternProperties.test.js +2 -2
- package/test/ref.test.js +282 -0
- package/test/sanitize.test.js +142 -0
|
@@ -4,43 +4,43 @@ const t = require('tap')
|
|
|
4
4
|
const build = require('..')
|
|
5
5
|
|
|
6
6
|
const schema = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
type: 'object',
|
|
8
|
+
properties: {
|
|
9
9
|
},
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
if: {
|
|
11
|
+
properties: {
|
|
12
|
+
kind: { type: 'string', enum: ['foobar'] }
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
then: {
|
|
16
|
+
properties: {
|
|
17
|
+
kind: { type: 'string', enum: ['foobar'] },
|
|
18
|
+
foo: { type: 'string' },
|
|
19
|
+
bar: { type: 'number' },
|
|
20
|
+
list: {
|
|
21
|
+
type: 'array',
|
|
22
|
+
items: {
|
|
23
|
+
type: 'object',
|
|
24
|
+
properties: {
|
|
25
|
+
name: { type: 'string' },
|
|
26
|
+
value: { type: 'string' }
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
else: {
|
|
33
|
+
properties: {
|
|
34
|
+
kind: { type: 'string', enum: ['greeting'] },
|
|
35
|
+
hi: { type: 'string' },
|
|
36
|
+
hello: { type: 'number' },
|
|
37
|
+
list: {
|
|
38
|
+
type: 'array',
|
|
39
|
+
items: {
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
name: { type: 'string' },
|
|
43
|
+
value: { type: 'string' }
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -49,104 +49,104 @@ const schema = {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
const nestedIfSchema = {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
type: 'object',
|
|
53
|
+
properties: { },
|
|
54
|
+
if: {
|
|
55
|
+
properties: {
|
|
56
|
+
kind: { type: 'string', enum: ['foobar', 'greeting'] }
|
|
57
57
|
}
|
|
58
58
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
then: {
|
|
60
|
+
if: {
|
|
61
|
+
properties: {
|
|
62
|
+
kind: { type: 'string', enum: ['foobar'] }
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
65
|
+
then: {
|
|
66
|
+
properties: {
|
|
67
|
+
kind: { type: 'string', enum: ['foobar'] },
|
|
68
|
+
foo: { type: 'string' },
|
|
69
|
+
bar: { type: 'number' },
|
|
70
|
+
list: {
|
|
71
|
+
type: 'array',
|
|
72
|
+
items: {
|
|
73
|
+
type: 'object',
|
|
74
|
+
properties: {
|
|
75
|
+
name: { type: 'string' },
|
|
76
|
+
value: { type: 'string' }
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
else: {
|
|
83
|
+
properties: {
|
|
84
|
+
kind: { type: 'string', enum: ['greeting'] },
|
|
85
|
+
hi: { type: 'string' },
|
|
86
|
+
hello: { type: 'number' }
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
},
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
else: {
|
|
91
|
+
properties: {
|
|
92
|
+
kind: { type: 'string', enum: ['alphabet'] },
|
|
93
|
+
a: { type: 'string' },
|
|
94
|
+
b: { type: 'number' }
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
const nestedElseSchema = {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
100
|
+
type: 'object',
|
|
101
|
+
properties: { },
|
|
102
|
+
if: {
|
|
103
|
+
properties: {
|
|
104
|
+
kind: { type: 'string', enum: ['foobar'] }
|
|
105
105
|
}
|
|
106
106
|
},
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
107
|
+
then: {
|
|
108
|
+
properties: {
|
|
109
|
+
kind: { type: 'string', enum: ['foobar'] },
|
|
110
|
+
foo: { type: 'string' },
|
|
111
|
+
bar: { type: 'number' },
|
|
112
|
+
list: {
|
|
113
|
+
type: 'array',
|
|
114
|
+
items: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: {
|
|
117
|
+
name: { type: 'string' },
|
|
118
|
+
value: { type: 'string' }
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
},
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
else: {
|
|
125
|
+
if: {
|
|
126
|
+
properties: {
|
|
127
|
+
kind: { type: 'string', enum: ['greeting'] }
|
|
128
128
|
}
|
|
129
129
|
},
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
130
|
+
then: {
|
|
131
|
+
properties: {
|
|
132
|
+
kind: { type: 'string', enum: ['greeting'] },
|
|
133
|
+
hi: { type: 'string' },
|
|
134
|
+
hello: { type: 'number' }
|
|
135
135
|
}
|
|
136
136
|
},
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
137
|
+
else: {
|
|
138
|
+
properties: {
|
|
139
|
+
kind: { type: 'string', enum: ['alphabet'] },
|
|
140
|
+
a: { type: 'string' },
|
|
141
|
+
b: { type: 'number' }
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
const nestedDeepElseSchema = {
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
type: 'object',
|
|
149
|
+
additionalProperties: schema
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
const fooBarInput = {
|
package/test/inferType.test.js
CHANGED
|
@@ -23,11 +23,11 @@ function buildTest (schema, toStringify) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
buildTest({
|
|
26
|
-
|
|
26
|
+
title: 'infer type object by keyword',
|
|
27
27
|
// 'type': 'object',
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
properties: {
|
|
29
|
+
name: {
|
|
30
|
+
type: 'string'
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
}, {
|
|
@@ -35,15 +35,15 @@ buildTest({
|
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
buildTest({
|
|
38
|
-
|
|
38
|
+
title: 'infer type of nested object by keyword',
|
|
39
39
|
// 'type': 'object',
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
properties: {
|
|
41
|
+
more: {
|
|
42
|
+
description: 'more properties',
|
|
43
43
|
// 'type': 'object',
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
properties: {
|
|
45
|
+
something: {
|
|
46
|
+
type: 'string'
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -55,13 +55,13 @@ buildTest({
|
|
|
55
55
|
})
|
|
56
56
|
|
|
57
57
|
buildTest({
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
title: 'infer type array by keyword',
|
|
59
|
+
type: 'object',
|
|
60
|
+
properties: {
|
|
61
|
+
ids: {
|
|
62
62
|
// 'type': 'array',
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
items: {
|
|
64
|
+
type: 'string'
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -70,12 +70,12 @@ buildTest({
|
|
|
70
70
|
})
|
|
71
71
|
|
|
72
72
|
buildTest({
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
title: 'infer type string by keyword',
|
|
74
|
+
type: 'object',
|
|
75
|
+
properties: {
|
|
76
|
+
name: {
|
|
77
77
|
// 'type': 'string',
|
|
78
|
-
|
|
78
|
+
maxLength: 3
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
}, {
|
|
@@ -83,12 +83,12 @@ buildTest({
|
|
|
83
83
|
})
|
|
84
84
|
|
|
85
85
|
buildTest({
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
title: 'infer type number by keyword',
|
|
87
|
+
type: 'object',
|
|
88
|
+
properties: {
|
|
89
|
+
age: {
|
|
90
90
|
// 'type': 'number',
|
|
91
|
-
|
|
91
|
+
maximum: 18
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
}, {
|
|
@@ -13,7 +13,7 @@ function runTests (t, testsuite, skippedTests) {
|
|
|
13
13
|
t.test(test.description, (t) => {
|
|
14
14
|
t.plan(1)
|
|
15
15
|
try {
|
|
16
|
-
|
|
16
|
+
const output = stringify(test.data)
|
|
17
17
|
t.equal(output, JSON.stringify(test.data), 'compare payloads')
|
|
18
18
|
} catch (err) {
|
|
19
19
|
if (test.valid === false) {
|
package/test/nullable.test.js
CHANGED
|
@@ -6,7 +6,7 @@ const build = require('..')
|
|
|
6
6
|
|
|
7
7
|
const nullable = true
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const complexObject = {
|
|
10
10
|
type: 'object',
|
|
11
11
|
properties: {
|
|
12
12
|
nullableString: { type: 'string', nullable: nullable },
|
|
@@ -45,7 +45,7 @@ let complexObject = {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
const complexData = {
|
|
49
49
|
nullableString: null,
|
|
50
50
|
nullableNumber: null,
|
|
51
51
|
nullableInteger: null,
|
|
@@ -62,10 +62,10 @@ let complexData = {
|
|
|
62
62
|
nullableNull: null,
|
|
63
63
|
nullableArray: null
|
|
64
64
|
},
|
|
65
|
-
arrayWithNullableItems: [
|
|
65
|
+
arrayWithNullableItems: [1, 2, null]
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
const complexExpectedResult = {
|
|
69
69
|
nullableString: null,
|
|
70
70
|
nullableNumber: null,
|
|
71
71
|
nullableInteger: null,
|
|
@@ -82,32 +82,32 @@ let complexExpectedResult = {
|
|
|
82
82
|
nullableNull: null,
|
|
83
83
|
nullableArray: null
|
|
84
84
|
},
|
|
85
|
-
arrayWithNullableItems: [
|
|
85
|
+
arrayWithNullableItems: [1, 2, null]
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
nullableString: [
|
|
90
|
-
nullableNumber: [
|
|
91
|
-
nullableInteger: [
|
|
92
|
-
nullableBoolean: [
|
|
93
|
-
nullableNull: [
|
|
94
|
-
nullableArray: [
|
|
88
|
+
const testSet = {
|
|
89
|
+
nullableString: [{ type: 'string', nullable: nullable }, null, null],
|
|
90
|
+
nullableNumber: [{ type: 'number', nullable: nullable }, null, null],
|
|
91
|
+
nullableInteger: [{ type: 'integer', nullable: nullable }, null, null],
|
|
92
|
+
nullableBoolean: [{ type: 'boolean', nullable: nullable }, null, null],
|
|
93
|
+
nullableNull: [{ type: 'null', nullable: nullable }, null, null],
|
|
94
|
+
nullableArray: [{
|
|
95
95
|
type: 'array',
|
|
96
96
|
nullable: true,
|
|
97
97
|
items: {}
|
|
98
|
-
}, null, null
|
|
99
|
-
nullableObject: [
|
|
100
|
-
complexObject: [
|
|
98
|
+
}, null, null],
|
|
99
|
+
nullableObject: [{ type: 'object', nullable: true }, null, null],
|
|
100
|
+
complexObject: [complexObject, complexData, complexExpectedResult]
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
Object.keys(testSet).forEach(key => {
|
|
104
104
|
test(`handle nullable:true in ${key} correctly`, (t) => {
|
|
105
105
|
t.plan(1)
|
|
106
106
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
const stringifier = build(testSet[key][0])
|
|
108
|
+
const data = testSet[key][1]
|
|
109
|
+
const expected = testSet[key][2]
|
|
110
|
+
const result = stringifier(data)
|
|
111
111
|
t.deepEqual(JSON.parse(result), expected)
|
|
112
112
|
})
|
|
113
113
|
})
|
|
@@ -14,13 +14,13 @@ test('patternProperties', (t) => {
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
patternProperties: {
|
|
17
|
-
|
|
17
|
+
foo: {
|
|
18
18
|
type: 'string'
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
})
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
const obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: { a: true }, notMe: false }
|
|
24
24
|
t.equal(stringify(obj), '{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}')
|
|
25
25
|
})
|
|
26
26
|
|