-
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.
Implement regex constraint for String fields (#9)
* Implement regex constraint for Strings * Add a test case for regex match on string field * Update README with regex example * Bump patch version to 0.1.9
- Loading branch information
1 parent
3ea0189
commit 6b2b379
Showing
7 changed files
with
70 additions
and
6 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
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,15 @@ | ||
defmodule AbsintheHelpers.Constraints.Regex do | ||
@moduledoc """ | ||
Applies regex constraint on node data. This constraint can only be applied to String types. | ||
""" | ||
|
||
@behaviour AbsintheHelpers.Constraint | ||
|
||
def call(node = %{data: data}, {:regex, regex}) do | ||
if data =~ Regex.compile!(regex) do | ||
{:ok, node} | ||
else | ||
{:error, :invalid_format, %{regex: regex}} | ||
end | ||
end | ||
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
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,17 @@ | ||
defmodule AbsintheHelpers.Constraints.RegexTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias AbsintheHelpers.Constraints.Regex | ||
|
||
test "returns :ok tuple on regex match" do | ||
input = %{data: "username"} | ||
|
||
assert {:ok, %{data: "username"}} = Regex.call(input, {:regex, "^[a-z]*$"}) | ||
end | ||
|
||
test "returns invalid_format error on regex match failure" do | ||
input = %{data: "user.name"} | ||
|
||
assert {:error, :invalid_format, %{regex: "^[a-z]*$"}} = Regex.call(input, {:regex, "^[a-z]*$"}) | ||
end | ||
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