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.71k stars 155 forks source link

issue: Valibot resolver optional not Found #646

Closed VigilioYonatan closed 9 months ago

VigilioYonatan commented 9 months ago

Version Number

7.48.2

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/headless-meadow-g9l2r7?file=%2Fsrc%2FApp.tsx%3A1%2C1

Steps to reproduce

optional not found in Valibot - @hookform/resolvers.

dependencies: "valibot": "^0.20.1", "react-hook-form": "^7.48.2", "@hookform/resolvers": "^3.3.2",

Expected behaviour

const user = object({
  email: optional(string([email("email incorrect")])),
  // optional not found because show email error.
// optional not found beacause when initial {email:""} its already when you register
// any solution?
});

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

VigilioYonatan commented 9 months ago

I found a solution.


  const settingsUpdateform = useForm<YourInterface>({
            resolver: (values, ...args) => {
                const defaultValues = Object.fromEntries(
                    Object.entries(values).filter(([k, v]) => {
                        return (
                            informationSchema.entries[
                                k as keyof YourInterface
                            ].type !== "optional" || v
                        );
                    })
                ) as YourInterface;
                return valibotResolver(informationSchema)(
                    defaultValues,
                    ...args
                );
            },
        });
Filter the optional object until the values have something
if you want to add it in other version it will be ok. THANKS OF LOT

https://codesandbox.io/p/sandbox/headless-meadow-g9l2r7?file=%2Fsrc%2FApp.tsx%3A17%2C1