Compare commits

...

2 Commits

Author SHA1 Message Date
kj
2e96aa886e test(validator): rename enum rule to in 2026-09-14 16:46:07 -03:00
kj
f4b112032e refactor(validator): rename enum rule and method to in 2026-09-14 16:43:09 -03:00
2 changed files with 20 additions and 12 deletions

View File

@@ -38,7 +38,7 @@ use Exception;
* | url | | Must be a valid URL |
* | date | | Must be a parseable date |
* | regex | :pattern | Must match a PCRE pattern (with delimiters) |
* | enum | :a,b,c | Must be one of the listed values |
* | in | :a,b,c | Must be one of the listed values |
* | min | :n | Length/number/count min (files: KB) |
* | max | :n | Length/number/count max (files: KB) |
* | between | :min,:max | Value within range (files: KB per upload) |
@@ -105,7 +105,7 @@ class Validator
* | :size | the size rule value |
* | :value | the required_if expected value |
* | :other | the required_if companion field / confirmation |
* | :values | a comma list (enum, mimes, required_with) |
* | :values | a comma list (in, mimes, required_with) |
* |----------+----------------------------------------------------|
*
* @var array<string,string>
@@ -122,7 +122,7 @@ class Validator
'array' => 'The :attribute must be an array.',
'email' => 'The :attribute must be a valid email address.',
'url' => 'The :attribute must be a valid URL.',
'enum' => 'The selected :attribute is invalid. Allowed: :values.',
'in' => 'The selected :attribute is invalid. Allowed: :values.',
'min' => 'The :attribute must be at least :min.',
'max' => 'The :attribute must not be greater than :max.',
'between' => 'The :attribute must be between :min and :max.',
@@ -243,7 +243,7 @@ class Validator
case 'size':
$replace[':size'] = $args[0] ?? '';
break;
case 'enum':
case 'in':
case 'mimes':
case 'required_with':
$replace[':values'] = implode(', ', $args);
@@ -305,7 +305,7 @@ class Validator
* arguments legitimately contain ':' or ',' - such as regex or mimes -
* are preserved intact. The caller decides how to split the arguments.
*
* @param string $rule The rule to parse. Ex: "regex:/^a,b$/" or "enum:a,b".
* @param string $rule The rule to parse. Ex: "regex:/^a,b$/" or "in:a,b".
*
* @return array A two element array: [name, rawArguments]. When the rule
* has no parameters, rawArguments is an empty string.
@@ -419,7 +419,7 @@ class Validator
* Splits a rule's raw argument string into the arguments to pass to the
* rule's method (the subject is not included).
*
* Most rules take a comma-separated list of values (e.g. "enum:a,b").
* Most rules take a comma-separated list of values (e.g. "in:a,b").
* A few rules receive an argument that must be kept intact because it can
* legitimately contain commas or colons: "regex" (a pattern) and "not"
* (a sub-rule that is itself parsed recursively). An empty argument string
@@ -810,7 +810,7 @@ class Validator
*
* @return bool
*/
public static function enum(mixed $subject, ...$values): bool
public static function in(mixed $subject, ...$values): bool
{
return in_array($subject, $values);
}

View File

@@ -81,10 +81,18 @@ final class ValidatorTest extends TestCase
#[Test]
public function enumIsLooseComparison(): void
public function inIsLooseComparison(): void
{
$this->assertTrue(Validator::checkRule('1', 'enum:1,2,3'));
$this->assertFalse(Validator::checkRule('9', 'enum:1,2,3'));
$this->assertTrue(Validator::checkRule('1', 'in:1,2,3'));
$this->assertFalse(Validator::checkRule('9', 'in:1,2,3'));
}
#[Test]
public function enumRuleIsNoLongerRecognized(): void
{
$this->expectException(\Exception::class);
Validator::checkRule('1', 'enum:1,2,3');
}
#[Test]
@@ -100,7 +108,7 @@ final class ValidatorTest extends TestCase
public function parseRule(): void
{
$this->assertSame(['required', ''], Validator::parseRule('required'));
$this->assertSame(['enum', 'a,b'], Validator::parseRule('enum:a,b'));
$this->assertSame(['in', 'a,b'], Validator::parseRule('in:a,b'));
$this->assertSame(['regex', '/^a,b$/'], Validator::parseRule('regex:/^a,b$/'));
}
@@ -168,7 +176,7 @@ final class ValidatorTest extends TestCase
);
$this->assertSame(
'The selected status is invalid. Allowed: a, b, c.',
Validator::message('status.enum:a,b,c')
Validator::message('status.in:a,b,c')
);
$this->assertSame(
'The reason field is required when mode is other.',