Skip to content

Commit 4787ae6

Browse files
committed
build: reinstate validation of schemas and examples, fixes #1739
1 parent 52b5861 commit 4787ae6

File tree

5 files changed

+234
-1
lines changed

5 files changed

+234
-1
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ node_js:
33
- "lts/*"
44
script:
55
- node node_modules/mdv/mdv versions/3.*.md
6+
- node scripts/validateSchema/index.js schemas/v1.2/apiDeclaration.json schemas/jsonSchema/draft-04/metaschema.json
7+
- node scripts/validateSchema/index.js schemas/v2.0/schema.json schemas/jsonSchema/draft-04/metaschema.json
8+
- node scripts/validateSchema/index.js schemas/v3.0/schema.yaml schemas/jsonSchema/draft-04/metaschema.json
9+
- node scripts/validateExamples/index.js

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
],
2020
"dependencies": {},
2121
"devDependencies": {
22-
"mdv": "^1.0.7"
22+
"js-yaml": "^3.13.1",
23+
"jsonschema": "^1.2.4",
24+
"mdv": "^1.0.7",
25+
"node-readfiles": "^0.2.0"
2326
},
2427
"keywords": [
2528
"OpenAPI",
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"id": "http://json-schema.org/draft-04/schema#",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"description": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"positiveInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"positiveIntegerDefault0": {
16+
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
17+
},
18+
"simpleTypes": {
19+
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
20+
},
21+
"stringArray": {
22+
"type": "array",
23+
"items": { "type": "string" },
24+
"minItems": 1,
25+
"uniqueItems": true
26+
}
27+
},
28+
"type": "object",
29+
"properties": {
30+
"id": {
31+
"type": "string"
32+
},
33+
"$schema": {
34+
"type": "string"
35+
},
36+
"title": {
37+
"type": "string"
38+
},
39+
"description": {
40+
"type": "string"
41+
},
42+
"default": {},
43+
"multipleOf": {
44+
"type": "number",
45+
"minimum": 0,
46+
"exclusiveMinimum": true
47+
},
48+
"maximum": {
49+
"type": "number"
50+
},
51+
"exclusiveMaximum": {
52+
"type": "boolean",
53+
"default": false
54+
},
55+
"minimum": {
56+
"type": "number"
57+
},
58+
"exclusiveMinimum": {
59+
"type": "boolean",
60+
"default": false
61+
},
62+
"maxLength": { "$ref": "#/definitions/positiveInteger" },
63+
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
64+
"pattern": {
65+
"type": "string",
66+
"format": "regex"
67+
},
68+
"additionalItems": {
69+
"anyOf": [
70+
{ "type": "boolean" },
71+
{ "$ref": "#" }
72+
],
73+
"default": {}
74+
},
75+
"items": {
76+
"anyOf": [
77+
{ "$ref": "#" },
78+
{ "$ref": "#/definitions/schemaArray" }
79+
],
80+
"default": {}
81+
},
82+
"maxItems": { "$ref": "#/definitions/positiveInteger" },
83+
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
84+
"uniqueItems": {
85+
"type": "boolean",
86+
"default": false
87+
},
88+
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
89+
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
90+
"required": { "$ref": "#/definitions/stringArray" },
91+
"additionalProperties": {
92+
"anyOf": [
93+
{ "type": "boolean" },
94+
{ "$ref": "#" }
95+
],
96+
"default": {}
97+
},
98+
"definitions": {
99+
"type": "object",
100+
"additionalProperties": { "$ref": "#" },
101+
"default": {}
102+
},
103+
"properties": {
104+
"type": "object",
105+
"additionalProperties": { "$ref": "#" },
106+
"default": {}
107+
},
108+
"patternProperties": {
109+
"type": "object",
110+
"additionalProperties": { "$ref": "#" },
111+
"default": {}
112+
},
113+
"dependencies": {
114+
"type": "object",
115+
"additionalProperties": {
116+
"anyOf": [
117+
{ "$ref": "#" },
118+
{ "$ref": "#/definitions/stringArray" }
119+
]
120+
}
121+
},
122+
"enum": {
123+
"type": "array",
124+
"minItems": 1,
125+
"uniqueItems": true
126+
},
127+
"type": {
128+
"anyOf": [
129+
{ "$ref": "#/definitions/simpleTypes" },
130+
{
131+
"type": "array",
132+
"items": { "$ref": "#/definitions/simpleTypes" },
133+
"minItems": 1,
134+
"uniqueItems": true
135+
}
136+
]
137+
},
138+
"format": { "type": "string" },
139+
"allOf": { "$ref": "#/definitions/schemaArray" },
140+
"anyOf": { "$ref": "#/definitions/schemaArray" },
141+
"oneOf": { "$ref": "#/definitions/schemaArray" },
142+
"not": { "$ref": "#" }
143+
},
144+
"dependencies": {
145+
"exclusiveMaximum": [ "maximum" ],
146+
"exclusiveMinimum": [ "minimum" ]
147+
},
148+
"default": {}
149+
}

