Normandy.Schema.Validator (normandy v0.6.1)

View Source

Runtime validation of data against Normandy schemas.

This module provides functions to validate arbitrary data against Normandy schema specifications, ensuring data conforms to the defined JSON Schema constraints.

Examples

defmodule UserSchema do
  use Normandy.Schema

  schema do
    field :name, :string, required: true, minLength: 1
    field :age, :integer, minimum: 0, maximum: 150
    field :email, :string, format: "email"
  end
end

# Valid data
{:ok, _} = Normandy.Schema.Validator.validate(
  UserSchema,
  %{name: "Alice", age: 30, email: "alice@example.com"}
)

# Invalid data
{:error, errors} = Normandy.Schema.Validator.validate(
  UserSchema,
  %{age: -5}
)

Validation Features

This validator supports the following JSON Schema validations:

  • Type validation - Ensures values match their declared types
  • Required fields - Validates required fields are present
  • String constraints
    • minLength / min_length - Minimum string length
    • maxLength / max_length - Maximum string length
    • pattern - Regular expression pattern matching
    • format - String format validation (email, uri, uuid, date-time, ipv4, ipv6)
  • Number constraints
    • minimum - Minimum value (inclusive)
    • maximum - Maximum value (inclusive)
    • exclusiveMinimum - Minimum value (exclusive)
    • exclusiveMaximum - Maximum value (exclusive)
  • Array constraints
    • minItems / min_items - Minimum array length
    • maxItems / max_items - Maximum array length
    • uniqueItems / unique_items - Ensures array items are unique
  • Enum validation - Ensures values are in allowed list
  • Composition - anyOf, oneOf, allOf for complex type unions and intersections
  • Conditionals - if/then/else schemas for context-dependent validation
  • Nested schemas - Validates nested objects and arrays recursively

Format Validation

The validator supports the following string formats:

  • email - RFC 5322 email addresses (simplified)
  • uri - Uniform Resource Identifiers
  • uuid - UUID (Universally Unique Identifier) format
  • date-time - ISO 8601 date-time format
  • ipv4 - IPv4 address format
  • ipv6 - IPv6 address format (simplified)

Error Format

Validation errors are returned as a list of maps with the following structure:

%{
  path: [:field, :nested_field],  # Path to the invalid field
  message: "error message",        # Human-readable error message
  constraint: :minimum             # The constraint that failed
}

Summary

Functions

Validates data against a Normandy schema module.

Validates data against a Normandy schema module, raising on error.

Functions

validate(schema_module, data)

Validates data against a Normandy schema module.

Returns {:ok, data} if validation succeeds, or {:error, errors} if validation fails.

Examples

iex> validate(MySchema, %{name: "Alice"})
{:ok, %{name: "Alice"}}

iex> validate(MySchema, %{age: -1})
{:error, [%{path: [:age], message: "must be >= 0", constraint: :minimum}]}

validate!(schema_module, data)

Validates data against a Normandy schema module, raising on error.

Returns the data if validation succeeds, or raises a Normandy.Schema.ValidationError if validation fails.

Examples

iex> validate!(MySchema, %{name: "Alice"})
%{name: "Alice"}

iex> validate!(MySchema, %{age: -1})
** (Normandy.Schema.ValidationError) Validation failed: age must be >= 0