2015-1-22【erlang】Cowboy学习记录-关于Constraints章节

Constraints

Cowboy provides an optional constraints based validation feature when interacting with user input.

Constraints are first used during routing. The router uses constraints to more accurately match bound values, allowing to create routes where a segment is an integer for example, and rejecting the others.

Constraints are also used when performing a match operation on input data, like the query string or cookies. There, a default value can also be provided for optional values.

Finally, constraints can be used to not only validate input, but also convert said input into proper Erlang terms, all in one step.

翻译:

当与用户的输入交换时,Cowboy提供了一个可选的基于验证机制的constraints字段。

在routing时,Constraints字段将第一个被使用。router模块使用constrains字段更加精确地去匹配绑定的值,比如说,允许创建一个段落是integer类型的routes,但是拒绝创建其他的。

Constraints也在对输入的数据进行匹配操作时被应用,比如query string或者cookies。在可选字段中也由默认值。

最后,Constraints不但可以用来验证输入,也可以用来将上述的所说的输入转化成合适的Erlang项式,这些都可以在一步内完成。

Structure

Constraints are provided as a list of fields and for each field a list of constraints for that field can be provided.

Fields are either the name of the field; the name and one or more constraints; or the name, one or more constraints and a default value.

When no default value is provided then the field is required. Otherwise the default value is used.

All constraints for a field will be used to match its value in the order they are given. If the value is modified by a constraint, the next constraint receives the updated value.

翻译:

Constrains以一个域列表的形式存在,每个域中都提供了一个constrains列表。

Fields可以指field的名字,一个或多个的constriants的名字,或者默认的值。

当没有默认的值时,则field is requeired(啥意思???)不然,则默认的值将被使用。

一个域中的全部的constraint以他们列出的顺序来匹配值。一个constraint修改了一个值,则下一个constriant讲会接受更新的值。

Built-in constraints

ConstraintDescription
intConvert binary value to integer
nonemptyEnsures the binary value is non-empty

Custom constraint

In addition to the predefined constraints, Cowboy will accept a fun. This fun must accept one argument and return one of true, {true, NewValue} or false. The result indicates whether the value matches the constraint, and if it does it can optionally be modified. This allows converting the value to a more appropriate Erlang term.

Note that constraint functions SHOULD be pure and MUST NOT crash.

翻译:

除了预定义的constraints,Cowboy也接受fun函数。该fun函数必须接受一个输入参数,起返回true, {true, NewValue} 或者 false中的三者中的一个。

结果预示了值是否匹配constrait,或者它确实是可以被修改的。这就允许讲这个值转换成更加适合的Erlang项式。

注意constraint函数必须是干净的和不能crash掉(啥叫pure?????????)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值