ExParamsSchema.Schema.Validator (ex_params_schema v0.1.1)

Copy Markdown View Source

Converts JSON Schema validation results into ExParamsSchema error formats.

This internal module integrates with ExJsonSchema. Use parse/2 or parse_detailed/2 in normal use.

Summary

Functions

Validates JSON-compatible data and normalizes the result to the first declared error reason.

Validates JSON-compatible data and returns all errors with their paths.

Functions

validate(schema, data)

@spec validate(ExParamsSchema.Schema.t(), map()) ::
  :ok | {:error, ExParamsSchema.error_reason()}

Validates JSON-compatible data and normalizes the result to the first declared error reason.

iex> schema = ExParamsSchema.compile!(%{count: {:integer, minimum: 1, error: :invalid_count}})
iex> ExParamsSchema.Schema.Validator.validate(schema, %{"count" => 2})
:ok
iex> ExParamsSchema.Schema.Validator.validate(schema, %{"count" => 0})
{:error, :invalid_count}

validate_detailed(schema, data)

@spec validate_detailed(ExParamsSchema.Schema.t(), map()) ::
  :ok | {:error, [ExParamsSchema.ValidationError.t()]}

Validates JSON-compatible data and returns all errors with their paths.

iex> schema = ExParamsSchema.compile!(%{count: {:integer, minimum: 1, error: :invalid_count}})
iex> {:error, [error]} = ExParamsSchema.Schema.Validator.validate_detailed(schema, %{"count" => 0})
iex> {error.path, error.keyword, error.reason}
{["count"], :minimum, :invalid_count}