resolvers: [Bug]: Document issue

Version Number

7.15.4

Codesandbox/Expo snack

https://codesandbox.io/s/sweet-dream-58m9v

Steps to reproduce

  1. Code was copied from the documentation.
  2. Run tsc
  3. Or in VSCode in the line it shows the error.

Expected behaviour

Without any issue.

What browsers are you seeing the problem on?

No response

Relevant log output

Type error: Type '(values: unknown, context: object, options: ResolverOptions<AssertsShape<Assign<ObjectShape, { firstName: RequiredStringSchema<string, Record<string, any>>; age: RequiredNumberSchema<number, Record<...>>; }>>>) => Promise<...>' is not assignable to type 'Resolver<IFormInputs, object>'.
  Types of parameters 'options' and 'options' are incompatible.
    Type 'ResolverOptions<IFormInputs>' is not assignable to type 'ResolverOptions<AssertsShape<Assign<ObjectShape, { firstName: RequiredStringSchema<string, Record<string, any>>; age: RequiredNumberSchema<number, Record<string, any>>; }>>>'.
      Type 'IFormInputs' is not assignable to type 'AssertsShape<Assign<ObjectShape, { firstName: RequiredStringSchema<string, Record<string, any>>; age: RequiredNumberSchema<number, Record<string, any>>; }>>'.
        Index signature for type 'string' is missing in type 'IFormInputs'.

  53 |     formState: { errors },
  54 |   } = useForm<IFormInputs>({
> 55 |     resolver: yupResolver(schema),
     |     ^
  56 |   });
  57 |   const onSubmit = (data: IFormInputs) => console.log(data);
  58 | 
error Command failed with exit code 1.

Code of Conduct

  • I agree to follow this project’s Code of Conduct

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 41 (22 by maintainers)

Commits related to this issue

Most upvoted comments

Having the same problem with version 3.1.1

image image image image

PS: as a solution, I did downgrade to 2.9.10 which is working

@wroughtec adding the .required() fix the issue as well.

const SignupSchema = Yup.object().shape(
  {
    min: Yup.number()
      .min(0)
      .max(20)
      .when("max", {
        is: (val) => val,
        then: Yup.number().required().max(Yup.ref("max"))
      }),
    max: Yup.number()
      .min(0)
      .max(20)
      .when("min", {
        is: (val) => val,
        then: Yup.number().required().min(Yup.ref("min"))
      })
  },
  [["min", "max"]]
).required();

Last clarification. This used to pass:

const form = useForm<{ field: number }>({
  resolver: yupResolver(yup.object({ field: yup.string() })),
  defaultValue: { field: 1 }
})

While with 2.8.1 it will show an error showing that the schema is incompatible with the manually specified type.

Also, if you do:

const form = useForm({
  resolver: yupResolver(yup.object({ field: yup.string() })),
  defaultValue: { field: 1 }
})

It will also show an error since the schema is incompatible with the default values! 🎉

this issue forced me to change from yup to zod in my new project. the issue still exist in @hookform/resolvers@3.3.2

I had a similar issue. The fix was to make the array of string required, ie:

  tags: array().of(string().required()).required(),
  links: array().of(string().required()).required(),

@bluebill1049 Just a thought, but maybe we can export both a strictly typed resolver and a less strict resolver? They would be the same code, the non strictly typed version could just be typecasted to “any” just like before.

We will revert this feature for now, and make it a potential break change for v3. Thanks very much for your contribution by the way.

Hey Guys,

Sorry for the delay, the last version of resolvers (2.8.2) should fix that issue. If you have a different error, please open a new issue.

great think safer to go down that route then 😃

@bluebill1049 In this example, changing the schema to:

const SignupSchema = yup
  .object({
    firstName: yup.string().required(),
    lastName: yup.string().defined(),
    age: yup.number().required().positive().integer(),
    website: yup.string().url().defined()
  })
  .required();

Works. Which actually fixes potential problems since not using defined or required means undefined is a valid value, and it’s better to be explicit about this.

Also now you don’t have to pass the type to useForm as it should be correctly inferred from the schema.

So using the following code is enough and will show problems with the schema.

useForm({
  resolver: yupResolver(SignupSchema)
});

It sure is stricter, but definitely safer!

Thanks @julienfouilhe no one’s fault here 😃 I do appreciate your contribution and help. we are all trying to make this works for everyone.

Take a look at this codesandobx: https://codesandbox.io/s/react-hook-form-validationschema-v7-ts-eb41q?file=/src/index.tsx the issue is probably more obvious, switching from 2.8.1 to 2.8.0