Skip to content

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

@github-actions
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

3 participants