Skip to content

Commit 6a2c90d

Browse files
committed
add a forEach to test all schema in an allOf
1 parent 1350d11 commit 6a2c90d

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

index.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,7 @@ function refFinder (ref, schema, externalSchema) {
359359
return (new Function('schema', code))(schema)
360360
}
361361

362-
function buildObject (schema, code, name, externalSchema, fullSchema) {
363-
code += `
364-
function ${name} (obj) {
365-
var json = '{'
366-
var addComma = false
367-
`
368-
369-
if (schema.patternProperties) {
370-
code += addPatternProperties(schema, externalSchema, fullSchema)
371-
} else if (schema.additionalProperties && !schema.patternProperties) {
372-
code += addAdditionalProperties(schema, externalSchema, fullSchema)
373-
}
374-
375-
var laterCode = ''
376-
362+
function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
377363
Object.keys(schema.properties || {}).forEach((key, i, a) => {
378364
// Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
379365
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
@@ -404,6 +390,38 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
404390
`
405391
})
406392

393+
return { code: code, laterCode: laterCode }
394+
}
395+
396+
function buildObject (schema, code, name, externalSchema, fullSchema) {
397+
code += `
398+
function ${name} (obj) {
399+
var json = '{'
400+
var addComma = false
401+
`
402+
403+
if (schema.patternProperties) {
404+
code += addPatternProperties(schema, externalSchema, fullSchema)
405+
} else if (schema.additionalProperties && !schema.patternProperties) {
406+
code += addAdditionalProperties(schema, externalSchema, fullSchema)
407+
}
408+
409+
var laterCode = ''
410+
411+
if (schema.allOf) {
412+
schema.allOf.forEach((ss) => {
413+
var builtCode = buildCode(ss, code, laterCode, name, externalSchema, fullSchema)
414+
415+
code = builtCode.code
416+
laterCode = builtCode.laterCode
417+
})
418+
} else {
419+
var builtCode = buildCode(schema, code, laterCode, name, externalSchema, fullSchema)
420+
421+
code = builtCode.code
422+
laterCode = builtCode.laterCode
423+
}
424+
407425
// Removes the comma if is the last element of the string (in case there are not properties)
408426
code += `
409427
json += '}'

test/allof.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test('object with multiple types field', (t) => {
4444
id: 1,
4545
name: 'string'
4646
})
47-
t.is(value, '{"id":1,"name":"string"}')
47+
t.is(value, '{"name":"string","id":1}')
4848
} catch (e) {
4949
t.fail()
5050
}
@@ -55,7 +55,7 @@ test('object with multiple types field', (t) => {
5555
name: 'string',
5656
tag: 'otherString'
5757
})
58-
t.is(value, '{"id":1,"name":"string","tag":"otherString"}')
58+
t.is(value, '{"name":"string","tag":"otherString","id":1}')
5959
} catch (e) {
6060
t.fail()
6161
}

0 commit comments

Comments
 (0)