Skip to content

Commit

Permalink
Adjust validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rdk08 committed Nov 10, 2023
1 parent aeb3c1a commit ac4bec8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/plausible/helpers/value.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Plausible.ValueHelpers do
@prefix_pattern "[a-zA-Z]+"
@prefix_pattern "[a-zA-Z]+(-[a-zA-Z]+)*"
@id_pattern "\\d+"
@uuid_pattern "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"

Expand Down
56 changes: 56 additions & 0 deletions test/plausible/helpers/value_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule Plausible.ValueHelpersTest do
use Plausible.DataCase
use Timex

alias Plausible.ValueHelpers

describe "validate/2" do
test "returns prefixed value (with integer id) if it is successfully validated" do
value = "vendor-123"

assert ValueHelpers.validate(value, type: :prefixed_id) == value
end

test "returns prefixed value (with uuid) if it is successfully validated" do
value = "vendor-c2b5da86-851a-4aee-ac48-19d6069556c5"

assert ValueHelpers.validate(value, type: :prefixed_id) == value
end

test "returns prefixed value (with multipart prefix and id) if it is successfully validated" do
value = "other-vendor-prefix-456"

assert ValueHelpers.validate(value, type: :prefixed_id) == value
end

test "returns prefixed value (with multipart prefix and uuid id) if it is successfully validated" do
value = "other-vendor-prefix-782f008a-b478-4aac-8448-16569a0e4501"

assert ValueHelpers.validate(value, type: :prefixed_id) == value
end

test "returns nil if value does not match predefined pattern (missing id)" do
value = "vendor"

refute ValueHelpers.validate(value, type: :prefixed_id)
end

test "returns nil if value does not match predefined pattern (wrong id)" do
value = "vendor-123d"

refute ValueHelpers.validate(value, type: :prefixed_id)
end

test "returns nil if value does not match predefined pattern (wrong uuid)" do
value = "vendor-abcdefgh123-782f008a-b478-4aac-8448"

refute ValueHelpers.validate(value, type: :prefixed_id)
end

test "returns nil if value does not match predefined pattern (no prefix)" do
value = "123"

refute ValueHelpers.validate(value, type: :prefixed_id)
end
end
end

0 comments on commit ac4bec8

Please sign in to comment.