2020-04-15 03:15:30 +00:00
|
|
|
# Input value validators
|
2020-05-09 21:39:04 +00:00
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
`Async-graphql` has some common validators built-in, you can use them on the parameters of object fields or on the fields of `InputObject`.
|
2020-05-09 21:39:04 +00:00
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
- **maximum=N** the number cannot be greater than `N`.
|
|
|
|
- **minimum=N** the number cannot be less than `N`.
|
|
|
|
- **multiple_of=N** the number must be a multiple of `N`.
|
|
|
|
- **max_items=N** the length of the list cannot be greater than `N`.
|
|
|
|
- **min_items=N** the length of the list cannot be less than `N`.
|
|
|
|
- **max_length=N** the length of the string cannot be greater than `N`.
|
|
|
|
- **min_length=N** the length of the string cannot be less than `N`.
|
2021-11-16 08:20:40 +00:00
|
|
|
- **chars_max_length=N** the count of the unicode chars cannot be greater than `N`.
|
|
|
|
- **chars_min_length=N** the count of the unicode chars cannot be less than `N`.
|
|
|
|
- **email** is valid email.
|
2021-11-17 03:08:34 +00:00
|
|
|
- **url** is valid url.
|
|
|
|
- **ip** is valid ip address.
|
|
|
|
- **regex=RE** is match for the regex.
|
2020-05-09 21:39:04 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
use async_graphql::*;
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-05-09 21:39:04 +00:00
|
|
|
impl Query {
|
2021-11-16 06:51:20 +00:00
|
|
|
/// The length of the name must be greater than or equal to 5 and less than or equal to 10.
|
|
|
|
async fn input(#[graphql(validator(min_length = 5, max_length = 10))] name: String) {
|
2020-05-09 21:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
## Check every member of the list
|
|
|
|
|
|
|
|
You can enable the `list` attribute, and the validator will check all members in list:
|
2020-05-09 21:39:04 +00:00
|
|
|
|
|
|
|
```rust
|
2021-07-20 07:16:34 +00:00
|
|
|
use async_graphql::*;
|
2020-05-09 21:39:04 +00:00
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-05-09 21:39:04 +00:00
|
|
|
impl Query {
|
2021-11-16 06:51:20 +00:00
|
|
|
async fn input(#[graphql(validator(list, max_length = 10))] names: Vec<String>) {
|
2021-07-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
## Custom validator
|
2021-07-20 07:16:34 +00:00
|
|
|
|
|
|
|
```rust
|
2021-11-16 06:51:20 +00:00
|
|
|
struct MyValidator {
|
|
|
|
expect: i32,
|
|
|
|
}
|
2021-07-20 07:16:34 +00:00
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
impl MyValidator {
|
|
|
|
pub fn new(n: i32) -> Self {
|
|
|
|
MyValidator { expect: n }
|
2020-05-09 21:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
impl CustomValidator<i32> for MyValidator {
|
2021-12-02 12:22:11 +00:00
|
|
|
fn check(&self, value: &i32) -> Result<(), String> {
|
2021-11-16 06:51:20 +00:00
|
|
|
if *value == self.expect {
|
2020-07-30 01:43:51 +00:00
|
|
|
Ok(())
|
2021-11-16 06:51:20 +00:00
|
|
|
} else {
|
|
|
|
Err(format!("expect 100, actual {}", value))
|
2020-05-09 21:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-11 16:31:43 +00:00
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
struct Query;
|
2021-09-11 16:31:43 +00:00
|
|
|
|
2021-11-16 06:51:20 +00:00
|
|
|
#[Object]
|
|
|
|
impl Query {
|
|
|
|
/// n must be equal to 100
|
|
|
|
async fn value(
|
|
|
|
&self,
|
|
|
|
#[graphql(validator(custom = "MyValidator::new(100)"))] n: i32,
|
|
|
|
) -> i32 {
|
|
|
|
n
|
2021-09-11 16:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|