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

Add schemaPath to transformAjvErrors #838

Closed
2 tasks done
dosborne opened this issue Feb 12, 2018 · 1 comment · Fixed by #1205
Closed
2 tasks done

Add schemaPath to transformAjvErrors #838

dosborne opened this issue Feb 12, 2018 · 1 comment · Fixed by #1205
Assignees

Comments

@dosborne
Copy link

dosborne commented Feb 12, 2018

Prerequisites

  • I have read the documentation;
  • In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Description

I'm using rjsf in a project and require custom messages that are part of the schema. For example:

const schema = {
title: "react-jsonschema-form demo",
type: "object",
required: ["name"],
properties: {
name: {
type: "string",
minLength: 3,
myCustomErrors: "You really should fill in your name"
},
description: {type: "string"},
}
};

Here myCustomErrors defines custom error messages. So when running rjsf validation I provide a transformErrors function, which is called with the error messages generated by rjsf. I need a way to map the error into the schema so that I can extract the proper error message. Looking at the source this should be as easy as updating validate.js to add the schemaPath to the error message structure.

Something like the following:

    const { dataPath, keyword, message, params, schemaPath } = e;
    let property = ``${dataPath}``;
    // put data in expected format
    return {
      name: keyword,
      property,
      message,
      params, // specific to ajv
      stack: ``${property} ${message}``.trim(),
      schemaPath
    };
  });

Steps to Reproduce

Here's a sample JSFiddle demonstrating that validation doesn't have the schemaPath.

Expected behavior

I need to have access to the schemaPath

Actual behavior

I don't have access to the schemaPath

Version

1.0.1

@doncesarts
Copy link
Contributor

doncesarts commented Feb 12, 2018

Hi @dosborne , I do agree that the schema path should be added to the transformErrors function, but a work around for this issue that worked for me could be the following:

1.- Customize the transformErrors messages.

2.-Create CustomFieldTemplate https://github.com/mozilla-services/react-jsonschema-form#field-template

function CustomFieldTemplate(props) {
  const {id, classNames, label, help, required, description, errors, children} = props;
  return (
    <div className={classNames}>
      <label htmlFor={id}>{label}{required ? "*" : null}</label>
      {description}
      {children}
        <ErrorInline id={id} schema={schema} errors={errors} />
      {help}
    </div>
  );
}

3.-Create a custom ErrorInline that will work as a wrapper of the Errors returned by AJV, pass the id, schema and errors. Here iterate over the list of errors of each field and update them as needed , you will have access to the schema.

@epicfaace epicfaace self-assigned this Jan 20, 2019
epicfaace added a commit to epicfaace/react-jsonschema-form that referenced this issue Jan 20, 2019
epicfaace added a commit that referenced this issue Mar 6, 2019
* #838 return schemaPath in transformErrors

* #818 document structure of errors object in transformErrors

* fix: take into account ajv bug

* doc: re-add doc for transformErrors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment