hecate_plugin_validate (hecate_sdk v0.6.2)

View Source

Input validation helpers for plugin API handlers.

Validates maps (typically JSON request bodies) against rules. Returns {ok, Validated} with only declared fields, or {error, Errors} with a list of validation failures.

Example: Rules = [ {title, required, binary}, {body, required, binary}, {tags, optional, {list, binary}, []}, {priority, optional, {one_of, [low, medium, high]}, medium} ], case hecate_plugin_validate:check(Input, Rules) of {ok, #{title := T, body := B}} -> ...; {error, Errors} -> ... end.

Summary

Functions

Validate a map against a list of rules.

Require a key exists in a map. Returns value or error.

Require a binary value.

Require an integer value.

Types

error/0

-type error() :: {atom(), binary()}.

rule/0

-type rule() :: {atom(), required, type()} | {atom(), optional, type(), term()}.

type/0

-type type() :: binary | integer | float | boolean | atom | {list, type()} | {one_of, [term()]} | any.

Functions

check(Input, Rules)

-spec check(Input :: map(), Rules :: [rule()]) -> {ok, map()} | {error, [error()]}.

Validate a map against a list of rules.

require(Map, Key)

-spec require(map(), atom()) -> {ok, term()} | {error, {atom(), binary()}}.

Require a key exists in a map. Returns value or error.

require_binary(Map, Key)

-spec require_binary(map(), atom()) -> {ok, binary()} | {error, {atom(), binary()}}.

Require a binary value.

require_integer(Map, Key)

-spec require_integer(map(), atom()) -> {ok, integer()} | {error, {atom(), binary()}}.

Require an integer value.