Skip to content

[12.x] Allow strict integer validation #56503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025

Conversation

zll600
Copy link
Contributor

@zll600 zll600 commented Jul 31, 2025

Changes

Add strict parameters to the Integer validation rule to fail the non-integer values for the Integer validation.

$v = new Validator($trans, ['k1' => '1'], ['k1' => 'Integer:strict']); // fails
$v = new Validator($trans, ['k1' => 1], ['k1' => 'Integer:strict']); // passes

Why

This would allow for blocking non-integers from being used in JSON requests for integers. It will help us reduce errors like Argument #1 ($xxx) must be of type int, string given.

And laravel already supported the strict boolean and numeric validation.
F.Y.I: #56328 and #56313

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@zll600 zll600 changed the title Allow strict integer validation [12.x] Allow strict integer validation Jul 31, 2025
@zll600 zll600 marked this pull request as ready for review July 31, 2025 15:21
@zll600
Copy link
Contributor Author

zll600 commented Aug 1, 2025

@peterfox Hi, would you like to help me review this PR when you are free?

@peterfox
Copy link
Contributor

peterfox commented Aug 1, 2025

Can you not just use numeric:strict and integer rules together to get what you need?

@zll600
Copy link
Contributor Author

zll600 commented Aug 2, 2025

Thank you very much for your review.

Can you not just use numeric:strict and integer rules together to get what you need?

I think numeric:strict|integer cannot help me solve my problems. Let me give you an example:

  1. The 1.0 can pass the numeric:strict|integer validation.
        $v = new Validator($trans, ['foo' => 1.0], ['foo' => 'numeric:strict|integer'])->passes()); // pass
        $v = new Validator($trans, ['foo' => 1.0], ['foo' => 'integer:strict'])->passes()); // fail
  1. The 1.0 that passes the validation still results in a TypeError
function test1(int $num): void {}
test1(1.0); // Throw type error here

Other consideration

  • integer:strict is clearer and more intuitive, which can:
    • express the intention clearly
    • reduce typing and cognitive load
  • numeric:strict|integer is more complex, which results:
    • Developers must understand more edge cases to use them correctly.
    • It may lead to bugs if you forget to add numeric:strict
    • Requires understanding two separate validation rules instead of one

If you have any questions, please let me know. I will try my best to find a good way with you to resolve this problem.

@taylorotwell taylorotwell merged commit a3267a3 into laravel:12.x Aug 3, 2025
45 of 60 checks passed
AhmedAlaa4611 referenced this pull request in laravel/docs Aug 6, 2025
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.

3 participants