fix(rule): membuat custom rule untuk validasi max integer dan big integer

This commit is contained in:
Yoga Pangestu 2025-09-28 22:17:35 +07:00
parent 0f7d1a131d
commit 59d7e8da2c
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class UnsignedBigInteger implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! ctype_digit((string) $value)) {
$fail("{$attribute} harus berupa bilangan bulat positif.");
return;
}
if (bccomp($value, '0') < 0 || bccomp($value, '18446744073709551615') > 0) {
$fail("{$attribute} harus berada di antara 0 dan 18446744073709551615.");
}
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class UnsignedInteger implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! ctype_digit((string) $value)) {
$fail("{$attribute} harus berupa bilangan bulat positif.");
return;
}
if ((int) $value < 0 || (int) $value > 4294967295) {
$fail("{$attribute} harus berada di antara 0 dan 4294967295.");
}
}
}