Skip to content

Commit 50a357a

Browse files
committed
Added anyOf
1 parent c9c628c commit 50a357a

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/FluentSchema.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ const FluentSchema = (
5959
},
6060
definition: (name, props = {}) =>
6161
FluentSchema({ ...schema }).prop(name, props, true),
62+
63+
//TODO LS move 'def' in the props
6264
prop: (name, props = {}, def = false) => {
6365
const target = def ? 'definitions' : 'properties'
66+
const attributes =
67+
typeof props.title === 'function' ? props.valueOf() : props
6468
const {
65-
type = 'string',
69+
type = attributes.anyOf || attributes.anyOf || attributes.anyOf
70+
? undefined
71+
: 'string',
6672
// TODO LS $id should be prefixed with the parent
6773
$id = `#${target}/${name}`,
6874
$ref,
@@ -71,7 +77,10 @@ const FluentSchema = (
7177
defaults,
7278
properties,
7379
required,
74-
} = typeof props.title === 'function' ? props.valueOf() : props
80+
anyOf,
81+
allOf,
82+
oneOf,
83+
} = attributes
7584

7685
return FluentSchema({
7786
...schema,
@@ -87,12 +96,29 @@ const FluentSchema = (
8796
$id ? { $id } : undefined,
8897
description ? { description } : undefined,
8998
properties ? { properties } : undefined,
90-
required ? { required } : undefined
99+
required ? { required } : undefined,
100+
anyOf ? { anyOf } : undefined,
101+
oneOf ? { oneOf } : undefined,
102+
allOf ? { allOf } : undefined
91103
),
92104
],
93105
})
94106
},
95107

108+
anyOf: attributes => {
109+
const currentProp = last(schema.properties)
110+
const { name, type, ...props } = currentProp
111+
const properties = attributes.valueOf().properties
112+
const values = Object.entries(properties).reduce((memo, [key, value]) => {
113+
return [...memo, value]
114+
}, [])
115+
const attr = {
116+
...props,
117+
anyOf: values,
118+
}
119+
return FluentSchema({ ...schema }).prop(name, attr)
120+
},
121+
96122
required: () => {
97123
const currentProp = last(schema.properties)
98124
return FluentSchema({

src/FluentSchema.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,36 @@ describe('FluentSchema', () => {
124124
).toEqual('object')
125125
})
126126
})
127+
128+
describe('anyOf', () => {
129+
it('sets two alternative', () => {
130+
expect(
131+
FluentSchema()
132+
.prop('gender')
133+
.anyOf(
134+
FluentSchema()
135+
.prop('male')
136+
.prop('female')
137+
)
138+
.valueOf()
139+
).toEqual({
140+
$schema: 'http://json-schema.org/draft-07/schema#',
141+
definitions: {},
142+
properties: {
143+
gender: {
144+
$id: '#properties/gender',
145+
anyOf: [
146+
{ $id: '#properties/male', type: 'string' },
147+
{ $id: '#properties/female', type: 'string' },
148+
],
149+
},
150+
},
151+
required: [],
152+
type: 'object',
153+
})
154+
})
155+
})
156+
127157
it('works', () => {
128158
// TODO LS https://json-schema.org/latest/json-schema-core.html#idExamples
129159
const schema = FluentSchema()

0 commit comments

Comments
 (0)