forked from tokensoft/launchlists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.js
41 lines (34 loc) · 990 Bytes
/
validate.js
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
40
41
const Ajv = require("ajv");
const saleSchema = require("./sale.schema.json");
const fs = require( 'fs' );
const path = require( 'path' );
const ajv = new Ajv()
const validate = ajv.compile(saleSchema)
function validateSchema (filePath, data) {
const valid = validate(data)
console.warn(valid ? 'Validation passed' : 'Validation failed')
if (!valid) {
console.error(filePath, validate.errors);
throw new Error(validate.errors);
}
}
async function loadSales() {
const filePaths = [
'./dev/sales_index.json',
'./staging/sales_index.json',
'./prod/sales_index.json',
'./sale.example.json'
]
for( const filePath of filePaths ) {
console.log(`Checking ${filePath}`)
const content = fs.readFileSync(filePath);
let saleConfig
try {
saleConfig = JSON.parse(content);
} catch(e) {
console.error(`Sale config in ${filePath} is not valid JSON: ${e.message}`)
}
validateSchema(filePath, saleConfig);
}
}
loadSales()