Skip to content

Commit

Permalink
chore: mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
njausteve committed Sep 23, 2024
1 parent 3958d7e commit 699e2ac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
35 changes: 26 additions & 9 deletions lib/utils/validators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ defmodule ExPass.Utils.Validators do

def validate_attributed_value(value) when is_binary(value) do
case contains_unsupported_html_tags?(value) do
true -> {:error, "Supported types are: String (including <a></a> tag), number, DateTime and Date"}
false -> :ok
true ->
{:error, "Supported types are: String (including <a></a> tag), number, DateTime and Date"}

false ->
:ok
end
end

Expand All @@ -90,7 +93,10 @@ defmodule ExPass.Utils.Validators do
:ok
end

def validate_attributed_value(_), do: {:error, "Invalid attributed_value type. Supported types are: String (including <a></a> tag), number, DateTime and Date"}
def validate_attributed_value(_),
do:
{:error,
"Invalid attributed_value type. Supported types are: String (including <a></a> tag), number, DateTime and Date"}

@doc """
Validates the change_message field.
Expand Down Expand Up @@ -124,11 +130,15 @@ defmodule ExPass.Utils.Validators do
if String.contains?(value, "%@") do
:ok
else
{:error, "The change_message must be a string containing the '%@' placeholder for the new value."}
{:error,
"The change_message must be a string containing the '%@' placeholder for the new value."}
end
end

def validate_change_message(_), do: {:error, "The change_message must be a string containing the '%@' placeholder for the new value."}
def validate_change_message(_),
do:
{:error,
"The change_message must be a string containing the '%@' placeholder for the new value."}

@doc """
Validates the currency_code field.
Expand Down Expand Up @@ -290,7 +300,9 @@ defmodule ExPass.Utils.Validators do
@spec validate_boolean_field(boolean() | nil, atom()) :: :ok | {:error, String.t()}
def validate_boolean_field(nil, _field_name), do: :ok
def validate_boolean_field(value, _field_name) when is_boolean(value), do: :ok
def validate_boolean_field(_, field_name), do: {:error, "#{field_name} must be a boolean value (true or false)"}

def validate_boolean_field(_, field_name),
do: {:error, "#{field_name} must be a boolean value (true or false)"}

@doc """
Validates a required string field.
Expand Down Expand Up @@ -323,13 +335,16 @@ defmodule ExPass.Utils.Validators do
"""
@spec validate_required_string(String.t() | nil, atom()) :: :ok | {:error, String.t()}
def validate_required_string(nil, field_name), do: {:error, "#{field_name} is a required field and must be a non-empty string"}
def validate_required_string(nil, field_name),
do: {:error, "#{field_name} is a required field and must be a non-empty string"}

def validate_required_string("", field_name),
do: {:error, "#{field_name} cannot be an empty string"}

def validate_required_string(value, _field_name) when is_binary(value), do: :ok
def validate_required_string(_, field_name), do: {:error, "#{field_name} is a required field and must be a non-empty string"}

def validate_required_string(_, field_name),
do: {:error, "#{field_name} is a required field and must be a non-empty string"}

@doc """
Validates an optional string field.
Expand Down Expand Up @@ -364,7 +379,9 @@ defmodule ExPass.Utils.Validators do
@spec validate_optional_string(String.t() | nil, atom()) :: :ok | {:error, String.t()}
def validate_optional_string(nil, _field_name), do: :ok
def validate_optional_string(value, _field_name) when is_binary(value), do: :ok
def validate_optional_string(_, field_name), do: {:error, "#{field_name} must be a string if provided"}

def validate_optional_string(_, field_name),
do: {:error, "#{field_name} must be a string if provided"}

@doc """
Validates the number_style field.
Expand Down
24 changes: 15 additions & 9 deletions test/structs/field_content_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ defmodule ExPass.Structs.FieldContentTest do
test "new/1 raises ArgumentError for invalid change_message without '%@' placeholder" do
message = "Balance updated"

assert_raise ArgumentError, "The change_message must be a string containing the '%@' placeholder for the new value.", fn ->
FieldContent.new(%{key: "test_key", change_message: message})
end
assert_raise ArgumentError,
"The change_message must be a string containing the '%@' placeholder for the new value.",
fn ->
FieldContent.new(%{key: "test_key", change_message: message})
end
end

test "new/1 creates a FieldContent struct with valid change_message containing '%@' placeholder" do
Expand Down Expand Up @@ -71,9 +73,11 @@ defmodule ExPass.Structs.FieldContentTest do
invalid_values = [%{}, [1, 2, 3], self(), :atom]

for invalid_value <- invalid_values do
assert_raise ArgumentError, "Invalid attributed_value type. Supported types are: String (including <a></a> tag), number, DateTime and Date", fn ->
FieldContent.new(%{key: "test_key", attributed_value: invalid_value})
end
assert_raise ArgumentError,
"Invalid attributed_value type. Supported types are: String (including <a></a> tag), number, DateTime and Date",
fn ->
FieldContent.new(%{key: "test_key", attributed_value: invalid_value})
end
end
end

Expand All @@ -100,9 +104,11 @@ defmodule ExPass.Structs.FieldContentTest do
test "new/1 raises ArgumentError for attributed_value with unsupported HTML tag" do
input_value = "<span>Unsupported tag</span>"

assert_raise ArgumentError, "Supported types are: String (including <a></a> tag), number, DateTime and Date", fn ->
FieldContent.new(%{key: "test_key", attributed_value: input_value})
end
assert_raise ArgumentError,
"Supported types are: String (including <a></a> tag), number, DateTime and Date",
fn ->
FieldContent.new(%{key: "test_key", attributed_value: input_value})
end
end

test "new/1 creates a valid FieldContent struct with supported HTML tag" do
Expand Down

0 comments on commit 699e2ac

Please sign in to comment.