View Source JSV.Validator (jsv v0.3.0)

This is the home of the recursive validation logic.

The validator is called on the root schema, and may be called by vocabulary implementations to validate sub parts of the data built withing each vocabulary module.

Summary

Functions

Reduce over an enum with two accumulators, a user one, and the context.

Validate the given data with the given validator. The validator is typically a sub-part of a JSV.Root struct built with JSV.build/2 such as a JSV.Subschema struct.

Validates data with a sub part of the schema, for instance if, then or else. Data path will not change in the context.

Validate the data with the given validators but separate the current evaluation context during the validation.

Validates a sub term of the data, identified by key, which can be a property name (a string), or an array index (an integer).

Types

context()

@type context() :: %JSV.Validator.ValidationContext{
  data_path: term(),
  errors: term(),
  eval_path: term(),
  evaluated: term(),
  opts: term(),
  scope: term(),
  validators: term()
}

eval_path_segment()

@type eval_path_segment() :: path_segment() | [path_segment()]

path_segment()

@type path_segment() :: binary() | non_neg_integer() | atom()

result()

@type result() :: {:ok, term(), context()} | {:error, context()}

validator()

@type validator() :: JSV.Subschema.t() | JSV.BooleanSchema.t() | {:alias_of, binary()}

Functions

context(validators, scope, opts)

@spec context(%{required(JSV.Key.t()) => validator()}, [JSV.Key.ns()], keyword()) ::
  context()

flat_errors(vctx)

@spec flat_errors(context()) :: [JSV.Validator.Error.t()]

list_evaluaded(vctx)

@spec list_evaluaded(context()) :: [path_segment()]

merge_evaluated(vctx, sub)

@spec merge_evaluated(context(), context()) :: context()

reduce(enum, init, vctx, fun)

@spec reduce(Enumerable.t(), term(), context(), function()) :: result()

Reduce over an enum with two accumulators, a user one, and the context.

  • The callback is called with item, acc, vctx for all items in the enum, regardless of previously returned values. Returning and error tuple does not stop the iteration.
  • When returning {:ok, value, vctx}, value will be the new user accumulator, and the new context is carried on.
  • When returning {:error, vctx}, the current accumulator is not changed, but the new returned context with errors is still carried on.
  • Returning an ok tuple after an error tuple on a previous item does not remove the errors from the context struct.

The final return value is {:ok, acc, vctx} if all calls of the callback returned an OK tuple, {:error, vctx} otherwise.

This is useful to call all possible validators for a given piece of data, collecting all possible errors without stopping, but still returning an error in the end if some error arose.

return(data, vctx)

@spec return(term(), context()) :: result()

to_error(vctx)

@spec to_error(context()) :: JSV.ValidationError.t()

validate(data, subschema, vctx)

@spec validate(term(), validator(), context()) :: result()

Validate the given data with the given validator. The validator is typically a sub-part of a JSV.Root struct built with JSV.build/2 such as a JSV.Subschema struct.

validate_as(data, add_eval_path, subvalidators, vctx)

@spec validate_as(term(), eval_path_segment(), validator(), context()) :: result()

Validates data with a sub part of the schema, for instance if, then or else. Data path will not change in the context.

See validate_in/5 to validate sub terms of the data.

validate_detach(data, add_eval_path, subschema, vctx)

@spec validate_detach(term(), eval_path_segment(), validator(), context()) :: result()

Validate the data with the given validators but separate the current evaluation context during the validation.

Currently evaluated properties or items will not be seen as evaluated during the validation by the given subschema.

validate_in(data, key, add_eval_path, subvalidators, vctx)

@spec validate_in(term(), path_segment(), eval_path_segment(), validator(), context()) ::
  result()

Validates a sub term of the data, identified by key, which can be a property name (a string), or an array index (an integer).

See validate_as/4 to validate the same data point with a nested keyword. For instance if, then or else.

validate_ref(data, ref, eval_path, vctx)

@spec validate_ref(term(), JSV.Key.t(), eval_path_segment(), context()) :: result()

with_error(vctx, kind, data, args)

(macro)