scripts/validateExamples/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const fs = require('fs');
6+
const util = require('util');
7+
8+
const yaml = require('js-yaml');
9+
const rf = require('node-readfiles');
10+
const jsonschema = require('jsonschema').Validator;
11+
const options = { };
12+
const validator = new jsonschema(options);
13+
14+
const schema = {};
15+
schema["v2.0"] = yaml.safeLoad(fs.readFileSync('./schemas/v2.0/schema.json','utf8'),{json:true});
16+
schema.draft4 = yaml.safeLoad(fs.readFileSync('./schemas/jsonSchema/draft-04/metaschema.json','utf8'),{json:true});
17+
schema["v3.0"] = yaml.safeLoad(fs.readFileSync('./schemas/v3.0/schema.yaml','utf8'),{json:true});
18+
19+
validator.addSchema(schema.draft4);
20+
21+
async function main(path,schema,propName) {
22+
return new Promise(async function(resolve,reject){
23+
let files = await rf(path, { readContents: false, filenameFormat: rf.FULL_PATH });
24+
files = files.sort();
25+
for (let file of files) {
26+
const contentStr = fs.readFileSync(file,'utf8');
27+
const contentObj = yaml.safeLoad(contentStr,{json:true});
28+
if (contentObj[propName]) {
29+
console.log('Validating',file);
30+
try {
31+
const result = await validator.validate(contentObj,schema);
32+
if (result.errors && result.errors.length) {
33+
process.exitCode = 1;
34+
console.warn(file,util.inspect(result.errors));
35+
}
36+
}
37+
catch (ex) {
38+
process.exitCode = 1;
39+
console.warn(file,ex.message);
40+
}
41+
}
42+
}
43+
resolve(files);
44+
});
45+
}
46+
47+
async function validateExamples(){
48+
await main('./examples/v2.0/',schema["v2.0"],'swagger');
49+
await main('./examples/v3.0/',schema["v3.0"],'openapi');
50+
}
51+
52+
validateExamples();

scripts/validateSchema/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const util = require('util');
5+
6+
const yaml = require('js-yaml');
7+
const jsonschema = require('jsonschema').Validator;
8+
const options = { base: process.argv[2] };
9+
const validator = new jsonschema(options);
10+
11+
const schema = yaml.safeLoad(fs.readFileSync(process.argv[2],'utf8'),{json:true});
12+
const metaSchema = yaml.safeLoad(fs.readFileSync(process.argv[3],'utf8'),{json:true});
13+
14+
console.log('Checking',process.argv[2]);
15+
16+
const result = validator.validate(schema, metaSchema);
17+
18+
if (result.errors.length) {
19+
console.warn(util.inspect(result.errors));
20+
process.exit(1);
21+
}
22+
else {
23+
console.log('OK');
24+
}
25+

0 commit comments

Comments
 (0)