Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58a9b62

Browse files
rewrite if/then/else as oneOf: [allOf: [X, Y], allOf: [not: X, Z]]
1 parent 17e4ffc commit 58a9b62

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function convertSchema(schema, path, parent, parentPath) {
3030
schema = stripIllegalKeywords(schema);
3131
schema = convertTypes(schema);
3232
schema = convertDependencies(schema);
33+
schema = rewriteIfThenElse(schema);
3334

3435
if (typeof schema['patternProperties'] === 'object') {
3536
schema = convertPatternProperties(schema);
@@ -140,4 +141,27 @@ function convertPatternProperties(schema) {
140141
return schema;
141142
}
142143

144+
function rewriteIfThenElse(schema) {
145+
/* @handrews https://github.com/OAI/OpenAPI-Specification/pull/1766#issuecomment-442652805
146+
if and the *Of keywords
147+
148+
There is a really easy solution for implementations, which is that
149+
150+
if: X, then: Y, else: Z
151+
152+
is equivalent to
153+
154+
oneOf: [allOf: [X, Y], allOf: [not: X, Z]]
155+
*/
156+
if (schema.if && schema.then) {
157+
schema.oneOf = [ { allOf: [ schema.if, schema.then ] },
158+
{ allOf: [ { not: schema.if }, schema.else ] } ];
159+
delete schema.if;
160+
delete schema.then;
161+
delete schema.else;
162+
}
163+
return schema;
164+
}
165+
143166
module.exports = convert;
167+

‎test/if-then-else.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const convert = require('../');
4+
const should = require('should');
5+
6+
it('if-then-else', () => {
7+
const schema = {
8+
$schema: 'http://json-schema.org/draft-04/schema#',
9+
if: { type: 'object' },
10+
then: { properties: { id: { type: 'string' } } },
11+
else: { format: 'uuid' }
12+
};
13+
14+
const result = convert(schema);
15+
16+
const expected = {
17+
oneOf: [
18+
{ allOf: [ { type: 'object' }, { properties: { id: { type: 'string' } } } ] },
19+
{ allOf: [ { not: { type: 'object' } }, { format: 'uuid' } ] }
20+
]
21+
};
22+
23+
should(result).deepEqual(expected, 'converted');
24+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /