pedantic/decode

Types

A Decoder describes how to parse, coerce, and validate a dynamic input.

pub type Decoder(t) {
  Decoder(
    graph: report.TypeGraph,
    function: fn(dynamic.Dynamic) -> Result(t, report.Report),
  )
}

Constructors

An intermediate builder structure used to construct record decoders.

pub opaque type ObjectBuilder(constructor, value)

Values

pub fn allow_chars(
  decoder: Decoder(String),
  characters: String,
) -> Decoder(String)

Keeps only graphemes contained in characters.

pub fn any_of(decoders: List(Decoder(t))) -> Decoder(t)
pub fn assert_that(
  decoder: Decoder(t),
  predicate: fn(t) -> Bool,
  error: report.ErrorKind,
) -> Decoder(t)

Asserts validation constraints using a boolean predicate function.

pub const bool: Decoder(Bool)
pub fn build(
  builder: ObjectBuilder(value, value),
) -> Decoder(value)

Finalizes the ObjectBuilder, converting it into a full Decoder.

pub fn check(
  decoder: Decoder(t),
  predicate: fn(t) -> Bool,
  msg: String,
) -> Decoder(t)

Standard check validator.

pub const coerced_bool: Decoder(Bool)

Primitive Bool decoder with safe type coercion from Int or String.

pub const coerced_float: Decoder(Float)

Primitive Float decoder with safe type coercion from Int or String.

pub const coerced_int: Decoder(Int)

Primitive Int decoder with safe type coercion from String.

pub const coerced_string: Decoder(String)

Primitive String decoder with safe type coercion from Int, Float, or Bool.

pub fn collapse_whitespace(
  decoder: Decoder(String),
) -> Decoder(String)

Replaces each run of whitespace with a single space and trims the result.

pub fn default(
  decoder: Decoder(option.Option(a)),
  default_val: a,
) -> Decoder(a)

Adapts an optional decoder with a default value fallback.

pub fn default_if_blank(
  decoder: Decoder(String),
  default_value: String,
) -> Decoder(String)

Trims a string and replaces blank values with default_value.

pub const default_to: fn(Decoder(option.Option(a)), a) -> Decoder(
  a,
)
pub fn describe(decoder: Decoder(t), desc: String) -> Decoder(t)

Sets the description metadata in the AST graph.

pub fn dict(
  value_decoder: Decoder(a),
) -> Decoder(dict.Dict(String, a))

Dict combinator to decode dynamic objects with string keys, accumulating errors.

pub fn digits_only(decoder: Decoder(String)) -> Decoder(String)

Keeps only ASCII digits from the string.

pub fn email(
  decoder: Decoder(String),
  msg: String,
) -> Decoder(String)
pub fn empty_as_none(
  decoder: Decoder(String),
) -> Decoder(option.Option(String))

Trims a string and turns blank values into None.

pub const exact_bool: Decoder(Bool)

Primitive Bool decoder verifying exact type without coercion.

pub const exact_float: Decoder(Float)

Primitive Float decoder verifying exact type without coercion.

pub const exact_int: Decoder(Int)

Primitive Int decoder verifying exact type without coercion.

pub const exact_string: Decoder(String)

Primitive String decoder verifying exact type without coercion.

pub fn example(
  decoder: Decoder(t),
  ex: dynamic.Dynamic,
) -> Decoder(t)

Sets the example metadata in the AST graph.

pub fn field(
  builder: ObjectBuilder(fn(field_type) -> next, value),
  name: String,
  field_decoder: Decoder(field_type),
) -> ObjectBuilder(next, value)

Applies a field description and decoder to the object builder.

pub const float: Decoder(Float)
pub fn float_range(
  minimum: Float,
  maximum: Float,
) -> Decoder(Float)
pub const int: Decoder(Int)
pub fn int_range(minimum: Int, maximum: Int) -> Decoder(Int)
pub const key: fn(
  ObjectBuilder(fn(field_type) -> next, value),
  String,
  Decoder(field_type),
) -> ObjectBuilder(next, value)
pub fn key_optional(
  builder: ObjectBuilder(
    fn(option.Option(field_type)) -> next,
    value,
  ),
  name: String,
  field_decoder: Decoder(field_type),
) -> ObjectBuilder(next, value)

Declares an optional key in the object builder, resolving to Option(t).

pub const key_required: fn(
  ObjectBuilder(fn(field_type) -> next, value),
  String,
  Decoder(field_type),
) -> ObjectBuilder(next, value)
pub fn key_with_default(
  builder: ObjectBuilder(fn(field_type) -> next, value),
  name: String,
  field_decoder: Decoder(field_type),
  default_val: field_type,
) -> ObjectBuilder(next, value)

Declares an optional key with fallback default value.

