-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Is it possible to display field titles in validation error messages instead of real field names? #3246
Comments
+1 |
1 similar comment
+1 |
This would be excellent, displaying the field name or using When would you want to display the field name? |
@dagda1 @shijistar @ademgashi There is an OLD PR (#2634) that would fix this, but it is built on top of the earlier 4.x release. The code itself needs to be refactored on top of all the changes made in 5.x. If someone were to redo the code in the PR we'd be willing to merge it |
@heath-freenome I am having a look at this now. What do you think would be the best approach in the The errors coming back from [
{
instancePath: '',
schemaPath: '#/required',
keyword: 'required',
params: { missingProperty: 'lastName' },
message: "must have required property 'lastName'"
}
] We would somehow need to change the I have had a rough hack as a means of getting some feedback. I'm not familiar with the codebase but would something like this work? I'm really not liking the string replace on the message, but I do not know a better way: private transformRJSFValidationErrors(
schema: S,
errors: ErrorObject[] = []
): RJSFValidationError[] {
return errors.map((e: ErrorObject) => {
const { instancePath, keyword, params, schemaPath, ...rest } = e;
let { message } = rest;
let property = instancePath.replace(/\//g, ".");
let stack = `${property} ${message}`.trim();
if ("missingProperty" in params) {
property = property
? `${property}.${params.missingProperty}`
: params.missingProperty;
const paths = property.replace(/^\./, '').split('.');
const titlePath= paths.map((p, i) => i !== paths.length - 1 ? `${p}.${PROPERTIES_KEY}.` : `${p}.title`).join('');
const title = get(schema, `${PROPERTIES_KEY}.${titlePath}`);
if (title) {
const existing = property.split('.').slice(-1)[0];
message = message?.replace(existing, title);
}
stack = message!;
}
// put data in expected format
return {
name: keyword,
property,
message,
params, // specific to ajv
stack,
schemaPath,
};
});
} Any feedback at all appreciated. |
@heath-freenome I've created #3337 as a better way of getting feedback. |
Prerequisites
What theme are you using?
core
What is your question?
Is it possible to display field titles in validation error messages instead of real property names? Field titles may be clearer to users.
If I have to replace the property name in
transformErrors
, then how to get the field title from the validation error object?The text was updated successfully, but these errors were encountered: