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

[10.x] Allow to specify protocols in URL validation #47381

Closed
wants to merge 1 commit into from

Conversation

MrMicky-FR
Copy link
Contributor

@MrMicky-FR MrMicky-FR commented Jun 8, 2023

This PR makes it possible to specify which protocols are allowed in URL validation.

This can be useful in two situations:

  • To use exotic protocols that are not in the hardcoded protocols list without having to modify the list directly in Laravel
  • To authorize only some specific protocols (e.g. only https to force secure links)

There is no breaking changes, as the default behavior (when no parameters are used) is the same as current: only the protocols in the existing list are allowed.

Example usage:

$v = Validator::make($data, [
    'url' => 'sometimes|url', // current behaviour
    'link' => 'sometimes|url:http,https', // only http and https
    'secure_link' => 'sometimes|url:https', // only https
]);

@taylorotwell
Copy link
Member

Just combine url and starts_with validation rules.

@MrMicky-FR
Copy link
Contributor Author

Just combine url and starts_with validation rules.

@taylorotwell Thank you, it work in the second case, but, in the first case it’s not a solution as the rule will always fail if the protocol isn't in the list (just realized I added a test for this use case, but forgot it in my code example, sorry!)

In my case the valid minecraft:// protocol is failing, so there is a need for a new PR each time a new protocol is required ?

Updated example usage:

$v = Validator::make($data, [
    'url' => 'sometimes|url', // current behaviour
    'link' => 'sometimes|url:http,https', // only http and https
    'game_join_url' => 'sometimes|url:minecraft,steam', // also support more protocols such as 'minecraft://'
]);

Thanks again

@MrMicky-FR MrMicky-FR deleted the validator_url_protocols branch July 18, 2023 14:51
@MrMicky-FR MrMicky-FR restored the validator_url_protocols branch July 24, 2023 15:42
@MrMicky-FR MrMicky-FR deleted the validator_url_protocols branch July 26, 2023 18:45
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.

2 participants