Skip to content

Commit 400774a

Browse files
authored
Fix: Omit all Non-JSON schema applicable properties from codegen (#279) (#280)
* replace util.inspect() with JSON.stringify() in codegen * use distinct symbols in test
1 parent a4ac6cd commit 400774a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
var Ajv = require('ajv')
66
var merge = require('deepmerge')
77

8-
var util = require('util')
98
var validate = require('./schema-validator')
109
var stringSimilarity = null
1110

@@ -834,7 +833,7 @@ function addIfThenElse (location, name) {
834833
var mergedLocation = mergeLocation(location, { schema: merged })
835834

836835
code += `
837-
valid = ajv.validate(${util.inspect(i, { depth: null })}, obj)
836+
valid = ajv.validate(${JSON.stringify(i)}, obj)
838837
if (valid) {
839838
`
840839
if (merged.if && merged.then) {

test/array.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,50 @@ buildTest({
209209
}, {
210210
foo: [1, 'string', {}, null]
211211
})
212+
213+
// https://github.com/fastify/fast-json-stringify/issues/279
214+
test('object array with anyOf and symbol', (t) => {
215+
t.plan(1)
216+
const ArrayKind = Symbol('ArrayKind')
217+
const ObjectKind = Symbol('LiteralKind')
218+
const UnionKind = Symbol('UnionKind')
219+
const LiteralKind = Symbol('LiteralKind')
220+
const StringKind = Symbol('StringKind')
221+
222+
const schema = {
223+
kind: ArrayKind,
224+
type: 'array',
225+
items: {
226+
kind: ObjectKind,
227+
type: 'object',
228+
properties: {
229+
name: {
230+
kind: StringKind,
231+
type: 'string'
232+
},
233+
option: {
234+
kind: UnionKind,
235+
anyOf: [
236+
{
237+
kind: LiteralKind,
238+
type: 'string',
239+
enum: ['Foo']
240+
},
241+
{
242+
kind: LiteralKind,
243+
type: 'string',
244+
enum: ['Bar']
245+
}
246+
]
247+
}
248+
},
249+
required: ['name', 'option']
250+
}
251+
}
252+
const stringify = build(schema)
253+
const value = stringify([
254+
{ name: 'name-0', option: 'Foo' },
255+
{ name: 'name-1', option: 'Bar' }
256+
])
257+
t.is(value, '[{"name":"name-0","option":"Foo"},{"name":"name-1","option":"Bar"}]')
258+
})

0 commit comments

Comments
 (0)