mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-13 07:45:22 +00:00
24 lines
653 B
JavaScript
24 lines
653 B
JavaScript
const validateJSON = async (json, schema) => {
|
|
let ajv;
|
|
|
|
try {
|
|
const { default: Ajv } = await import('https://cdn.jsdelivr.net/npm/ajv@8.17.1/+esm');
|
|
ajv = new Ajv({ allErrors: true });
|
|
} catch (e) {
|
|
return { valid: false, errors: ["Failed to load validator: " + e.message] };
|
|
}
|
|
|
|
try {
|
|
const validate = ajv.compile(schema);
|
|
const valid = validate(json);
|
|
|
|
const errors = validate.errors?.map(
|
|
e => `${e.instancePath || 'root'} ${e.message}`.trim()
|
|
) ?? [];
|
|
|
|
return { valid, errors };
|
|
} catch (e) {
|
|
return { valid: false, errors: ["Invalid schema: " + e.message] };
|
|
}
|
|
};
|
|
export default validateJSON; |