pedantic/codec

Types

A Codec wraps a standalone Decoder and Encoder, sharing their underlying TypeGraph.

pub type Codec(t) {
  Codec(
    graph: report.TypeGraph,
    decoder: decode.Decoder(t),
    encoder: encode.Encoder(t),
  )
}

Constructors

An intermediate builder structure used to construct record/object codecs.

pub opaque type ObjectBuilder(constructor, value)

Values

pub fn allow_chars(
  codec: Codec(String),
  characters: String,
) -> Codec(String)
pub fn assert_that(
  codec: Codec(t),
  predicate: fn(t) -> Bool,
  error: report.ErrorKind,
) -> Codec(t)
pub const bool: Codec(Bool)
pub fn build(
  builder: ObjectBuilder(value, value),
) -> Codec(value)

Finalizes the ObjectBuilder, converting it into a full bidirectional Codec.

pub fn check(
  codec: Codec(t),
  predicate: fn(t) -> Bool,
  msg: String,
) -> Codec(t)
pub const coerced_bool: Codec(Bool)
pub const coerced_float: Codec(Float)
pub const coerced_int: Codec(Int)
pub const coerced_string: Codec(String)
pub fn collapse_whitespace(codec: Codec(String)) -> Codec(String)
pub fn custom(
  decoder: decode.Decoder(t),
  encoder: encode.Encoder(t),
) -> Codec(t)

Bidirectional custom pair constructor.

pub fn default(
  codec: Codec(option.Option(a)),
  default_val: a,
) -> Codec(a)

Adapts an optional codec with a default value fallback.

pub fn default_if_blank(
  codec: Codec(String),
  default_value: String,
) -> Codec(String)
pub const default_to: fn(Codec(option.Option(a)), a) -> Codec(a)
pub fn describe(codec: Codec(t), desc: String) -> Codec(t)

Sets the description metadata in the AST graph.

pub fn dict(value_codec: Codec(a)) -> Codec(dict.Dict(String, a))

Dict combinator.

pub fn digits_only(codec: Codec(String)) -> Codec(String)
pub fn email(codec: Codec(String), msg: String) -> Codec(String)
pub fn empty_as_none(
  codec: Codec(String),
) -> Codec(option.Option(String))
pub const exact_bool: Codec(Bool)
pub const exact_float: Codec(Float)
pub const exact_int: Codec(Int)
pub const exact_string: Codec(String)
pub fn example(codec: Codec(t), ex: dynamic.Dynamic) -> Codec(t)

Sets the example metadata in the AST graph.

pub fn field(
  builder: ObjectBuilder(fn(field_type) -> next, value),
  name: String,
  field_codec: Codec(field_type),
  get: fn(value) -> field_type,
) -> ObjectBuilder(next, value)

Applies a field description, codec, and getter function to the object builder.

pub const float: Codec(Float)
pub const int: Codec(Int)
pub const key: fn(
  ObjectBuilder(fn(field_type) -> next, value),
  String,
  Codec(field_type),
  fn(value) -> field_type,
) -> ObjectBuilder(next, value)
pub fn key_optional(
  builder: ObjectBuilder(
    fn(option.Option(field_type)) -> next,
    value,
  ),
  name: String,
  field_codec: Codec(field_type),
  get: fn(value) -> option.Option(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,
  Codec(field_type),
  fn(value) -> field_type,
) -> ObjectBuilder(next, value)
pub fn key_with_default(
  builder: ObjectBuilder(fn(field_type) -> next, value),
  name: String,
  field_codec: Codec(field_type),
  default_val: field_type,
  get: fn(value) -> field_type,
) -> ObjectBuilder(next, value)

Declares an optional key with fallback default value.

pub fn length(codec: Codec(String), len: Int) -> Codec(String)
pub fn letters_only(codec: Codec(String)) -> Codec(String)
pub fn list(element_codec: Codec(a)) -> Codec(List(a))

List combinator.

pub fn lowercase(codec: Codec(String)) -> Codec(String)
pub fn max(codec: Codec(Int), maximum: Int) -> Codec(Int)

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

pub fn max_length(
  codec: Codec(String),
  maximum: Int,
) -> Codec(String)

Restricts a String codec to a maximum length.

pub fn max_size(
  codec: Codec(List(a)),
  maximum: Int,
) -> Codec(List(a))
pub fn min(codec: Codec(Int), minimum: Int) -> Codec(Int)

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

pub fn min_length(
  codec: Codec(String),
  minimum: Int,
) -> Codec(String)

Restricts a String codec to a minimum length.

pub fn min_size(
  codec: Codec(List(a)),
  minimum: Int,
) -> Codec(List(a))
pub fn negative(codec: Codec(Int)) -> Codec(Int)
pub fn non_empty_list(element_codec: Codec(a)) -> Codec(List(a))
pub fn non_negative(codec: Codec(Int)) -> Codec(Int)
pub fn non_positive(codec: Codec(Int)) -> Codec(Int)
pub fn normalize_email(codec: Codec(String)) -> Codec(String)
pub fn normalize_line_endings(
  codec: Codec(String),
) -> Codec(String)
pub fn normalize_uuid(codec: Codec(String)) -> Codec(String)
pub fn object(
  name: String,
  constructor: constructor,
) -> ObjectBuilder(constructor, value)

Initializes an ObjectBuilder for a custom record constructor.

pub fn optional(inner_codec: Codec(a)) -> Codec(option.Option(a))

Option/Optional combinator representing absent or nullable values.

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

Refines a codec with custom validation rules.

pub fn remove_chars(
  codec: Codec(String),
  characters: String,
) -> Codec(String)
pub fn remove_whitespace(codec: Codec(String)) -> Codec(String)
pub fn replace(
  codec: Codec(String),
  pattern: String,
  replacement: String,
) -> Codec(String)
pub const string: Codec(String)
pub fn strip_control_chars(codec: Codec(String)) -> Codec(String)
pub fn strip_non_ascii(codec: Codec(String)) -> Codec(String)
pub fn title(codec: Codec(t), name: String) -> Codec(t)

Sets the title metadata in the AST graph.

pub fn trim(codec: Codec(String)) -> Codec(String)
pub const unwrap_or: fn(Codec(option.Option(a)), a) -> Codec(a)
pub fn uppercase(codec: Codec(String)) -> Codec(String)
pub fn uuid(codec: Codec(String), msg: String) -> Codec(String)
pub fn with_error_formatter(
  codec: Codec(t),
  formatter: fn(dynamic.Dynamic, report.ErrorKind) -> String,
) -> Codec(t)
pub fn with_error_message(
  codec: Codec(t),
  msg: String,
) -> Codec(t)
pub fn with_template(
  codec: Codec(t),
  template: String,
) -> Codec(t)
Search Document