forked from awesome-opencode/awesome-opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
74 lines (62 loc) · 1.91 KB
/
Copy pathvalidation.js
File metadata and controls
74 lines (62 loc) · 1.91 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
67
68
69
70
71
72
73
74
/**
* Schema validation utilities for awesome-opencode
*/
const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
// Initialize AJV with all errors option
const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
// Load schema lazily (on first use)
let validateFn = null;
/**
* Get the compiled validation function
* @returns {Function} AJV validate function
*/
function getValidator() {
if (!validateFn) {
const schemaPath = path.join(__dirname, '../../data/schema.json');
const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
validateFn = ajv.compile(schema);
}
return validateFn;
}
/**
* Validate an entry against the schema
* @param {object} data - Parsed YAML data
* @param {string} filePath - File path for error messages
* @returns {object} { valid: boolean, errors: array|null }
*/
function validateEntry(data, filePath) {
const validate = getValidator();
// Remove internal metadata fields before validation
const cleanData = { ...data };
delete cleanData._filePath;
delete cleanData._fileName;
const valid = validate(cleanData);
if (!valid) {
const errors = validate.errors.map(err => ({
path: err.instancePath || '/',
message: err.message,
keyword: err.keyword,
params: err.params
}));
return { valid: false, errors, filePath };
}
return { valid: true, errors: null, filePath };
}
/**
* Format validation errors for display
* @param {object} result - Result from validateEntry
* @returns {string} Formatted error message
*/
function formatValidationErrors(result) {
if (result.valid) return '';
const lines = [`Validation failed for ${result.filePath}:`];
for (const err of result.errors) {
lines.push(` - ${err.path}: ${err.message}`);
}
return lines.join('\n');
}
module.exports = { validateEntry, formatValidationErrors, getValidator };