React-jsonschema-form: "should be equal to one of the allowed values" for async field

Created on 14 Feb 2018  路  5Comments  路  Source: rjsf-team/react-jsonschema-form

Description

I created an Async Select widget out of react-select, this select widget can download/update options based on user input.

Steps to Reproduce

[Default enum] -> jsonschema -> Display Select with custom widget -> User type words to search -> The Select update the options -> User choose a new option -> "should be equal to one of the allowed values"

Expected behavior

The new options are allowed.

Please let me know:

  1. How can I remove this validate
  2. Or, how can I added the new options downloaded from async function and make them allowed at the widget level (field).

Actual behavior

Error: should be equal to one of the allowed values

Version

"react-jsonschema-form": "^1.0.0",

All 5 comments

I'm having this same issue when trying to create a custom checkboxes widget with an other field (with a text input on it, so you can put any text).

Same question here. Can this validation be removed?

The validation doesn't come from react-jsonschema-form, but from ajv, which, I guess, makes it harder to disable.

After many tests, I think I found the solution:

  1. Create function transformErrors to remove errors of should be equal to one of the allowed values
    transformErrors=(errors) =>{
        console.log(errors);
        var e = [];
          errors.map(error => {
            if (error.message !== "should be equal to one of the allowed values"){
                e.push(error)
            }
          });
          console.log(e);
          return e;
    }
  1. Pass the transformErrors to form:
<Form
    schema={this.state.schema}
    onChange={this.handleChange}
    formData={this.state.formData}
    transformErrors={this.transformErrors}
/>

That is it. It will ignore all validation.

A second option is updating the form schema with the new enum. This solution is field specific.

    handleChange = (data)=>{
        console.log(data)
        data.formData.name = "bb";
        var e = [
            "option #0",
            "option #1",
            "option #2",
        ];
        e.push("bb")
        this.setState({
            schema: {...this.state.schema,
                properties:{...this.state.schema.properties,
                    name:{...this.state.schema.properties.name,
                        enum:e
                    }
                }

            }
        });
        this.setState({formData:{name:"bb"}});
    }

I think if the schema can allow arbitrary values, then enum isn't really right for this field. Modifying the schema is I guess one option, but maybe you should just override that one widget using uiSchema and provide a select widget that has an initial set of values instead of using enum to provide those values.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mammad2c picture mammad2c  路  3Comments

elyobo picture elyobo  路  3Comments

ebower12 picture ebower12  路  3Comments

jabaren picture jabaren  路  3Comments

mfulton26 picture mfulton26  路  3Comments