ExJsonschema.Options (ExJsonschema v0.1.17)

View Source

Configuration options for JSON Schema compilation and validation.

This module provides a structured way to configure JSON Schema operations, including:

  • Draft version selection
  • Format validation settings
  • Performance optimizations

Examples

# Default options with automatic draft detection
opts = ExJsonschema.Options.new()

# Strict validation with format checking
opts = ExJsonschema.Options.new(
  draft: :draft202012,
  validate_formats: true
)

# Performance-optimized options
opts = ExJsonschema.Options.new(
  regex_engine: :regex
)

Summary

Types

JSON Schema draft version specification.

Output format for validation results.

Regular expression engine used for pattern validation.

t()

Functions

Creates options optimized for Draft 4 schemas.

Creates options optimized for Draft 6 schemas.

Creates options optimized for Draft 7 schemas.

Creates options optimized for Draft 2019-09 schemas.

Creates options optimized for Draft 2020-12 schemas.

Creates a new Options struct with default values.

Creates options from a profile with optional overrides.

Validates the options struct and returns {:ok, options} or {:error, reason}.

Types

draft()

@type draft() :: :auto | :draft4 | :draft6 | :draft7 | :draft201909 | :draft202012

JSON Schema draft version specification.

  • :auto - Automatically detect draft from schema's $schema property
  • :draft4 - JSON Schema Draft 4 (2013)
  • :draft6 - JSON Schema Draft 6 (2017)
  • :draft7 - JSON Schema Draft 7 (2019)
  • :draft201909 - JSON Schema 2019-09
  • :draft202012 - JSON Schema 2020-12 (latest)

When :auto is used, the library examines the $schema property to determine the appropriate draft version. Defaults to :draft202012 if no $schema is found.

output_format()

@type output_format() :: :basic | :detailed | :verbose

Output format for validation results.

  • :basic - :ok or {:error, :validation_failed}
  • :detailed - Structured error information with paths and messages (default)
  • :verbose - Comprehensive error details with context, values, and suggestions

Use :basic for maximum performance when you only need pass/fail results. Use :detailed for structured error handling. Use :verbose for debugging and user-friendly error reporting.

regex_engine()

@type regex_engine() :: :fancy_regex | :regex

Regular expression engine used for pattern validation.

  • :fancy_regex - Full-featured regex engine with advanced features (default)
  • :regex - Simpler, faster regex engine for basic patterns

The :fancy_regex engine supports advanced features like lookahead/lookbehind while :regex provides better performance for simple pattern matching.

t()

@type t() :: %ExJsonschema.Options{
  draft: draft(),
  output_format: output_format(),
  regex_engine: regex_engine(),
  validate_formats: boolean()
}

Functions

draft4(overrides \\ [])

Creates options optimized for Draft 4 schemas.

draft6(overrides \\ [])

Creates options optimized for Draft 6 schemas.

draft7(overrides \\ [])

Creates options optimized for Draft 7 schemas.

draft201909(overrides \\ [])

Creates options optimized for Draft 2019-09 schemas.

draft202012(overrides \\ [])

Creates options optimized for Draft 2020-12 schemas.

new(profile_or_overrides \\ [])

Creates a new Options struct with default values.

Options

  • :draft - JSON Schema draft to use (default: :auto)
  • :validate_formats - Enable format validation (default: false)
  • :regex_engine - Regex engine to use (default: :fancy_regex)
  • :output_format - Error output format (default: :detailed)

Examples

iex> opts = ExJsonschema.Options.new()
iex> opts.draft
:auto

iex> opts = ExJsonschema.Options.new(draft: :draft202012, validate_formats: true)
iex> {opts.draft, opts.validate_formats}
{:draft202012, true}

Profile Integration

You can also create Options from predefined profiles:

iex> opts = ExJsonschema.Options.new(:strict)
iex> opts.validate_formats
true

iex> opts = ExJsonschema.Options.new({:performance, [output_format: :basic]})
iex> opts.output_format
:basic

profile(profile_name, overrides \\ [])

Creates options from a profile with optional overrides.

This is a convenience function that's equivalent to ExJsonschema.Profile.get/2 but provides a consistent API within the Options module.

Examples

iex> opts = ExJsonschema.Options.profile(:strict)
iex> opts.validate_formats
true

iex> opts = ExJsonschema.Options.profile(:performance, output_format: :basic)
iex> opts.output_format
:basic

validate(options)

Validates the options struct and returns {:ok, options} or {:error, reason}.

Examples

iex> opts = ExJsonschema.Options.new(draft: :draft202012)
iex> ExJsonschema.Options.validate(opts)
{:ok, opts}

iex> opts = %ExJsonschema.Options{draft: :invalid}
iex> ExJsonschema.Options.validate(opts)
{:error, "Invalid draft version: :invalid"}