Skip to content

Commit 142dc1f

Browse files
fix: replace message field with title or ui:title correctly
1 parent c77378b commit 142dc1f

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ should change the heading of the (upcoming) version to include a major version b
3131
- Updated `getDefaultFormState()` to pass true to `mergeDefaultsWithFormData` for `defaultSupercedesUndefined` when `mergeDefaultsIntoFormData` has the value `useDefaultIfFormDataUndefined`, fixing [#4322](https://github.com/rjsf-team/react-jsonschema-form/issues/4322)
3232
- Updated `getClosestMatchingOption()` to improve the scoring of sub-property objects that are provided over ones that aren't, fixing [#3997](https://github.com/rjsf-team/react-jsonschema-form/issues/3977) and [#4314](https://github.com/rjsf-team/react-jsonschema-form/issues/4314)
3333

34+
## @rjsf/validator-ajv8
35+
36+
- Fixed `AJV8Validator#transformRJSFValidationErrors` to replace the error message field with either the `uiSchema`'s `ui:title` field if one exists or the `parentSchema` title if one exists. Fixes [#4348](https://github.com/rjsf-team/react-jsonschema-form/issues/4348)
37+
3438
## Dev / docs / playground
3539

3640
- Updated the `form-props.md` to add documentation for the new `experimental_customMergeAllOf` props and the `experimental_defaultFormStateBehavior.mergeDefaultsIntoFormData` option

packages/validator-ajv8/src/processRawValidationErrors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ export function transformRJSFValidationErrors<
4343
const uiSchemaTitle = getUiOptions(get(uiSchema, `${property.replace(/^\./, '')}`)).title;
4444

4545
if (uiSchemaTitle) {
46-
message = message.replace(currentProperty, uiSchemaTitle);
46+
message = message.replace(`'${currentProperty}'`, `'${uiSchemaTitle}'`);
4747
} else {
4848
const parentSchemaTitle = get(parentSchema, [PROPERTIES_KEY, currentProperty, 'title']);
4949

5050
if (parentSchemaTitle) {
51-
message = message.replace(currentProperty, parentSchemaTitle);
51+
message = message.replace(`'${currentProperty}'`, `'${parentSchemaTitle}'`);
5252
}
5353
}
5454

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,67 @@ describe('AJV8Validator', () => {
14011401
expect(errorSchema.nested!.numberOfChildren!.__errors![0]).toEqual('must match pattern "\\d+"');
14021402
});
14031403
});
1404+
describe('replace the error message field with schema property title', () => {
1405+
beforeAll(() => {
1406+
const schema: RJSFSchema = {
1407+
type: 'object',
1408+
required: ['a', 'r'],
1409+
properties: {
1410+
a: { title: 'First Name', type: 'string' },
1411+
r: { title: 'Last Name', type: 'string' },
1412+
},
1413+
};
1414+
1415+
const formData = {};
1416+
const result = validator.validateFormData(formData, schema);
1417+
errors = result.errors;
1418+
errorSchema = result.errorSchema;
1419+
});
1420+
it('should return an error list', () => {
1421+
expect(errors).toHaveLength(2);
1422+
1423+
const stack = errors.map((e) => e.stack);
1424+
1425+
expect(stack).toEqual([
1426+
"must have required property 'First Name'",
1427+
"must have required property 'Last Name'",
1428+
]);
1429+
});
1430+
});
1431+
describe('replace the error message field with uiSchema property title', () => {
1432+
beforeAll(() => {
1433+
const schema: RJSFSchema = {
1434+
type: 'object',
1435+
required: ['a', 'r'],
1436+
properties: {
1437+
a: { type: 'string', title: 'First Name' },
1438+
r: { type: 'string', title: 'Last Name' },
1439+
},
1440+
};
1441+
const uiSchema: UiSchema = {
1442+
a: {
1443+
'ui:title': 'uiSchema First Name',
1444+
},
1445+
r: {
1446+
'ui:title': 'uiSchema Last Name',
1447+
},
1448+
};
1449+
1450+
const formData = {};
1451+
const result = validator.validateFormData(formData, schema, undefined, undefined, uiSchema);
1452+
errors = result.errors;
1453+
errorSchema = result.errorSchema;
1454+
});
1455+
it('should return an error list', () => {
1456+
expect(errors).toHaveLength(2);
1457+
const stack = errors.map((e) => e.stack);
1458+
1459+
expect(stack).toEqual([
1460+
"must have required property 'uiSchema First Name'",
1461+
"must have required property 'uiSchema Last Name'",
1462+
]);
1463+
});
1464+
});
14041465
});
14051466
describe('No custom validate function, single additionalProperties value', () => {
14061467
let errors: RJSFValidationError[];

0 commit comments

Comments
 (0)