Skip to content

Commit c85352a

Browse files
committed
Throw on invalid type
1 parent ede48f6 commit c85352a

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

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

test/invalid_types.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var test = require('tape')
2+
, join = require('path').join
3+
, fs = require('fs')
4+
, convert = require('../')
5+
;
6+
7+
test('invalid types', function(assert) {
8+
var schema, msg;
9+
10+
assert.plan(3);
11+
12+
schema = {
13+
type: 'dateTime'
14+
};
15+
16+
msg = 'dateTime is invalid type';
17+
assert.throws(function() { convert(schema); }, /InvalidTypeError/, msg);
18+
19+
schema = {
20+
type: 'foo'
21+
};
22+
23+
msg = 'foo is invalid type';
24+
assert.throws(function() { convert(schema); }, /InvalidTypeError/, msg);
25+
26+
schema = getSchema('schema-2-invalid-type.json');
27+
28+
msg = 'invalid type inside complex schema';
29+
assert.throws(function() { convert(schema); }, /InvalidTypeError.*invalidtype/, msg);
30+
});
31+
32+
function getSchema(file) {
33+
var path = join(__dirname, 'schemas', file);
34+
return JSON.parse(fs.readFileSync(path));
35+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"allOf": [
3+
{
4+
"anyOf": [
5+
{
6+
"type": "object",
7+
"properties": {
8+
"cats": {
9+
"type": "array",
10+
"items": {
11+
"type": "integer",
12+
"format": "int64",
13+
"example": [
14+
1
15+
]
16+
}
17+
}
18+
}
19+
},
20+
{
21+
"type": "object",
22+
"properties": {
23+
"dogs": {
24+
"type": "array",
25+
"items": {
26+
"type": "integer",
27+
"format": "int64",
28+
"example": [
29+
1
30+
]
31+
}
32+
}
33+
}
34+
},
35+
{
36+
"type": "object",
37+
"properties": {
38+
"bring_cats": {
39+
"type": "array",
40+
"items": {
41+
"allOf": [
42+
{
43+
"type": "object",
44+
"properties": {
45+
"email": {
46+
"type": "string",
47+
"example": "[email protected]"
48+
},
49+
"sms": {
50+
"type": "string",
51+
"nullable": true,
52+
"example": "+12345678"
53+
},
54+
"properties": {
55+
"type": "object",
56+
"additionalProperties": {
57+
"type": "invalidtype"
58+
},
59+
"example": {
60+
"name": "Wookie"
61+
}
62+
}
63+
}
64+
},
65+
{
66+
"required": [
67+
"email"
68+
]
69+
}
70+
]
71+
}
72+
}
73+
}
74+
}
75+
]
76+
},
77+
{
78+
"type": "object",
79+
"properties": {
80+
"playground": {
81+
"type": "object",
82+
"required": [
83+
"feeling",
84+
"child"
85+
],
86+
"properties": {
87+
"feeling": {
88+
"type": "string",
89+
"example": "Good feeling"
90+
},
91+
"child": {
92+
"type": "object",
93+
"required": [
94+
"name",
95+
"age"
96+
],
97+
"properties": {
98+
"name": {
99+
"type": "string",
100+
"example": "Steven"
101+
},
102+
"age": {
103+
"type": "integer",
104+
"example": 5
105+
}
106+
}
107+
},
108+
"toy": {
109+
"type": "object",
110+
"properties": {
111+
"breaks_easily": {
112+
"type": "boolean",
113+
"default": false
114+
},
115+
"color": {
116+
"type": "string",
117+
"description": "Color of the toy"
118+
},
119+
"type": {
120+
"type": "string",
121+
"enum": ["bucket", "shovel"],
122+
"description": "Toy type"
123+
}
124+
}
125+
}
126+
}
127+
}
128+
}
129+
}
130+
]
131+
}

0 commit comments

Comments
 (0)