16 February 2022

TIL the difference between Elixir URI's parse/1 and new/1

TIL the difference between URI​.new/1 and URI.parse/1 in Elixir 1.13.

  • new/1 does a bunch of validation to ensure the URI is within spec
  • parse/1 does no validation at all

Lots of URIs in the wild are technically invalid, but web browsers handle them just fine.

Obviously URI.parse/1 is somewhat unsafe, so in our app we’ve combined it with some hand-rolled validation to ensure the URI is vaguely URL-like:

  • It has a scheme of HTTP or HTTPS
  • The authority contains a plausible TLD