Skip to content

Add Multiple Built-in Validation Attributes#31

Merged
MayMeow merged 8 commits into
masterfrom
copilot/fix-19
May 28, 2025
Merged

Add Multiple Built-in Validation Attributes#31
MayMeow merged 8 commits into
masterfrom
copilot/fix-19

Conversation

Copilot AI commented May 28, 2025

Copy link
Copy Markdown
Contributor

This PR adds a comprehensive set of built-in validation attributes to reduce the need for custom rule implementations for common validation scenarios.

Added Validation Attributes

Basic Constraints:

  • Required - Field must be present (but can be empty)
  • NotEmpty (existing) - Field must be present and not empty

String Constraints:

  • MaxLength - Limits string length to a specified number of characters
  • MinLength - Ensures string has at least a specified number of characters

Numerical Constraints:

  • Min - Ensures numeric value is greater than or equal to specified minimum
  • Max - Ensures numeric value is less than or equal to specified maximum
  • Between - Ensures numeric value is within a specified range

Format Constraints:

  • Email - Validates correct email format
  • Url - Validates correct URL format
  • Regex - Allows custom regex pattern validation

Collection Constraints:

  • Each - Applies a validator to each element in an array

Implementation Details

All attributes:

  • Extend BaseValidatorAttribute
  • Implement the ValidatorAttributeInterface
  • Include appropriate validation logic in their validate() method
  • Support custom error messages

Example Usage

class UserModel 
{
    #[Required]
    #[NotEmpty]
    #[MaxLength(50)]
    public string $name;

    #[Email]
    #[Required]
    public string $email;

    #[Min(1)]
    #[Max(100)]
    public int $age;
}

Documentation

  • Updated README.md with examples and details for all new validation attributes
  • Added comprehensive tests for each validation attribute

Fixes #19.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.github.com
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/z6uqU1 /usr/bin/composer install (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits May 28, 2025 09:27
Co-authored-by: MayMeow <3164256+MayMeow@users.noreply.github.com>
Co-authored-by: MayMeow <3164256+MayMeow@users.noreply.github.com>
Copilot AI changed the title [WIP] Feature Request: Provide More Built-in Validation Attributes Add Multiple Built-in Validation Attributes May 28, 2025
Copilot AI requested a review from MayMeow May 28, 2025 09:29
$notEmpty = new NotEmpty();
$this->assertFalse($notEmpty->validate(''));
$this->assertTrue($notEmpty->validate('value'));
$this->assertTrue($notEmpty->validate(0));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

php's empty() function considering zero as empty value so the NotEmpty atribute will return wrong validation. Can you fix it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 5b953f7. Changed the NotEmpty validator implementation from using PHP's empty() function to explicit checks for null and empty string only. Now integer 0 is correctly treated as valid (not empty).

}

return $value >= $this->min && $value <= $this->max;
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

You testing here length of string not the value itself... as few lines above you returning false when the length is numeric.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit 6bb7661. Changed the numeric validators (Min, Max, Between) to use is_int($value) || is_float($value) instead of is_numeric($value) to only accept actual numeric types, not string representations. This ensures consistency with the test expectations where string values like '7' should be rejected.

@MayMeow MayMeow left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I reviewed changes

Co-authored-by: MayMeow <3164256+MayMeow@users.noreply.github.com>

Copilot AI commented May 28, 2025

Copy link
Copy Markdown
Contributor Author

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.github.com
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/jJqN7w /usr/bin/composer install --no-interaction (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

…resentations

Co-authored-by: MayMeow <3164256+MayMeow@users.noreply.github.com>

Copilot AI commented May 28, 2025

Copy link
Copy Markdown
Contributor Author

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.github.com
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/E3Vxg3 /usr/bin/composer install --no-interaction (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Comment thread tests/ValidatorAttributesTest.php
Co-authored-by: MayMeow <3164256+MayMeow@users.noreply.github.com>
@MayMeow

MayMeow commented May 28, 2025

Copy link
Copy Markdown
Owner

@copilot can you also add docblock to other validators you created?

Co-authored-by: MayMeow <3164256+MayMeow@users.noreply.github.com>

Copilot AI commented May 28, 2025

Copy link
Copy Markdown
Contributor Author

@copilot can you also add docblock to other validators you created?

Added comprehensive docblocks to all validation attributes in commit 5fd386b. Each validator now includes class-level documentation, constructor parameter details, and validate method documentation.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.github.com
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/q2LsUY /usr/bin/composer install --no-interaction (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@MayMeow MayMeow marked this pull request as ready for review May 28, 2025 19:53
@MayMeow MayMeow merged commit b952549 into master May 28, 2025
9 checks passed
MayMeow

This comment was marked as resolved.

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.

Feature Request: Provide More Built-in Validation Attributes

2 participants