Skip to content

Commit 7d903ed

Browse files
authored
Merge pull request #7 from mikunn/6-throw-on-invalid-type
Throw on invalid type
2 parents ede48f6 + fc56f03 commit 7d903ed

17 files changed

+216
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ If set to `true` and `x-patternProperties` property is present, change `x-patter
120120

121121
Provide a function to handle pattern properties and set `supportPatternProperties` to take effect. The function takes the schema where `x-patternProperties` is defined on the root level. At this point `x-patternProperties` is changed to `patternProperties`. It must return the modified schema.
122122

123-
If the handler is not provided, the default handler is used. If `additionalProperties` is set and is an object, the default handler sets it to false if the `additionaProperties` object has deep equality with a pattern object inside `patternProperties`. This is because we might want to define `additionalProperties` in OpenAPI spec file, but want to validate against a pattern. The pattern would turn out to be useless if `additionalProperties` of the same structure were allowed. Create you own handler to override this functionality.
123+
If the handler is not provided, the default handler is used. If `additionalProperties` is set and is an object, the default handler sets it to false if the `additionalProperties` object has deep equality with a pattern object inside `patternProperties`. This is because we might want to define `additionalProperties` in OpenAPI spec file, but want to validate against a pattern. The pattern would turn out to be useless if `additionalProperties` of the same structure were allowed. Create you own handler to override this functionality.
124124

125-
See `test/pattern_properties.js` for examples how this works.
125+
See `test/pattern_properties.test.js` for examples how this works.
126126

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ var deepEqual = require('deep-equal');
22

33
module.exports = convert;
44

5+
function InvalidTypeError(message) {
6+
this.name = 'InvalidTypeError';
7+
this.message = message;
8+
}
9+
10+
InvalidTypeError.prototype = new Error();
11+
512
function convert(schema, options) {
613
var notSupported = [
714
'nullable', 'discriminator', 'readOnly',
@@ -78,6 +85,7 @@ function convertSchema(schema, options) {
7885

7986
}
8087

88+
validateType(schema.type);
8189
schema = convertTypes(schema, options);
8290

8391
if (typeof schema['x-patternProperties'] === 'object'
@@ -119,6 +127,14 @@ function convertProperties(properties, options) {
119127
return props;
120128
}
121129

130+
function validateType(type) {
131+
var validTypes = ['integer', 'number', 'string', 'boolean', 'object', 'array'];
132+
133+
if (validTypes.indexOf(type) < 0 && type !== undefined) {
134+
throw new InvalidTypeError('Type "' + type + '" is not a valid type');
135+
}
136+
}
137+
122138
function convertTypes(schema, options) {
123139
var toDateTime = options.dateToDateTime;
124140

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Converts OpenAPI Schema Object to JSON Schema",
55
"main": "index.js",
66
"scripts": {
7-
"test": "./node_modules/.bin/tape test/**/*.js | ./node_modules/.bin/tap-spec"
7+
"test": "./node_modules/.bin/tape test/**/*.test.js | ./node_modules/.bin/tap-spec"
88
},
99
"repository": "github:mikunn/openapi-schema-to-json-schema",
1010
"author": "Mika Kunnas",
File renamed without changes.
File renamed without changes.

test/complex_schemas.js renamed to test/complex_schemas.test.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
var fs = require('fs')
2-
, join = require('path').join
3-
, test = require('tape')
1+
var test = require('tape')
2+
, getSchema = require('./helpers').getSchema
43
, convert = require('../')
54
;
65

@@ -35,7 +34,3 @@ test('converting complex schema in place', function(assert) {
3534
assert.deepEqual(result, expected, 'converted');
3635
});
3736

38-
function getSchema(file) {
39-
var path = join(__dirname, 'schemas', file);
40-
return JSON.parse(fs.readFileSync(path));
41-
}

test/helpers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var fs = require('fs')
2+
, join = require('path').join
3+
;
4+
5+
module.exports = {
6+
getSchema: getSchema
7+
};
8+
9+
function getSchema(file) {
10+
var path = join(__dirname, 'schemas', file);
11+
return JSON.parse(fs.readFileSync(path));
12+
}

test/invalid_types.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var test = require('tape')
2+
, getSchema = require('./helpers').getSchema
3+
, convert = require('../')
4+
;
5+
6+
test('invalid types', function(assert) {
7+
var schema, msg;
8+
9+
assert.plan(3);
10+
11+
schema = {
12+
type: 'dateTime'
13+
};
14+
15+
msg = 'dateTime is invalid type';
16+
assert.throws(function() { convert(schema); }, /InvalidTypeError/, msg);
17+
18+
schema = {
19+
type: 'foo'
20+
};
21+
22+
msg = 'foo is invalid type';
23+
assert.throws(function() { convert(schema); }, /InvalidTypeError/, msg);
24+
25+
schema = getSchema('schema-2-invalid-type.json');
26+
27+
msg = 'invalid type inside complex schema';
28+
assert.throws(function() { convert(schema); }, /InvalidTypeError.*invalidtype/, msg);
29+
});
30+
31+
test('valid types', function(assert) {
32+
var types = ['integer', 'number', 'string', 'boolean', 'object', 'array'];
33+
34+
assert.plan(types.length);
35+
36+
types.forEach(function(type) {
37+
var schema, result, expected;
38+
39+
schema = {
40+
type: type
41+
};
42+
43+
result = convert(schema);
44+
45+
expected = {
46+
$schema: 'http://json-schema.org/draft-04/schema#',
47+
type: type,
48+
};
49+
50+
assert.deepEqual(result, expected, type + ' ok');
51+
});
52+
});
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)