-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.js
More file actions
66 lines (54 loc) · 1.71 KB
/
Copy pathvalidate.js
File metadata and controls
66 lines (54 loc) · 1.71 KB
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
#!/usr/bin/env node
/**
* Custom validation script for Project JSON Schema
* Properly integrates ajv with ajv-formats for URI and date-time validation
*/
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const fs = require('fs');
const path = require('path');
// Initialize AJV with formats
const ajv = new Ajv({ allErrors: true, verbose: true });
addFormats(ajv);
// Load schema
const schemaPath = path.join(__dirname, 'schema/project.schema.json');
const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
const validate = ajv.compile(schema);
// Get files to validate from command line arguments
const filesToValidate = process.argv.slice(2);
if (filesToValidate.length === 0) {
console.error('Usage: node validate.js <file1.json> [file2.json] ...');
process.exit(1);
}
let hasErrors = false;
// Validate each file
filesToValidate.forEach(filePath => {
try {
console.log(`Validating ${filePath}...`);
if (!fs.existsSync(filePath)) {
console.error(`❌ File not found: ${filePath}`);
hasErrors = true;
return;
}
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const isValid = validate(data);
if (isValid) {
console.log(`✅ ${filePath} is valid`);
} else {
console.error(`❌ ${filePath} is invalid:`);
validate.errors.forEach(error => {
console.error(` - ${error.instancePath || 'root'}: ${error.message}`);
});
hasErrors = true;
}
} catch (error) {
console.error(`❌ Error validating ${filePath}: ${error.message}`);
hasErrors = true;
}
});
if (hasErrors) {
console.error('\n❌ Validation failed');
process.exit(1);
} else {
console.log('\n✅ All files are valid');
}