pub fn length(
  decoder: Decoder(String),
  len: Int,
) -> Decoder(String)
pub fn letters_only(decoder: Decoder(String)) -> Decoder(String)

Keeps only ASCII letters from the string.

pub fn list(element_decoder: Decoder(a)) -> Decoder(List(a))

List combinator that decodes dynamic arrays into typed Lists, accumulating errors.

pub fn lowercase(decoder: Decoder(String)) -> Decoder(String)
pub fn map(
  decoder: Decoder(a),
  transform_fn: fn(a) -> b,
) -> Decoder(b)
pub fn max(decoder: Decoder(Int), maximum: Int) -> Decoder(Int)

Restricts an Int decoder to values less than or equal to the maximum.

pub fn max_length(
  decoder: Decoder(String),
  maximum: Int,
) -> Decoder(String)

Restricts a String decoder to a maximum length.

pub fn max_size(
  decoder: Decoder(List(a)),
  maximum: Int,
) -> Decoder(List(a))
pub fn min(decoder: Decoder(Int), minimum: Int) -> Decoder(Int)

Restricts an Int decoder to values greater than or equal to the minimum.

pub fn min_length(
  decoder: Decoder(String),
  minimum: Int,
) -> Decoder(String)

Restricts a String decoder to a minimum length.

pub fn min_size(
  decoder: Decoder(List(a)),
  minimum: Int,
) -> Decoder(List(a))
pub fn negative(decoder: Decoder(Int)) -> Decoder(Int)
pub fn non_empty_list(
  element_decoder: Decoder(a),
) -> Decoder(List(a))
pub const non_empty_string: Decoder(String)
pub fn non_negative(decoder: Decoder(Int)) -> Decoder(Int)
pub fn non_positive(decoder: Decoder(Int)) -> Decoder(Int)
pub fn normalize_email(
  decoder: Decoder(String),
) -> Decoder(String)

Trims and lowercases a string, useful for case-insensitive identifiers.

pub fn normalize_line_endings(
  decoder: Decoder(String),
) -> Decoder(String)

Converts CRLF and CR line endings to LF line endings.

pub fn normalize_uuid(
  decoder: Decoder(String),
) -> Decoder(String)

Trims and lowercases a UUID string.

pub fn object(
  name: String,
  constructor: constructor,
) -> ObjectBuilder(constructor, value)

Initializes an ObjectBuilder for a custom record constructor.

pub fn optional(
  inner_decoder: Decoder(a),
) -> Decoder(option.Option(a))

Option/Optional combinator representing absent or nullable values.

pub fn positive(decoder: Decoder(Int)) -> Decoder(Int)
pub fn refine(
  decoder: Decoder(t),
  rule: fn(t) -> Result(t, report.ErrorKind),
) -> Decoder(t)

Refines a decoder with a custom validation rule.

pub fn remove_chars(
  decoder: Decoder(String),
  characters: String,
) -> Decoder(String)

Removes every grapheme contained in characters.

pub fn remove_whitespace(
  decoder: Decoder(String),
) -> Decoder(String)

Removes spaces, tabs, newlines, carriage returns, and non-breaking spaces.

pub fn replace(
  decoder: Decoder(String),
  pattern: String,
  replacement: String,
) -> Decoder(String)

Replaces every occurrence of pattern with replacement.

pub const string: Decoder(String)
pub fn string_limit(
  minimum: Int,
  maximum: Int,
) -> Decoder(String)
pub fn strip_control_chars(
  decoder: Decoder(String),
) -> Decoder(String)

Removes Unicode control code points from the string.

pub fn strip_non_ascii(
  decoder: Decoder(String),
) -> Decoder(String)

Removes all non-ASCII Unicode code points.

pub fn title(decoder: Decoder(t), name: String) -> Decoder(t)

Sets the title metadata in the AST graph.

pub fn trim(decoder: Decoder(String)) -> Decoder(String)
pub const unwrap_or: fn(Decoder(option.Option(a)), a) -> Decoder(
  a,
)
pub fn uppercase(decoder: Decoder(String)) -> Decoder(String)
pub fn uuid(
  decoder: Decoder(String),
  msg: String,
) -> Decoder(String)
pub fn with_error_formatter(
  decoder: Decoder(t),
  formatter: fn(dynamic.Dynamic, report.ErrorKind) -> String,
) -> Decoder(t)

Custom error formatter decorator.

pub fn with_error_message(
  decoder: Decoder(t),
  msg: String,
) -> Decoder(t)

Exposes simple custom validation string messages.

pub fn with_template(
  decoder: Decoder(t),
  template: String,
) -> Decoder(t)

Placeholder template parser replacing {value}, {min}, {max}, {length}.

Search Document