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

Non-required object field can't be left empty if at least one its property is required. #2150

Closed
OleksiL opened this issue Nov 30, 2020 · 3 comments · Fixed by #3287
Closed

Comments

@OleksiL
Copy link

OleksiL commented Nov 30, 2020

Prerequisites

Description

playground

I would expect that if object field is not required and left empty then form can be submitted without error. If at least one property of such field is provided then the rest should be validated for requireness.

Steps to Reproduce

Create non-required object field with some required properties.

Expected behavior

If non-required object field doesn't have any properties entered (is empty) then it is possible to submit the form - property is not marked as required

Actual behavior

If at least one property of the non-required object field is required on its own, then validation shows error.

@gaitat
Copy link

gaitat commented Feb 9, 2021

Any updates on this one?

@OleksiL
Copy link
Author

OleksiL commented Feb 10, 2021

I use transformErrors={this.suppressEmptyOptionalObjectError} property to suppress this error:

  suppressEmptyOptionalObjectError = (errors: IReactJsonSchemaError[]) => {
    function shouldSuppressError(formData, error: IReactJsonSchemaError): boolean {
      if (error.name === 'required') {
        const parentFieldId = error.property
          .replace(`['${error.params.missingProperty}']`, '')
          .replace("['", '')
          .replace("']", '');
        const parentFieldValue = formData[parentFieldId];
        return parentFieldId && JSON.stringify(parentFieldValue) === '{}';
      }
      return false;
    }

    return errors.filter(error => !shouldSuppressError(this.state.formData, error));
  };

@ranihorev
Copy link
Contributor

The issue is due to this call. It sets the default value to be an empty object which later on causing the validation to fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment