-
-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ajv resolver not using custom error messages on required errors #615
Comments
I'm unsure if we can resolve this issue. When you configure a custom error message using AJV (ajv-error) for the required property, AJV doesn't substitute the default error message with your customized one. Instead, it introduces a new error at the top level. After delving into this, I haven't discovered a way to substitute the default error message with the customized version. If you're aware of a solution, kindly share it, and I'll attempt to update the resolver accordingly. This CodeSandbox shows a basic AJV validation with a custom error message The result from the validation: [
{
"instancePath": "",
"schemaPath": "#/errorMessage",
"keyword": "errorMessage",
"params": {
"errors": [
{
"instancePath": "",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "username"
},
"message": "must have required property 'username'",
"emUsed": true
}
]
},
"message": "username is required" // ⚠️ we can't associate this error message with the error
}
] |
@jorisre Can you explain this more to me? I'm trying to fix it |
Me too |
I just encountered this issue, not being able to display a custom error message for a required property. What I don't get is that ajvResolver already handles the case of a required error that is bind to the root and not to the path of the field.
This piece of code rebuilds the path from the missing property in the case of a required error, so it feels like ajvResolver will handle the case, but actually, it does not match the payload of the actual error (which is the format mentioned in #615 (comment). ajvResolver's code is quite standalone and easy to fork, it may be a dirty quick fix (not having every react-hook-form/resolver points in mind), but adding the following loop after the first one did the trick for me:
As stated, it may be simplistic and/or dirty, but it definitely feels like ajvResolver could handle this. |
When `errorMessage` is used, ajv returns a special error object,`error.keyword='errorMessage'`, and keeps original validation errors in `error.errors` field. The proposed changes normalze the ajv validation errors, so the resolver returns original error types, like `required` and `pattern` while using custom error messages. This also fixes react-hook-form#615
Describe the bug
Without a custom required error message set on my AJV form schema, required form field errors are returned as expected with the default AJV required error message. With a custom required error message set, no error is set on the form field. The required error message is attached to the error body on a non existent
""
form fieldTo Reproduce
Steps to reproduce the behaviour:
Set the form schema:
Set the form value:
Expected react hook form error when both name and email are not defined:
Actual error returned:
Codesandbox link (Required)
Example
First name is using the default ajv error messages. Last name is using a custom required error message. On submitting the form, only the required first name error message is displayed.
Expected behavior
Custom required error messages should be returned by form errors on the relevant form field.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: