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

Conversation

Projects
None yet
8 participants
@MASNathan
Copy link
Contributor

commented Oct 16, 2018

Often there is the need to extend the Validator to add a uuid validation rule like this below.

Validator::extend('uuid', function ($attribute, $value, $parameters, $validator) {
    return \Ramsey\Uuid\Uuid::isValid($value);
});

Since Laravel already depends on ramsey/uuid, I think this would be a nice little addition to Laravel's validation ruleset.

Let me know what you think

@MASNathan MASNathan changed the title Added uuid validation rule to validator [5.7] Add uuid validation rule to validator Oct 16, 2018

@X-Coder264

This comment has been minimized.

Copy link
Contributor

commented Oct 16, 2018

Since Laravel already depends on ramsey/uuid, I think this would be a nice little addition to Laravel's validation ruleset.

The illuminate/validation component/package does not depend on ramsey/uuid though and that component can be used outside of a full Laravel framework application.

@MASNathan

This comment has been minimized.

Copy link
Contributor Author

commented Oct 17, 2018

The illuminate/validation component/package does not depend on ramsey/uuid though and that component can be used outside of a full Laravel framework application.

@X-Coder264 Didn't think of that, I removed the dependency. Now it uses regex directly

return false;
}
if ($value === '00000000-0000-0000-0000-000000000000') {

This comment has been minimized.

Copy link
@mfn

mfn Oct 17, 2018

Contributor

🤔

This comment has been minimized.

Copy link
@MASNathan

MASNathan Oct 17, 2018

Author Contributor

Following ramsey/uuid validation, this is a nil uuid, which is considered valid. We can choose to accept nil uuids or not. What do you think?

This comment has been minimized.

Copy link
@jmarcher

jmarcher Oct 17, 2018

Contributor

I think he means that you just hardcoded a case. Make your regex include that case.

Edit: I just saw your regex, it already validate the nil UUID, did you tested this regex or did you just made a copypasta from StackOverflow?

This comment has been minimized.

Copy link
@MASNathan

MASNathan Oct 17, 2018

Author Contributor

Gotcha!

I followed the ramsey/uuid validation workflow. They check it agains 00000000-0000-0000-0000-000000000000 before the regex so I did the same.

return false;
}
return preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/D', $value);

This comment has been minimized.

Copy link
@lucasmichot

lucasmichot Oct 17, 2018

Contributor

As the check is case insensitive it makes sense to use the i option.
Also you can use \d for digits.

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

This comment has been minimized.

Copy link
@ntzm

ntzm Oct 17, 2018

Contributor

preg_match doesn't return bool, it returns int:

Suggested change
return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value);
return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) === 1;

This comment has been minimized.

Copy link
@MASNathan

MASNathan Oct 17, 2018

Author Contributor

Followed validateUrl condition

return preg_match($pattern, $value) > 0;

MASNathan added some commits Oct 17, 2018

Ups
@taylorotwell

This comment has been minimized.

Copy link
Member

commented Oct 18, 2018

Does this regex match the regex used by Ramsey UUID?

@svenluijten

This comment has been minimized.

Copy link
Contributor

commented Oct 18, 2018

@taylorotwell Functionally it's the same, but the regex this PR uses \d instead of 0-9, and adds the i flag to indicate case-insensitivity instead of being explicit (A-Fa-f). See ramsey/uuid's regex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.