You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
})
)
})
Submit the form and observe that startDate and endDate are null inside the custom validation function.
Screen shot
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
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:
Submit the form and observe that startDate and endDate are null inside the custom validation function.
Screen shot
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:
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?
The text was updated successfully, but these errors were encountered: