cowboy_constraints:from_fun(3)

Name

cowboy_constraints:from_fun - Create a constraint from a 1-arity function

Description

from_fun(Fun) -> Constraint

Fun :: fun((any()) -> any())
Constraint :: fun()

Create a constraint function from a 1-arity validation or conversion function.

The provided function is called as Fun(Value) for the forward operation. The generated constraint function succeeds when a value is returned, and fails with reason not_valid when an exception is thrown.

The generated constraint does not support the reverse operation and only provides limited format_error support.

Arguments

Fun

A function of arity 1. On success it must return the (optionally converted) value. On failure it must throw an exception.

Return value

A constraint function.

Changelog

  • 2.15: Function introduced.

Examples

Create a constraint using an existing validation function
Constraint = cowboy_constraints:from_fun(fun cow_http:ensure_token/1).

See also

cowboy_constraints(3), cowboy_constraints:int(3), cowboy_constraints:nonempty(3), cowboy_router(3), cowboy_req:match_cookies(3), cowboy_req:match_qs(3)

Cowboy 2.17 Function Reference

Navigation

Version select

Like my work? Donate!

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.