Altar.ADM.Schema (Altar v0.2.0)

View Source

Schema definition for ALTAR Data Model (ADM).

Provides a complete type system following OpenAPI 3.0 patterns for describing function parameters, return values, and data structures. Supports nested objects, arrays, enumerations, and comprehensive validation constraints.

Schema Types

  • :STRING - Text values with optional pattern/format constraints
  • :NUMBER - Floating-point numeric values
  • :INTEGER - Whole number values
  • :BOOLEAN - True/false values
  • :OBJECT - Structured objects with typed properties
  • :ARRAY - Ordered collections with item type constraints

Examples

# Simple string schema
{:ok, schema} = Schema.new(%{
  type: :STRING,
  description: "User's email address"
})

# Object schema with nested properties
{:ok, schema} = Schema.new(%{
  type: :OBJECT,
  properties: %{
    "name" => %{type: :STRING},
    "age" => %{type: :INTEGER, minimum: 0}
  },
  required: ["name"]
})

# Array schema
{:ok, schema} = Schema.new(%{
  type: :ARRAY,
  items: %{type: :STRING},
  description: "List of tags"
})

Summary

Types

t()

A validated Schema structure.

Functions

Parse schema from JSON-deserialized map.

Construct a new validated Schema.

Convert schema to JSON-serializable map.

Validate a value against this schema.

Types

schema_type()

@type schema_type() :: :STRING | :NUMBER | :INTEGER | :BOOLEAN | :OBJECT | :ARRAY

t()

@type t() :: %Altar.ADM.Schema{
  description: String.t() | nil,
  enum: [any()] | nil,
  format: String.t() | nil,
  items: t() | nil,
  max_items: non_neg_integer() | nil,
  max_length: non_neg_integer() | nil,
  maximum: number() | nil,
  min_items: non_neg_integer() | nil,
  min_length: non_neg_integer() | nil,
  minimum: number() | nil,
  pattern: String.t() | nil,
  properties: %{optional(String.t()) => t()} | nil,
  required: [String.t()] | nil,
  type: schema_type()
}

A validated Schema structure.

Functions

from_map(map)

@spec from_map(map()) :: {:ok, t()} | {:error, String.t()}

Parse schema from JSON-deserialized map.

new(attrs)

@spec new(map() | keyword()) :: {:ok, t()} | {:error, String.t()}

Construct a new validated Schema.

Required Fields

  • :type - One of [:STRING, :NUMBER, :INTEGER, :BOOLEAN, :OBJECT, :ARRAY]

Optional Fields

  • :description - Human-readable description
  • :properties - Map of property schemas (for OBJECT type)
  • :required - List of required property names (for OBJECT type)
  • :items - Schema for array items (for ARRAY type)
  • :enum - List of allowed values
  • :format - Format hint (e.g., "email", "uri", "date-time")
  • :minimum - Minimum numeric value (for NUMBER/INTEGER)
  • :maximum - Maximum numeric value (for NUMBER/INTEGER)
  • :pattern - Regex pattern (for STRING)
  • :min_items - Minimum array length (for ARRAY)
  • :max_items - Maximum array length (for ARRAY)
  • :min_length - Minimum string length (for STRING)
  • :max_length - Maximum string length (for STRING)

Returns {:ok, schema} or {:error, reason}.

to_map(schema)

@spec to_map(t()) :: map()

Convert schema to JSON-serializable map.

validate(schema, value)

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

Validate a value against this schema.

Returns :ok if the value conforms to the schema, or {:error, reason} otherwise.

Examples

{:ok, schema} = Schema.new(%{type: :STRING, min_length: 3})
:ok = Schema.validate(schema, "hello")
{:error, _} = Schema.validate(schema, "hi")