Skip to content

fix: Validation error messages are inconsistent #4348 #4349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ should change the heading of the (upcoming) version to include a major version b

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

## @rjsf/validator-ajv8

- 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)

# 5.22.1

## Dev / docs / playground
Expand Down
4 changes: 2 additions & 2 deletions packages/validator-ajv8/src/processRawValidationErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ export function transformRJSFValidationErrors<
const uiSchemaTitle = getUiOptions(get(uiSchema, `${property.replace(/^\./, '')}`)).title;

if (uiSchemaTitle) {
message = message.replace(currentProperty, uiSchemaTitle);
message = message.replace(`'${currentProperty}'`, `'${uiSchemaTitle}'`);
} else {
const parentSchemaTitle = get(parentSchema, [PROPERTIES_KEY, currentProperty, 'title']);

if (parentSchemaTitle) {
message = message.replace(currentProperty, parentSchemaTitle);
message = message.replace(`'${currentProperty}'`, `'${parentSchemaTitle}'`);
}
}

Expand Down
61 changes: 61 additions & 0 deletions packages/validator-ajv8/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,67 @@ describe('AJV8Validator', () => {
expect(errorSchema.nested!.numberOfChildren!.__errors![0]).toEqual('must match pattern "\\d+"');
});
});
describe('replace the error message field with schema property title', () => {
beforeAll(() => {
const schema: RJSFSchema = {
type: 'object',
required: ['a', 'r'],
properties: {
a: { title: 'First Name', type: 'string' },
r: { title: 'Last Name', type: 'string' },
},
};

const formData = {};
const result = validator.validateFormData(formData, schema);
errors = result.errors;
errorSchema = result.errorSchema;
});
it('should return an error list', () => {
expect(errors).toHaveLength(2);

const stack = errors.map((e) => e.stack);

expect(stack).toEqual([
"must have required property 'First Name'",
"must have required property 'Last Name'",
]);
});
});
describe('replace the error message field with uiSchema property title', () => {
beforeAll(() => {
const schema: RJSFSchema = {
type: 'object',
required: ['a', 'r'],
properties: {
a: { type: 'string', title: 'First Name' },
r: { type: 'string', title: 'Last Name' },
},
};
const uiSchema: UiSchema = {
a: {
'ui:title': 'uiSchema First Name',
},
r: {
'ui:title': 'uiSchema Last Name',
},
};

const formData = {};
const result = validator.validateFormData(formData, schema, undefined, undefined, uiSchema);
errors = result.errors;
errorSchema = result.errorSchema;
});
it('should return an error list', () => {
expect(errors).toHaveLength(2);
const stack = errors.map((e) => e.stack);

expect(stack).toEqual([
"must have required property 'uiSchema First Name'",
"must have required property 'uiSchema Last Name'",
]);
});
});
});
describe('No custom validate function, single additionalProperties value', () => {
let errors: RJSFValidationError[];
Expand Down