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

MASNathan
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor Author

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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

@jmarcher jmarcher Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed validateUrl condition

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

@taylorotwell
Copy link
Member

Does this regex match the regex used by Ramsey UUID?

@svenluijten
Copy link
Contributor

@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants