dry-rb / dry-logic

Predicate logic with rule composition
https://dry-rb.org/gems/dry-logic/
MIT License
179 stars 66 forks source link

Add uri_rfc3986? matcher to support validating full URL #94

Closed hieuk09 closed 2 years ago

hieuk09 commented 2 years ago

I use uri? predicate to validate whether a string is an valid URI. However, this case fails:

schema = Dry::Schema.JSON do
  required(:picture_url).filled(:string, uri?: 'https')
end

schema.call(picture_url: '{https://picture.com/some_image.png}')
# => return successful, while it should be failed.

Upon reading code of URI module, I notice that URI::DEFAULT_PARSER.make_regexp creates a regex that matches only part of the string, and I have no way to avoid that behavior, aside from switching to URI::RFC3986_Parser::RFC3986_URI:

schema = Dry::Schema.JSON do
  required(:picture_url).filled(:string, format?: URI::RFC3986_Parser::RFC3986_URI)
end

However, this prevent me from using the nice error message that comes with uri? predicate. I suppose some people would have the same problems as me, so I would like to contribute it back to the repo.

I understand that changing uri? itself would be a breaking change, so I want to avoid that, and add a new uri_rfc3986? predicate instead. If people start using it over uri?, maybe we can consider deprecate uri? and replace it with uri_rfc3986? in v2.0.

hieuk09 commented 2 years ago

I see that there are some rubocop errors that are not related to the changes in this PR, should I fix them?

flash-gordon commented 2 years ago

@hieuk09 I fixed them in master, pls rebase!

hieuk09 commented 2 years ago

Thank you, I rebased the branch.

flash-gordon commented 2 years ago

@solnic thoughts?