If we have a field schema set to optional and the field value is empty, then the custom() validation is not triggered
- Dominant language
- JavaScript
- Stars
- 21.2k
- Forks
- 1.5k
- Avg merge
- 4h 57m
- Merged PRs (30d)
- 14
Description
### 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.
Contributor guide
Assessment
This issue has not been assessed yet.