$rules['memo'], 'allowed_ips' => [...$rules['allowed_ips'], 'list', 'max:50'], 'allowed_ips.*' => ['string'], ]; } /** * Check that each of the values entered is actually valid. */ /** * @return list */ public function after(): array { return [function (Validator $validator): void { if (! is_array($ips = $this->input('allowed_ips'))) { return; } foreach ($ips as $index => $ip) { $valid = false; if (! is_string($ip)) { $validator->errors()->add("allowed_ips.{$index}", 'The IP address or CIDR range must be a string.'); continue; } try { $valid = Range::parse($ip)->valid(); } catch (Exception $exception) { throw_if($exception->getMessage() !== 'Invalid IP address format', $exception); } finally { $validator->errors()->addIf(! $valid, "allowed_ips.{$index}", '"'.$ip.'" is not a valid IP address or CIDR range.'); } } }]; } }