Skip to content
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

Support custom error messages from schema #4119

Open
1 task done
Rozamo opened this issue Mar 7, 2024 · 2 comments
Open
1 task done

Support custom error messages from schema #4119

Rozamo opened this issue Mar 7, 2024 · 2 comments
Labels

Comments

@Rozamo
Copy link
Contributor

Rozamo commented Mar 7, 2024

Prerequisites

What theme are you using?

validator-ajv8

Is your feature request related to a problem? Please describe.

Forms should be user friendly and so should be the errors. Therefore in cases where the form creator wants to have more readable errors for fields they should be allowed to do so.

For example, if a form is asking for a name and is requiring a pattern ^([^0-9]*)$, the current error shown in must match pattern "^([^0-9]*)$". The creator of form might want to show a custom error like Name should not contain numbers.

Adding support for this would enable more user friendly forms. This is handy when the schema comes from the backend and the UI just renders it.

I think this was being discussed before here.

Describe the solution you'd like

Some implementation using ajv-errors or transformErrors.

Describe alternatives you've considered

Support for custom error messages is present but it has to be driven through code. For the use cases where the schema is coming from the backend there is no way to know the error message before hand.

@Rozamo Rozamo added feature Is a feature request needs triage Initial label given, to be assigned correct labels and assigned labels Mar 7, 2024
@nickgros
Copy link
Contributor

nickgros commented Mar 8, 2024

@Rozamo have you tried to use ajv-errors in your own code with customizeValidator? We have an example for ajv-keywords, which is very similar:

See Using the raw Ajv instance in the docs.

@nickgros nickgros added question validation and removed needs triage Initial label given, to be assigned correct labels and assigned labels Mar 8, 2024
@Rozamo
Copy link
Contributor Author

Rozamo commented Mar 10, 2024

@nickgros Thanks, this worked. This plugin architecture is great! Attaching the code here for future references.

import Form from "@rjsf/core";
import { RJSFSchema } from "@rjsf/utils";
import { customizeValidator } from "@rjsf/validator-ajv8";
import ajvErrors from "ajv-errors";

const validator = customizeValidator();
ajvErrors(validator.ajv);

const schema: RJSFSchema = {
  type: "object",
  properties: {
    name: {
      type: "string",
      title: "Name",
      pattern: "^([^0-9]*)$",
      minLength: 3,
      errorMessage: {
        pattern: "Name must not contain numbers",
        minLength: "Name must be at least 3 characters",
      },
    },
  },
};

const App = () => {
  return <Form schema={schema} validator={validator} />;
};

export default App;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants