-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalidate-jsonc.js
92 lines (84 loc) · 3.21 KB
/
validate-jsonc.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Uses https://www.npmjs.com/package/jsonc-parser
// Generated in a conversation with ChatGPT, https://chat.openai.com/share/e2c56ea4-7069-4916-ad39-afa1200ab555
// node validate-jsonc.js remote-config.jsonc
// It does not seem to capture flagrant json errors
// but does catch schema violations, so that's a step forward
const fs = require('fs');
const jsoncParser = require('jsonc-parser');
const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true }); // Enabling detailed errors for more informative validation messages
function parseJsoncFile(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf-8');
return jsoncParser.parse(fileContent);
} catch (error) {
console.error(`Error parsing JSONC file (${filePath}):`, error.message);
process.exit(1);
}
}
function validateJson(data, schema, filePath) {
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) {
console.error(`Validation errors in file (${filePath}):`, validate.errors);
return false;
}
return true;
}
function validateJsoncFile(filePath, schema) {
const jsonData = parseJsoncFile(filePath);
const isValid = validateJson(jsonData, schema, filePath);
if (isValid) {
console.log(`JSONC file (${filePath}) is valid.`);
} else {
console.error(`JSONC file (${filePath}) is invalid.`);
}
}
if (process.argv.length < 3) {
console.log('Usage: node validateJsonc.js <path/to/your/file.jsonc>');
process.exit(1);
}
const filePath = process.argv[2]; // Taking the filename from the command line argument
// Define the schema based on the structure provided earlier
const schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"update-interval": {
"type": "number",
"description": "Update interval of the remote config in hours."
},
"messages": {
"type": "object",
"properties": {
"notifications": {
"type": "object",
"properties": {
"interval": {
"type": "number",
"description": "Interval for showing notifications."
},
"infos": {
"type": "array",
"items": {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "Information message to be displayed."
}
},
"required": ["message"]
},
"description": "Array of info messages."
}
},
"required": ["interval",]
}
},
"description": "Messages shown to the user."
}
},
"required": ["update-interval", "messages"]
};
validateJsoncFile(filePath, schema);