Skip to content

Commit 713d04b

Browse files
authored
Fixed issue where error messages do not have title or ui:title if a Localizer function is used. Fixes #4387. (#4391)
1 parent 011659d commit 713d04b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ should change the heading of the (upcoming) version to include a major version b
2828
- Updated `getDefaultFormState()` to use the new `constAsDefaults` option to control how const is used for defaulting, fixing [#4344](https://github.com/rjsf-team/react-jsonschema-form/issues/4344), [#4361](https://github.com/rjsf-team/react-jsonschema-form/issues/4361) and [#4377](https://github.com/rjsf-team/react-jsonschema-form/issues/4377)
2929
- Use `experimental_customMergeAllOf` option in functions that have previously missed it.
3030

31+
## @rjsf/validator-ajv8
32+
33+
- Fixed issue where error messages do not have `title` or `ui:title` if a `Localizer` function is used. Fixes [#4387](https://github.com/rjsf-team/react-jsonschema-form/issues/4387)
34+
3135
## Dev / docs / playground
3236

3337
- Updated the playground to add a selector for the `constAsDefaults` option

packages/validator-ajv8/src/validator.ts

+14
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,21 @@ export default class AJV8Validator<T = any, S extends StrictRJSFSchema = RJSFSch
9090
let errors;
9191
if (compiledValidator) {
9292
if (typeof this.localizer === 'function') {
93+
// Missing properties need to be enclosed with quotes so that
94+
// `AJV8Validator#transformRJSFValidationErrors` replaces property names
95+
// with `title` or `ui:title`. See #4348, #4349, and #4387.
96+
(compiledValidator.errors ?? []).forEach((error) => {
97+
if (error.params?.missingProperty) {
98+
error.params.missingProperty = `'${error.params.missingProperty}'`;
99+
}
100+
});
93101
this.localizer(compiledValidator.errors);
102+
// Revert to originals
103+
(compiledValidator.errors ?? []).forEach((error) => {
104+
if (error.params?.missingProperty) {
105+
error.params.missingProperty = error.params.missingProperty.slice(1, -1);
106+
}
107+
});
94108
}
95109
errors = compiledValidator.errors || undefined;
96110

packages/validator-ajv8/test/validator.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,36 @@ describe('AJV8Validator', () => {
18271827
});
18281828
});
18291829
});
1830+
describe('validating required fields with localizer', () => {
1831+
beforeAll(() => {
1832+
localizer = jest.fn().mockImplementation();
1833+
validator = new AJV8Validator({}, localizer);
1834+
schema = {
1835+
type: 'object',
1836+
required: ['a'],
1837+
properties: {
1838+
a: {
1839+
type: 'string',
1840+
title: 'A',
1841+
},
1842+
},
1843+
};
1844+
});
1845+
it('should enclose missing properties with quotes', () => {
1846+
const errors = validator.validateFormData({}, schema);
1847+
const errMessage = "must have required property 'A'";
1848+
expect(errors.errors[0].message).toEqual(errMessage);
1849+
expect(errors.errors[0].stack).toEqual(errMessage);
1850+
expect(errors.errorSchema).toEqual({
1851+
a: { __errors: [errMessage] },
1852+
});
1853+
expect(errors.errors[0].params.missingProperty).toEqual('a');
1854+
});
1855+
it('should handle the case when errors are not present', () => {
1856+
const errors = validator.validateFormData({ a: 'some kind of text' }, schema);
1857+
expect(errors.errors).toHaveLength(0);
1858+
});
1859+
});
18301860
});
18311861
describe('validator.validateFormData(), custom options, localizer and Ajv2019', () => {
18321862
let validator: AJV8Validator;

0 commit comments

Comments
 (0)