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

Support for async/promise-driven validators #71

Open
marcelchastain opened this issue Jun 18, 2020 · 1 comment
Open

Support for async/promise-driven validators #71

marcelchastain opened this issue Jun 18, 2020 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@marcelchastain
Copy link

Our app has some validation logic that requires calling a remote API.

Are there any plans to support asynchronous/promise-driven validators?


Also, here's an abbreviated version of my hacky method using onUpdate, setError, and some manual promise management. Hopefully I can arrive at a cleaner solution!

handleUpdate = ({ fields, errors, setError }) => {
  if (fields.url && this.state.validatedUrls[fields.url] === undefined) {
    this.setState(prevState => ({
      ...prevState,
      validatedUrls: {
        ...prevState.validatedUrls, 
        [fields.url]: null
      }
    });
  );
  
  const validationPromise = new Promise((resolve, reject) => {
    // async stuff here, manually call resolve or reject.
    setTimeout(() => reject(), 1000);
  });
  
  validationPromise
    .then(() => {/* setState validatedUrls[fields.url] = true */}))
    .catch(() => {
      // setState validatedUrls[fields.url] = false
      setError({ url: "Error!" });
    });
  }
};
@selbekk
Copy link
Owner

selbekk commented Jul 17, 2020

Sounds like a great feature, but I'd be happy to accept a pull request for it.

@selbekk selbekk added the help wanted Extra attention is needed label Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants