@canard/schema-form-ajv8-plugin
Version:
AJV 8.x validator plugin for @canard/schema-form providing JSON Schema validation with latest Draft 2020-12 and Draft 2019-09 support
36 lines (33 loc) • 1.14 kB
JavaScript
;
const JSON_POINTER_SEPARATOR = '/';
const transformErrors = (errors) => {
if (!Array.isArray(errors))
return [];
const result = new Array(errors.length);
for (let i = 0, l = errors.length; i < l; i++) {
const ajvError = errors[i];
result[i] = {
dataPath: transformDataPath(ajvError),
schemaPath: ajvError.schemaPath,
keyword: ajvError.keyword,
message: ajvError.message,
details: ajvError.params,
source: ajvError,
};
}
return result;
};
const transformDataPath = (error) => {
const instancePath = error.instancePath;
const missingProperty = error.params?.missingProperty;
const hasMissingProperty = error.keyword === 'required' && missingProperty;
if (instancePath)
return hasMissingProperty
? instancePath + JSON_POINTER_SEPARATOR + missingProperty
: instancePath;
else
return hasMissingProperty
? JSON_POINTER_SEPARATOR + missingProperty
: JSON_POINTER_SEPARATOR;
};
exports.transformErrors = transformErrors;