react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.67k stars 151 forks source link

AJV resolver is async mode is not working #665

Open denyskublytskyi opened 4 months ago

denyskublytskyi commented 4 months ago

Describe the bug The Async mode for AJV is not working. It's not handling promise here https://github.com/react-hook-form/resolvers/blob/master/ajv/src/ajv.ts#L71

https://ajv.js.org/guide/async-validation.html

To Reproduce Steps to reproduce the behavior:

Codesandbox link (Required) Include a codesandbox will help us to investigate the issue quicker.

https://codesandbox.io/p/sandbox/react-hook-form-validationschema-v6-2l77g

Expected behavior Error message for field instead of the uncaught error.

Screenshots

Screenshot 2024-03-03 at 18 53 43

Desktop (please complete the following information):

Additional context https://github.com/react-hook-form/resolvers/blob/master/ajv/src/ajv.ts#L71

Fix:

let valid = false;
if (resolverOptions?.mode === 'sync') {
  valid = validate(values);
} else {
  try {
    valid = await validate(values);
  } catch (e) {
    if (e instanceof Ajv.ValidationError) {
      validate.errors = e.errors as DefinedError[];
    }
  }
}