Skip to content

Commit 9569fee

Browse files
Marsupmcollina
authored andcommitted
Fix serialization of anyOf with both array and object types (#128)
* Fix serialization of anyOf with both array and object types * Fix test command
1 parent 413d656 commit 9569fee

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
796796
`
797797
break
798798
case 'array':
799-
funcName = (name + key + subKey).replace(/[-.\[\] ]/g, '') // eslint-disable-line
799+
funcName = '$arr' + (name + key + subKey).replace(/[-.\[\] ]/g, '') // eslint-disable-line
800800
laterCode = buildArray(schema, laterCode, funcName, externalSchema, fullSchema)
801801
code += `
802802
json += ${funcName}(obj${accessor})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"benchmark": "node bench.js",
88
"lint": "standard",
9-
"unit": "tap -j4 test/**/*.test.js",
9+
"unit": "tap -j4 test/*.test.js test/**/*.test.js",
1010
"test": "npm run lint && npm run unit"
1111
},
1212
"precommit": "test",

test/anyof.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,43 @@ test('object with field of type object or null', (t) => {
8181
t.fail()
8282
}
8383
})
84+
85+
test('object with field of type object or array', (t) => {
86+
t.plan(2)
87+
88+
const schema = {
89+
title: 'object with field of type object or array',
90+
type: 'object',
91+
properties: {
92+
prop: {
93+
anyOf: [{
94+
type: 'object',
95+
properties: {},
96+
additionalProperties: true
97+
}, {
98+
type: 'array',
99+
items: { type: 'string' }
100+
}]
101+
}
102+
}
103+
}
104+
const stringify = build(schema)
105+
106+
try {
107+
const value = stringify({
108+
prop: { str: 'string' }
109+
})
110+
t.is(value, '{"prop":{"str":"string"}}')
111+
} catch (e) {
112+
t.fail()
113+
}
114+
115+
try {
116+
const value = stringify({
117+
prop: ['string']
118+
})
119+
t.is(value, '{"prop":["string"]}')
120+
} catch (e) {
121+
t.fail()
122+
}
123+
})

0 commit comments

Comments
 (0)