Sanitize ops
v0.1.4
Sanitize ops change a value before the validate ops see it. This page lists the 23 built-in sanitize ops. You write them in the sanitize(...) group of a derive string, as in "sanitize(trim, downcase)". A sanitize op never reports an error. An op that works on strings or lists returns any other kind of value unchanged. The HTML ops use the html_sanitize_ex package, which GuardedStruct depends on.
Text
| Op | Works on | Effect |
|---|---|---|
trim | Strings | Removes leading and trailing whitespace with String.trim/1. |
upcase | Strings | Upper-cases with String.upcase/1. |
downcase | Strings | Lower-cases with String.downcase/1. |
capitalize | Strings | Capitalizes with String.capitalize/1. "hello WORLD" becomes "Hello world". |
squish | Strings | Collapses every run of whitespace, including newlines, into one space and trims the ends. " a b \n c " becomes "a b c". |
no_control | Strings | Removes ASCII control characters: U+0000 to U+0008, U+000B, U+000C, U+000E to U+001F and U+007F. Tab, line feed and carriage return stay. |
no_zero_width | Strings | Removes the zero-width characters U+200B, U+200C, U+200D, U+FEFF and U+2060. |
rich_text_safe | Strings | Applies no_zero_width, then no_control. |
HTML
| Op | Works on | Effect |
|---|---|---|
strip_tags | Strings | Removes every tag with HtmlSanitizeEx.strip_tags/1. "<p>Hi <b>there</b></p>" becomes "Hi there". |
basic_html | Strings | Cleans the string with HtmlSanitizeEx.basic_html/1. |
html5 | Strings | Cleans the string with HtmlSanitizeEx.html5/1. |
markdown_html | Strings | Cleans the string with HtmlSanitizeEx.markdown_html/1. |
tag=op | Strings | Trims, applies the sanitize op named after the equals sign, and trims again. tag=strip_tags turns " <p>Hi</p> " into "Hi". |
Numbers
| Op | Works on | Effect |
|---|---|---|
string_float | Strings | Strips tags, then reads the start of the string with Float.parse/1. "3.14abc" becomes 3.14. A string with no number becomes 0.0. |
string_integer | Strings | Strips tags, then reads the start of the string with Integer.parse/1. "42abc" becomes 42. A string with no number becomes 0. |
clamp=[min, max] | Numbers | Moves a number below min up to min and a number above max down to max. clamp=[0, 100] turns 150 into 100. |
Lists and defaults
| Op | Works on | Effect |
|---|---|---|
uniq | Lists | Removes duplicates with Enum.uniq/1. |
compact | Lists | Removes nil items only. |
reject_empty | Lists | Removes nil, "", [] and %{} items. |
sort | Lists | Sorts with Enum.sort/1. |
each=[ops] | Lists | Applies the inner sanitize ops, in order, to every item. |
default_when_nil=value | Any | Replaces nil with the value. It also fills a field that is nil or missing, and then the derive string runs on the value. Every other op skips a nil field. |
default_when_empty=value | Any | Replaces nil, "", [] and %{} with the value. default_when_empty="anonymous" turns "" into "anonymous". A field that is nil is skipped, so on a field this op replaces the empty values only. A nil item inside each=[...] is replaced too. |
Call an op directly
Each op is also a function. The value comes first, so calls pipe.
elixir
alias GuardedStruct.Derive.SanitizerDerive
" Hello "
|> SanitizerDerive.sanitize(:trim)
|> SanitizerDerive.sanitize(:downcase)
# "hello"
SanitizerDerive.sanitize(150, {:clamp, [0, 100]})
# 100