Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add ascii and ulid validation rules #45218

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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