Skip to content

Commit 9ee055c

Browse files
committed
add removeReadOnly and removeWriteOnly options
1 parent 5ddc38a commit 9ee055c

File tree

3 files changed

+436
-4
lines changed

3 files changed

+436
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ prints
100100

101101
By default, the following fields are removed from the result schema: `nullable`, `discriminator`, `readOnly`, `writeOnly`, `xml`, `externalDocs`, `example` and `deprecated` as they are not supported by JSON Schema Draft 4. Provide an array of the ones you want to keep (as strings) and they won't be removed.
102102

103+
#### `removeReadOnly` (boolean)
104+
105+
If set to `true`, will remove properties set as `readOnly`. If the property is set as `required`, it will be removed from the `required` array as well. The property will be removed even if `readOnly` is set to be kept with `keepNotSupported`.
106+
107+
#### `removeWriteOnly` (boolean)
108+
109+
Similar to `removeReadOnly`, but for `writeOnly` properties.
110+
103111
#### `supportPatternProperties` (boolean)
104112

105113
If set to `true` and `x-patternProperties` property is present, change `x-patternProperties` to `patternProperties` and call `patternPropertiesHandler`. If `patternPropertiesHandler` is not defined, call the default handler. See `patternPropertiesHandler` for more information.

index.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ function convert(schema, options) {
1919
options.patternPropertiesHandler = patternPropertiesHandler;
2020
}
2121

22+
options._removeProps = [];
23+
24+
if (options.removeReadOnly === true) {
25+
options._removeProps.push('readOnly');
26+
}
27+
28+
if (options.removeWriteOnly === true) {
29+
options._removeProps.push('writeOnly');
30+
}
31+
2232
options._structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
2333
options._notSupported = resolveNotSupported(notSupported, options.keepNotSupported);
2434

@@ -54,9 +64,22 @@ function convertSchema(schema, options) {
5464

5565
if (typeof schema.properties === 'object') {
5666
schema.properties = convertProperties(schema.properties, options);
67+
68+
if (Array.isArray(schema.required)) {
69+
schema.required = cleanRequired(schema.required, schema.properties);
70+
71+
if (schema.required.length === 0) {
72+
delete schema.required;
73+
}
74+
}
75+
if (Object.keys(schema.properties).length === 0) {
76+
delete schema.properties;
77+
}
78+
5779
}
5880

5981
schema = convertTypes(schema, options);
82+
6083
if (typeof schema['x-patternProperties'] === 'object'
6184
&& options.supportPatternProperties) {
6285
schema = convertPatternProperties(schema, options.patternPropertiesHandler);
@@ -70,13 +93,30 @@ function convertSchema(schema, options) {
7093
}
7194

7295
function convertProperties(properties, options) {
73-
var key;
96+
var key
97+
, property
98+
, props = {}
99+
, removeProp
100+
;
74101

75102
for (key in properties) {
76-
properties[key] = convertSchema(properties[key], options);
103+
removeProp = false;
104+
property = properties[key];
105+
106+
options._removeProps.forEach(function(prop) {
107+
if (property[prop] === true) {
108+
removeProp = true;
109+
}
110+
});
111+
112+
if (removeProp) {
113+
continue;
114+
}
115+
116+
props[key] = convertSchema(property, options);
77117
}
78118

79-
return properties;
119+
return props;
80120
}
81121

82122
function convertTypes(schema, options) {
@@ -182,7 +222,7 @@ function resolveNotSupported(notSupported, toRetain) {
182222
, index
183223
;
184224

185-
for(i; i < toRetain.length; i++) {
225+
for (i; i < toRetain.length; i++) {
186226
index = notSupported.indexOf(toRetain[i]);
187227

188228
if (index >= 0) {
@@ -192,3 +232,18 @@ function resolveNotSupported(notSupported, toRetain) {
192232

193233
return notSupported;
194234
}
235+
236+
function cleanRequired(required, properties) {
237+
var i = 0;
238+
239+
required = required || [];
240+
properties = properties || {};
241+
242+
for (i; i < required.length; i++) {
243+
if (properties[required[i]] === undefined) {
244+
required.splice(i, 1);
245+
}
246+
}
247+
248+
return required;
249+
}

0 commit comments

Comments
 (0)