jvalid v1.0.0 JValid

Macro allows to include JSON Schema into your code and validate maps based on them.

Link to this section Summary

Functions

Load schema at compile time to be later used in validator. This macro will include in inline in a function scope.

Load schema at compile time to be later used in validator. This macro will include in inline in a module scope. It can save resources and reduce application size.

Validate map using previously loaded schema. Returns true if schema is valid.

Validate map using previously loaded schema. Returns :ok if map is valid, otherwise - {:error, reason}.

Link to this section Functions

Link to this macro

load_schema(file)

(macro)

Load schema at compile time to be later used in validator. This macro will include in inline in a function scope.

Example:

  schema = load_schema "test/support/schema.json"
Link to this macro

use_schema(name, file)

(macro)

Load schema at compile time to be later used in validator. This macro will include in inline in a module scope. It can save resources and reduce application size.

Example:

  defmodule Sample
    use_schema :schema, "test/support/schema.json"
  end
Link to this macro

valid_schema?(schema, map)

(macro)

Validate map using previously loaded schema. Returns true if schema is valid.

Example:

  use JValid

  # Include schema in a module scope
  use_schema :schema, "test/support/schema.json"
  # ...
  valid_schema?(:schema, map)

  # Include schema in a function scope
  "test/support/schema.json"
  |> load_schema
  |> valid_schema?(map)
Link to this macro

validate_schema(schema, map)

(macro)

Validate map using previously loaded schema. Returns :ok if map is valid, otherwise - {:error, reason}.

Example:

  use JValid

  # Include schema in a module scope
  use_schema :schema, "test/support/schema.json"
  # ...
  validate_schema?(:schema, map)

  # Include schema in a function scope
  "test/support/schema.json"
  |> load_schema
  |> validate_schema?(map)