Skip to content

Commit 45a5c78

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

File tree

6 files changed

+295
-1
lines changed

6 files changed

+295
-1
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: validate-examples
2+
3+
# Author: @MikeRalphson
4+
# Issue: https://github.com/OAI/OpenAPI-Specification/issues/1739
5+
6+
#
7+
# This workflow validates files in the examples/v2 and /v3 directories
8+
#
9+
10+
# run this on push to any branch and creation of pull-requests
11+
on: [push, pull_request]
12+
13+
jobs:
14+
validate-examples:
15+
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- uses: actions/checkout@v1 # checkout repo content
20+
- uses: actions/setup-node@v1 # setup Node.js
21+
with:
22+
node-version: '12.x'
23+
- name: Install dependencies
24+
run: npm i
25+
- name: Validate examples
26+
run: node scripts/validateExamples/index.js
27+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: validate-schemas
2+
3+
# Author: @MikeRalphson
4+
# Issue: https://github.com/OAI/OpenAPI-Specification/issues/1739
5+
6+
#
7+
# This workflow validates files in the schemas directory against their
8+
# appropriate meta-schemas
9+
#
10+
11+
# run this on push to any branch and creation of pull-requests
12+
on: [push, pull_request]
13+
14+
jobs:
15+
mdv:
16+
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- uses: actions/checkout@v1 # checkout repo content
21+
- uses: actions/setup-node@v1 # setup Node.js
22+
with:
23+
node-version: '12.x'
24+
- name: Install dependencies
25+
run: npm i
26+
- name: Validate schemas
27+
run: |
28+
node scripts/validateSchema/index.js schemas/v1.2/apiDeclaration.json schemas/jsonSchema/draft-04/metaschema.json
29+
node scripts/validateSchema/index.js schemas/v2.0/schema.json schemas/jsonSchema/draft-04/metaschema.json
30+
node scripts/validateSchema/index.js schemas/v3.0/schema.yaml schemas/jsonSchema/draft-04/metaschema.json
31+

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+
"jsonschema": "^1.2.4",
23+
"mdv": "^1.0.7",
24+
"node-readfiles": "^0.2.0",
25+
"yaml": "^1.7.2"
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

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

scripts/validateSchema/index.js

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

0 commit comments

Comments
 (0)