Skip to content

Commit

Permalink
add sanitize(string_float)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Nov 7, 2023
1 parent a1f3c68 commit 558ae20
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions guidance/guarded-struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ end
| `"sanitize(markdown_html)"` | `:html_sanitize_ex` | Sanitize your string base on `markdown_html` |
| `"sanitize(strip_tags)"` | `:html_sanitize_ex` | Sanitize your string base on `strip_tags` |
| `"sanitize(tag)"` | `:html_sanitize_ex` | Sanitize your string base on `html_sanitize_ex` selection |
| `"sanitize(string_float)"` | `:html_sanitize_ex` or NO | Sanitize your string base on `html_sanitize_ex` and `Float.parse/1` |

#### Validate

Expand Down
21 changes: 21 additions & 0 deletions lib/helper/derive/sanitizer_derive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ defmodule MishkaDeveloperTools.Helper.Derive.SanitizerDerive do
|> then(&sanitize(:downcase, &1))
|> then(&sanitize(:trim, &1))
end

def sanitize(:string_float, input) when is_binary(input) do
sanitize(:strip_tags, input)
|> Float.parse()
|> case do
:error -> 0.0
{converted_float, _} -> converted_float
end
rescue
_ -> 0.0
end
else
def sanitize(:string_float, input) when is_binary(input) do
Float.parse(input)
|> case do
:error -> 0.0
{converted_float, _} -> converted_float
end
rescue
_ -> 0.0
end
end

def sanitize(action, input) do
Expand Down
2 changes: 1 addition & 1 deletion lib/macros/guarded_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ defmodule GuardedStruct do
| `"sanitize(markdown_html)"` | `:html_sanitize_ex` | Sanitize your string base on `markdown_html` |
| `"sanitize(strip_tags)"` | `:html_sanitize_ex` | Sanitize your string base on `strip_tags` |
| `"sanitize(tag)"` | `:html_sanitize_ex` | Sanitize your string base on `html_sanitize_ex` selection |
| `"sanitize(string_float)"` | `:html_sanitize_ex` or `none` | Sanitize your string base on `html_sanitize_ex` and `Float.parse/1` |
#### Validate
Expand Down Expand Up @@ -1975,7 +1976,6 @@ defmodule GuardedStruct do

errors || {:ok, Enum.map(builders_output, &elem(&1, 1))}
else
IO.inspect(attrs)
{:error, :bad_parameters, "Your input must be a list of items"}
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/guarded_struct_derive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ defmodule MishkaDeveloperToolsTest.GuardedStructDeriveTest do
"<p>Hi Shahryar</p>" = assert SanitizerDerive.sanitize(:not_exist, "<p>Hi Shahryar</p>")
end

test "sanitize(:string_float, input)" do
2369.0 = assert SanitizerDerive.sanitize(:string_float, "<p>2369</p>")
3.0 = assert SanitizerDerive.sanitize(:string_float, "3s4s6.65")
346.65 = assert SanitizerDerive.sanitize(:string_float, "346.65sss")
346.65 = assert SanitizerDerive.sanitize(:string_float, "346.65")
346.65 = assert SanitizerDerive.sanitize(:string_float, 346.65)
end

############## (▰˘◡˘▰) Validation Derive (▰˘◡˘▰) ##############
test "validate(:string, input, field)" do
"Mishka" = assert ValidationDerive.validate(:string, "Mishka", :title)
Expand Down

0 comments on commit 558ae20

Please sign in to comment.