kata/schema

Types

Opaque bidirectional schema type. Holds decode, encode, ast, and dummy functions.

pub opaque type Schema(a)

Values

pub fn bool() -> Schema(Bool)
pub fn brand(
  base: Schema(a),
  name: String,
  wrap: fn(a) -> b,
  unwrap: fn(b) -> a,
) -> Schema(b)
pub fn coerce_bool() -> Schema(Bool)

Bool schema that also accepts VString “true”/“false”.

pub fn coerce_float() -> Schema(Float)

Float schema that also accepts VString parseable as float, or VInt.

pub fn coerce_int() -> Schema(Int)

Int schema that also accepts VString parseable as integer.

pub fn decode(
  schema: Schema(a),
  value: value.Value,
) -> Result(a, List(error.Error))

Decode a Value into a typed value using the schema

pub fn dict(
  key_schema: Schema(k),
  val_schema: Schema(v),
) -> Schema(dict.Dict(k, v))
pub fn done(value: a) -> Schema(a)

Terminal combinator for record builder.

pub fn encode(schema: Schema(a), value: a) -> value.Value

Encode a typed value into a Value using the schema

pub fn field(
  key: String,
  field_schema: Schema(a),
  get: fn(final) -> a,
  next: fn(a) -> Schema(final),
) -> Schema(final)

Define a field in an object schema.

  • key: field name
  • field_schema: schema for the field value
  • get: extract field from the final record (for encode)
  • next: continuation receiving the decoded field value
pub fn float() -> Schema(Float)
pub fn from_bool(
  schema: Schema(a),
  value: Bool,
) -> Result(a, List(error.Error))

Construct a validated value from a Bool.

pub fn from_float(
  schema: Schema(a),
  value: Float,
) -> Result(a, List(error.Error))

Construct a validated value from a Float.

pub fn from_int(
  schema: Schema(a),
  value: Int,
) -> Result(a, List(error.Error))

Construct a validated value from an Int.

pub fn from_string(
  schema: Schema(a),
  value: String,
) -> Result(a, List(error.Error))

Construct a validated value from a String.

pub fn int() -> Schema(Int)
pub fn lazy(f: fn() -> Schema(a)) -> Schema(a)
pub fn list(item: Schema(a)) -> Schema(List(a))
pub fn optional(inner: Schema(a)) -> Schema(option.Option(a))
pub fn optional_field(
  key: String,
  field_schema: Schema(a),
  default: a,
  get: fn(final) -> a,
  next: fn(a) -> Schema(final),
) -> Schema(final)

Optional field with a default value when missing.

pub fn refine_float(
  s: Schema(Float),
  ref: ast.FloatRef,
  check: fn(Float) -> Result(Float, List(error.Error)),
) -> Schema(Float)

Add a float refinement to a Schema(Float).

pub fn refine_int(
  s: Schema(Int),
  ref: ast.IntRef,
  check: fn(Int) -> Result(Int, List(error.Error)),
) -> Schema(Int)

Add an int refinement to a Schema(Int).

pub fn refine_string(
  s: Schema(String),
  ref: ast.StringRef,
  check: fn(String) -> Result(String, List(error.Error)),
) -> Schema(String)

Add a string refinement to a Schema(String).

pub fn string() -> Schema(String)
pub fn tagged_union(
  discriminator: String,
  get_tag: fn(a) -> String,
  variants: List(#(String, Schema(a))),
) -> Schema(a)

Discriminated union schema.

  • discriminator: field name holding the tag
  • get_tag: extract tag string from a value (for encode)
  • variants: list of (tag, schema) pairs
pub fn to_ast(schema: Schema(a)) -> ast.Ast

Get the AST for introspection

pub fn transform(
  schema: Schema(a),
  name: String,
  forward: fn(a) -> Result(b, String),
  backward: fn(b) -> a,
  dummy: fn() -> b,
) -> Schema(b)
Search Document