Skip to content

Commit 1544f6f

Browse files
[9.x] Add ascii and ulid validation rules (#45218)
* add `ulid` validation rule * add `ascii` validation rule * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1c10e4f commit 1544f6f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ protected function getDnsRecords($hostname, $type)
142142
return dns_get_record($hostname, $type);
143143
}
144144

145+
/**
146+
* Validate that an attribute is 7 bit ASCII.
147+
*
148+
* @param string $attribute
149+
* @param mixed $value
150+
* @return bool
151+
*/
152+
public function validateAscii($attribute, $value)
153+
{
154+
return Str::isAscii($value);
155+
}
156+
145157
/**
146158
* "Break" on first validation fail.
147159
*
@@ -2139,6 +2151,18 @@ public function validateUrl($attribute, $value)
21392151
return preg_match($pattern, $value) > 0;
21402152
}
21412153

2154+
/**
2155+
* Validate that an attribute is a valid ULID.
2156+
*
2157+
* @param string $attribute
2158+
* @param mixed $value
2159+
* @return bool
2160+
*/
2161+
public function validateUlid($attribute, $value)
2162+
{
2163+
return Str::isUlid($value);
2164+
}
2165+
21422166
/**
21432167
* Validate that an attribute is a valid UUID.
21442168
*

tests/Validation/ValidationValidatorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6726,6 +6726,34 @@ public static function invalidUuidList()
67266726
];
67276727
}
67286728

6729+
public function testValidateWithValidAscii()
6730+
{
6731+
$trans = $this->getIlluminateArrayTranslator();
6732+
$v = new Validator($trans, ['foo' => 'Dusseldorf'], ['foo' => 'ascii']);
6733+
$this->assertTrue($v->passes());
6734+
}
6735+
6736+
public function testValidateWithInvalidAscii()
6737+
{
6738+
$trans = $this->getIlluminateArrayTranslator();
6739+
$v = new Validator($trans, ['foo' => 'Düsseldorf'], ['foo' => 'ascii']);
6740+
$this->assertFalse($v->passes());
6741+
}
6742+
6743+
public function testValidateWithValidUlid()
6744+
{
6745+
$trans = $this->getIlluminateArrayTranslator();
6746+
$v = new Validator($trans, ['foo' => '01gd6r360bp37zj17nxb55yv40'], ['foo' => 'ulid']);
6747+
$this->assertTrue($v->passes());
6748+
}
6749+
6750+
public function testValidateWithInvalidUlid()
6751+
{
6752+
$trans = $this->getIlluminateArrayTranslator();
6753+
$v = new Validator($trans, ['foo' => '01gd6r36-bp37z-17nx-55yv40'], ['foo' => 'ulid']);
6754+
$this->assertFalse($v->passes());
6755+
}
6756+
67296757
public static function providesPassingExcludeIfData()
67306758
{
67316759
return [

0 commit comments

Comments
 (0)