UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

23 lines 1.17 kB
import * as React from 'react'; import { useTranslate } from "../../i18n/index.js"; const ValidationErrorSpecialFormatPrefix = '@@react-admin@@'; export const ValidationError = (props) => { const { error } = props; let errorMessage = error; const translate = useTranslate(); // react-hook-form expects errors to be plain strings but our validators can return objects // that have message and args. // To avoid double translation for users that validate with a schema instead of our validators // we use a special format for our validators errors. // The useInput hook handle the special formatting if (typeof error === 'string' && error.startsWith(ValidationErrorSpecialFormatPrefix)) { errorMessage = JSON.parse(error.substring(ValidationErrorSpecialFormatPrefix.length)); } if (errorMessage.message) { const { message, args } = errorMessage; return React.createElement(React.Fragment, null, translate(message, { _: message, ...args })); } return React.createElement(React.Fragment, null, translate(errorMessage, { _: errorMessage })); }; //# sourceMappingURL=ValidationError.js.map