### Update Dependencies Source: https://hexdocs.pm/ex_json_schema/readme.html Command to fetch the newly added dependencies. ```bash $ mix deps.get ``` -------------------------------- ### Add Dependency to mix.exs Source: https://hexdocs.pm/ex_json_schema/readme.html Include the ex_json_schema package in your project's dependency list. ```elixir defp deps do [ {:ex_json_schema, "~> 0.11.5"} ] end ``` -------------------------------- ### Define options() type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Configuration options for validation, specifically for setting an error formatter. ```elixir @type options() :: [{:error_formatter, module() | false}] ``` -------------------------------- ### version() Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.Draft7.html Retrieves the version number of the Draft 7 schema implementation. ```APIDOC ## version() ### Description Returns the integer version of the schema. ### Signature `version() :: integer()` ``` -------------------------------- ### Configure global custom format validator Source: https://hexdocs.pm/ex_json_schema/readme.html Sets a global callback module and function for handling custom format validation. ```elixir config :ex_json_schema, :custom_format_validator, {MyModule, :validate} ``` -------------------------------- ### Configure Remote Schema Resolver Source: https://hexdocs.pm/ex_json_schema/readme.html Register a function or module to fetch remote schemata at runtime in your configuration file. ```elixir config :ex_json_schema, :remote_schema_resolver, fn url -> HTTPoison.get!(url).body |> Jason.decode! end ``` ```elixir config :ex_json_schema, :remote_schema_resolver, {MyModule, :my_resolver} ``` -------------------------------- ### get_fragment(root, ref) Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Retrieves a fragment from the schema root based on a reference or path. ```APIDOC ## get_fragment(root, ref) ### Description Retrieves a specific fragment from the schema root using a reference or JSON path. ### Parameters - **root** (ExJsonSchema.Schema.Root.t) - Required - The schema root object. - **ref** (ExJsonSchema.Schema.Ref.t | ExJsonSchema.json_path) - Required - The reference or path to the fragment. ### Response - Returns {:ok, resolved()} or an invalid_reference_error. ``` -------------------------------- ### Retrieve schema fragment (bang version) Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Retrieves a fragment from the schema root, raising an error if not found. ```elixir @spec get_fragment!( ExJsonSchema.Schema.Root.t(), ExJsonSchema.Schema.Ref.t() | ExJsonSchema.json_path() ) :: resolved() | no_return() ``` -------------------------------- ### Check validity with valid?/2 Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Determines if the provided data is valid against the given schema root or object. ```elixir @spec valid?( ExJsonSchema.Schema.Root.t() | ExJsonSchema.object(), ExJsonSchema.data() ) :: boolean() | no_return() ``` -------------------------------- ### Implement a custom error formatter Source: https://hexdocs.pm/ex_json_schema/readme.html Defines a module to format raw errors and applies it during validation. ```elixir defmodule MyFormatter do def format(errors) do Enum.map(errors, fn %ExJsonSchema.Validator.Error{error: error, path: path} -> {error.__struct__, path} end) end end ``` ```elixir iex> ExJsonSchema.Validator.validate(schema, %{"foo" => 1}, error_formatter: MyFormatter) {:error, [{ExJsonSchema.Validator.Error.Type, "#/foo"}]} ``` -------------------------------- ### schema() Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.Draft7.html Retrieves the JSON Schema Draft 7 data structure. ```APIDOC ## schema() ### Description Returns the schema data for Draft 7. ### Signature `schema() :: ExJsonSchema.data()` ``` -------------------------------- ### Define json_path() type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.html Represents a path within a JSON structure as a string. ```elixir @type json_path() :: String.t() ``` -------------------------------- ### Retrieve schema fragment Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Retrieves a fragment from the schema root based on a reference or path. ```elixir @spec get_fragment( ExJsonSchema.Schema.Root.t(), ExJsonSchema.Schema.Ref.t() | ExJsonSchema.json_path() ) :: {:ok, resolved()} | invalid_reference_error() | no_return() ``` -------------------------------- ### Resolve a Schema Source: https://hexdocs.pm/ex_json_schema/readme.html Validate a schema against its meta-schema and resolve references. Perform this once to optimize validation performance. ```elixir schema = %{ "type" => "object", "properties" => %{ "foo" => %{ "type" => "string" } } } |> ExJsonSchema.Schema.resolve() ``` -------------------------------- ### Define the Ref type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.Ref.html Represents the structure of a JSON schema reference, including its location and fragment information. ```elixir @type t() :: %ExJsonSchema.Schema.Ref{ fragment: [String.t()], fragment_pointer?: boolean(), location: String.t() | nil } ``` -------------------------------- ### Check fragment validity with valid_fragment?/3 Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Validates data against a specific schema fragment or reference. ```elixir @spec valid_fragment?( ExJsonSchema.Schema.Root.t(), ExJsonSchema.json_path() | ExJsonSchema.Schema.resolved(), ExJsonSchema.data() ) :: boolean() | ExJsonSchema.Schema.invalid_reference_error() | no_return() ``` -------------------------------- ### resolve(schema, options) Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Resolves a schema into a root structure. ```APIDOC ## resolve(schema, options) ### Description Resolves a given schema into an ExJsonSchema.Schema.Root structure. ### Parameters - **schema** (boolean | ExJsonSchema.Schema.Root.t | ExJsonSchema.object) - Required - The schema to resolve. - **options** (list) - Optional - A list of options, such as {:custom_format_validator, {module(), atom()}}. ### Response - Returns an ExJsonSchema.Schema.Root.t structure. ``` -------------------------------- ### Retrieve referenced schema Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Fetches the schema associated with a specific reference. ```elixir @spec get_ref_schema(ExJsonSchema.Schema.Root.t(), ExJsonSchema.Schema.Ref.t()) :: ExJsonSchema.data() | no_return() ``` -------------------------------- ### Implement validate callback Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Callback signature for custom validation logic within the validator behaviour. ```elixir @callback validate( ExJsonSchema.Schema.Root.t(), ExJsonSchema.data(), {String.t(), ExJsonSchema.data()}, ExJsonSchema.data(), ExJsonSchema.json_path() ) :: errors() ``` -------------------------------- ### Validate fragment with validate_fragment/4 Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Validates data against a schema fragment, returning :ok or an error tuple. ```elixir @spec validate_fragment( ExJsonSchema.Schema.Root.t(), ExJsonSchema.json_path() | ExJsonSchema.Schema.resolved(), ExJsonSchema.data(), options() ) :: :ok | {:error, errors()} | ExJsonSchema.Schema.invalid_reference_error() | no_return() ``` -------------------------------- ### ExJsonSchema.Schema.Root Type Definition Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.Root.html Represents the internal structure of the root schema, including configuration for custom format validation and schema resolution. ```elixir @type t() :: %ExJsonSchema.Schema.Root{ custom_format_validator: {module(), atom()} | (String.t(), any() -> boolean() | {:error, any()}) | nil, location: :root | String.t(), refs: %{required(String.t()) => ExJsonSchema.Schema.resolved()}, schema: ExJsonSchema.Schema.resolved(), version: non_neg_integer() | nil } ``` -------------------------------- ### format(errors) Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.Error.StringFormatter.html Formats a list of validation errors into a list of string tuples. ```APIDOC ## format(errors) ### Description Formats the provided validation errors into a list of tuples containing string representations of the error details. ### Signature `format(ExJsonSchema.Validator.errors()) :: [{String.t(), String.t()}]` ### Parameters - **errors** (ExJsonSchema.Validator.errors()) - Required - The list of validation errors to be formatted. ### Returns - **List** ([{String.t(), String.t()}]) - A list of tuples where each tuple contains a string representation of the error path and the error message. ``` -------------------------------- ### Resolve schema Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Resolves a schema definition into a root structure, optionally accepting custom format validators. ```elixir @spec resolve(boolean() | ExJsonSchema.Schema.Root.t() | ExJsonSchema.object(), [ {:custom_format_validator, {module(), atom()}} ]) :: ExJsonSchema.Schema.Root.t() | no_return() ``` -------------------------------- ### Define object() type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.html Represents a JSON object as a map with string keys and data values. ```elixir @type object() :: %{required(String.t()) => data()} ``` -------------------------------- ### Define data() type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.html Represents the supported JSON data structures including primitives, lists, and objects. ```elixir @type data() :: nil | true | false | list() | float() | integer() | String.t() | [data()] | object() ``` -------------------------------- ### Validate data with detailed errors Source: https://hexdocs.pm/ex_json_schema/readme.html Performs validation and returns :ok or a list of error tuples. ```elixir iex> ExJsonSchema.Validator.validate(schema, %{"foo" => "bar"}) :ok iex> ExJsonSchema.Validator.validate(schema, %{"foo" => 1}) {:error, [{"Type mismatch. Expected String but got Integer.", "#/foo"}]} ``` -------------------------------- ### Retrieve raw validation error structs Source: https://hexdocs.pm/ex_json_schema/readme.html Disables the default string formatter to receive raw error structs. ```elixir iex> ExJsonSchema.Validator.validate(schema, %{"foo" => 1}, error_formatter: false) {:error, [ %ExJsonSchema.Validator.Error{ error: %ExJsonSchema.Validator.Error.Type{ actual: "integer", expected: ["string"] }, path: "#/foo" } ]} ``` -------------------------------- ### decode_json(json) Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Decodes a JSON string. ```APIDOC ## decode_json(json) ### Description Decodes a JSON string into a data structure. ### Parameters - **json** (String.t) - Required - The JSON string to decode. ### Response - Returns {:ok, String.t()} on success or {:error, String.t()} on failure. ``` -------------------------------- ### Retrieve validation errors with validation_errors/4 Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Returns a list of errors found when validating data against a schema fragment. ```elixir @spec validation_errors( ExJsonSchema.Schema.Root.t(), ExJsonSchema.json_path() | ExJsonSchema.Schema.resolved(), ExJsonSchema.data(), String.t() ) :: errors() | ExJsonSchema.Schema.invalid_reference_error() | no_return() ``` -------------------------------- ### Resolve schema with custom format validator Source: https://hexdocs.pm/ex_json_schema/readme.html Passes a custom validator function during schema resolution. ```elixir ExJsonSchema.Schema.resolve(%{"format" => "custom"}, custom_format_validator: {MyModule, :validate}) ``` -------------------------------- ### Raise invalid reference error Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Utility to raise an error when a reference is invalid. ```elixir @spec raise_invalid_reference_error(any()) :: no_return() ``` -------------------------------- ### Define errors() type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Validator.html Represents a list of validation errors, each containing an error term and its corresponding path. ```elixir @type errors() :: [%ExJsonSchema.Validator.Error{error: term(), path: term()}] | list() ``` -------------------------------- ### Resolve schema with anonymous custom validator Source: https://hexdocs.pm/ex_json_schema/readme.html Uses an anonymous function for custom format validation during schema resolution. This is not supported in global application configuration. ```elixir ExJsonSchema.Schema.resolve(%{"format" => "custom"}, custom_format_validator: fn format, data -> true end) ``` -------------------------------- ### Decode JSON string Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Decodes a JSON string into a data structure. ```elixir @spec decode_json(String.t()) :: {:ok, String.t()} | {:error, String.t()} ``` -------------------------------- ### Define resolved type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Alias for the resolved data type within the schema. ```elixir @type resolved() :: ExJsonSchema.data() ``` -------------------------------- ### Define invalid_reference_error type Source: https://hexdocs.pm/ex_json_schema/ExJsonSchema.Schema.html Represents an error state when a reference cannot be resolved. ```elixir @type invalid_reference_error() :: {:error, :invalid_reference} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.