@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
75 lines (74 loc) • 3.55 kB
TypeScript
import ajvInstance, { ErrorObject } from 'ajv/dist/2020';
import { JsonObject } from './json-pointer';
import { DefaultErrorMessages, Path } from '../types';
import { FormError } from './FormError';
import type Ajv from 'ajv/dist/2020';
import type { FormsTranslation } from '../hooks/useTranslation';
export type AjvInstance = typeof ajvInstance;
export { ajvInstance, Ajv };
/**
* Creates an instance of Ajv (Another JSON Schema Validator) with optional custom instance.
* If no instance is provided, a new instance of Ajv is created with the specified options.
* The created Ajv instance is enhanced with custom error handling.
*
* @param instance - Optional custom instance of Ajv.
* @returns The created or provided instance of Ajv.
*/
export declare function makeAjvInstance(instance?: Ajv): ajvInstance;
/**
* Returns the instance path of the given Ajv error.
* If the error is of type 'required', it is considered an object error and the missing property is shown under the relevant field.
* If the error is of type 'errorMessage', it is a wrapped error and the instance path is found from the original error to avoid issues like required-errors pointing at the parent object.
* @param ajvError - The Ajv error object.
* @returns The instance path of the error.
*/
export declare function getInstancePath(ajvError: ErrorObject): Path;
/**
* Retrieves the validation rule from an AJV error object.
* If the error object has an 'errorMessage' keyword, it unwraps the original error
* to avoid issues like required-errors pointing at the parent object.
* @param ajvError - The AJV error object.
* @returns The validation rule.
*/
export declare function getValidationRule(ajvError: ErrorObject): string;
/**
* Retrieves the message values from an AJV error object.
* @param ajvError The AJV error object.
* @returns The message values extracted from the error object.
*/
export declare function getMessageValues(ajvError: ErrorObject): FormError['messageValues'];
/**
* Overwrite the internal translation messages with given messaged that uses the Ajv keywords.
*
* @deprecated – can be removed in v11
*/
export declare function overwriteErrorMessagesWithGivenAjvKeys(messages: DefaultErrorMessages): DefaultErrorMessages;
/**
* Extend the error messages with relevant translation messages.
*/
export declare function extendErrorMessagesWithTranslationMessages(messages: DefaultErrorMessages, translation: FormsTranslation): DefaultErrorMessages;
/**
* Get the translation key from the Ajv validation rule
*/
export declare function getTranslationKeyFromValidationRule(validationRule: string): string;
/**
* Converts an AJV error object to a FormError object.
*
* @param ajvError - The AJV error object to convert.
* @returns The converted FormError object.
*/
export declare function ajvErrorToFormError(ajvError: ErrorObject): FormError;
/**
* Converts an array of Ajv errors to a single FormError.
* @param errors - An array of Ajv errors.
* @returns A single FormError or undefined if there are no errors.
*/
export declare function ajvErrorsToOneFormError(errors?: ErrorObject[] | null, value?: unknown): FormError | undefined;
/**
* Converts AJV validation errors to form errors.
*
* @param errors - The array of AJV validation errors.
* @param data - The data object being validated.
* @returns The converted form errors as a record of path and form error pairs.
*/
export declare const ajvErrorsToFormErrors: (errors?: ErrorObject[] | null, data?: JsonObject) => Record<string, FormError>;