Skip to content

Commit

Permalink
[9.x] Add ascii and ulid validation rules (#45218)
Browse files Browse the repository at this point in the history
* add `ulid` validation rule

* add `ascii` validation rule

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
hafezdivandari and taylorotwell committed Dec 9, 2022
1 parent 1c10e4f commit 1544f6f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Expand Up @@ -142,6 +142,18 @@ protected function getDnsRecords($hostname, $type)
return dns_get_record($hostname, $type);
}

/**
* Validate that an attribute is 7 bit ASCII.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateAscii($attribute, $value)
{
return Str::isAscii($value);
}

/**
* "Break" on first validation fail.
*
Expand Down Expand Up @@ -2139,6 +2151,18 @@ public function validateUrl($attribute, $value)
return preg_match($pattern, $value) > 0;
}

/**
* Validate that an attribute is a valid ULID.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateUlid($attribute, $value)
{
return Str::isUlid($value);
}

/**
* Validate that an attribute is a valid UUID.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -6726,6 +6726,34 @@ public static function invalidUuidList()
];
}

public function testValidateWithValidAscii()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'Dusseldorf'], ['foo' => 'ascii']);
$this->assertTrue($v->passes());
}

public function testValidateWithInvalidAscii()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'Düsseldorf'], ['foo' => 'ascii']);
$this->assertFalse($v->passes());
}

public function testValidateWithValidUlid()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => '01gd6r360bp37zj17nxb55yv40'], ['foo' => 'ulid']);
$this->assertTrue($v->passes());
}

public function testValidateWithInvalidUlid()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => '01gd6r36-bp37z-17nx-55yv40'], ['foo' => 'ulid']);
$this->assertFalse($v->passes());
}

public static function providesPassingExcludeIfData()
{
return [
Expand Down

0 comments on commit 1544f6f

Please sign in to comment.