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

Valibot infinate loop in resolver #655

Closed richardvanbergen closed 6 months ago

richardvanbergen commented 7 months ago

Describe the bug In recent versions of valibot they've introduced a variant method. This method does not set a path value in the issues array. Because of the way the valibotResolver loops over the issues returned from valibot, this can cause an infinite loop.

To Reproduce

  1. Using the following schema:
const numberSchema = object({
  type: literal("number")
})

const stringSchema = object({
  type: literal("string")
})

const typeSchema = variant("type", [numberSchema, stringSchema])
  1. Try to call the valibot resolver.
const data = {
  type: undefined
}

await valibotResolver(typeSchema, undefined, {
  mode: 'sync',
})(data, undefined, {
  fields,
  shouldUseNativeValidation,
});
  1. Browser or test runner freezes.

Additional context

I've included tests in this PR. If you wish to demonstrate the issue you can revert the changes I made to valibot.ts.

Because there's no path returned from valibot we can't add the error to the resolver. I've requested a change in valibot to address this and the author seems to agree. This PR simply fixes the infinite loop and I will submit a second once the other issue has been addressed to update the dependency to the fixed version.

I've tried to follow the contributing guidelines as best as I can. It seems as they're out of date or were copied from somewhere else the following commands listed aren't in this repo as far as I can tell:

github-actions[bot] commented 6 months ago

:tada: This PR is included in version 3.3.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket: