From c45e4edcf66b165484afd33be4f97556503dbc07 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Mon, 1 Jul 2024 15:12:03 -0400 Subject: [PATCH] Allow port rule to be optional --- app/Rules/Port.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/Rules/Port.php b/app/Rules/Port.php index 910d00379..9f99ed622 100644 --- a/app/Rules/Port.php +++ b/app/Rules/Port.php @@ -8,26 +8,30 @@ use Illuminate\Contracts\Validation\ValidationRule; class Port implements ValidationRule { - /** - * Run the validation rule. - * - * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail - */ public function validate(string $attribute, mixed $value, Closure $fail): void { + // Allow port to be optional + if (empty($value)) { + return; + } + + // Require port to be a number if (!is_numeric($value)) { $fail('The :attribute must be numeric.'); } + // Require port to be an integer $value = intval($value); if (floatval($value) !== (float) $value) { $fail('The :attribute must be an integer.'); } + // Require minimum valid port if ($value <= Endpoint::PORT_FLOOR) { $fail('The :attribute must be greater than 1024.'); } + // Require maximum valid port if ($value > Endpoint::PORT_CEIL) { $fail('The :attribute must be less than 65535.'); }