Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.
This repository was archived by the owner on Apr 24, 2023. It is now read-only.

Support async validation in field level #173

Description

@Mosoc

Is your feature request related to a problem? Please describe.

It's a wide-used feature for checking username availability, filtering forbidden words in the content, or any other computing/request which need to take a certain amount of run time.

Describe the solution you'd like
It seems that making validation.validator perform not only synchronous function call but also asynchronous one with Promise is Intuitive way.

How to implement
The quick way in babel era is use async/await syntax.

validate = async () => {
 // ...
  let validatorResult = null;
  if(validator && isFunction(validator)) {
    validatorResult = await validator(value, reject);
  }
  // ...
}

But I think there are still some compatibility or performance issues.

For Promise only case, I prefer the method in Formik source code and doc, which Promisify either async function or normal one.

  runSingleFieldLevelValidation = (
    field: string,
    value: void | string
  ): Promise<string> => {
    return new Promise(resolve =>
      resolve(this.fields[field].props.validate(value))
    ).then(x => x, e => e);
  };

As this way, we can Promisify all the validate handle include orginal ajv validation.
Then, we can use Promise.all and map reduce to handle results and errors clearly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions