jvalid v0.3.0 JValid

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

Summary

Macros

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}

Macros

load_schema(file)

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"
use_schema(name, file)

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
valid_schema?(schema, map)

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)
validate_schema(schema, map)

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)