Skip to content

Commit 974e871

Browse files
mikunnPhil Sturgeon
authored andcommitted
Throw on invalid type
1 parent 3e06720 commit 974e871

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
const structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
22

3+
function InvalidTypeError(message) {
4+
this.name = 'InvalidTypeError';
5+
this.message = message;
6+
}
7+
8+
InvalidTypeError.prototype = new Error();
9+
310
function convert(schema, options) {
411
options = options || {};
512
options.cloneSchema = ! (options.cloneSchema === false);
@@ -47,6 +54,7 @@ function convertSchema(schema) {
4754

4855
}
4956

57+
validateType(schema.type);
5058
schema = convertTypes(schema);
5159

5260
if (typeof schema['patternProperties'] === 'object') {

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": "cats@email.com"
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)