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

Schema validation does not work if AutoForm as onValidate #1257

Closed
chiester opened this issue Apr 19, 2023 · 3 comments
Closed

Schema validation does not work if AutoForm as onValidate #1257

chiester opened this issue Apr 19, 2023 · 3 comments
Assignees
Labels
Type: Question Questions and other discussions

Comments

@chiester
Copy link

Using AutoForm with JSON Schema, if AutoForm has onValidate property, schema validation does not work. Remove the onValidate and schema validation works. Remove it and it it stops working. The test onValidate is simply returning null.

Reproduction repository, /~https://github.com/chiester/uniforms-onvalidate-bug
CodeSandbox: https://codesandbox.io/p/github/chiester/uniforms-onvalidate-bug

@chiester
Copy link
Author

It appears that if onValidate returns the error, it works. However, the documentation at https://uniforms.tools/docs/api-forms/#props-1 seems to suggest returning null to skip async validation, so this is misleading. Also, in previous versions, I returned null from onValidate and had no problem so this seems to have changed in recent versions.

@radekmie radekmie self-assigned this Apr 28, 2023
@radekmie radekmie added the Type: Question Questions and other discussions label Apr 28, 2023
@radekmie radekmie moved this from Needs triage to In progress in Open Source Apr 28, 2023
@github-project-automation github-project-automation bot moved this to Needs triage in Open Source Apr 28, 2023
@radekmie
Copy link
Contributor

Hi @chiester. There's a misunderstanding regarding how the onValidate works. This function receives both the model as well as the validation error and decides what the resulting error is.

Returning null at all times simply disabled validation, as whenever the form validates, it passes the validation result through onValidate and it says "none" (null means no error).

In your case, if you'd like to have some additional async validation, then it should look like this instead:

async function onValidate(model, error) {
  // If there's any error from the schema, leave it.
  if (error) {
    return error;
  }

  // If the schema passed, validate using our custom logic.
  return await validate(model);
}

I hope it's clear now.

@radekmie
Copy link
Contributor

No response so far, so I'm closing. Feel free to comment further!

@github-project-automation github-project-automation bot moved this from In progress to Closed in Open Source May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question Questions and other discussions
Projects
Archived in project
Development

No branches or pull requests

2 participants