Skip to content

Commit 9f00d5b

Browse files
Merge branch 'main' into valid-formdata
2 parents 8b6cfce + 713d04b commit 9f00d5b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ should change the heading of the (upcoming) version to include a major version b
2929
- Use `experimental_customMergeAllOf` option in functions that have previously missed it.
3030
- - Fixed issue with formData not updating when dependencies change, fixing [#4325](https://github.com/rjsf-team/react-jsonschema-form/issues/4325)
3131

32+
## @rjsf/validator-ajv8
33+
34+
- 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)
35+
3236
## Dev / docs / playground
3337

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

packages/validator-ajv8/src/validator.ts

Lines changed: 14 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
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)