forked from plausible/analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Recruitee/additional-data-validation
Validate special props
- Loading branch information
Showing
4 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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 | ||
pattern = ~r/\A(#{@prefix_pattern})-(#{@id_pattern}|#{@uuid_pattern})\Z/ | ||
|
||
if Regex.match?(pattern, value), do: value, else: nil | ||
end | ||
|
||
def validate(nil, _), do: nil | ||
def validate("", _), do: nil | ||
def validate(value, _), do: value | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters