1个不稳定版本
0.0.0 | 2020年10月22日 |
---|
#10 in #ruleset
每月 22 次下载
在 vale 中使用
25KB
316 行
Vale
Vale 代表有效实体,是一个简单的库,它通过注释或通过Fluent风格的实现提供实体验证。该库的核心是 vale::Validate
trait,这意味着数据可以被验证。该库还提供了对 rocket
网页框架的支持。如果您有兴趣添加其他框架的支持,请毫不犹豫地提交PR!
示例
此示例展示了如何派生验证特征
use vale::Validate;
#[derive(Validate)]
struct Entity {
#[validate(gt(0))]
id: i32,
#[validate(len_lt(5), with(add_element))]
others: Vec<i32>,
#[validate(eq("hello world"))]
msg: String,
}
fn add_element(v: &mut Vec<i32>) -> bool {
v.push(3);
true
}
fn main() {
let mut e = get_entity();
if let Err(errs) = e.validate() {
println!("The following validations failed: {:?}", errs);
}
}
还可以使用Fluent风格的验证
struct Entity {
id: i32,
others: Vec<i32>,
msg: String,
}
impl vale::Validate for Entity {
#[vale::ruleset]
fn validate(&mut self) -> vale::Result {
vale::rule!(self.id > 0, "`id` is nonpositive!");
vale::rule!(self.others.len() < 5, "Too many others");
}
}
依赖关系
~1.5MB
~34K SLoC