UNPKG

@sjsf/ajv8-validator

Version:

The ajv-8 based validator for svelte-jsonschema-form

85 lines (84 loc) 3.08 kB
import { encodePseudoElement, getRootUiSchemaTitleByPath, } from "@sjsf/form"; import { pathFromLocation } from "@sjsf/form/core"; import { Ajv, } from "ajv"; function createInstancePath({ params: { missingProperty, propertyName: propertyNameParam }, propertyName = propertyNameParam, }, path) { let id = path; id = missingProperty !== undefined ? path.concat(missingProperty) : id; id = propertyName !== undefined ? path.concat(propertyName, encodePseudoElement("key-input")) : id; return id; } function errorObjectToMessage({ params: { missingProperty }, parentSchema, message }, getPropertyTitle) { if (!message) { return ""; } if (missingProperty === undefined) { return message; } const propertyTitle = getPropertyTitle(missingProperty, parentSchema); if (propertyTitle === undefined) { return message; } return message.replace(missingProperty, propertyTitle); } export function createFormErrorsTransformer({ uiSchema = {}, }) { return (errors, data) => { return { value: data, errors: errors.map((error) => { const path = pathFromLocation(error.instancePath, data); return { path: createInstancePath(error, path), message: errorObjectToMessage(error, (missingProperty, parentSchema) => { const uiSchemaTitle = getRootUiSchemaTitleByPath(uiSchema, path.concat(missingProperty)); if (uiSchemaTitle !== undefined) { return uiSchemaTitle; } const prop = parentSchema?.properties?.[missingProperty]; if (typeof prop === "object") { return prop.title; } return undefined; }), }; }), }; }; } function isRootFieldError(error) { return error.instancePath === ""; } function isRootAndNonTypeError(error) { return isRootFieldError(error) && error.keyword !== "type"; } export function createFieldErrorsTransformer(config) { return (errors) => errors .filter(config.required ? isRootFieldError : isRootAndNonTypeError) .map((error) => errorObjectToMessage(error, () => config.title)); } export function validateAndTransformErrors(validate, data, transformData, transformErrors) { validate(data); const errors = validate.errors; validate.errors = null; if (!errors) { return transformData(data); } return transformErrors(errors, data); } export async function validateAndTransformErrorsAsync(validate, data, transformInput, transformErrors) { try { await validate(data); } catch (e) { if (!(e instanceof Ajv.ValidationError)) { throw e; } return transformErrors(e.errors, data); } finally { validate.errors = null; } return transformInput(data); }