ExParamsSchema.Schema (ex_params_schema v0.1.1)

Copy Markdown View Source

Represents a compiled schema containing normalized fields, JSON Schema, and validation data.

It is created by ExParamsSchema.compile!/2. The struct's internal representation and its direct-operation functions may change, so use the public ExParamsSchema API in normal use.

Summary

Functions

Compiles validated fields into a schema that can be reused at runtime.

Returns the declaration order and error reason for a JSON Pointer.

Returns JSON Schema Draft 7 from a compiled schema or validated fields. Field definitions are delegated to Definition.compile_fields!/1 for backward compatibility.

Returns the declaration order, error reason, and resolved path for a JSON Pointer.

Converts cast values into values that JSON Schema can validate.

Types

error_definition()

@type error_definition() ::
  {pattern :: ExParamsSchema.Schema.JsonPointer.pattern(),
   error_reason :: ExParamsSchema.error_reason(), order :: non_neg_integer()}

error_reference()

@type error_reference() ::
  {order :: non_neg_integer(), error_reason :: ExParamsSchema.error_reason()}

resolved_error_path()

@type resolved_error_path() ::
  {order :: non_neg_integer(), error_reason :: ExParamsSchema.error_reason(),
   path :: ExParamsSchema.Schema.JsonPointer.resolved_path()}

t()

@type t() :: %ExParamsSchema.Schema{
  errors: [error_definition()],
  fields: [ExParamsSchema.Definition.Field.t()],
  json_schema: map(),
  resolved: ExJsonSchema.Schema.Root.t(),
  strict: boolean()
}

Functions

compile!(fields, strict \\ false)

@spec compile!(
  [ExParamsSchema.field()] | [ExParamsSchema.Definition.Field.t()],
  boolean()
) :: t()

Compiles validated fields into a schema that can be reused at runtime.

ExParamsSchema.Definition validates and compiles field definitions. Normally, use ExParamsSchema.compile!/2, which accepts a map-based definition. Field definitions are also accepted for backward compatibility and delegated to Definition.compile_fields!/1.

iex> fields = ExParamsSchema.Definition.compile_fields!([{:count, :integer, [minimum: 1]}])
iex> schema = ExParamsSchema.Schema.compile!(fields)
iex> schema.strict
false
iex> get_in(schema.json_schema, ["properties", "count"])
%{"minimum" => 1, "type" => "integer"}

error_for_path(schema, path)

This function is deprecated. Use resolve_error_path/2, which also returns the resolved path.
@spec error_for_path(t(), String.t()) :: error_reference() | nil

Returns the declaration order and error reason for a JSON Pointer.

json_schema(fields, strict \\ nil)

@spec json_schema(t(), boolean() | nil) :: map()
@spec json_schema(
  [ExParamsSchema.field()] | [ExParamsSchema.Definition.Field.t()],
  boolean() | nil
) ::
  map()

Returns JSON Schema Draft 7 from a compiled schema or validated fields. Field definitions are delegated to Definition.compile_fields!/1 for backward compatibility.

When strict is specified for a compiled schema, regenerates JSON Schema with that setting. Otherwise, returns the JSON Schema generated at compile time.

iex> fields = ExParamsSchema.Definition.compile_fields!([{:name, :string, []}])
iex> ExParamsSchema.Schema.json_schema(fields)["required"]
["name"]

resolve_error_path(schema, pointer)

@spec resolve_error_path(t(), String.t()) :: resolved_error_path() | nil

Returns the declaration order, error reason, and resolved path for a JSON Pointer.

validation_data(schema, parsed)

@spec validation_data(t(), map()) :: map()

Converts cast values into values that JSON Schema can validate.

iex> fields = ExParamsSchema.Definition.compile_fields!([{:published_on, :date, []}])
iex> schema = ExParamsSchema.Schema.compile!(fields)
iex> ExParamsSchema.Schema.validation_data(schema, %{published_on: ~D[2026-07-23]})
%{"published_on" => "2026-07-23"}