tide-validator is a middleware working with Tide, a web framework in Rust which let you validate your data coming from a request. You'll be able to create custom validators to validate your HTTP parameters, query parameters, cookies and headers.
Features
- Custom validators: you can chain multiple validators and develop a custom validator is very easy. It's just a closure.
- Validate everything: with the enum
HttpField
you can validate different fields like cookies, headers, query parameters and parameters. - Your own errors: thanks to generics in Rust you can use your own custom error when the data is invalid. need.
Validators
To create your own validator it's just a closure to create with this form:
// The first closure's parameter is the parameter/queryparameter/cookie/header name.
// The second parameter is the value of this HTTP element.
// None means the field doesn't exist in the request (useful to force specific fields to be required).
Fn + Send + Sync + 'static where T: Serialize + Send + Sync + 'static
Documentation
The full documentation is available here
Examples
- simple validation
// Our own validator is a simple closure to check if the field is a number
//... in main function
let mut app = new;
let mut validator_middleware = new;
// 'age' is the parameter name inside the route '/test/:age'
validator_middleware.add_validator;
// You can assign different middleware for each routes therefore different validators for each routes
app.at
.middleware
.get;
app.listen.await?;
- chain multiple validators
// This validator force element to be required
// ... your main function
let mut app = new;
let mut validator_middleware = new;
// Here 'age' is a query parameter, the validator stay the same as in previous example
validator_middleware.add_validator;
// You can also add multiple validators on a single query parameter to check different things
validator_middleware.add_validator;
// You can assign different middleware for each routes therefore different validators for each routes
app.at
.middleware
.get;
app.listen.await?;
- Use your own custom error
// Your custom error which your api will send if an error occurs
// Your validator can also return your own error type
// ... your main function
- Dynamic validators
// Validator inside a function as a closure to be dynamic with max_length
// Simply call it on a cookie `session` for example:
validator_middleware.add_validator;