Skip to content

Commit e25d97e

Browse files
authored
Merge pull request #27 from Recruitee/adjust-validation-for-additional-fields
Adjust validation
2 parents aeb3c1a + ac4bec8 commit e25d97e

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

lib/plausible/helpers/value.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule Plausible.ValueHelpers do
2-
@prefix_pattern "[a-zA-Z]+"
2+
@prefix_pattern "[a-zA-Z]+(-[a-zA-Z]+)*"
33
@id_pattern "\\d+"
44
@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}"
55

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
defmodule Plausible.ValueHelpersTest do
2+
use Plausible.DataCase
3+
use Timex
4+
5+
alias Plausible.ValueHelpers
6+
7+
describe "validate/2" do
8+
test "returns prefixed value (with integer id) if it is successfully validated" do
9+
value = "vendor-123"
10+
11+
assert ValueHelpers.validate(value, type: :prefixed_id) == value
12+
end
13+
14+
test "returns prefixed value (with uuid) if it is successfully validated" do
15+
value = "vendor-c2b5da86-851a-4aee-ac48-19d6069556c5"
16+
17+
assert ValueHelpers.validate(value, type: :prefixed_id) == value
18+
end
19+
20+
test "returns prefixed value (with multipart prefix and id) if it is successfully validated" do
21+
value = "other-vendor-prefix-456"
22+
23+
assert ValueHelpers.validate(value, type: :prefixed_id) == value
24+
end
25+
26+
test "returns prefixed value (with multipart prefix and uuid id) if it is successfully validated" do
27+
value = "other-vendor-prefix-782f008a-b478-4aac-8448-16569a0e4501"
28+
29+
assert ValueHelpers.validate(value, type: :prefixed_id) == value
30+
end
31+
32+
test "returns nil if value does not match predefined pattern (missing id)" do
33+
value = "vendor"
34+
35+
refute ValueHelpers.validate(value, type: :prefixed_id)
36+
end
37+
38+
test "returns nil if value does not match predefined pattern (wrong id)" do
39+
value = "vendor-123d"
40+
41+
refute ValueHelpers.validate(value, type: :prefixed_id)
42+
end
43+
44+
test "returns nil if value does not match predefined pattern (wrong uuid)" do
45+
value = "vendor-abcdefgh123-782f008a-b478-4aac-8448"
46+
47+
refute ValueHelpers.validate(value, type: :prefixed_id)
48+
end
49+
50+
test "returns nil if value does not match predefined pattern (no prefix)" do
51+
value = "123"
52+
53+
refute ValueHelpers.validate(value, type: :prefixed_id)
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)