mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-18 09:47:55 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
async function validateJSON(data, schema) {
|
||||
const {default: Ajv} = await import('https://cdn.jsdelivr.net/npm/ajv@8/dist/ajv.min.js')
|
||||
const ajv = new Ajv({allErrors:true, strict:false})
|
||||
const validate = ajv.compile(schema)
|
||||
const valid = validate(data)
|
||||
if (valid) return {valid:true, errors:[]}
|
||||
const errors = (validate.errors || []).map(e => {
|
||||
const path = (e.instancePath || e.dataPath || '') || (e.schemaPath || '')
|
||||
const msg = e.message || 'Invalid value'
|
||||
const params = e.params ? JSON.stringify(e.params) : ''
|
||||
return [path, msg, params].filter(Boolean).join(' - ')
|
||||
})
|
||||
return {valid:false, errors}
|
||||
const { default: Ajv } = await import('https://cdn.skypack.dev/ajv@8?min');
|
||||
const { default: addFormats } = await import('https://cdn.skypack.dev/ajv-formats@2?min');
|
||||
const ajv = new Ajv({ allErrors: true, strict: false });
|
||||
addFormats(ajv);
|
||||
const compile = s => {
|
||||
const c = ajv.compile(s);
|
||||
return d => ({ ok: c(d), errs: c.errors || [] });
|
||||
};
|
||||
const run = compile(schema);
|
||||
const r = run(data);
|
||||
if (r.ok) return { valid: true, errors: [] };
|
||||
const errors = r.errs.map(e => {
|
||||
const p = e.instancePath || e.dataPath || '';
|
||||
const loc = p || e.schemaPath || '';
|
||||
const msg = e.message || 'Invalid value';
|
||||
return loc ? loc + ' ' + msg : msg;
|
||||
});
|
||||
return { valid: false, errors };
|
||||
}
|
||||
export default validateJSON;
|
||||
Reference in New Issue
Block a user