GuardedStruct.Derive.SanitizerDerive (GuardedStruct v0.1.7)

Copy Markdown View Source

Built-in sanitizer ops. Every clause follows the pipe-friendly sanitize(value, op) argument order:

"  Hello  " |> SanitizerDerive.sanitize(:trim) |> SanitizerDerive.sanitize(:downcase)
# => "hello"

An op is served by the first that knows it: the built-in ops, then the derive extensions of the module being built, then its sanitize_derive modules in order. A sanitize_derive module passes an op on to the next one by returning nil, or by having no sanitize/2 clause that matches the value and the op.

Sanitizing fails closed:

  • a sanitizer that raises never lets the raw value through. In a derives string the field fails with a :sanitize error and the exception is logged (see call/2); sanitize/2 raises the exception.
  • an op that nothing serves raises ArgumentError, because the op is a programming mistake.

Summary

Functions

Runs the sanitize ops of a field in order and returns {field, value}.

Applies one sanitize op to input. Raises what the sanitizer raises, and ArgumentError for an op that nothing serves.

Functions

call(arg, actions)

@spec call({atom(), any()}, [any()] | nil) :: {atom(), any()}

Runs the sanitize ops of a field in order and returns {field, value}.

When a sanitizer raises, value is the field's :sanitize error ({:error, field, :sanitize, message}), which GuardedStruct.Derive.ValidationDerive.call/3 reports like any other field error. The exception is logged at error level with its stacktrace, without the arguments of each call, so the value itself is never logged.

sanitize(input, op)

@spec sanitize(any(), atom() | tuple() | map()) :: any()

Applies one sanitize op to input. Raises what the sanitizer raises, and ArgumentError for an op that nothing serves.