Coelho.Schema.Attr (coelho v0.2.0)

Copy Markdown View Source

Specification and validation of a single node or mark attribute.

An attribute spec declares a default value, whether the attribute is required, and an optional validator. Validators are declarative terms rather than closures so that a schema stays inspectable and comparable.

Supported validators:

  • :string, :integer, :boolean — type checks
  • {:one_of, list} — value must be a member of list
  • :safe_url — relative URL, or absolute URL with an allowed scheme
  • {:nullable, validator} — accepts nil, otherwise delegates
  • a fun/1 returning :ok or {:error, message} — escape hatch

Summary

Functions

Builds an attribute spec from a keyword list.

Runs a validator against a value.

Types

t()

@type t() :: %Coelho.Schema.Attr{
  default: term(),
  required: boolean(),
  validate: validator()
}

validator()

@type validator() ::
  :string
  | :integer
  | :boolean
  | {:one_of, [term()]}
  | :safe_url
  | {:nullable, validator()}
  | (term() -> :ok | {:error, String.t()})
  | nil

Functions

new(opts)

@spec new(keyword()) :: t()

Builds an attribute spec from a keyword list.

validate(fun, value)

@spec validate(validator(), term()) :: :ok | {:error, String.t()}

Runs a validator against a value.