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

UrlValidator should mark URLs w/ crap in them as invalid. #134

Merged

Conversation

mjgiarlo
Copy link
Contributor

@mjgiarlo mjgiarlo commented Jul 7, 2016

Currently, if you enter a string like 'http://example.com/ (this is kinda crappy)' as your website, the UrlValidator throws URI::InvalidURIError which was not previously rescued. The validator now raises that error and marks the record as invalid in two cases:

  1. When there is a crappy URL per above; and
  2. When a non-HTTP URL is entered as a website

@mjgiarlo
Copy link
Contributor Author

mjgiarlo commented Jul 7, 2016

It's not immediately clear to me why Travis barfed on this PR -- the 🔴 tests do not at first glance appear to be related to the changes I made, but I'm happy to be convinced otherwise and to make the changes to get this 💚.

@phiggins
Copy link
Member

phiggins commented Jul 9, 2016

It's not immediately clear to me why Travis barfed on this PR

There are tests that rely on live, 3rd party servers so they fail quite often. 😞 If those are the only ones failing I would not worry about it.

end
raise URI::InvalidURIError unless URI.parse(value).kind_of?(URI::HTTP)
rescue URI::InvalidURIError
record.errors[attribute] << (options[:message] || "is an invalid URL")
Copy link
Member

Choose a reason for hiding this comment

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

Raising an exception and then immediately rescuing it seems bad. What if you separated the URL parsing from the bit dealing with the validation error?

unless valid_url?(value)
  record.errors[attribute] << (options[:message] || "is an invalid URL")
end

def valid_url?(value)
  URI.parse(value).kind_of?(URI::HTTP)
rescue URI::InvalidURIError
  false
end

@phiggins
Copy link
Member

phiggins commented Jul 9, 2016

Thanks for your contribution! 💖

Currently, if you enter a string like 'http://example.com/ (this is kinda crappy)' as your website, the UrlValidator throws  which was not previously rescued. The validator now raises that error in two cases:

 1. When there is a crappy URL per above; and
 2. When a non-HTTP URI is entered as a website
@mjgiarlo mjgiarlo force-pushed the check_for_different_sort_of_invalid_URIs branch from 9620cfc to 6f46361 Compare July 11, 2016 03:29
@mjgiarlo
Copy link
Contributor Author

Changes made, @phiggins. Could use another review.

@phiggins
Copy link
Member

Looks good to me. Thanks! 👍

@phiggins phiggins merged commit 76f56d4 into seattlerb:master Jul 20, 2016
@mjgiarlo mjgiarlo deleted the check_for_different_sort_of_invalid_URIs branch July 20, 2016 04:02
@phiggins
Copy link
Member

I just deployed this. Please let me know if everything's not working as you'd expect!

@mjgiarlo
Copy link
Contributor Author

Worked like a charm. Thanks, @phiggins!

@seattlerb seattlerb locked and limited conversation to collaborators Jan 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants