Skip to content

add ifPresent validator to skip missing vars - #2

Open
iyang-pythnon wants to merge 1 commit into
base-add-ifpresent-validator-r-11722390from
iyang-pythnon/add-ifpresent-validator-r-11722390
Open

iyang-pythnon wants to merge 1 commit into
base-add-ifpresent-validator-r-11722390from
iyang-pythnon/add-ifpresent-validator-r-11722390

Conversation

@iyang-pythnon

@iyang-pythnon iyang-pythnon commented Feb 14, 2026 •

Copy link
Copy Markdown

Note

Medium Risk
Changes validation semantics by allowing missing variables to bypass certain assertions, which could hide configuration mistakes if callers switch from required() to ifPresent(). Otherwise it’s a small, well-tested API addition with default behavior preserved.

Overview
Adds Dotenv::ifPresent() to create a Validator that does not fail on missing variables, enabling optional env vars to be type/emptiness/value-validated only when present.

Updates Validator to accept a $required flag (defaulting to current behavior) and adjusts notEmpty(), isInteger(), and isBoolean() to treat null (missing) as passing when using the optional mode; expands PHPUnit coverage for ifPresent across allowed-values, emptiness, boolean, and integer validations.

Written by Cursor Bugbot for commit f656e8b. This will update automatically on new commits. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread src/Validator.php
return true;
}

if ($value === '') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowedValues doesn't skip null for ifPresent validators

High Severity

The allowedValues() method was not updated to return true when $value === null, unlike notEmpty(), isInteger(), and isBoolean() which all received this treatment. When a variable is absent and validated via ifPresent()->allowedValues(...), in_array(null, $choices, true) returns false, causing a spurious validation exception. This defeats the purpose of ifPresent for the allowedValues check.

Fix in Cursor Fix in Web

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant