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

[5.7] Add uuid validation rule to validator #26135

Merged
merged 8 commits into from
Oct 22, 2018
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
16 changes: 16 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,22 @@ public function validateUrl($attribute, $value)
return preg_match($pattern, $value) > 0;
}

/**
* Validate that an attribute is a valid UUID.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateUuid($attribute, $value)
{
if (! is_string($value)) {
return false;
}

return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;
}

/**
* Get the size of an attribute.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4211,6 +4211,58 @@ public function testValidateReturnsValidatedDataNestedArrayRules()
$this->assertEquals(['nested' => [['bar' => 'baz'], ['bar' => 'baz2']]], $data);
}

/**
* @dataProvider validUuidList
*/
public function testValidateWithValidUuid($uuid)
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => $uuid], ['foo' => 'uuid']);
$this->assertTrue($v->passes());
}

/**
* @dataProvider invalidUuidList
*/
public function testValidateWithInvalidUuid($uuid)
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => $uuid], ['foo' => 'uuid']);
$this->assertFalse($v->passes());
}

public function validUuidList()
{
return [
['a0a2a2d2-0b87-4a18-83f2-2529882be2de'],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
['00000000-0000-0000-0000-000000000000'],
['e60d3f48-95d7-4d8d-aad0-856f29a27da2'],
['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
];
}

public function invalidUuidList()
{
return [
['not a valid uuid so we can test this'],
['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'.PHP_EOL],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1 '],
[' 145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
['145a1e72-d11d-11e8-a8d5-f2z01f1b9fd1'],
['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'],
['af6f8cb-c57d-11e1-9b21-0800200c9a66'],
['af6f8cb0c57d11e19b210800200c9a66'],
['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
];
}

protected function getTranslator()
{
return m::mock(TranslatorContract::class);
Expand Down