Errors in `onError` loose all ajv properties when a custom `validator` property is specified.
See original GitHub issuePrerequisites
- I have read the documentation;
- In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.
- Ideally, I’m providing a sample JSFiddle or a shared playground link demonstrating the issue.
Description
When a custom validator is passed through the validate
property, all properties except the stack
property on onError errors are wiped.
Steps to Reproduce
See https://codesandbox.io/s/react-example-22s01 Click submit to see proper errors. Click “Enable custom validation” to add a no-op custom validator, and submit again. Observe errors now are missing most of their properties.
Expected behavior
Existing AJV errors should mantain their properties. Custom validator errors should at the very least specify the property
property so consumers of onError know what field is having the error.
Actual behavior
All errors, both custom and ajv, only have a stack
property.
Error with custom validation off: (all properties specified as expected)
[
{
"name": "maxLength",
"property": ".test",
"message": "should NOT be longer than 3 characters",
"params": {
"limit": 3
},
"stack": ".test should NOT be longer than 3 characters",
"schemaPath": "#/properties/test/maxLength"
}
]
Error with custom validation on:
(No properties aside from stack
specified for both schema and custom errors)
[
{
"stack": "test: should NOT be longer than 3 characters"
},
{
"stack": "test: This is a custom error"
}
]
Version
1.8.1
Cause:
The error properties are generated by transformAjvErrors here. If custom validation is off, this is the error object returned here.
However, if a custom validator is used, the error list is entirely regenerated by toErrorList here. This function only populates the stack
property.
Fix
Fixing this might be difficult if we want to completely regenerate the list to allow the validate handler to delete existing errors.
However, at the very least we should populate the property
key in toErrorList so that the consumer knows where the errors occurred. This would be enough to fix our particular use case.
Edit: validate does not receive the existing errors, but a new schema object. This means the user cannot delete existing errors and we can simply compute the toErrorList
of the validator errors and concat it.
We can accomplish this by replacing the code here
const errorHandler = customValidate(formData, createErrorHandler(formData));
const userErrorSchema = unwrapErrorHandler(errorHandler);
const newErrorSchema = mergeObjects(errorSchema, userErrorSchema, true);
// XXX: The errors list produced is not fully compliant with the format
// exposed by the jsonschema lib, which contains full field paths and other
// properties.
const newErrors = toErrorList(newErrorSchema);
with
const errorHandler = customValidate(formData, createErrorHandler(formData));
const userErrorSchema = unwrapErrorHandler(errorHandler);
const newErrorSchema = mergeObjects(errorSchema, userErrorSchema, true);
// Concat the new errors to the existing error list
const newErrors = [].concat(errors, toErrorList(userErrorSchema));
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:6 (2 by maintainers)
Fixed in v5 beta via https://github.com/rjsf-team/react-jsonschema-form/pull/3047, see the 5.x migration guide
Not stale. Open PR at #2002