Schema creation, checking, and compiled-schema metadata.
Formata keeps two independent schema systems:
:native-Formata.Backplane.Schemadefinitions (Elixir data). SeeFormata.Schema.Native.:json_schema- JSON Schema documents (Draft 7 or Draft 2020-12), compiled and validated byFormata.Backplane.JsonSchema. SeeFormata.Schema.JSONSchema.
A JSON Schema document is never converted into a native schema, and native schemas are never exported to JSON Schema to validate data.
Compiling
iex> {:ok, schema} = Formata.Schema.compile(:native, %{name: {:required, :string}})
iex> schema.kind
:native
iex> {:ok, schema} = Formata.Schema.compile(:json_schema, %{"type" => "integer"})
iex> {schema.kind, schema.dialect}
{:json_schema, :draft2020_12}A compiled %Formata.Schema{} is an immutable value: compile once, then pass
it to Formata.validate/3 and Formata.cast/3 as often as needed.
Fields
:kind-:nativeor:json_schema:dialect-:draft7or:draft2020_12for JSON Schema,nilfor native:backend-Formata.Backplane.SchemaorFormata.Backplane.JsonSchema:compiled- the backend representation (the checked native definition or aFormata.Backplane.JsonSchema.Types.Schema):source- the definition/document as given:options- the compile options:plans- precomputed native execution plans (internal)
Summary
Functions
Checks a schema definition without keeping the compiled result.
Compiles a schema of the given kind.
Shortcut for compile(:json_schema, document, opts).
Shortcut for compile(:native, definition, opts).
Types
Functions
@spec check(kind(), term(), keyword()) :: :ok | {:error, [Formata.Error.t(), ...]}
Checks a schema definition without keeping the compiled result.
Returns :ok or {:error, errors} with :schema errors. Checking the
definition is separate from validating data.
iex> Formata.Schema.check(:native, %{age: :integer})
:ok
iex> {:error, [%Formata.Error{kind: :schema}]} = Formata.Schema.check(:native, %{age: :intger})
@spec compile(kind(), term(), keyword()) :: {:ok, t()} | {:error, [Formata.Error.t(), ...]}
Compiles a schema of the given kind.
See Formata.Schema.Native.compile/2 and Formata.Schema.JSONSchema.compile/2
for the options of each kind.
@spec json_schema(map() | boolean(), keyword()) :: {:ok, t()} | {:error, [Formata.Error.t(), ...]}
Shortcut for compile(:json_schema, document, opts).
@spec native(term(), keyword()) :: {:ok, t()} | {:error, [Formata.Error.t(), ...]}
Shortcut for compile(:native, definition, opts).