2015-1-22【erlang】Cowboy学习记录-关于Constraints章节
2015-01-22 14:45
218 查看
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
Constraint | Description |
---|---|
int | Convert binary value to integer |
nonempty | Ensures 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 oftrue,
{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?????????)
相关文章推荐
- 2015-1-22【erlang】Cowboy学习记录-关于Routing章节
- 2015-1-22【erlang】Cowboy学习记录-关于Static files章节
- Erlang 学习:关于rebar 和 自带debugger 小记录
- 初学Asp.net关于GridView的一些学习记录
- 实习结束了,临走还捞了一票...和Jason聊了许多关于以后在校园技术实践以及学习的方法,宝贵经验,记录下来。
- VGScene 学习日志一:关于TvgScene设置为透明后,里面的按钮不能点击的问题记录
- 关于手机的CPU和GPU,看到了,纪念一下!并且记录一下手机、平板电脑、MP5的学习。
- 20110126 学习记录:一些关于html中布局的代码 & CSS hack速查表
- [dika 记录] cowboy 扩展socket 协议, 以解决flash ,erlang通讯的沙箱为例子
- 20110226 学习记录:关于gridview选中行的字体变色..
- 开发板的移植过程,API~章节学习记录
- [Erlang 学习笔记]关于 erlang application 的一些疑问
- 20071107学习记录 关于实体类的理解
- 20100821 学习记录:VB中WebBrowser控件关于..\ieframe.dll\1'未找到之解决方法
- 王爽汇编学习之作业记录第一章节至第二章节
- 关于PE病毒编写的学习(五)——病毒如何做标记和记录信息
- 关于c++的学习情况,不再更新了,因为找到了更好的笔记记录工具
- 关于Linux的快速启动(fastboot)和低功耗(low power)的学习记录
- 20100921 学习记录:关于sqlserver2005与ASP中 转换日期格式
- C++ 对象模型学习记录(3)--- 第1章 关于对象(未完)