React-jsonschema-form: Error Messages Question

Created on 28 Feb 2017  路  3Comments  路  Source: rjsf-team/react-jsonschema-form

I changed the default error messages using transformErrors, but I think is more useful to use an schema to chagen them. So my question is, is there any way to change them just using an schema?

I would like that behavior, because is simple and very easy to maintain (and becuase we're defining most of the components and validations in the same way).

thanks!!

question

All 3 comments

looks like its not standard yet to include messages in the JSON schema see https://github.com/json-schema-org/json-schema-spec/issues/148

here's an example to use json schema messages property :

let field = {
    type: "string",
    title: "FirstName",
    minLength: 12,
    required: true,
    messages: {
      required: "Please enter your First name",
      minLength: "First name should be > 12 characters"
    }
  };
const MySchemaForm = props => {
  // make errors more readable based on schema definition if any
  const transformErrors = errors => {
    return errors.map(error => {
      // use error messages from JSON schema if any
      if (error.schema.messages && error.schema.messages[error.name]) {
        return Object.assign({}, error, {
          message: error.schema.messages[error.name]
        });
      }
      return error;
    });
  };

  return (
    <SchemaForm
      {...props}
      liveValidate
      showErrorList={false}
      transformErrors={transformErrors}
   / >
  );
};

I like @revolunet's approach here, pretty smart and pragmatic. Let's consider this an official trick. Ideally we could add a sample jsfiddle and link to it from the README as we did with other useful examples. I'd warmly welcome a PR about that :)

Meanwhile, closing.

The jsfiddle example by @revolunet doesn't work anymore. It's not possible to access the schema from the error object. I checked it in both @revolunet jsfiddle and in my local version. @n1k0 could you please help me on this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FBurner picture FBurner  路  3Comments

n1k0 picture n1k0  路  3Comments

elyobo picture elyobo  路  3Comments

ebower12 picture ebower12  路  3Comments

videni picture videni  路  3Comments