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 aae1805

Browse files
author
Phil Sturgeon
authored
Merge pull request #15 from MikeRalphson/if-then-else
rewrite if/then/else as oneOf: [allOf: [X, Y], allOf: [not: X, Z]]
2 parents 974538d + 08b682e commit aae1805

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
schema = rewriteExclusiveMinMax(schema);
3435

3536
if (typeof schema['patternProperties'] === 'object') {
@@ -145,6 +146,28 @@ function convertPatternProperties(schema) {
145146
return schema;
146147
}
147148

149+
function rewriteIfThenElse(schema) {
150+
/* @handrews https://github.com/OAI/OpenAPI-Specification/pull/1766#issuecomment-442652805
151+
if and the *Of keywords
152+
153+
There is a really easy solution for implementations, which is that
154+
155+
if: X, then: Y, else: Z
156+
157+
is equivalent to
158+
159+
oneOf: [allOf: [X, Y], allOf: [not: X, Z]]
160+
*/
161+
if (schema.if && schema.then) {
162+
schema.oneOf = [ { allOf: [ schema.if, schema.then ] },
163+
{ allOf: [ { not: schema.if }, schema.else ] } ];
164+
delete schema.if;
165+
delete schema.then;
166+
delete schema.else;
167+
}
168+
return schema;
169+
}
170+
148171
function rewriteExclusiveMinMax(schema) {
149172
if (typeof schema.exclusiveMaximum === 'number') {
150173
schema.maximum = schema.exclusiveMaximum;
@@ -158,3 +181,4 @@ function rewriteExclusiveMinMax(schema) {
158181
}
159182

160183
module.exports = convert;
184+

‎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 によって変換されたページ (->オリジナル) /