2020-03-21 01:32:13 +00:00
|
|
|
mod int_validators;
|
2020-03-22 01:34:32 +00:00
|
|
|
mod list_validators;
|
2020-03-21 07:07:11 +00:00
|
|
|
mod string_validators;
|
2020-03-21 01:32:13 +00:00
|
|
|
|
|
|
|
use graphql_parser::schema::Value;
|
|
|
|
|
2020-03-22 01:34:32 +00:00
|
|
|
pub use int_validators::{IntGreaterThan, IntLessThan, IntNonZero, IntRange};
|
|
|
|
pub use list_validators::{ListMaxLength, ListMinLength};
|
|
|
|
pub use string_validators::{Email, StringMaxLength, StringMinLength, MAC};
|
2020-03-21 07:07:11 +00:00
|
|
|
|
2020-03-21 01:32:13 +00:00
|
|
|
/// Input value validator
|
|
|
|
///
|
|
|
|
/// You can create your own input value validator by implementing this trait.
|
2020-03-21 07:07:11 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use async_graphql::*;
|
|
|
|
/// use async_graphql::validators::{Email, MAC, IntRange};
|
|
|
|
///
|
|
|
|
/// struct QueryRoot;
|
|
|
|
///
|
|
|
|
/// #[Object]
|
|
|
|
/// impl QueryRoot {
|
|
|
|
/// // Input is email address
|
|
|
|
/// #[field]
|
2020-03-22 01:34:32 +00:00
|
|
|
/// async fn value1(&self, #[arg(validator(Email))] email: String) -> i32 {
|
2020-03-21 07:07:11 +00:00
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Input is email or MAC address
|
|
|
|
/// #[field]
|
2020-03-22 01:34:32 +00:00
|
|
|
/// async fn value2(&self, #[arg(validator(or(Email, MAC(colon = false))))] email_or_mac: String) -> i32 {
|
2020-03-21 07:07:11 +00:00
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Input is integer between 100 and 200
|
|
|
|
/// #[field]
|
2020-03-22 01:34:32 +00:00
|
|
|
/// async fn value3(&self, #[arg(validator(IntRange(min = 100, max = 200)))] value: i32) -> i32 {
|
2020-03-21 07:07:11 +00:00
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-03-21 01:32:13 +00:00
|
|
|
pub trait InputValueValidator
|
|
|
|
where
|
|
|
|
Self: Sync + Send,
|
|
|
|
{
|
|
|
|
/// Check value is valid, returns the reason for the error if it fails, otherwise None.
|
2020-03-22 01:34:32 +00:00
|
|
|
///
|
|
|
|
/// If the input type is different from the required type, return None directly, and other validators will find this error.
|
2020-03-21 01:32:13 +00:00
|
|
|
fn is_valid(&self, value: &Value) -> Option<String>;
|
|
|
|
}
|
|
|
|
|
2020-03-21 07:07:11 +00:00
|
|
|
/// Merge the two validators and return None only if both validators are successful.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn and<A, B>(a: A, b: B) -> And<A, B>
|
|
|
|
where
|
|
|
|
A: InputValueValidator,
|
|
|
|
B: InputValueValidator,
|
|
|
|
{
|
|
|
|
And(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge two validators, and return None when either validator verifies successfully.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn or<A, B>(a: A, b: B) -> Or<A, B>
|
|
|
|
where
|
|
|
|
A: InputValueValidator,
|
|
|
|
B: InputValueValidator,
|
|
|
|
{
|
|
|
|
Or(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct And<A, B>(A, B);
|
|
|
|
|
|
|
|
impl<A, B> InputValueValidator for And<A, B>
|
|
|
|
where
|
|
|
|
A: InputValueValidator,
|
|
|
|
B: InputValueValidator,
|
|
|
|
{
|
|
|
|
fn is_valid(&self, value: &Value) -> Option<String> {
|
|
|
|
self.0.is_valid(value).and(self.1.is_valid(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct Or<A, B>(A, B);
|
|
|
|
|
|
|
|
impl<A, B> InputValueValidator for Or<A, B>
|
|
|
|
where
|
|
|
|
A: InputValueValidator,
|
|
|
|
B: InputValueValidator,
|
|
|
|
{
|
|
|
|
fn is_valid(&self, value: &Value) -> Option<String> {
|
|
|
|
self.0.is_valid(value).or_else(|| self.1.is_valid(value))
|
|
|
|
}
|
|
|
|
}
|