Skip to content

Commit 3afc219

Browse files
author
Mika Kunnas
committed
support for pattern properties hack
1 parent aa27888 commit 3afc219

File tree

3 files changed

+316
-1
lines changed

3 files changed

+316
-1
lines changed

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ function convert(schema, options) {
44
options = options || {};
55
options.dateToDateTime = options.dateToDateTime || false;
66
options.cloneSchema = options.cloneSchema == false ? false : true;
7+
options.supportPatternProperties = options.supportPatternProperties || false;
8+
9+
if (typeof options.patternPropertiesHandler !== 'function') {
10+
options.patternPropertiesHandler = patternPropertiesHandler;
11+
}
712

813
options._structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
914
options._notSupported = [
@@ -47,6 +52,10 @@ function convertSchema(schema, options) {
4752
}
4853

4954
schema = convertTypes(schema, options);
55+
if (typeof schema['x-patternProperties'] === 'object'
56+
&& options.supportPatternProperties) {
57+
schema = convertPatternProperties(schema, options.patternPropertiesHandler);
58+
}
5059

5160
for (i=0; i < notSupported.length; i++) {
5261
delete schema[notSupported[i]];
@@ -136,3 +145,32 @@ function convertTypes(schema, options) {
136145
return schema;
137146
}
138147

148+
function convertPatternProperties(schema, handler) {
149+
schema.patternProperties = schema['x-patternProperties'];
150+
delete schema['x-patternProperties'];
151+
152+
return handler(schema);
153+
}
154+
155+
function patternPropertiesHandler(schema) {
156+
var pattern
157+
, patternsObj = schema.patternProperties
158+
, additProps = schema.additionalProperties
159+
, type
160+
;
161+
162+
if (typeof additProps !== 'object') {
163+
return schema;
164+
}
165+
166+
for (pattern in patternsObj) {
167+
type = patternsObj[pattern].type;
168+
169+
if ((type === additProps.type) && type !== 'object') {
170+
delete schema.additionalProperties;
171+
break;
172+
}
173+
}
174+
175+
return schema;
176+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-schema-to-json-schema",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Converts OpenAPI Schema Object to JSON Schema",
55
"main": "index.js",
66
"scripts": {

test/pattern_properties.js

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
var test = require('tape')
2+
, convert = require('../')
3+
;
4+
5+
test('handling additional properties of the same type: string', function(assert) {
6+
var schema
7+
, result
8+
, expected
9+
;
10+
11+
assert.plan(1);
12+
13+
schema = {
14+
type: 'object',
15+
additionalProperties: {
16+
type: 'string'
17+
},
18+
'x-patternProperties': {
19+
'^[a-z]*$': {
20+
type: 'string'
21+
}
22+
}
23+
};
24+
25+
result = convert(schema, {supportPatternProperties: true});
26+
27+
expected = {
28+
$schema: 'http://json-schema.org/draft-04/schema#',
29+
type: 'object',
30+
patternProperties: {
31+
'^[a-z]*$': {
32+
type: 'string'
33+
}
34+
}
35+
};
36+
37+
assert.deepEqual(result, expected, 'additionalProperties deleted');
38+
});
39+
40+
test('handling additional properties of the same type: number', function(assert) {
41+
var schema
42+
, result
43+
, expected
44+
;
45+
46+
assert.plan(1);
47+
48+
schema = {
49+
type: 'object',
50+
additionalProperties: {
51+
type: 'number'
52+
},
53+
'x-patternProperties': {
54+
'^[a-z]*$': {
55+
type: 'number'
56+
}
57+
}
58+
};
59+
60+
result = convert(schema, {supportPatternProperties: true});
61+
62+
expected = {
63+
$schema: 'http://json-schema.org/draft-04/schema#',
64+
type: 'object',
65+
patternProperties: {
66+
'^[a-z]*$': {
67+
type: 'number'
68+
}
69+
}
70+
};
71+
72+
assert.deepEqual(result, expected, 'additionalProperties deleted');
73+
});
74+
75+
test('handling additional properties with one of patternProperty types', function(assert) {
76+
var schema
77+
, result
78+
, expected
79+
;
80+
81+
assert.plan(1);
82+
83+
schema = {
84+
type: 'object',
85+
additionalProperties: {
86+
type: 'number'
87+
},
88+
'x-patternProperties': {
89+
'^[a-z]*$': {
90+
type: 'string'
91+
},
92+
'^[A-Z]*$': {
93+
type: 'number'
94+
}
95+
}
96+
};
97+
98+
result = convert(schema, {supportPatternProperties: true});
99+
100+
expected = {
101+
$schema: 'http://json-schema.org/draft-04/schema#',
102+
type: 'object',
103+
patternProperties: {
104+
'^[a-z]*$': {
105+
type: 'string'
106+
},
107+
'^[A-Z]*$': {
108+
type: 'number'
109+
}
110+
}
111+
};
112+
113+
assert.deepEqual(result, expected, 'additionalProperties deleted');
114+
});
115+
116+
test('keeping additionalProperties with object type', function(assert) {
117+
var schema
118+
, result
119+
, expected
120+
;
121+
122+
assert.plan(1);
123+
124+
schema = {
125+
type: 'object',
126+
additionalProperties: {
127+
type: 'object'
128+
},
129+
'x-patternProperties': {
130+
'^[a-z]*$': {
131+
type: 'string'
132+
},
133+
'^[A-Z]*$': {
134+
type: 'object'
135+
}
136+
}
137+
};
138+
139+
result = convert(schema, {supportPatternProperties: true});
140+
141+
expected = {
142+
$schema: 'http://json-schema.org/draft-04/schema#',
143+
type: 'object',
144+
additionalProperties: {
145+
type: 'object'
146+
},
147+
patternProperties: {
148+
'^[a-z]*$': {
149+
type: 'string'
150+
},
151+
'^[A-Z]*$': {
152+
type: 'object'
153+
}
154+
}
155+
};
156+
157+
assert.deepEqual(result, expected, 'additionalProperties kept');
158+
});
159+
160+
test('not supporting patternProperties', function(assert) {
161+
var schema
162+
, result
163+
, expected
164+
;
165+
166+
assert.plan(1);
167+
168+
schema = {
169+
type: 'object',
170+
additionalProperties: {
171+
type: 'string'
172+
},
173+
'x-patternProperties': {
174+
'^[a-z]*$': {
175+
type: 'string'
176+
}
177+
}
178+
};
179+
180+
result = convert(schema, {supportPatternProperties: false});
181+
182+
expected = {
183+
$schema: 'http://json-schema.org/draft-04/schema#',
184+
type: 'object',
185+
additionalProperties: {
186+
type: 'string'
187+
},
188+
'x-patternProperties': {
189+
'^[a-z]*$': {
190+
type: 'string'
191+
}
192+
}
193+
};
194+
195+
assert.deepEqual(result, expected, 'nothing done');
196+
});
197+
198+
test('not supporting patternProperties by default', function(assert) {
199+
var schema
200+
, result
201+
, expected
202+
;
203+
204+
assert.plan(1);
205+
206+
schema = {
207+
type: 'object',
208+
additionalProperties: {
209+
type: 'string'
210+
},
211+
'x-patternProperties': {
212+
'^[a-z]*$': {
213+
type: 'string'
214+
}
215+
}
216+
};
217+
218+
result = convert(schema);
219+
220+
expected = {
221+
$schema: 'http://json-schema.org/draft-04/schema#',
222+
type: 'object',
223+
additionalProperties: {
224+
type: 'string'
225+
},
226+
'x-patternProperties': {
227+
'^[a-z]*$': {
228+
type: 'string'
229+
}
230+
}
231+
};
232+
233+
assert.deepEqual(result, expected, 'nothing done');
234+
});
235+
236+
test('setting custom patternProperties handler', function(assert) {
237+
var schema
238+
, result
239+
, expected
240+
, options
241+
;
242+
243+
assert.plan(1);
244+
245+
schema = {
246+
type: 'object',
247+
additionalProperties: {
248+
type: 'string'
249+
},
250+
'x-patternProperties': {
251+
'^[a-z]*$': {
252+
type: 'string'
253+
}
254+
}
255+
};
256+
257+
options = {
258+
supportPatternProperties: true,
259+
patternPropertiesHandler: function(schema) {
260+
schema.patternProperties = false;
261+
return schema;
262+
}
263+
};
264+
265+
result = convert(schema, options);
266+
267+
expected = {
268+
$schema: 'http://json-schema.org/draft-04/schema#',
269+
type: 'object',
270+
additionalProperties: {
271+
type: 'string'
272+
},
273+
patternProperties: false
274+
};
275+
276+
assert.deepEqual(result, expected, 'handler with custom handler');
277+
});

0 commit comments

Comments
 (0)