Skip to content

Commit 0325292

Browse files
authored
Merge pull request #65 from keldorus/allOf
Add a forEach to test each schema when there is an allOf
2 parents cd8886a + 96a440b commit 0325292

File tree

2 files changed

+160
-15
lines changed

2 files changed

+160
-15
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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('object with allOf and multiple schema on the allOf', (t) => {
7+
t.plan(4)
8+
9+
const schema = {
10+
title: 'object with allOf and multiple schema on the allOf',
11+
type: 'object',
12+
allOf: [
13+
{
14+
type: 'object',
15+
required: [
16+
'name'
17+
],
18+
properties: {
19+
name: {
20+
type: 'string'
21+
},
22+
tag: {
23+
type: 'string'
24+
}
25+
}
26+
},
27+
{
28+
required: [
29+
'id'
30+
],
31+
type: 'object',
32+
properties: {
33+
id: {
34+
type: 'integer'
35+
}
36+
}
37+
}
38+
]
39+
}
40+
const stringify = build(schema)
41+
42+
try {
43+
stringify({
44+
id: 1
45+
})
46+
} catch (e) {
47+
t.is(e.message, 'name is required!')
48+
}
49+
50+
try {
51+
stringify({
52+
name: 'string'
53+
})
54+
} catch (e) {
55+
t.is(e.message, 'id is required!')
56+
}
57+
58+
try {
59+
const value = stringify({
60+
id: 1,
61+
name: 'string'
62+
})
63+
t.is(value, '{"name":"string","id":1}')
64+
} catch (e) {
65+
t.fail()
66+
}
67+
68+
try {
69+
const value = stringify({
70+
id: 1,
71+
name: 'string',
72+
tag: 'otherString'
73+
})
74+
t.is(value, '{"name":"string","tag":"otherString","id":1}')
75+
} catch (e) {
76+
t.fail()
77+
}
78+
})
79+
80+
test('object with allOf and one schema on the allOf', (t) => {
81+
t.plan(1)
82+
83+
const schema = {
84+
title: 'object with allOf and one schema on the allOf',
85+
type: 'object',
86+
allOf: [
87+
{
88+
required: [
89+
'id'
90+
],
91+
type: 'object',
92+
properties: {
93+
id: {
94+
type: 'integer'
95+
}
96+
}
97+
}
98+
]
99+
}
100+
const stringify = build(schema)
101+
102+
try {
103+
const value = stringify({
104+
id: 1
105+
})
106+
t.is(value, '{"id":1}')
107+
} catch (e) {
108+
t.fail()
109+
}
110+
})
111+
112+
test('object with allOf and no schema on the allOf', (t) => {
113+
t.plan(1)
114+
115+
const schema = {
116+
title: 'object with allOf and no schema on the allOf',
117+
type: 'object',
118+
allOf: []
119+
}
120+
121+
try {
122+
build(schema)
123+
t.fail()
124+
} catch (e) {
125+
t.is(e.message, 'schema is invalid: data.allOf should NOT have less than 1 items')
126+
}
127+
})

0 commit comments

Comments
 (0)