Skip to content

Commit 13550f0

Browse files
fix: Validation error messages are inconsistent #4348 (#4349)
* fix: replace message field with title or ui:title correctly * fix: add new changelog section & add description * chore: removed extra empty line --------- Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com>
1 parent d7bd130 commit 13550f0

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
@@ -22,6 +22,10 @@ should change the heading of the (upcoming) version to include a major version b
2222

2323
- Fixed validation regression Form not revalidating after formData change, fixing [#4343](https://github.com/rjsf-team/react-jsonschema-form/issues/4343)
2424

25+
## @rjsf/validator-ajv8
26+
27+
- 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)
28+
2529
# 5.22.1
2630

2731
## Dev / docs / playground

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)