-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate-context.mjs
39 lines (35 loc) · 1.18 KB
/
validate-context.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import fetch from "node-fetch"
import Ajv from "ajv"
import { readFileSync } from "fs"
const ajv = new Ajv({ allowUnionTypes: true })
const resource = JSON.parse(readFileSync("./src/api/resource.schema.json"))
const resourceValidator = ajv.compile(resource)
const context = JSON.parse(readFileSync("./src/api/context.schema.json"))
const contextValidator = ajv.compile(context)
function validateContext(context) {
const valid = contextValidator(resource)
if (valid) {
console.log("Context successfully validated!")
} else console.error(contextValidator.errors)
}
function validateResource(resource) {
const valid = resourceValidator(resource)
if (valid) {
console.log("Resource successfully validated!")
validateContext(resource.Resource.data.data)
} else console.error(resourceValidator.errors)
}
if (process.argv.length >= 3) {
const url = process.argv[2]
fetch(url, {
headers: {
Accept: "application/json",
},
})
.then((resp) => resp.json())
.then(validateResource)
.catch((e) => console.error(e.message))
} else {
console.log("validate-context <context-url>")
process.exit(1)
}