react-jsonschema-form-validation
Version:
Simple form validation using JSON Schema and AJV
35 lines (30 loc) • 1.41 kB
JavaScript
/**
* @import { FormattedError } from '../Form/helpers'
* @import { ErrorMessagesMap } from '../Form/Context.types'
*/
/**
* Find the error message to display when a field is invalid.
*
* Resolution priority (highest first):
* 1. The corresponding error message passed to `<FieldError>` as a prop
* 2. The corresponding error message passed to `<Form>` as a prop
* 3. The `defaultMessage` passed to `<Form>` (catch-all)
* 4. The raw AJV message
*
* @param {FormattedError} fieldError
* @param {ErrorMessagesMap | undefined} errorMessages
* @returns {string | undefined}
*/
var getErrorMessage = function getErrorMessage(fieldError, errorMessages) {
var _process, _process$env;
// customMessage = the errorMessage provided by the <FieldError> as props
// or the message passed as props to the <Form> as props
var customErrorMessage = errorMessages && (errorMessages[fieldError.keyword] || errorMessages.defaultMessage);
var message = customErrorMessage ? customErrorMessage(fieldError) : fieldError.message;
/* istanbul ignore next */
if (typeof process !== 'undefined' && ((_process = process) === null || _process === void 0 ? void 0 : (_process$env = _process.env) === null || _process$env === void 0 ? void 0 : _process$env.REACT_APP_JFV_DEBUG) === 'true') {
message += " [#".concat(fieldError.keyword, "]");
}
return message;
};
export default getErrorMessage;