diff --git a/lib/plausible/helpers/value.ex b/lib/plausible/helpers/value.ex index 8f706bca46ec..93005005dafa 100644 --- a/lib/plausible/helpers/value.ex +++ b/lib/plausible/helpers/value.ex @@ -1,9 +1,13 @@ defmodule Plausible.ValueHelpers do + @prefix_pattern "[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}" + @spec validate(any(), keyword()) :: any() def validate(value, type: :prefixed_id) when is_binary(value) do - prefixed_id_pattern = ~r/\A\w+-\d+\Z/ + pattern = ~r/\A(#{@prefix_pattern})-(#{@id_pattern}|#{@uuid_pattern})\Z/ - if Regex.match?(prefixed_id_pattern, value), do: value, else: nil + if Regex.match?(pattern, value), do: value, else: nil end def validate(nil, _), do: nil diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 170b38302604..948e9a56ec44 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -502,7 +502,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do company_id: 10, job_id: 12, page_id: 15, - product_id: "vendor-11", + product_id: "vendor-6a01cfb6-717a-11ee-b962-0242ac120002", site_id: 9, careers_application_form_uuid: "313a26c2-741c-421c-9a6b-f39c02c8d35c" } @@ -514,7 +514,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do event = get_event("special-props-test.com") assert Map.get(event, :campaign_id) == "vendor-8" - assert Map.get(event, :product_id) == "vendor-11" + assert Map.get(event, :product_id) == "vendor-6a01cfb6-717a-11ee-b962-0242ac120002" assert Map.get(event, :company_id) == 10 assert Map.get(event, :job_id) == 12 assert Map.get(event, :page_id) == 15