Skip to content

Commit 9506749

Browse files
committedJul 11, 2020
jsonschema validator v1
1 parent a9ff1ba commit 9506749

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed
 

‎engines-schema.json

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "http://example.com/example.json",
4+
"type": "object",
5+
"title": "GuardRails Engines root schema",
6+
"description": "This jsonschema is to validate and standardize all engine outputs.",
7+
"required": [
8+
"engine",
9+
"language",
10+
"status",
11+
"executionTime",
12+
"issues",
13+
"process",
14+
"output"
15+
],
16+
"properties": {
17+
"engine": {
18+
"$id": "#/properties/engine",
19+
"type": "object",
20+
"title": "The engine schema",
21+
"description": "An explanation about the purpose of this instance.",
22+
"default": {},
23+
"examples": [{
24+
"name": "@guardrails/guardrails-engine-typescript-tslint",
25+
"version": "1.0.0"
26+
}],
27+
"required": ["name", "version"],
28+
"properties": {
29+
"name": {
30+
"$id": "#/properties/engine/properties/name",
31+
"type": "string",
32+
"pattern": "^@guardrails/guardrails-engine-.*$",
33+
"title": "The name schema",
34+
"description": "An explanation about the purpose of this instance.",
35+
"examples": [
36+
"@guardrails/guardrails-engine-typescript-tslint"
37+
]
38+
},
39+
"version": {
40+
"$id": "#/properties/engine/properties/version",
41+
"type": "string",
42+
"title": "The version schema",
43+
"description": "Version number of the engine"
44+
}
45+
}
46+
},
47+
"language": {
48+
"$id": "#/properties/language",
49+
"type": "string",
50+
"enum": ["c", "ruby", "php", "python", "solidity", "elixir", "rust", "terraform", "mobile", "go", "java", "javascript", "kubernetes", "general", "typescript"],
51+
"title": "The language schema",
52+
"description": "Main engine lanaguage"
53+
},
54+
"status": {
55+
"$id": "#/properties/status",
56+
"type": "string",
57+
"enum": ["success", "completed", "errror"],
58+
"title": "The status schema",
59+
"description": "An explanation about the purpose of this instance."
60+
},
61+
"executionTime": {
62+
"$id": "#/properties/executionTime",
63+
"type": "integer",
64+
"title": "The executionTime schema",
65+
"description": "Execution time of engine",
66+
"default": 0
67+
},
68+
"issues": {
69+
"$id": "#/properties/issues",
70+
"type": "integer",
71+
"title": "The issues schema",
72+
"description": "Number of issues reported",
73+
"default": 0
74+
},
75+
"process": {
76+
"$id": "#/properties/process",
77+
"type": "object",
78+
"title": "The process schema",
79+
"description": "An explanation about the purpose of this instance.",
80+
"required": ["name", "version"],
81+
"properties": {
82+
"name": {
83+
"$id": "#/properties/process/properties/name",
84+
"type": "string",
85+
"title": "The name schema",
86+
"description": "An explanation about the purpose of this instance."
87+
},
88+
"version": {
89+
"$id": "#/properties/process/properties/version",
90+
"type": "string",
91+
"title": "The version schema",
92+
"description": "An explanation about the purpose of this instance."
93+
}
94+
}
95+
},
96+
"output": {
97+
"$id": "#/properties/output",
98+
"type": "array",
99+
"title": "The output schema",
100+
"description": "An explanation about the purpose of this instance.",
101+
"items": {
102+
"allOf": [{
103+
"$id": "#/properties/output/items/allOf/0",
104+
"type": "object",
105+
"title": "The first allOf schema",
106+
"description": "An explanation about the purpose of this instance.",
107+
"required": ["type", "ruleId", "location", "metadata"],
108+
"if": {
109+
"properties": {
110+
"type": {
111+
"const": "sca"
112+
}
113+
}
114+
},
115+
"then": {
116+
"properties": {
117+
"metadata": {
118+
"required": ["title"]
119+
}
120+
}
121+
},
122+
"properties": {
123+
"type": {
124+
"$id": "#/properties/output/items/allOf/0/properties/type",
125+
"type": "string",
126+
"enum": ["sast", "sca"],
127+
"title": "The type schema",
128+
"description": "Type of engine"
129+
},
130+
"ruleId": {
131+
"$id": "#/properties/output/items/allOf/0/properties/ruleId",
132+
"type": "string",
133+
"title": "The ruleId schema",
134+
"description": "An explanation about the purpose of this instance."
135+
},
136+
"location": {
137+
"$id": "#/properties/output/items/allOf/0/properties/location",
138+
"type": "object",
139+
"title": "The location schema",
140+
"description": "Location details of the issue.",
141+
"required": ["path", "positions"],
142+
"properties": {
143+
"path": {
144+
"$id": "#/properties/output/items/allOf/0/properties/location/properties/path",
145+
"type": "string",
146+
"pattern": "^(?!\/opt\/mount).*$",
147+
"title": "The path schema",
148+
"description": "Path to the affected file. Path should not start with /opt/mount"
149+
},
150+
"positions": {
151+
"$id": "#/properties/output/items/allOf/0/properties/location/properties/positions",
152+
"type": "object",
153+
"title": "The positions schema",
154+
"description": "An explanation about the purpose of this instance.",
155+
"required": ["begin"],
156+
"properties": {
157+
"begin": {
158+
"$id": "#/properties/output/items/allOf/0/properties/location/properties/positions/properties/begin",
159+
"type": "object",
160+
"title": "The begin schema",
161+
"description": "An explanation about the purpose of this instance.",
162+
"required": ["line"],
163+
"properties": {
164+
"line": {
165+
"$id": "#/properties/output/items/allOf/0/properties/location/properties/positions/properties/begin/properties/line",
166+
"type": "integer",
167+
"title": "The line schema",
168+
"description": "An explanation about the purpose of this instance.",
169+
"default": 0
170+
}
171+
}
172+
},
173+
"end": {
174+
"$id": "#/properties/output/items/allOf/0/properties/location/properties/positions/properties/end",
175+
"type": "object",
176+
"title": "The end schema",
177+
"description": "An explanation about the purpose of this instance.",
178+
"required": ["line"],
179+
"properties": {
180+
"line": {
181+
"$id": "#/properties/output/items/allOf/0/properties/location/properties/positions/properties/begin/properties/line",
182+
"type": "integer",
183+
"title": "The line schema",
184+
"description": "An explanation about the purpose of this instance.",
185+
"default": 0
186+
}
187+
}
188+
}
189+
}
190+
}
191+
}
192+
},
193+
"metadata": {
194+
"$id": "#/properties/output/items/allOf/0/properties/metadata",
195+
"type": "object",
196+
"title": "The metadata schema",
197+
"description": "An explanation about the purpose of this instance.",
198+
"required": ["description"],
199+
"properties": {
200+
"title": {
201+
"$id": "#/properties/output/items/allOf/0/properties/metadata/properties/title",
202+
"type": "string",
203+
"title": "The title schema",
204+
"description": "An explanation about the purpose of this instance."
205+
},
206+
"description": {
207+
"$id": "#/properties/output/items/allOf/0/properties/metadata/properties/description",
208+
"type": "string",
209+
"title": "The description schema",
210+
"description": "An explanation about the purpose of this instance."
211+
},
212+
"lineContent": {
213+
"$id": "#/properties/output/items/allOf/0/properties/metadata/properties/lineContent",
214+
"type": "string",
215+
"title": "The lineContent schema",
216+
"description": "An explanation about the purpose of this instance."
217+
}
218+
}
219+
}
220+
}
221+
}],
222+
"$id": "#/properties/output/items"
223+
}
224+
}
225+
}
226+
}

‎jsonschema.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { validator } = require("@exodus/schemasafe");
2+
const fs = require("fs");
3+
const grschema = require("./engines-schema.json");
4+
5+
6+
// pass the external schemas as an option
7+
const validate = validator(grschema, { includeErrors: true, allErrors: true });
8+
9+
function readFromStdin() {
10+
return readFromFile("/dev/stdin");
11+
}
12+
13+
function readFromFile(filePath) {
14+
try {
15+
let data = fs.readFileSync(filePath).toString();
16+
return JSON.parse(data);
17+
} catch (err) {
18+
console.log(err.message);
19+
process.exit(1);
20+
}
21+
}
22+
23+
reportData = readFromStdin();
24+
25+
validate(reportData);
26+
console.log(validate.errors);

‎package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15+
"@exodus/schemasafe": "^1.0.0-beta.1",
1516
"@semantic-release/git": "^5.0.0",
1617
"commander": "^2.17.1",
1718
"joi": "^13.6.0",

0 commit comments

Comments
 (0)
Failed to load comments.