| 1 |
= cowboy_constraints:from_fun(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_constraints:from_fun - Create a constraint from a 1-arity function |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
from_fun(Fun) -> Constraint |
| 12 |
|
| 13 |
Fun :: fun((any()) -> any()) |
| 14 |
Constraint :: fun() |
| 15 |
---- |
| 16 |
|
| 17 |
Create a constraint function from a 1-arity validation or |
| 18 |
conversion function. |
| 19 |
|
| 20 |
The provided function is called as `Fun(Value)` for the |
| 21 |
`forward` operation. The generated constraint function |
| 22 |
succeeds when a value is returned, and fails with reason |
| 23 |
`not_valid` when an exception is thrown. |
| 24 |
|
| 25 |
The generated constraint does not support the `reverse` |
| 26 |
operation and only provides limited `format_error` support. |
| 27 |
|
| 28 |
== Arguments |
| 29 |
|
| 30 |
Fun:: |
| 31 |
|
| 32 |
A function of arity 1. On success it must return the |
| 33 |
(optionally converted) value. On failure it must throw |
| 34 |
an exception. |
| 35 |
|
| 36 |
== Return value |
| 37 |
|
| 38 |
A constraint function. |
| 39 |
|
| 40 |
== Changelog |
| 41 |
|
| 42 |
* *2.15*: Function introduced. |
| 43 |
|
| 44 |
== Examples |
| 45 |
|
| 46 |
.Create a constraint using an existing validation function |
| 47 |
|
| 48 |
[source,erlang] |
| 49 |
---- |
| 50 |
Constraint = cowboy_constraints:from_fun(fun cow_http:ensure_token/1). |
| 51 |
---- |
| 52 |
|
| 53 |
== See also |
| 54 |
|
| 55 |
link:man:cowboy_constraints(3)[cowboy_constraints(3)], |
| 56 |
link:man:cowboy_constraints:int(3)[cowboy_constraints:int(3)], |
| 57 |
link:man:cowboy_constraints:nonempty(3)[cowboy_constraints:nonempty(3)], |
| 58 |
link:man:cowboy_router(3)[cowboy_router(3)], |
| 59 |
link:man:cowboy_req:match_cookies(3)[cowboy_req:match_cookies(3)], |
| 60 |
link:man:cowboy_req:match_qs(3)[cowboy_req:match_qs(3)] |