Skip to content

Commit de6fcb0

Browse files
committed
Add support for all OpenAPI formats
1 parent 60c08e8 commit de6fcb0

File tree

1 file changed

+79
-12
lines changed

1 file changed

+79
-12
lines changed

index.js

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ function convertSchema(schema, options) {
8686
}
8787

8888
validateType(schema.type);
89-
schema = convertTypes(schema, options);
89+
schema = convertTypes(schema);
90+
schema = convertFormat(schema, options);
9091

9192
if (typeof schema['x-patternProperties'] === 'object'
9293
&& options.supportPatternProperties) {
@@ -135,28 +136,94 @@ function validateType(type) {
135136
}
136137
}
137138

138-
function convertTypes(schema, options) {
139-
var toDateTime = options.dateToDateTime;
139+
function convertTypes(schema) {
140+
if (schema.type !== undefined && schema.nullable === true) {
141+
schema.type = [schema.type, 'null'];
142+
}
143+
144+
return schema;
145+
}
140146

141-
if (schema.type === undefined) {
147+
function convertFormat(schema, options) {
148+
var format = schema.format;
149+
150+
if (format === undefined || FORMATS.indexOf(format) !== -1) {
142151
return schema;
143152
}
144153

145-
if (schema.type == 'string' && schema.format == 'date' && toDateTime === true) {
146-
schema.format = 'date-time';
147-
}
154+
var converter = formatConverters[format];
148155

149-
if (! schema.format) {
150-
delete schema.format;
151-
}
156+
if (converter === undefined) { return schema; }
152157

153-
if (schema.nullable === true) {
154-
schema.type = [schema.type, 'null'];
158+
return converter(schema, options);
159+
}
160+
161+
// Valid JSON schema v4 formats
162+
var FORMATS = ['date-time', 'email', 'hostname', 'ipv4', 'ipv6', 'uri', 'uri-reference'];
163+
164+
function convertFormatInt32 (schema) {
165+
schema.minimum = MIN_INT_32;
166+
schema.maximum = MAX_INT_32;
167+
return schema;
168+
}
169+
170+
var MIN_INT_32 = 0 - Math.pow(2, 31);
171+
var MAX_INT_32 = Math.pow(2, 31) - 1;
172+
173+
function convertFormatInt64 (schema) {
174+
schema.minimum = MIN_INT_64;
175+
schema.maximum = MAX_INT_64;
176+
return schema;
177+
}
178+
179+
var MIN_INT_64 = 0 - Math.pow(2, 63);
180+
var MAX_INT_64 = Math.pow(2, 63) - 1;
181+
182+
function convertFormatFloat (schema) {
183+
schema.minimum = MIN_FLOAT;
184+
schema.maximum = MAX_FLOAT;
185+
return schema;
186+
}
187+
188+
var MIN_FLOAT = 0 - Math.pow(2, 128);
189+
var MAX_FLOAT = Math.pow(2, 128) - 1;
190+
191+
function convertFormatDouble (schema) {
192+
schema.minimum = MIN_DOUBLE;
193+
schema.maximum = MAX_DOUBLE;
194+
return schema;
195+
}
196+
197+
var MIN_DOUBLE = 0 - Number.MAX_VALUE;
198+
var MAX_DOUBLE = Number.MAX_VALUE;
199+
200+
function convertFormatDate (schema, options) {
201+
if (options.dateToDateTime === true) {
202+
schema.format = 'date-time';
155203
}
156204

157205
return schema;
158206
}
159207

208+
function convertFormatByte (schema) {
209+
schema.pattern = BYTE_PATTERN;
210+
return schema;
211+
}
212+
213+
// Matches base64 (RFC 4648)
214+
// Matches `standard` base64 not `base64url`. The specification does not
215+
// exclude it but current ongoing OpenAPI plans will distinguish btoh.
216+
var BYTE_PATTERN = '^[\\w\\d+\\/=]*$';
217+
218+
var formatConverters = {
219+
int32: convertFormatInt32,
220+
int64: convertFormatInt64,
221+
float: convertFormatFloat,
222+
double: convertFormatDouble,
223+
date: convertFormatDate,
224+
byte: convertFormatByte
225+
};
226+
160227
function convertPatternProperties(schema, handler) {
161228
schema.patternProperties = schema['x-patternProperties'];
162229
delete schema['x-patternProperties'];

0 commit comments

Comments
 (0)