Kotoba.Node.Field (Kotoba v0.5.0)

Copy Markdown View Source

The description of one attribute of a Kotoba.Node.

Kotoba.Node.field/3 makes one Kotoba.Node.Field for each attribute. Kotoba.Node.fields/0 returns them in the order of declaration.

Struct keys

  • :name - the struct key of the node, an atom.
  • :key - the JSON key.
  • :type - :string, :integer, :boolean, :map or {:array, type}.
  • :required, :default, :in and :omit_nil - the values of the options below.

Options of field/3

  • :key - the JSON key. The default is the name as a string.
  • :required - when true, the value must not be nil. The default is false.
  • :default - the struct default. from_json/1 also uses it when the JSON has no value for the key. The default is nil.
  • :in - when set, a list of the permitted values.
  • :omit_nil - when true, to_json/1 does not write the key if the value is nil. When false (the default), to_json/1 writes null.

Summary

Types

t()

A field type.

Functions

Checks one value against the field. Returns :ok or {:error, message}.

Builds a field. Raises ArgumentError when the name, the type or an option is not valid.

Types

t()

@type t() :: %Kotoba.Node.Field{
  default: term(),
  in: [term()] | nil,
  key: String.t(),
  name: atom(),
  omit_nil: boolean(),
  required: boolean(),
  type: type()
}

type()

@type type() :: :string | :integer | :boolean | :map | {:array, type()}

A field type.

Functions

check(field, value)

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

Checks one value against the field. Returns :ok or {:error, message}.

new(name, type, opts \\ [])

@spec new(atom(), type(), keyword()) :: t()

Builds a field. Raises ArgumentError when the name, the type or an option is not valid.