Expand description
A library to check the values from a submitted form or a query string.
This library provides a validator, with the hope of helping Web developers check user-submitted values in an easy and declarative way.
§Examples
// Prepare params, this is just for illustrating. Usually, we get
// params through decoding a URL-encoded string into a
// HashMap<String, Vec<String>>.
let mut params = std::collections::HashMap::new();
params.insert("name".to_string(), vec!["bob".to_string()]);
params.insert("age".to_string(), vec!["20".to_string()]);
// Make a new Validator.
let mut validator = Validator::new();
// Add Checkers to Validator.
validator
.check(Checker::new("name", "姓名", Str)
.meet(Rule::Max(5))
.meet(Rule::Min(2)))
.check(Checker::new("age", "年龄", I64)
.meet(Rule::Max(100))
.meet(Rule::Min(18)));
// Validate it!
validator.validate(¶ms);
// Decide whether it is valid.
assert!(validator.is_valid());
// Show me the valid data, assuming it is valid.
assert_eq!(validator.get_required("name").as_str().unwrap(), "bob".to_string());
assert_eq!(validator.get_required("age").as_i64().unwrap(), 20);
Structs§
- Checker
- The checker for a field.
- China
Mobile - A field type to represent a mobile number used in China.
- A field type to represent an Email.
- I64
- A general field type to represent an integer field.
- Some
Message - A specific message
- Str
- A general field type to represent a string field.
- Validator
- The Validator type.
Enums§
- Checker
Option - Option you can set to a checker.
- Field
Value - An enum to represent the primitive value extracted, resulting from applying a checker.
- Message
- A general message wrapper
- Message
Kind - This enum is used to mark a type of a
Message
- Rule
- This enum offers rules avalable to be applied to a
FieldValue
.
Traits§
- Checkable
- Represents a type to fed to the Validator.
- Field
Type - This trait represents the field type.
- Message
Renderer - If you want to control how the message is displayed, implement this trait.