fast-json-stringify 2.5.1 → 2.6.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/.github/workflows/ci.yml +31 -15
- package/.taprc +0 -1
- package/README.md +8 -6
- package/example.js +1 -3
- package/index.js +10 -14
- package/package.json +2 -2
- package/test/allof.test.js +11 -11
- package/test/any.test.js +25 -25
- package/test/anyof.test.js +22 -22
- package/test/array.test.js +3 -3
- package/test/basic.test.js +3 -3
- package/test/debug-mode.test.js +4 -4
- package/test/defaults.test.js +1 -1
- package/test/inferType.test.js +1 -1
- package/test/integer.test.js +1 -1
- package/test/nestedObjects.test.js +1 -1
- package/test/nullable.test.js +1 -1
- package/test/oneof.test.js +17 -17
- package/test/ref.test.js +54 -7
- package/test/required.test.js +10 -10
- package/test/requiresAjv.test.js +48 -0
- package/test/sanitize.test.js +2 -2
- package/test/side-effect.test.js +39 -5
- package/test/typesArray.test.js +26 -26
package/.github/workflows/ci.yml
CHANGED
|
@@ -1,27 +1,43 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
paths-ignore:
|
|
6
|
+
- 'docs/**'
|
|
7
|
+
- '*.md'
|
|
8
|
+
pull_request:
|
|
9
|
+
paths-ignore:
|
|
10
|
+
- 'docs/**'
|
|
11
|
+
- '*.md'
|
|
12
|
+
|
|
3
13
|
jobs:
|
|
4
14
|
test:
|
|
5
|
-
runs-on:
|
|
15
|
+
runs-on: ${{ matrix.os }}
|
|
6
16
|
strategy:
|
|
7
17
|
matrix:
|
|
8
18
|
node-version: [10.x, 12.x, 13.x, 14.x]
|
|
19
|
+
os: [macos-latest, ubuntu-latest, windows-latest]
|
|
20
|
+
|
|
9
21
|
steps:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
- uses: actions/checkout@v2
|
|
23
|
+
|
|
24
|
+
- name: Use Node.js
|
|
25
|
+
uses: actions/setup-node@v2.1.5
|
|
26
|
+
with:
|
|
27
|
+
node-version: ${{ matrix.node-version }}
|
|
28
|
+
|
|
29
|
+
- name: Install Dependencies
|
|
30
|
+
run: |
|
|
31
|
+
npm install --ignore-scripts
|
|
32
|
+
|
|
33
|
+
- name: Run Tests
|
|
34
|
+
run: |
|
|
35
|
+
npm run test
|
|
19
36
|
|
|
20
37
|
automerge:
|
|
21
38
|
needs: test
|
|
22
39
|
runs-on: ubuntu-latest
|
|
23
40
|
steps:
|
|
24
|
-
- uses: fastify/github-action-merge-dependabot@
|
|
25
|
-
if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request' }}
|
|
41
|
+
- uses: fastify/github-action-merge-dependabot@v2.0.0
|
|
26
42
|
with:
|
|
27
|
-
github-token: ${{secrets.
|
|
43
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
package/.taprc
CHANGED
package/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
# fast-json-stringify
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-

|
|
4
|
+
[](https://www.npmjs.com/package/fast-json-stringify)
|
|
5
|
+
[](https://snyk.io/test/github/fastify/fast-json-stringify)
|
|
6
|
+
[](https://standardjs.com/)
|
|
5
7
|
[](https://www.npmjs.com/package/fast-json-stringify)
|
|
6
8
|
|
|
7
9
|
|
|
@@ -106,7 +108,7 @@ const stringify = fastJson(mySchema, {
|
|
|
106
108
|
<a name="fastJsonStringify"></a>
|
|
107
109
|
### fastJsonStringify(schema)
|
|
108
110
|
|
|
109
|
-
Build a `stringify()` function based on [jsonschema](
|
|
111
|
+
Build a `stringify()` function based on [jsonschema](https://json-schema.org/).
|
|
110
112
|
|
|
111
113
|
Supported types:
|
|
112
114
|
|
|
@@ -399,7 +401,7 @@ console.log(stringify({
|
|
|
399
401
|
})) // {"kind":"foobar","foo":"FOO","bar":42}
|
|
400
402
|
```
|
|
401
403
|
|
|
402
|
-
**NB
|
|
404
|
+
**NB** Do not declare the properties twice or you will print them twice!
|
|
403
405
|
|
|
404
406
|
<a name="ref"></a>
|
|
405
407
|
#### Reuse - $ref
|
|
@@ -514,7 +516,7 @@ const stringify = fastJson(schema, { schema: externalSchema })
|
|
|
514
516
|
|
|
515
517
|
<a name="long"></a>
|
|
516
518
|
#### Long integers
|
|
517
|
-
By default the library will handle
|
|
519
|
+
By default the library will automatically handle [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) from Node.js v10.3 and above.
|
|
518
520
|
If you can't use BigInts in your environment, long integers (64-bit) are also supported using the [long](https://github.com/dcodeIO/long.js) module.
|
|
519
521
|
Example:
|
|
520
522
|
```javascript
|
|
@@ -641,7 +643,7 @@ console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"F
|
|
|
641
643
|
<a name="acknowledgements"></a>
|
|
642
644
|
## Acknowledgements
|
|
643
645
|
|
|
644
|
-
This project was kindly sponsored by [nearForm](
|
|
646
|
+
This project was kindly sponsored by [nearForm](https://nearform.com).
|
|
645
647
|
|
|
646
648
|
<a name="license"></a>
|
|
647
649
|
## License
|
package/example.js
CHANGED
package/index.js
CHANGED
|
@@ -132,6 +132,7 @@ function build (schema, options) {
|
|
|
132
132
|
case 'array':
|
|
133
133
|
main = '$main'
|
|
134
134
|
code = buildArray(location, code, main)
|
|
135
|
+
schema = location.schema
|
|
135
136
|
break
|
|
136
137
|
case undefined:
|
|
137
138
|
main = '$asAny'
|
|
@@ -145,13 +146,8 @@ function build (schema, options) {
|
|
|
145
146
|
return ${main}
|
|
146
147
|
`
|
|
147
148
|
|
|
148
|
-
const dependencies = []
|
|
149
|
-
const dependenciesName = []
|
|
150
|
-
if (dependsOnAjv(schema)) {
|
|
151
|
-
dependencies.push(new Ajv(options.ajv))
|
|
152
|
-
dependenciesName.push('ajv')
|
|
153
|
-
}
|
|
154
|
-
|
|
149
|
+
const dependencies = [new Ajv(options.ajv)]
|
|
150
|
+
const dependenciesName = ['ajv']
|
|
155
151
|
dependenciesName.push(code)
|
|
156
152
|
|
|
157
153
|
if (options.debugMode) {
|
|
@@ -222,11 +218,6 @@ function inferTypeByKeyword (schema) {
|
|
|
222
218
|
return schema.type
|
|
223
219
|
}
|
|
224
220
|
|
|
225
|
-
function dependsOnAjv (schema) {
|
|
226
|
-
const str = JSON.stringify(schema)
|
|
227
|
-
return (/"if":{.*"then":{|"(anyOf|oneOf)":\[|"const":/.test(str))
|
|
228
|
-
}
|
|
229
|
-
|
|
230
221
|
const stringSerializerMap = {
|
|
231
222
|
'date-time': '$asDatetime',
|
|
232
223
|
date: '$asDate',
|
|
@@ -957,7 +948,7 @@ function buildObject (location, code, name) {
|
|
|
957
948
|
}
|
|
958
949
|
|
|
959
950
|
function buildArray (location, code, name, key = null) {
|
|
960
|
-
|
|
951
|
+
let schema = location.schema
|
|
961
952
|
code += `
|
|
962
953
|
function ${name} (obj) {
|
|
963
954
|
`
|
|
@@ -979,6 +970,11 @@ function buildArray (location, code, name, key = null) {
|
|
|
979
970
|
}
|
|
980
971
|
|
|
981
972
|
if (schema.items.$ref) {
|
|
973
|
+
if (!schema[fjsCloned]) {
|
|
974
|
+
location.schema = clone(location.schema)
|
|
975
|
+
schema = location.schema
|
|
976
|
+
schema[fjsCloned] = true
|
|
977
|
+
}
|
|
982
978
|
location = refFinder(schema.items.$ref, location)
|
|
983
979
|
schema.items = location.schema
|
|
984
980
|
}
|
|
@@ -1215,7 +1211,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1215
1211
|
`
|
|
1216
1212
|
} else if ('const' in schema) {
|
|
1217
1213
|
code += `
|
|
1218
|
-
if(ajv.validate(${
|
|
1214
|
+
if(ajv.validate(${JSON.stringify(schema)}, obj${accessor}))
|
|
1219
1215
|
json += '${JSON.stringify(schema.const)}'
|
|
1220
1216
|
else
|
|
1221
1217
|
throw new Error(\`Item $\{JSON.stringify(obj${accessor})} does not match schema definition.\`)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-json-stringify",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Stringify your JSON at max speed",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"proxyquire": "^2.1.3",
|
|
41
41
|
"semver": "^7.1.0",
|
|
42
42
|
"standard": "^16.0.1",
|
|
43
|
-
"tap": "^
|
|
43
|
+
"tap": "^15.0.0",
|
|
44
44
|
"typescript": "^4.0.2"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
package/test/allof.test.js
CHANGED
|
@@ -44,7 +44,7 @@ test('object with allOf and multiple schema on the allOf', (t) => {
|
|
|
44
44
|
id: 1
|
|
45
45
|
})
|
|
46
46
|
} catch (e) {
|
|
47
|
-
t.
|
|
47
|
+
t.equal(e.message, '"name" is required!')
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
try {
|
|
@@ -52,15 +52,15 @@ test('object with allOf and multiple schema on the allOf', (t) => {
|
|
|
52
52
|
name: 'string'
|
|
53
53
|
})
|
|
54
54
|
} catch (e) {
|
|
55
|
-
t.
|
|
55
|
+
t.equal(e.message, '"id" is required!')
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
t.
|
|
58
|
+
t.equal(stringify({
|
|
59
59
|
id: 1,
|
|
60
60
|
name: 'string'
|
|
61
61
|
}), '{"name":"string","id":1}')
|
|
62
62
|
|
|
63
|
-
t.
|
|
63
|
+
t.equal(stringify({
|
|
64
64
|
id: 1,
|
|
65
65
|
name: 'string',
|
|
66
66
|
tag: 'otherString'
|
|
@@ -92,7 +92,7 @@ test('object with allOf and one schema on the allOf', (t) => {
|
|
|
92
92
|
const value = stringify({
|
|
93
93
|
id: 1
|
|
94
94
|
})
|
|
95
|
-
t.
|
|
95
|
+
t.equal(value, '{"id":1}')
|
|
96
96
|
})
|
|
97
97
|
|
|
98
98
|
test('object with allOf and no schema on the allOf', (t) => {
|
|
@@ -108,7 +108,7 @@ test('object with allOf and no schema on the allOf', (t) => {
|
|
|
108
108
|
build(schema)
|
|
109
109
|
t.fail()
|
|
110
110
|
} catch (e) {
|
|
111
|
-
t.
|
|
111
|
+
t.equal(e.message, 'schema is invalid: data.allOf should NOT have fewer than 1 items')
|
|
112
112
|
}
|
|
113
113
|
})
|
|
114
114
|
|
|
@@ -160,7 +160,7 @@ test('object with nested allOfs', (t) => {
|
|
|
160
160
|
id3: 3,
|
|
161
161
|
id4: 4 // extra prop shouldn't be in result
|
|
162
162
|
})
|
|
163
|
-
t.
|
|
163
|
+
t.equal(value, '{"id1":1,"id2":2,"id3":3}')
|
|
164
164
|
})
|
|
165
165
|
|
|
166
166
|
test('object with $ref in allOf', (t) => {
|
|
@@ -191,7 +191,7 @@ test('object with $ref in allOf', (t) => {
|
|
|
191
191
|
id1: 1,
|
|
192
192
|
id2: 2 // extra prop shouldn't be in result
|
|
193
193
|
})
|
|
194
|
-
t.
|
|
194
|
+
t.equal(value, '{"id1":1}')
|
|
195
195
|
})
|
|
196
196
|
|
|
197
197
|
test('object with $ref and other object in allOf', (t) => {
|
|
@@ -231,7 +231,7 @@ test('object with $ref and other object in allOf', (t) => {
|
|
|
231
231
|
id2: 2,
|
|
232
232
|
id3: 3 // extra prop shouldn't be in result
|
|
233
233
|
})
|
|
234
|
-
t.
|
|
234
|
+
t.equal(value, '{"id1":1,"id2":2}')
|
|
235
235
|
})
|
|
236
236
|
|
|
237
237
|
test('object with multiple $refs in allOf', (t) => {
|
|
@@ -274,7 +274,7 @@ test('object with multiple $refs in allOf', (t) => {
|
|
|
274
274
|
id2: 2,
|
|
275
275
|
id3: 3 // extra prop shouldn't be in result
|
|
276
276
|
})
|
|
277
|
-
t.
|
|
277
|
+
t.equal(value, '{"id1":1,"id2":2}')
|
|
278
278
|
})
|
|
279
279
|
|
|
280
280
|
test('object with external $refs in allOf', (t) => {
|
|
@@ -325,5 +325,5 @@ test('object with external $refs in allOf', (t) => {
|
|
|
325
325
|
id2: 2,
|
|
326
326
|
id3: 3 // extra prop shouldn't be in result
|
|
327
327
|
})
|
|
328
|
-
t.
|
|
328
|
+
t.equal(value, '{"id1":1,"id2":2}')
|
|
329
329
|
})
|
package/test/any.test.js
CHANGED
|
@@ -16,19 +16,19 @@ test('object with nested random property', (t) => {
|
|
|
16
16
|
}
|
|
17
17
|
const stringify = build(schema)
|
|
18
18
|
|
|
19
|
-
t.
|
|
19
|
+
t.equal(stringify({
|
|
20
20
|
id: 1, name: 'string'
|
|
21
21
|
}), '{"id":1,"name":"string"}')
|
|
22
22
|
|
|
23
|
-
t.
|
|
23
|
+
t.equal(stringify({
|
|
24
24
|
id: 1, name: { first: 'name', last: 'last' }
|
|
25
25
|
}), '{"id":1,"name":{"first":"name","last":"last"}}')
|
|
26
26
|
|
|
27
|
-
t.
|
|
27
|
+
t.equal(stringify({
|
|
28
28
|
id: 1, name: null
|
|
29
29
|
}), '{"id":1,"name":null}')
|
|
30
30
|
|
|
31
|
-
t.
|
|
31
|
+
t.equal(stringify({
|
|
32
32
|
id: 1, name: ['first', 'last']
|
|
33
33
|
}), '{"id":1,"name":["first","last"]}')
|
|
34
34
|
})
|
|
@@ -45,7 +45,7 @@ test('object with empty schema with $id: undefined set', (t) => {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
const stringify = build(schema)
|
|
48
|
-
t.
|
|
48
|
+
t.equal(stringify({
|
|
49
49
|
name: 'string'
|
|
50
50
|
}), '{"name":"string"}')
|
|
51
51
|
})
|
|
@@ -61,7 +61,7 @@ test('array with random items', (t) => {
|
|
|
61
61
|
const stringify = build(schema)
|
|
62
62
|
|
|
63
63
|
const value = stringify([1, 'string', null])
|
|
64
|
-
t.
|
|
64
|
+
t.equal(value, '[1,"string",null]')
|
|
65
65
|
})
|
|
66
66
|
|
|
67
67
|
test('empty schema', (t) => {
|
|
@@ -71,13 +71,13 @@ test('empty schema', (t) => {
|
|
|
71
71
|
|
|
72
72
|
const stringify = build(schema)
|
|
73
73
|
|
|
74
|
-
t.
|
|
75
|
-
t.
|
|
76
|
-
t.
|
|
77
|
-
t.
|
|
78
|
-
t.
|
|
79
|
-
t.
|
|
80
|
-
t.
|
|
74
|
+
t.equal(stringify(null), 'null')
|
|
75
|
+
t.equal(stringify(1), '1')
|
|
76
|
+
t.equal(stringify(true), 'true')
|
|
77
|
+
t.equal(stringify('hello'), '"hello"')
|
|
78
|
+
t.equal(stringify({}), '{}')
|
|
79
|
+
t.equal(stringify({ x: 10 }), '{"x":10}')
|
|
80
|
+
t.equal(stringify([true, 1, 'hello']), '[true,1,"hello"]')
|
|
81
81
|
})
|
|
82
82
|
|
|
83
83
|
test('empty schema on nested object', (t) => {
|
|
@@ -92,13 +92,13 @@ test('empty schema on nested object', (t) => {
|
|
|
92
92
|
|
|
93
93
|
const stringify = build(schema)
|
|
94
94
|
|
|
95
|
-
t.
|
|
96
|
-
t.
|
|
97
|
-
t.
|
|
98
|
-
t.
|
|
99
|
-
t.
|
|
100
|
-
t.
|
|
101
|
-
t.
|
|
95
|
+
t.equal(stringify({ x: null }), '{"x":null}')
|
|
96
|
+
t.equal(stringify({ x: 1 }), '{"x":1}')
|
|
97
|
+
t.equal(stringify({ x: true }), '{"x":true}')
|
|
98
|
+
t.equal(stringify({ x: 'hello' }), '{"x":"hello"}')
|
|
99
|
+
t.equal(stringify({ x: {} }), '{"x":{}}')
|
|
100
|
+
t.equal(stringify({ x: { x: 10 } }), '{"x":{"x":10}}')
|
|
101
|
+
t.equal(stringify({ x: [true, 1, 'hello'] }), '{"x":[true,1,"hello"]}')
|
|
102
102
|
})
|
|
103
103
|
|
|
104
104
|
test('empty schema on array', (t) => {
|
|
@@ -111,7 +111,7 @@ test('empty schema on array', (t) => {
|
|
|
111
111
|
|
|
112
112
|
const stringify = build(schema)
|
|
113
113
|
|
|
114
|
-
t.
|
|
114
|
+
t.equal(stringify([1, true, 'hello', [], { x: 1 }]), '[1,true,"hello",[],{"x":1}]')
|
|
115
115
|
})
|
|
116
116
|
|
|
117
117
|
test('empty schema on anyOf', (t) => {
|
|
@@ -147,8 +147,8 @@ test('empty schema on anyOf', (t) => {
|
|
|
147
147
|
|
|
148
148
|
const stringify = build(schema)
|
|
149
149
|
|
|
150
|
-
t.
|
|
151
|
-
t.
|
|
152
|
-
t.
|
|
153
|
-
t.
|
|
150
|
+
t.equal(stringify({ kind: 'Bar', value: 1 }), '{"kind":"Bar","value":1}')
|
|
151
|
+
t.equal(stringify({ kind: 'Foo', value: 1 }), '{"kind":"Foo","value":1}')
|
|
152
|
+
t.equal(stringify({ kind: 'Foo', value: true }), '{"kind":"Foo","value":true}')
|
|
153
|
+
t.equal(stringify({ kind: 'Foo', value: 'hello' }), '{"kind":"Foo","value":"hello"}')
|
|
154
154
|
})
|
package/test/anyof.test.js
CHANGED
|
@@ -21,11 +21,11 @@ test('object with multiple types field', (t) => {
|
|
|
21
21
|
}
|
|
22
22
|
const stringify = build(schema)
|
|
23
23
|
|
|
24
|
-
t.
|
|
24
|
+
t.equal(stringify({
|
|
25
25
|
str: 'string'
|
|
26
26
|
}), '{"str":"string"}')
|
|
27
27
|
|
|
28
|
-
t.
|
|
28
|
+
t.equal(stringify({
|
|
29
29
|
str: true
|
|
30
30
|
}), '{"str":true}')
|
|
31
31
|
})
|
|
@@ -53,11 +53,11 @@ test('object with field of type object or null', (t) => {
|
|
|
53
53
|
}
|
|
54
54
|
const stringify = build(schema)
|
|
55
55
|
|
|
56
|
-
t.
|
|
56
|
+
t.equal(stringify({
|
|
57
57
|
prop: null
|
|
58
58
|
}), '{"prop":null}')
|
|
59
59
|
|
|
60
|
-
t.
|
|
60
|
+
t.equal(stringify({
|
|
61
61
|
prop: {
|
|
62
62
|
str: 'string'
|
|
63
63
|
}
|
|
@@ -87,13 +87,13 @@ test('object with field of type object or array', (t) => {
|
|
|
87
87
|
}
|
|
88
88
|
const stringify = build(schema)
|
|
89
89
|
|
|
90
|
-
t.
|
|
90
|
+
t.equal(stringify({
|
|
91
91
|
prop: {
|
|
92
92
|
str: 'string'
|
|
93
93
|
}
|
|
94
94
|
}), '{"prop":{"str":"string"}}')
|
|
95
95
|
|
|
96
|
-
t.
|
|
96
|
+
t.equal(stringify({
|
|
97
97
|
prop: ['string']
|
|
98
98
|
}), '{"prop":["string"]}')
|
|
99
99
|
})
|
|
@@ -117,7 +117,7 @@ test('object with field of type string and coercion disable ', (t) => {
|
|
|
117
117
|
const value = stringify({
|
|
118
118
|
str: 1
|
|
119
119
|
})
|
|
120
|
-
t.
|
|
120
|
+
t.equal(value, '{"str":null}')
|
|
121
121
|
})
|
|
122
122
|
|
|
123
123
|
test('object with field of type string and coercion enable ', (t) => {
|
|
@@ -145,7 +145,7 @@ test('object with field of type string and coercion enable ', (t) => {
|
|
|
145
145
|
const value = stringify({
|
|
146
146
|
str: 1
|
|
147
147
|
})
|
|
148
|
-
t.
|
|
148
|
+
t.equal(value, '{"str":"1"}')
|
|
149
149
|
})
|
|
150
150
|
|
|
151
151
|
test('object with field with type union of multiple objects', (t) => {
|
|
@@ -179,9 +179,9 @@ test('object with field with type union of multiple objects', (t) => {
|
|
|
179
179
|
|
|
180
180
|
const stringify = build(schema)
|
|
181
181
|
|
|
182
|
-
t.
|
|
182
|
+
t.equal(stringify({ anyOfSchema: { baz: 5 } }), '{"anyOfSchema":{"baz":5}}')
|
|
183
183
|
|
|
184
|
-
t.
|
|
184
|
+
t.equal(stringify({ anyOfSchema: { bar: 'foo' } }), '{"anyOfSchema":{"bar":"foo"}}')
|
|
185
185
|
})
|
|
186
186
|
|
|
187
187
|
test('null value in schema', (t) => {
|
|
@@ -221,10 +221,10 @@ test('symbol value in schema', (t) => {
|
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
const stringify = build(schema)
|
|
224
|
-
t.
|
|
225
|
-
t.
|
|
226
|
-
t.
|
|
227
|
-
t.
|
|
224
|
+
t.equal(stringify({ value: 'foo' }), '{"value":"foo"}')
|
|
225
|
+
t.equal(stringify({ value: 'bar' }), '{"value":"bar"}')
|
|
226
|
+
t.equal(stringify({ value: 'baz' }), '{"value":"baz"}')
|
|
227
|
+
t.equal(stringify({ value: 'qux' }), '{"value":null}')
|
|
228
228
|
})
|
|
229
229
|
|
|
230
230
|
test('anyOf and $ref together', (t) => {
|
|
@@ -253,9 +253,9 @@ test('anyOf and $ref together', (t) => {
|
|
|
253
253
|
|
|
254
254
|
const stringify = build(schema)
|
|
255
255
|
|
|
256
|
-
t.
|
|
256
|
+
t.equal(stringify({ cs: 'franco' }), '{"cs":"franco"}')
|
|
257
257
|
|
|
258
|
-
t.
|
|
258
|
+
t.equal(stringify({ cs: true }), '{"cs":true}')
|
|
259
259
|
})
|
|
260
260
|
|
|
261
261
|
test('anyOf and $ref: 2 levels are fine', (t) => {
|
|
@@ -291,7 +291,7 @@ test('anyOf and $ref: 2 levels are fine', (t) => {
|
|
|
291
291
|
|
|
292
292
|
const stringify = build(schema)
|
|
293
293
|
const value = stringify({ cs: 3 })
|
|
294
|
-
t.
|
|
294
|
+
t.equal(value, '{"cs":3}')
|
|
295
295
|
})
|
|
296
296
|
|
|
297
297
|
test('anyOf and $ref: multiple levels should throw at build.', (t) => {
|
|
@@ -330,9 +330,9 @@ test('anyOf and $ref: multiple levels should throw at build.', (t) => {
|
|
|
330
330
|
|
|
331
331
|
const stringify = build(schema)
|
|
332
332
|
|
|
333
|
-
t.
|
|
334
|
-
t.
|
|
335
|
-
t.
|
|
333
|
+
t.equal(stringify({ cs: 3 }), '{"cs":3}')
|
|
334
|
+
t.equal(stringify({ cs: true }), '{"cs":true}')
|
|
335
|
+
t.equal(stringify({ cs: 'pippo' }), '{"cs":"pippo"}')
|
|
336
336
|
})
|
|
337
337
|
|
|
338
338
|
test('anyOf and $ref - multiple external $ref', (t) => {
|
|
@@ -423,7 +423,7 @@ test('anyOf looks for all of the array items', (t) => {
|
|
|
423
423
|
const stringify = build(schema)
|
|
424
424
|
|
|
425
425
|
const value = stringify([{ savedId: 'great' }, { error: 'oops' }])
|
|
426
|
-
t.
|
|
426
|
+
t.equal(value, '[{"savedId":"great"},{"error":"oops"}]')
|
|
427
427
|
})
|
|
428
428
|
|
|
429
429
|
test('anyOf with enum with more than 100 entries', (t) => {
|
|
@@ -445,5 +445,5 @@ test('anyOf with enum with more than 100 entries', (t) => {
|
|
|
445
445
|
const stringify = build(schema)
|
|
446
446
|
|
|
447
447
|
const value = stringify(['EUR', 'USD', null])
|
|
448
|
-
t.
|
|
448
|
+
t.equal(value, '["EUR","USD",null]')
|
|
449
449
|
})
|
package/test/array.test.js
CHANGED
|
@@ -13,7 +13,7 @@ function buildTest (schema, toStringify) {
|
|
|
13
13
|
const stringify = build(schema)
|
|
14
14
|
const output = stringify(toStringify)
|
|
15
15
|
|
|
16
|
-
t.
|
|
16
|
+
t.same(JSON.parse(output), toStringify)
|
|
17
17
|
t.equal(output, JSON.stringify(toStringify))
|
|
18
18
|
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
19
19
|
})
|
|
@@ -195,7 +195,7 @@ test('moment array', (t) => {
|
|
|
195
195
|
const value = stringify({
|
|
196
196
|
times: [moment('2018-04-21T07:52:31.017Z')]
|
|
197
197
|
})
|
|
198
|
-
t.
|
|
198
|
+
t.equal(value, '{"times":["2018-04-21T07:52:31.017Z"]}')
|
|
199
199
|
})
|
|
200
200
|
|
|
201
201
|
buildTest({
|
|
@@ -254,5 +254,5 @@ test('object array with anyOf and symbol', (t) => {
|
|
|
254
254
|
{ name: 'name-0', option: 'Foo' },
|
|
255
255
|
{ name: 'name-1', option: 'Bar' }
|
|
256
256
|
])
|
|
257
|
-
t.
|
|
257
|
+
t.equal(value, '[{"name":"name-0","option":"Foo"},{"name":"name-1","option":"Bar"}]')
|
|
258
258
|
})
|
package/test/basic.test.js
CHANGED
|
@@ -12,7 +12,7 @@ function buildTest (schema, toStringify) {
|
|
|
12
12
|
const stringify = build(schema)
|
|
13
13
|
const output = stringify(toStringify)
|
|
14
14
|
|
|
15
|
-
t.
|
|
15
|
+
t.same(JSON.parse(output), toStringify)
|
|
16
16
|
t.equal(output, JSON.stringify(toStringify))
|
|
17
17
|
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
18
18
|
})
|
|
@@ -280,14 +280,14 @@ test('skip or coerce numbers and integers that are not numbers', (t) => {
|
|
|
280
280
|
distance: 'long'
|
|
281
281
|
})
|
|
282
282
|
|
|
283
|
-
t.
|
|
283
|
+
t.same(JSON.parse(result), {})
|
|
284
284
|
|
|
285
285
|
result = stringify({
|
|
286
286
|
age: '42',
|
|
287
287
|
distance: true
|
|
288
288
|
})
|
|
289
289
|
|
|
290
|
-
t.
|
|
290
|
+
t.same(JSON.parse(result), { age: 42, distance: 1 })
|
|
291
291
|
t.end()
|
|
292
292
|
})
|
|
293
293
|
|
package/test/debug-mode.test.js
CHANGED
|
@@ -20,14 +20,14 @@ test('activate debug mode', t => {
|
|
|
20
20
|
t.plan(2)
|
|
21
21
|
const debugMode = build({ debugMode: true })
|
|
22
22
|
t.type(debugMode, Array)
|
|
23
|
-
t.
|
|
23
|
+
t.match(debugMode.toString.toString(), 'join', 'to string override')
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
test('activate debug mode truthy', t => {
|
|
27
27
|
t.plan(2)
|
|
28
28
|
const debugMode = build({ debugMode: 'yes' })
|
|
29
29
|
t.type(debugMode, Array)
|
|
30
|
-
t.
|
|
30
|
+
t.match(debugMode.toString.toString(), 'join', 'to string override')
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
test('to string auto-consistent', t => {
|
|
@@ -38,7 +38,7 @@ test('to string auto-consistent', t => {
|
|
|
38
38
|
const str = debugMode.toString()
|
|
39
39
|
const compiled = fjs.restore(str)
|
|
40
40
|
const tobe = JSON.stringify({ firstName: 'Foo' })
|
|
41
|
-
t.
|
|
41
|
+
t.same(compiled({ firstName: 'Foo', surname: 'bar' }), tobe, 'surname evicted')
|
|
42
42
|
})
|
|
43
43
|
|
|
44
44
|
test('to string auto-consistent with ajv', t => {
|
|
@@ -61,5 +61,5 @@ test('to string auto-consistent with ajv', t => {
|
|
|
61
61
|
const str = debugMode.toString()
|
|
62
62
|
const compiled = fjs.restore(str)
|
|
63
63
|
const tobe = JSON.stringify({ str: 'Foo' })
|
|
64
|
-
t.
|
|
64
|
+
t.same(compiled({ str: 'Foo', void: 'me' }), tobe)
|
|
65
65
|
})
|
package/test/defaults.test.js
CHANGED
package/test/inferType.test.js
CHANGED
|
@@ -12,7 +12,7 @@ function buildTest (schema, toStringify) {
|
|
|
12
12
|
const stringify = build(schema)
|
|
13
13
|
const output = stringify(toStringify)
|
|
14
14
|
|
|
15
|
-
t.
|
|
15
|
+
t.same(JSON.parse(output), toStringify)
|
|
16
16
|
t.equal(output, JSON.stringify(toStringify))
|
|
17
17
|
t.ok(validate(JSON.parse(output)), 'valid schema')
|
|
18
18
|
})
|
package/test/integer.test.js
CHANGED
|
@@ -32,7 +32,7 @@ test('render a float as an integer', (t) => {
|
|
|
32
32
|
}, { rounding: 'foobar' })
|
|
33
33
|
} catch (error) {
|
|
34
34
|
t.ok(error)
|
|
35
|
-
t.
|
|
35
|
+
t.equal(error.message, 'Unsupported integer rounding method foobar')
|
|
36
36
|
}
|
|
37
37
|
})
|
|
38
38
|
|
|
@@ -28,5 +28,5 @@ test('nested objects with same properties', (t) => {
|
|
|
28
28
|
numberProperty: 42
|
|
29
29
|
}
|
|
30
30
|
})
|
|
31
|
-
t.
|
|
31
|
+
t.equal(value, '{"stringProperty":"string1","objectProperty":{"stringProperty":"string2","numberProperty":42}}')
|
|
32
32
|
})
|
package/test/nullable.test.js
CHANGED
package/test/oneof.test.js
CHANGED
|
@@ -21,8 +21,8 @@ test('object with multiple types field', (t) => {
|
|
|
21
21
|
}
|
|
22
22
|
const stringify = build(schema)
|
|
23
23
|
|
|
24
|
-
t.
|
|
25
|
-
t.
|
|
24
|
+
t.equal(stringify({ str: 'string' }), '{"str":"string"}')
|
|
25
|
+
t.equal(stringify({ str: true }), '{"str":true}')
|
|
26
26
|
})
|
|
27
27
|
|
|
28
28
|
test('object with field of type object or null', (t) => {
|
|
@@ -48,9 +48,9 @@ test('object with field of type object or null', (t) => {
|
|
|
48
48
|
}
|
|
49
49
|
const stringify = build(schema)
|
|
50
50
|
|
|
51
|
-
t.
|
|
51
|
+
t.equal(stringify({ prop: null }), '{"prop":null}')
|
|
52
52
|
|
|
53
|
-
t.
|
|
53
|
+
t.equal(stringify({
|
|
54
54
|
prop: {
|
|
55
55
|
str: 'string', remove: 'this'
|
|
56
56
|
}
|
|
@@ -80,11 +80,11 @@ test('object with field of type object or array', (t) => {
|
|
|
80
80
|
}
|
|
81
81
|
const stringify = build(schema)
|
|
82
82
|
|
|
83
|
-
t.
|
|
83
|
+
t.equal(stringify({
|
|
84
84
|
prop: { str: 'string' }
|
|
85
85
|
}), '{"prop":{"str":"string"}}')
|
|
86
86
|
|
|
87
|
-
t.
|
|
87
|
+
t.equal(stringify({
|
|
88
88
|
prop: ['string']
|
|
89
89
|
}), '{"prop":["string"]}')
|
|
90
90
|
})
|
|
@@ -108,7 +108,7 @@ test('object with field of type string and coercion disable ', (t) => {
|
|
|
108
108
|
const value = stringify({
|
|
109
109
|
str: 1
|
|
110
110
|
})
|
|
111
|
-
t.
|
|
111
|
+
t.equal(value, '{"str":null}')
|
|
112
112
|
})
|
|
113
113
|
|
|
114
114
|
test('object with field of type string and coercion enable ', (t) => {
|
|
@@ -136,7 +136,7 @@ test('object with field of type string and coercion enable ', (t) => {
|
|
|
136
136
|
const value = stringify({
|
|
137
137
|
str: 1
|
|
138
138
|
})
|
|
139
|
-
t.
|
|
139
|
+
t.equal(value, '{"str":"1"}')
|
|
140
140
|
})
|
|
141
141
|
|
|
142
142
|
test('object with field with type union of multiple objects', (t) => {
|
|
@@ -170,9 +170,9 @@ test('object with field with type union of multiple objects', (t) => {
|
|
|
170
170
|
|
|
171
171
|
const stringify = build(schema)
|
|
172
172
|
|
|
173
|
-
t.
|
|
173
|
+
t.equal(stringify({ oneOfSchema: { baz: 5 } }), '{"oneOfSchema":{"baz":5}}')
|
|
174
174
|
|
|
175
|
-
t.
|
|
175
|
+
t.equal(stringify({ oneOfSchema: { bar: 'foo' } }), '{"oneOfSchema":{"bar":"foo"}}')
|
|
176
176
|
})
|
|
177
177
|
|
|
178
178
|
test('null value in schema', (t) => {
|
|
@@ -214,9 +214,9 @@ test('oneOf and $ref together', (t) => {
|
|
|
214
214
|
|
|
215
215
|
const stringify = build(schema)
|
|
216
216
|
|
|
217
|
-
t.
|
|
217
|
+
t.equal(stringify({ cs: 'franco' }), '{"cs":"franco"}')
|
|
218
218
|
|
|
219
|
-
t.
|
|
219
|
+
t.equal(stringify({ cs: true }), '{"cs":true}')
|
|
220
220
|
})
|
|
221
221
|
|
|
222
222
|
test('oneOf and $ref: 2 levels are fine', (t) => {
|
|
@@ -254,7 +254,7 @@ test('oneOf and $ref: 2 levels are fine', (t) => {
|
|
|
254
254
|
const value = stringify({
|
|
255
255
|
cs: 3
|
|
256
256
|
})
|
|
257
|
-
t.
|
|
257
|
+
t.equal(value, '{"cs":3}')
|
|
258
258
|
})
|
|
259
259
|
|
|
260
260
|
test('oneOf and $ref: multiple levels should throw at build.', (t) => {
|
|
@@ -293,9 +293,9 @@ test('oneOf and $ref: multiple levels should throw at build.', (t) => {
|
|
|
293
293
|
|
|
294
294
|
const stringify = build(schema)
|
|
295
295
|
|
|
296
|
-
t.
|
|
297
|
-
t.
|
|
298
|
-
t.
|
|
296
|
+
t.equal(stringify({ cs: 3 }), '{"cs":3}')
|
|
297
|
+
t.equal(stringify({ cs: true }), '{"cs":true}')
|
|
298
|
+
t.equal(stringify({ cs: 'pippo' }), '{"cs":"pippo"}')
|
|
299
299
|
})
|
|
300
300
|
|
|
301
301
|
test('oneOf and $ref - multiple external $ref', (t) => {
|
|
@@ -373,5 +373,5 @@ test('oneOf with enum with more than 100 entries', (t) => {
|
|
|
373
373
|
const stringify = build(schema)
|
|
374
374
|
|
|
375
375
|
const value = stringify(['EUR', 'USD', null])
|
|
376
|
-
t.
|
|
376
|
+
t.equal(value, '["EUR","USD",null]')
|
|
377
377
|
})
|
package/test/ref.test.js
CHANGED
|
@@ -1026,7 +1026,7 @@ test('Bad key', t => {
|
|
|
1026
1026
|
})
|
|
1027
1027
|
t.fail('Should throw')
|
|
1028
1028
|
} catch (err) {
|
|
1029
|
-
t.
|
|
1029
|
+
t.equal(err.message, 'Cannot find reference "porjectId", did you mean "projectId"?')
|
|
1030
1030
|
}
|
|
1031
1031
|
})
|
|
1032
1032
|
|
|
@@ -1051,7 +1051,7 @@ test('Bad key', t => {
|
|
|
1051
1051
|
})
|
|
1052
1052
|
t.fail('Should throw')
|
|
1053
1053
|
} catch (err) {
|
|
1054
|
-
t.
|
|
1054
|
+
t.equal(err.message, 'Cannot find reference "foobar"')
|
|
1055
1055
|
}
|
|
1056
1056
|
})
|
|
1057
1057
|
|
|
@@ -1081,7 +1081,7 @@ test('Bad key', t => {
|
|
|
1081
1081
|
})
|
|
1082
1082
|
t.fail('Should throw')
|
|
1083
1083
|
} catch (err) {
|
|
1084
|
-
t.
|
|
1084
|
+
t.equal(err.message, 'Cannot find reference "porjectId", did you mean "projectId"?')
|
|
1085
1085
|
}
|
|
1086
1086
|
})
|
|
1087
1087
|
|
|
@@ -1111,7 +1111,7 @@ test('Bad key', t => {
|
|
|
1111
1111
|
})
|
|
1112
1112
|
t.fail('Should throw')
|
|
1113
1113
|
} catch (err) {
|
|
1114
|
-
t.
|
|
1114
|
+
t.equal(err.message, 'Cannot find reference "foobar"')
|
|
1115
1115
|
}
|
|
1116
1116
|
})
|
|
1117
1117
|
|
|
@@ -1141,7 +1141,7 @@ test('Bad key', t => {
|
|
|
1141
1141
|
})
|
|
1142
1142
|
t.fail('Should throw')
|
|
1143
1143
|
} catch (err) {
|
|
1144
|
-
t.
|
|
1144
|
+
t.equal(err.message, 'Cannot find reference "deifnitions", did you mean "definitions"?')
|
|
1145
1145
|
}
|
|
1146
1146
|
})
|
|
1147
1147
|
|
|
@@ -1166,7 +1166,7 @@ test('Bad key', t => {
|
|
|
1166
1166
|
})
|
|
1167
1167
|
t.fail('Should throw')
|
|
1168
1168
|
} catch (err) {
|
|
1169
|
-
t.
|
|
1169
|
+
t.equal(err.message, 'Cannot find reference "deifnitions", did you mean "definitions"?')
|
|
1170
1170
|
}
|
|
1171
1171
|
})
|
|
1172
1172
|
|
|
@@ -1196,9 +1196,56 @@ test('Bad key', t => {
|
|
|
1196
1196
|
})
|
|
1197
1197
|
t.fail('Should throw')
|
|
1198
1198
|
} catch (err) {
|
|
1199
|
-
t.
|
|
1199
|
+
t.equal(err.message, 'Cannot find reference "extrenal", did you mean "external"?')
|
|
1200
1200
|
}
|
|
1201
1201
|
})
|
|
1202
1202
|
|
|
1203
1203
|
t.end()
|
|
1204
1204
|
})
|
|
1205
|
+
|
|
1206
|
+
test('Regression 2.5.2', t => {
|
|
1207
|
+
t.plan(1)
|
|
1208
|
+
|
|
1209
|
+
const externalSchema = {
|
|
1210
|
+
'/models/Bar': {
|
|
1211
|
+
$id: '/models/Bar',
|
|
1212
|
+
$schema: 'http://json-schema.org/schema#',
|
|
1213
|
+
definitions: {
|
|
1214
|
+
entity: {
|
|
1215
|
+
type: 'object',
|
|
1216
|
+
properties: { field: { type: 'string' } }
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
},
|
|
1220
|
+
'/models/Foo': {
|
|
1221
|
+
$id: '/models/Foo',
|
|
1222
|
+
$schema: 'http://json-schema.org/schema#',
|
|
1223
|
+
definitions: {
|
|
1224
|
+
entity: {
|
|
1225
|
+
type: 'object',
|
|
1226
|
+
properties: {
|
|
1227
|
+
field: { type: 'string' },
|
|
1228
|
+
sub: {
|
|
1229
|
+
oneOf: [
|
|
1230
|
+
{ $ref: '/models/Bar#/definitions/entity' },
|
|
1231
|
+
{ type: 'null' }
|
|
1232
|
+
]
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
const schema = {
|
|
1241
|
+
type: 'array',
|
|
1242
|
+
items: {
|
|
1243
|
+
$ref: '/models/Foo#/definitions/entity'
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
const stringify = build(schema, { schema: externalSchema })
|
|
1248
|
+
const output = stringify([{ field: 'parent', sub: { field: 'joined' } }])
|
|
1249
|
+
|
|
1250
|
+
t.equal(output, '[{"field":"parent","sub":{"field":"joined"}}]')
|
|
1251
|
+
})
|
package/test/required.test.js
CHANGED
|
@@ -32,7 +32,7 @@ test('object with required field', (t) => {
|
|
|
32
32
|
})
|
|
33
33
|
t.fail()
|
|
34
34
|
} catch (e) {
|
|
35
|
-
t.
|
|
35
|
+
t.equal(e.message, '"str" is required!')
|
|
36
36
|
t.pass()
|
|
37
37
|
}
|
|
38
38
|
})
|
|
@@ -56,7 +56,7 @@ test('object with required field not in properties schema', (t) => {
|
|
|
56
56
|
stringify({})
|
|
57
57
|
t.fail()
|
|
58
58
|
} catch (e) {
|
|
59
|
-
t.
|
|
59
|
+
t.equal(e.message, '"str" is required!')
|
|
60
60
|
t.pass()
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -66,7 +66,7 @@ test('object with required field not in properties schema', (t) => {
|
|
|
66
66
|
})
|
|
67
67
|
t.fail()
|
|
68
68
|
} catch (e) {
|
|
69
|
-
t.
|
|
69
|
+
t.equal(e.message, '"str" is required!')
|
|
70
70
|
t.pass()
|
|
71
71
|
}
|
|
72
72
|
})
|
|
@@ -91,7 +91,7 @@ test('object with required field not in properties schema with additional proper
|
|
|
91
91
|
stringify({})
|
|
92
92
|
t.fail()
|
|
93
93
|
} catch (e) {
|
|
94
|
-
t.
|
|
94
|
+
t.equal(e.message, '"str" is required!')
|
|
95
95
|
t.pass()
|
|
96
96
|
}
|
|
97
97
|
|
|
@@ -101,7 +101,7 @@ test('object with required field not in properties schema with additional proper
|
|
|
101
101
|
})
|
|
102
102
|
t.fail()
|
|
103
103
|
} catch (e) {
|
|
104
|
-
t.
|
|
104
|
+
t.equal(e.message, '"str" is required!')
|
|
105
105
|
t.pass()
|
|
106
106
|
}
|
|
107
107
|
})
|
|
@@ -126,7 +126,7 @@ test('object with multiple required field not in properties schema', (t) => {
|
|
|
126
126
|
stringify({})
|
|
127
127
|
t.fail()
|
|
128
128
|
} catch (e) {
|
|
129
|
-
t.
|
|
129
|
+
t.equal(e.message, '"num" is required!')
|
|
130
130
|
t.pass()
|
|
131
131
|
}
|
|
132
132
|
|
|
@@ -136,7 +136,7 @@ test('object with multiple required field not in properties schema', (t) => {
|
|
|
136
136
|
})
|
|
137
137
|
t.fail()
|
|
138
138
|
} catch (e) {
|
|
139
|
-
t.
|
|
139
|
+
t.equal(e.message, '"key1" is required!')
|
|
140
140
|
t.pass()
|
|
141
141
|
}
|
|
142
142
|
|
|
@@ -147,7 +147,7 @@ test('object with multiple required field not in properties schema', (t) => {
|
|
|
147
147
|
})
|
|
148
148
|
t.fail()
|
|
149
149
|
} catch (e) {
|
|
150
|
-
t.
|
|
150
|
+
t.equal(e.message, '"key2" is required!')
|
|
151
151
|
t.pass()
|
|
152
152
|
}
|
|
153
153
|
})
|
|
@@ -172,7 +172,7 @@ test('object with required bool', (t) => {
|
|
|
172
172
|
stringify({})
|
|
173
173
|
t.fail()
|
|
174
174
|
} catch (e) {
|
|
175
|
-
t.
|
|
175
|
+
t.equal(e.message, '"bool" is required!')
|
|
176
176
|
t.pass()
|
|
177
177
|
}
|
|
178
178
|
|
|
@@ -232,7 +232,7 @@ test('required numbers', (t) => {
|
|
|
232
232
|
})
|
|
233
233
|
t.fail()
|
|
234
234
|
} catch (e) {
|
|
235
|
-
t.
|
|
235
|
+
t.equal(e.message, '"num" is required!')
|
|
236
236
|
t.pass()
|
|
237
237
|
}
|
|
238
238
|
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const build = require('..')
|
|
5
|
+
|
|
6
|
+
t.test('nested ref requires ajv', async t => {
|
|
7
|
+
const schemaA = {
|
|
8
|
+
$id: 'urn:schema:a',
|
|
9
|
+
definitions: {
|
|
10
|
+
foo: { anyOf: [{ type: 'string' }, { type: 'null' }] }
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const schemaB = {
|
|
15
|
+
$id: 'urn:schema:b',
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
results: {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
items: {
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
bar: {
|
|
25
|
+
type: 'array',
|
|
26
|
+
items: { $ref: 'urn:schema:a#/definitions/foo' }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const stringify = build(schemaB, {
|
|
36
|
+
schema: {
|
|
37
|
+
[schemaA.$id]: schemaA
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
const result = stringify({
|
|
41
|
+
results: {
|
|
42
|
+
items: {
|
|
43
|
+
bar: ['baz']
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
t.same(result, '{"results":{"items":{"bar":["baz"]}}}')
|
|
48
|
+
})
|
package/test/sanitize.test.js
CHANGED
|
@@ -105,7 +105,7 @@ const stringify2 = build({
|
|
|
105
105
|
}
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
-
t.
|
|
108
|
+
t.same(JSON.parse(stringify2({
|
|
109
109
|
'"\'phra////': 42,
|
|
110
110
|
asd: 42
|
|
111
111
|
})), {
|
|
@@ -135,7 +135,7 @@ const stringify4 = build({
|
|
|
135
135
|
}
|
|
136
136
|
})
|
|
137
137
|
|
|
138
|
-
t.
|
|
138
|
+
t.same(JSON.parse(stringify4({})), {
|
|
139
139
|
'"\\\\\\\\\'w00t': '"\'w00t'
|
|
140
140
|
})
|
|
141
141
|
|
package/test/side-effect.test.js
CHANGED
|
@@ -32,8 +32,8 @@ test('oneOf with $ref should not change the input schema', t => {
|
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
const value = stringify({ people: { name: 'hello', foo: 'bar' } })
|
|
35
|
-
t.
|
|
36
|
-
t.
|
|
35
|
+
t.equal(value, '{"people":{"name":"hello"}}')
|
|
36
|
+
t.same(schema, clonedSchema)
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
test('oneOf and anyOf with $ref should not change the input schema', t => {
|
|
@@ -77,7 +77,7 @@ test('oneOf and anyOf with $ref should not change the input schema', t => {
|
|
|
77
77
|
|
|
78
78
|
t.equal(valueAny1, '{"people":{"name":"hello"},"love":"music"}')
|
|
79
79
|
t.equal(valueAny2, '{"people":{"name":"hello"},"love":true}')
|
|
80
|
-
t.
|
|
80
|
+
t.same(schema, clonedSchema)
|
|
81
81
|
})
|
|
82
82
|
|
|
83
83
|
test('multiple $ref tree', t => {
|
|
@@ -118,6 +118,40 @@ test('multiple $ref tree', t => {
|
|
|
118
118
|
})
|
|
119
119
|
|
|
120
120
|
const value = stringify({ people: { name: 'hello', foo: 'bar', age: 42 } })
|
|
121
|
-
t.
|
|
122
|
-
t.
|
|
121
|
+
t.equal(value, '{"people":{"name":"hello","age":42}}')
|
|
122
|
+
t.same(schema, clonedSchema)
|
|
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.equal(value, '[{"name":"foo"}]')
|
|
156
|
+
t.same(schema, clonedSchema)
|
|
123
157
|
})
|
package/test/typesArray.test.js
CHANGED
|
@@ -22,7 +22,7 @@ test('possibly nullable integer primitive alternative', (t) => {
|
|
|
22
22
|
const value = stringify({
|
|
23
23
|
data: 4
|
|
24
24
|
})
|
|
25
|
-
t.
|
|
25
|
+
t.equal(value, '{"data":4}')
|
|
26
26
|
})
|
|
27
27
|
|
|
28
28
|
test('possibly nullable number primitive alternative', (t) => {
|
|
@@ -43,7 +43,7 @@ test('possibly nullable number primitive alternative', (t) => {
|
|
|
43
43
|
const value = stringify({
|
|
44
44
|
data: 4
|
|
45
45
|
})
|
|
46
|
-
t.
|
|
46
|
+
t.equal(value, '{"data":4}')
|
|
47
47
|
})
|
|
48
48
|
|
|
49
49
|
test('possibly nullable integer primitive alternative with null value', (t) => {
|
|
@@ -64,7 +64,7 @@ test('possibly nullable integer primitive alternative with null value', (t) => {
|
|
|
64
64
|
const value = stringify({
|
|
65
65
|
data: null
|
|
66
66
|
})
|
|
67
|
-
t.
|
|
67
|
+
t.equal(value, '{"data":0}')
|
|
68
68
|
})
|
|
69
69
|
|
|
70
70
|
test('possibly nullable number primitive alternative with null value', (t) => {
|
|
@@ -85,7 +85,7 @@ test('possibly nullable number primitive alternative with null value', (t) => {
|
|
|
85
85
|
const value = stringify({
|
|
86
86
|
data: null
|
|
87
87
|
})
|
|
88
|
-
t.
|
|
88
|
+
t.equal(value, '{"data":0}')
|
|
89
89
|
})
|
|
90
90
|
|
|
91
91
|
test('nullable integer primitive', (t) => {
|
|
@@ -106,7 +106,7 @@ test('nullable integer primitive', (t) => {
|
|
|
106
106
|
const value = stringify({
|
|
107
107
|
data: 4
|
|
108
108
|
})
|
|
109
|
-
t.
|
|
109
|
+
t.equal(value, '{"data":4}')
|
|
110
110
|
})
|
|
111
111
|
|
|
112
112
|
test('nullable number primitive', (t) => {
|
|
@@ -127,7 +127,7 @@ test('nullable number primitive', (t) => {
|
|
|
127
127
|
const value = stringify({
|
|
128
128
|
data: 4
|
|
129
129
|
})
|
|
130
|
-
t.
|
|
130
|
+
t.equal(value, '{"data":4}')
|
|
131
131
|
})
|
|
132
132
|
|
|
133
133
|
test('nullable primitive with null value', (t) => {
|
|
@@ -148,7 +148,7 @@ test('nullable primitive with null value', (t) => {
|
|
|
148
148
|
const value = stringify({
|
|
149
149
|
data: null
|
|
150
150
|
})
|
|
151
|
-
t.
|
|
151
|
+
t.equal(value, '{"data":null}')
|
|
152
152
|
})
|
|
153
153
|
|
|
154
154
|
test('nullable number primitive with null value', (t) => {
|
|
@@ -169,7 +169,7 @@ test('nullable number primitive with null value', (t) => {
|
|
|
169
169
|
const value = stringify({
|
|
170
170
|
data: null
|
|
171
171
|
})
|
|
172
|
-
t.
|
|
172
|
+
t.equal(value, '{"data":null}')
|
|
173
173
|
})
|
|
174
174
|
|
|
175
175
|
test('possibly null object with multi-type property', (t) => {
|
|
@@ -191,19 +191,19 @@ test('possibly null object with multi-type property', (t) => {
|
|
|
191
191
|
}
|
|
192
192
|
const stringify = build(schema)
|
|
193
193
|
|
|
194
|
-
t.
|
|
194
|
+
t.equal(stringify({
|
|
195
195
|
objectOrNull: {
|
|
196
196
|
stringOrNumber: 'string'
|
|
197
197
|
}
|
|
198
198
|
}), '{"objectOrNull":{"stringOrNumber":"string"}}')
|
|
199
199
|
|
|
200
|
-
t.
|
|
200
|
+
t.equal(stringify({
|
|
201
201
|
objectOrNull: {
|
|
202
202
|
stringOrNumber: 42
|
|
203
203
|
}
|
|
204
204
|
}), '{"objectOrNull":{"stringOrNumber":42}}')
|
|
205
205
|
|
|
206
|
-
t.
|
|
206
|
+
t.equal(stringify({
|
|
207
207
|
objectOrNull: null
|
|
208
208
|
}), '{"objectOrNull":null}')
|
|
209
209
|
})
|
|
@@ -229,7 +229,7 @@ test('object with possibly null array of multiple types', (t) => {
|
|
|
229
229
|
const value = stringify({
|
|
230
230
|
arrayOfStringsAndNumbers: null
|
|
231
231
|
})
|
|
232
|
-
t.
|
|
232
|
+
t.equal(value, '{"arrayOfStringsAndNumbers":null}')
|
|
233
233
|
} catch (e) {
|
|
234
234
|
console.log(e)
|
|
235
235
|
t.fail()
|
|
@@ -239,21 +239,21 @@ test('object with possibly null array of multiple types', (t) => {
|
|
|
239
239
|
const value = stringify({
|
|
240
240
|
arrayOfStringsAndNumbers: ['string1', 'string2']
|
|
241
241
|
})
|
|
242
|
-
t.
|
|
242
|
+
t.equal(value, '{"arrayOfStringsAndNumbers":["string1","string2"]}')
|
|
243
243
|
} catch (e) {
|
|
244
244
|
console.log(e)
|
|
245
245
|
t.fail()
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
t.
|
|
248
|
+
t.equal(stringify({
|
|
249
249
|
arrayOfStringsAndNumbers: [42, 7]
|
|
250
250
|
}), '{"arrayOfStringsAndNumbers":[42,7]}')
|
|
251
251
|
|
|
252
|
-
t.
|
|
252
|
+
t.equal(stringify({
|
|
253
253
|
arrayOfStringsAndNumbers: ['string1', 42, 7, 'string2']
|
|
254
254
|
}), '{"arrayOfStringsAndNumbers":["string1",42,7,"string2"]}')
|
|
255
255
|
|
|
256
|
-
t.
|
|
256
|
+
t.equal(stringify({
|
|
257
257
|
arrayOfStringsAndNumbers: ['string1', null, 42, 7, 'string2', null]
|
|
258
258
|
}), '{"arrayOfStringsAndNumbers":["string1",null,42,7,"string2",null]}')
|
|
259
259
|
})
|
|
@@ -287,7 +287,7 @@ test('object with tuple of multiple types', (t) => {
|
|
|
287
287
|
const value = stringify({
|
|
288
288
|
fixedTupleOfStringsAndNumbers: ['string1', 42, 7]
|
|
289
289
|
})
|
|
290
|
-
t.
|
|
290
|
+
t.equal(value, '{"fixedTupleOfStringsAndNumbers":["string1",42,7]}')
|
|
291
291
|
} catch (e) {
|
|
292
292
|
console.log(e)
|
|
293
293
|
t.fail()
|
|
@@ -297,7 +297,7 @@ test('object with tuple of multiple types', (t) => {
|
|
|
297
297
|
const value = stringify({
|
|
298
298
|
fixedTupleOfStringsAndNumbers: ['string1', 42, 'string2']
|
|
299
299
|
})
|
|
300
|
-
t.
|
|
300
|
+
t.equal(value, '{"fixedTupleOfStringsAndNumbers":["string1",42,"string2"]}')
|
|
301
301
|
} catch (e) {
|
|
302
302
|
console.log(e)
|
|
303
303
|
t.fail()
|
|
@@ -334,17 +334,17 @@ test('object with anyOf and multiple types', (t) => {
|
|
|
334
334
|
const value = stringify({
|
|
335
335
|
objectOrBoolean: { stringOrNumber: 'string' }
|
|
336
336
|
})
|
|
337
|
-
t.
|
|
337
|
+
t.equal(value, '{"objectOrBoolean":{"stringOrNumber":"string"}}')
|
|
338
338
|
} catch (e) {
|
|
339
339
|
console.log(e)
|
|
340
340
|
t.fail()
|
|
341
341
|
}
|
|
342
342
|
|
|
343
|
-
t.
|
|
343
|
+
t.equal(stringify({
|
|
344
344
|
objectOrBoolean: { stringOrNumber: 42 }
|
|
345
345
|
}), '{"objectOrBoolean":{"stringOrNumber":42}}')
|
|
346
346
|
|
|
347
|
-
t.
|
|
347
|
+
t.equal(stringify({
|
|
348
348
|
objectOrBoolean: true
|
|
349
349
|
}), '{"objectOrBoolean":true}')
|
|
350
350
|
})
|
|
@@ -363,7 +363,7 @@ test('string type array can handle dates', (t) => {
|
|
|
363
363
|
date: new Date('2018-04-20T07:52:31.017Z'),
|
|
364
364
|
dateObject: moment('2018-04-21T07:52:31.017Z')
|
|
365
365
|
})
|
|
366
|
-
t.
|
|
366
|
+
t.equal(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
|
|
367
367
|
})
|
|
368
368
|
|
|
369
369
|
test('object that is simultaneously a string and a json', (t) => {
|
|
@@ -386,10 +386,10 @@ test('object that is simultaneously a string and a json', (t) => {
|
|
|
386
386
|
|
|
387
387
|
const stringify = build(schema)
|
|
388
388
|
const valueStr = stringify({ simultaneously: likeObjectId })
|
|
389
|
-
t.
|
|
389
|
+
t.equal(valueStr, '{"simultaneously":"hello"}')
|
|
390
390
|
|
|
391
391
|
const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
|
|
392
|
-
t.
|
|
392
|
+
t.equal(valueObj, '{"simultaneously":{"foo":"hello"}}')
|
|
393
393
|
})
|
|
394
394
|
|
|
395
395
|
test('object that is simultaneously a string and a json switched', (t) => {
|
|
@@ -412,10 +412,10 @@ test('object that is simultaneously a string and a json switched', (t) => {
|
|
|
412
412
|
|
|
413
413
|
const stringify = build(schema)
|
|
414
414
|
const valueStr = stringify({ simultaneously: likeObjectId })
|
|
415
|
-
t.
|
|
415
|
+
t.equal(valueStr, '{"simultaneously":{}}')
|
|
416
416
|
|
|
417
417
|
const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
|
|
418
|
-
t.
|
|
418
|
+
t.equal(valueObj, '{"simultaneously":{"foo":"hello"}}')
|
|
419
419
|
})
|
|
420
420
|
|
|
421
421
|
test('should throw an error when type is array and object is null', (t) => {
|