| 1 |
[[constraints]] |
| 2 |
== Constraints |
| 3 |
|
| 4 |
Constraints are validation and conversion functions applied |
| 5 |
to user input. |
| 6 |
|
| 7 |
They are used in various places in Cowboy, including the |
| 8 |
router and the `cowboy_req` match functions. |
| 9 |
|
| 10 |
=== Syntax |
| 11 |
|
| 12 |
Constraints are provided as a list of fields. For each field |
| 13 |
in the list, specific constraints can be applied, as well as |
| 14 |
a default value if the field is missing. |
| 15 |
|
| 16 |
A field can take the form of an atom `field`, a tuple with |
| 17 |
constraints `{field, Constraints}` or a tuple with constraints |
| 18 |
and a default value `{field, Constraints, Default}`. |
| 19 |
The `field` form indicates the field is mandatory. |
| 20 |
|
| 21 |
Note that when used with the router, only the second form |
| 22 |
makes sense, as it does not use the default and the field |
| 23 |
is always defined. |
| 24 |
|
| 25 |
Constraints for each field are provided as an ordered list |
| 26 |
of atoms or funs to apply. Built-in constraints are provided |
| 27 |
as atoms, while custom constraints are provided as funs. |
| 28 |
|
| 29 |
When multiple constraints are provided, they are applied in |
| 30 |
the order given. If the value has been modified by a constraint |
| 31 |
then the next one receives the new value. |
| 32 |
|
| 33 |
For example, the following constraints will first validate |
| 34 |
and convert the field `my_value` to an integer, and then |
| 35 |
check that the integer is positive: |
| 36 |
|
| 37 |
[source,erlang] |
| 38 |
---- |
| 39 |
PositiveFun = fun |
| 40 |
(_, V) when V > 0 -> |
| 41 |
{ok, V}; |
| 42 |
(_, _) -> |
| 43 |
{error, not_positive} |
| 44 |
end, |
| 45 |
{my_value, [int, PositiveFun]}. |
| 46 |
---- |
| 47 |
|
| 48 |
We ignore the first fun argument in this snippet. We shouldn't. |
| 49 |
We will simply learn what it is later in this chapter. |
| 50 |
|
| 51 |
When there's only one constraint, it can be provided directly |
| 52 |
without wrapping it into a list: |
| 53 |
|
| 54 |
[source,erlang] |
| 55 |
---- |
| 56 |
{my_value, int} |
| 57 |
---- |
| 58 |
|
| 59 |
=== Built-in constraints |
| 60 |
|
| 61 |
Built-in constraints are specified as an atom: |
| 62 |
|
| 63 |
[cols="<,<",options="header"] |
| 64 |
|=== |
| 65 |
| Constraint | Description |
| 66 |
| int | Converts binary value to integer. |
| 67 |
| nonempty | Ensures the binary value is non-empty. |
| 68 |
|=== |
| 69 |
|
| 70 |
=== Custom constraints |
| 71 |
|
| 72 |
Custom constraints are specified as a fun. This fun takes |
| 73 |
two arguments. The first argument indicates the operation |
| 74 |
to be performed, and the second is the value. What the |
| 75 |
value is and what must be returned depends on the operation. |
| 76 |
|
| 77 |
Cowboy currently defines three operations. The operation |
| 78 |
used for validating and converting user input is the `forward` |
| 79 |
operation. |
| 80 |
|
| 81 |
[source,erlang] |
| 82 |
---- |
| 83 |
int(forward, Value) -> |
| 84 |
try |
| 85 |
{ok, binary_to_integer(Value)} |
| 86 |
catch _:_ -> |
| 87 |
{error, not_an_integer} |
| 88 |
end; |
| 89 |
---- |
| 90 |
|
| 91 |
The value must be returned even if it is not converted |
| 92 |
by the constraint. |
| 93 |
|
| 94 |
The two other operations are currently experimental. They are |
| 95 |
meant to help implement HATEOAS type services, but proper |
| 96 |
support for HATEOAS is not expected to be available before |
| 97 |
Cowboy 3.0 because of Cowboy's current router's limitations. |
| 98 |
|
| 99 |
The `reverse` operation does the opposite: it |
| 100 |
takes a converted value and changes it back to what the |
| 101 |
user input would have been. |
| 102 |
|
| 103 |
[source,erlang] |
| 104 |
---- |
| 105 |
int(reverse, Value) -> |
| 106 |
try |
| 107 |
{ok, integer_to_binary(Value)} |
| 108 |
catch _:_ -> |
| 109 |
{error, not_an_integer} |
| 110 |
end; |
| 111 |
---- |
| 112 |
|
| 113 |
Finally, the `format_error` operation takes an error |
| 114 |
returned by any other operation and returns a formatted |
| 115 |
human-readable error message. |
| 116 |
|
| 117 |
[source,erlang] |
| 118 |
---- |
| 119 |
int(format_error, {not_an_integer, Value}) -> |
| 120 |
io_lib:format("The value ~p is not an integer.", [Value]). |
| 121 |
---- |
| 122 |
|
| 123 |
Notice that for this case you get both the error and |
| 124 |
the value that was given to the constraint that produced |
| 125 |
this error. |
| 126 |
|
| 127 |
For the common case of constraints that only need to implement |
| 128 |
the `forward` operation, the `cowboy_constraints:from_fun/1` |
| 129 |
function can be used. It turns a 1-arity function (that returns |
| 130 |
a value on success or throws on failure) into a proper constraint. |
| 131 |
|
| 132 |
[source,erlang] |
| 133 |
---- |
| 134 |
Constraint = cowboy_constraints:from_fun(fun cow_http:ensure_token/1). |
| 135 |
---- |
| 136 |
|
| 137 |
Cowboy will not catch exceptions coming from constraint |
| 138 |
functions. They should be written to not emit any exceptions. |