Skip to content

Document yupResolver context handling in react-hook-form/resolvers #834

@eotkd4791

Description

@eotkd4791

In @hookform/resolvers yupResolver, context should be provided via useForm({ context }) rather than via Yup validate options passed as schemaOptions (the 2nd argument to yupResolver).

Description

While debugging an issue where Yup conditional validation (when('$...')) kept receiving undefined, I looked into the internal implementation of yupResolver and noticed there is a development warning:

console.warn(
  "You should not used the yup options context. Please, use the 'useForm' context object instead",
);

This behavior is unintuitive and easy to miss, especially because Yup’s native API encourages passing context via validate options (e.g. schema.validate(values, { context })). As a result, users may naturally try:

yupResolver(schema, { context: { foo } });

and then spend time debugging why the context can appear undefined or be ignored when passed via schemaOptions.context, because yupResolver uses the form context from useForm({ context }) instead.

In contrast, the intended approach is to pass context via React Hook Form:

useForm({
  resolver: yupResolver(schema),
  context: { foo },
});

Proposal

Could we update the react-hook-form/resolvers README (and/or the Yup resolver section) to explicitly document:

  • schemaOptions (the 2nd argument to yupResolver) should not include context

  • context should be passed via useForm({ context })

  • schemaOptions.context should not be used with yupResolver, because the resolver injects the context from useForm({ context }) at execution time and overrides schemaOptions.context

  • include a short example showing the recommended usage

Why this helps

  • Prevents unnecessary debugging for a common “Yup mental model → resolver usage” mismatch
  • Makes the existing warning’s intent discoverable even if users don’t notice console output
  • Clarifies the correct integration pattern upfront

If this sounds reasonable, I can follow up with a PR that adds the documentation snippet and example.

Thanks!

Related discussion

This came up in the following discussion thread as well:
https://github.com/orgs/react-hook-form/discussions/9084#discussioncomment-15529178

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions