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

Validate UUIDs with regex. (#41) #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/uuid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,22 @@ defmodule UUID do
version: 4,
variant: :rfc4122]

iex> UUID.info("0000000000000000")
{:error, "Invalid argument; Not a valid UUID: 0000000000000000"}

```

"""
def info!(<<uuid::binary>> = uuid_string) do
regex = ~r/[\w]{8}-?[\w]{4}-?[\w]{4}-?[\w]{4}-?[\w]{12}/
{type, <<uuid::128>>} = uuid_string_to_hex_pair(uuid)

# A bitstring can be an invalid string, in which case its a raw UUID,
# otherwise we match it against a regex to see if it's a valid UUID.
if String.valid?(uuid_string), do:
if not String.match?(uuid_string, regex), do:
raise ArgumentError, message: "Invalid argument; Not a valid UUID: #{uuid_string}"

<<_::48, version::4, _::12, v0::1, v1::1, v2::1, _::61>> = <<uuid::128>>
[uuid: uuid_string,
binary: <<uuid::128>>,
Expand Down