Skip to content

Support for async/promise-driven validators #71

@marcelchastain

Description

@marcelchastain

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!" });
    });
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions