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

OpWorks onEffect
trimStringsRemoves leading and trailing whitespace with String.trim/1.
upcaseStringsUpper-cases with String.upcase/1.
downcaseStringsLower-cases with String.downcase/1.
capitalizeStringsCapitalizes with String.capitalize/1. "hello WORLD" becomes "Hello world".
squishStringsCollapses every run of whitespace, including newlines, into one space and trims the ends. " a b \n c " becomes "a b c".
no_controlStringsRemoves 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_widthStringsRemoves the zero-width characters U+200B, U+200C, U+200D, U+FEFF and U+2060.
rich_text_safeStringsApplies no_zero_width, then no_control.

HTML

OpWorks onEffect
strip_tagsStringsRemoves every tag with HtmlSanitizeEx.strip_tags/1. "<p>Hi <b>there</b></p>" becomes "Hi there".
basic_htmlStringsCleans the string with HtmlSanitizeEx.basic_html/1.
html5StringsCleans the string with HtmlSanitizeEx.html5/1.
markdown_htmlStringsCleans the string with HtmlSanitizeEx.markdown_html/1.
tag=opStringsTrims, applies the sanitize op named after the equals sign, and trims again. tag=strip_tags turns " <p>Hi</p> " into "Hi".

Numbers

OpWorks onEffect
string_floatStringsStrips 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_integerStringsStrips 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]NumbersMoves 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

OpWorks onEffect
uniqListsRemoves duplicates with Enum.uniq/1.
compactListsRemoves nil items only.
reject_emptyListsRemoves nil, "", [] and %{} items.
sortListsSorts with Enum.sort/1.
each=[ops]ListsApplies the inner sanitize ops, in order, to every item.
default_when_nil=valueAnyReplaces 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=valueAnyReplaces 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