Files
lynchmark.com/tests/8_json_validator/outputs_gemini/gemini-3-pro-preview TEMP_0.8.js
T

14 lines
457 B
JavaScript

const validateJSON = async (json, schema) => {
try {
const { default: Ajv } = await import('https://esm.sh/ajv@8.12.0')
const validate = new Ajv({ allErrors: true }).compile(schema)
const valid = validate(json)
return {
valid,
errors: valid ? [] : validate.errors.map(e => `${e.instancePath} ${e.message}`.trim())
}
} catch (error) {
return { valid: false, errors: [error.message] }
}
}
export default validateJSON;