Bug Description
The notEmptyOrNull extension method on SimpleValidationBuilder<String?> contains incorrect logic in its validation predicate.
Currently, the predicate is written as:
(value, entity) => value == null || value.isNotEmpty
This causes null values to evaluate to true (valid), bypassing the validation entirely when a field is null. As a result, nullable strings with null values pass the notEmptyOrNull() check instead of failing as intended.
Additional Findings & Redundancy
While reviewing the extension file, we also noticed that NotEmptyNullableValidation.notEmpty() and NotEmptyOrNullableValidation.notEmptyOrNull() both target SimpleValidationBuilder<String?> and perform the exact same validation check (value != null && value.isNotEmpty).
Consider consolidating or deprecating one of them to avoid duplicate API behavior on nullable strings.
Expected Behavior
notEmptyOrNull() should fail (return false) when value is null or when value is an empty string (""). It should only pass (return true) when value is non-null and non-empty.
Steps to Reproduce
- Define a nullable string property (e.g.,
String? name = null;).
- Add a rule using
builder.ruleFor((p) => p.name).notEmptyOrNull();.
- Validate the entity.
- Result: The validation passes as valid even though
name is null.
Suggested Fix
Update the predicate in NotEmptyOrNullableValidation:
extension NotEmptyOrNullableValidation on SimpleValidationBuilder<String?> {
SimpleValidationBuilder<String?> notEmptyOrNull({
String? message,
String? code,
}) {
return useValidation(
// here is the fix
(value, entity) => value != null && value.isNotEmpty,
code: code ?? Language.code.notEmpty,
message: message,
);
}
}
Bug Description
The
notEmptyOrNullextension method onSimpleValidationBuilder<String?>contains incorrect logic in its validation predicate.Currently, the predicate is written as:
This causes
nullvalues to evaluate totrue(valid), bypassing the validation entirely when a field isnull. As a result, nullable strings withnullvalues pass thenotEmptyOrNull()check instead of failing as intended.Additional Findings & Redundancy
While reviewing the extension file, we also noticed that
NotEmptyNullableValidation.notEmpty()andNotEmptyOrNullableValidation.notEmptyOrNull()both targetSimpleValidationBuilder<String?>and perform the exact same validation check (value != null && value.isNotEmpty).Consider consolidating or deprecating one of them to avoid duplicate API behavior on nullable strings.
Expected Behavior
notEmptyOrNull()should fail (returnfalse) whenvalueisnullor whenvalueis an empty string (""). It should only pass (returntrue) whenvalueis non-null and non-empty.Steps to Reproduce
String? name = null;).builder.ruleFor((p) => p.name).notEmptyOrNull();.nameisnull.Suggested Fix
Update the predicate in
NotEmptyOrNullableValidation: