Arc.Schema (cratis_arc v0.1.0)

Copy Markdown View Source

Declares the fields of a command, a parameter object or a nested type.

defmodule MyApp.Authors.Address do
  use Arc.Schema

  schema do
    field :street, :string, required: true
    field :city, :string
  end
end

schema defines the struct and records typed metadata that Arc uses to bind requests, validate input and generate TypeScript.

Field options

  • :required - the value must be present (client rule notEmpty)
  • :nullable - the value may be nil; defaults to true unless :required
  • :key - this field identifies the command's event source
  • :default - the struct default
  • :doc - documentation, written into the generated proxy
  • :min_length, :max_length, :email, :matches (a regex source string), :min, :max, :greater_than, :less_than - declarative rules, evaluated on the server and written into the generated validators

Summary

Functions

Binds a wire map (string keys, camelCase) to the struct of module.

Declares one field. See the module documentation for options.

The fields of any struct module: declared fields for an Arc schema, otherwise inferred from the struct's defaults (for example a Chronicle read model). An nil default is inferred as a string, matching how Chronicle registers it.

Groups field/3 declarations and defines the struct, so %__MODULE__{} can be used in the functions that follow.

Functions

cast(module, wire)

@spec cast(module(), map()) :: {:ok, struct()} | {:error, [Arc.ValidationResult.t()]}

Binds a wire map (string keys, camelCase) to the struct of module.

Returns {:ok, struct} or {:error, results} where each result has reason "malformedRequest". Unknown fields are ignored.

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

(macro)

Declares one field. See the module documentation for options.

model_fields(module)

@spec model_fields(module()) :: [Arc.Schema.Field.t()]

The fields of any struct module: declared fields for an Arc schema, otherwise inferred from the struct's defaults (for example a Chronicle read model). An nil default is inferred as a string, matching how Chronicle registers it.

schema(list)

(macro)

Groups field/3 declarations and defines the struct, so %__MODULE__{} can be used in the functions that follow.