Skip to content

If we have a field schema set to optional and the field value is empty, then the custom() validation is not triggered #3075

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

Open
callmeyaz opened this issue May 13, 2025 · 0 comments
Labels
bug Bug or defect

Comments

@callmeyaz
Copy link

Runtime

node

Runtime version

v22.14.0

Module version

17.13.3

Last module version without issue

No response

Used with

react

Any other relevant information

The issue is that if we have a field schema set to optional and the field value is empty, then the custom() validation is not triggered.
At the surface this seems trivial that a field with optional schema, having no value may not require validation, but that is not the case as the original issue creator mentions.

I am writing a wrapper to bring Yup, Zod and Joi under one standard library and it seems like Joi is the only one which has this problem. This is a deal breaker for me and if this issue is not fixed then I would abandon my Joi port.

Even google know this problem and gave me the following description of the problem.

It appears you're encountering an issue with Joi where a custom validation function isn't being executed when a field is marked as optional. Here's a breakdown of the problem and potential solutions:
Understanding the Issue:
Optional Fields: In Joi, fields are optional by default, meaning a value doesn't have to be present for validation to pass.
Custom Validation Logic: You might use .custom() or similar methods to implement custom validation logic for a field.
Problem: When a field is optional and not present, Joi might skip directly to passing validation, bypassing your custom logic.

Can this be "fixed"?

What are you trying to achieve or the steps to reproduce?

Here's a breakdown of the problem and potential solutions:
Understanding the Issue:
Optional Fields: In Joi, fields are optional by default, meaning a value doesn't have to be present for validation to pass.
Custom Validation Logic: You might use .custom() or similar methods to implement custom validation logic for a field.
Problem: When a field is optional and not present, Joi might skip directly to passing validation, bypassing your custom logic.

What was the result you got?

custom() validation not triggered when field value were empty

What result did you expect?

custom() validation should be triggered regardless of the field value is empty or not.

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

No branches or pull requests

1 participant