import { ErrorObject } from 'ajv' ; import { UISchemaElement } from '../models' ; import { getControlPath } from '../reducers' ; import { formatErrorMessage } from '../util' ; import { i18nJsonSchema , ErrorTranslator , Translator } from './i18nTypes' ; export const getI18nKeyPrefixBySchema = ( schema , uischema ) : string | undefined => { return uischema ?. options ?. i18n ?? schema ?. i18n ?? undefined ; } ; /**
 * Transforms a given path to a prefix which can be used for i18n keys.
 * Returns 'root' for empty paths and removes array indices
 */ export const transformPathToI18nPrefix = ( path ) => { return ( path ?. split ( '.' ) . filter ( segment => ! /^\d+$/ . test ( segment ) ) . join ( '.' ) || 'root' ) ; } ; export const getI18nKeyPrefix = ( schema , uischema , path ) : string | undefined => { return ( getI18nKeyPrefixBySchema ( schema , uischema ) ?? transformPathToI18nPrefix ( path ) ) ; } ; export const getI18nKey = ( schema , uischema , path , key ) : string | undefined => { return `${ getI18nKeyPrefix ( schema , uischema , path ) }.${ key }` ; } ; export const defaultTranslator = ( _id , defaultMessage ) => defaultMessage ; export const defaultErrorTranslator = ( error , t , uischema ) => { const i18nKey = getI18nKey ( error . parentSchema , uischema , getControlPath ( error ) , `error.${ error . keyword }` ) ; const specializedKeywordMessage = t ( i18nKey , undefined ) ; if ( specializedKeywordMessage !== undefined ) { return specializedKeywordMessage ; } const genericKeywordMessage = t ( `error.${ error . keyword }` , undefined ) ; if ( genericKeywordMessage !== undefined ) { return genericKeywordMessage ; } const messageCustomization = t ( error . message , undefined ) ; if ( messageCustomization !== undefined ) { return messageCustomization ; } if ( error . keyword === 'required' && error . message ?. startsWith ( 'must have required property' ) ) { return t ( 'is a required property' , 'is a required property' ) ; } return error . message ; } ; /**
 * Returns the determined error message for the given errors.
 * All errors must correspond to the given schema, uischema or path.
 */ export const getCombinedErrorMessage = ( errors , et , t , schema ? , uischema ? , path ? ) => { if ( errors . length > 0 && t ) { const customErrorKey = getI18nKey ( schema , uischema , path , 'error.custom' ) ; const specializedErrorMessage = t ( customErrorKey , undefined ) ; if ( specializedErrorMessage !== undefined ) { return specializedErrorMessage ; } } return formatErrorMessage ( errors . map ( error => et ( error , t , uischema ) ) ) ; } ;