Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report: Custom Validation in Valibot 0.36.0 with React Hook Form 7.52.1 Doesn't Receive Properties #754 #1066

Open
nadeemkhoury opened this issue Feb 28, 2025 · 0 comments

Comments

@nadeemkhoury
Copy link

Describe the bug
When using Valibot 0.36.0 with React Hook Form 7.52.1, custom validations do not receive the expected properties (they are null).

To Reproduce

Steps to reproduce the behavior:

  1. Install Valibot and React Hook Form: npm install @hookform/resolvers valibot react-hook-form
  2. Define a schema with a custom validation function:
import { object, string, date, pipe, nonEmpty, minLength, custom } from 'valibot';

const challengeDetailsScheme = object({
  title: pipe(string(), nonEmpty('This field is required'), minLength(1, 'Title must not be empty')),
  description: pipe(string(), nonEmpty('This field is required'), minLength(1, 'Description must not be empty')),
  startDate: pipe(date(), nonEmpty('Start date is required')),
  endDate: pipe(
    date(),
    nonEmpty('End date is required'),
    custom((endDate, context) => {
      if (endDate < startDate) {
        return 'End date must not be earlier than start date'
      }

      return true
    })
  )
})
  1. Use the schema with React Hook Form:

import { useForm } from 'react-hook-form';
import { valibotResolver } from '@hookform/resolvers/valibot';

const { register, handleSubmit, formState: { errors } } = useForm({
  resolver: valibotResolver(challengeDetailsScheme)
});

Submit the form and observe that startDate and endDate are null inside the custom validation function.

Screen shot

Image

Expected Behavior

The custom validation function should receive startDate and endDate correctly.

Tried Solutions

Using an alternative object structure with a global custom validation rule:

const challengeDetailsScheme = object(
  {
    title: pipe(string(), nonEmpty('This field is required'), minLength(1, 'Title must not be empty')),
    description: pipe(string(), nonEmpty('This field is required'), minLength(1, 'Description must not be empty')),
    startDate: pipe(date(), nonEmpty('Start date is required')),
    endDate: pipe(date(), nonEmpty('End date is required'))
  },
  [
    custom(({ startDate, endDate }) => {
      if (startDate && endDate) {
        return startDate < endDate;
      }
      return true;
    }, 'Start date must be before end date')
  ]
);

Result: This solution also does not work, as the whole custom validator is not triggered

Additional Context

I already created an issue in reach-hook-form resolvers: react-hook-form/resolvers#754

but looking forward for input here as well.

I found similar issues on GitHub but none of the suggested workarounds have resolved this problem.

This issue affects form validation logic, making it impossible to validate dependent fields.

Request

Any insights or fixes for ensuring that custom validation receives the expected properties in Valibot 0.36.0 with React Hook Form 7.52.1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant