Skip to content

bug: notEmptyOrNull extension returns true for null values #9

Description

@MadalyImbate

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

  1. Define a nullable string property (e.g., String? name = null;).
  2. Add a rule using builder.ruleFor((p) => p.name).notEmptyOrNull();.
  3. Validate the entity.
  4. 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,
    );
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions