From 3bc4aa19bb74cacef27c827df626a2451ba3de99 Mon Sep 17 00:00:00 2001 From: Rodgers Date: Sat, 11 Sep 2021 23:31:43 +0700 Subject: [PATCH] Add document for Input Validator with Extensions --- docs/en/src/input_value_validators.md | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/en/src/input_value_validators.md b/docs/en/src/input_value_validators.md index 49ae65d4..46bfb9eb 100644 --- a/docs/en/src/input_value_validators.md +++ b/docs/en/src/input_value_validators.md @@ -85,3 +85,35 @@ impl InputValueValidator for MustBeZero { } } ``` + +Here is an example of a custom validator with extensions (return `async_graphql::Error`): + +```rust +pub struct Email; + +impl InputValueValidator for Email { + fn is_valid_with_extensions(&self, value: &Value) -> Result<(), Error> { + if let Value::String(s) = value { + if &s.to_lowercase() != s { + return Err(Error::new("Validation Error").extend_with(|_, e| { + e.set("key", "email_must_lowercase") + })); + } + + if !validate_non_control_character(s) { + return Err(Error::new("Validation Error").extend_with(|_, e| { + e.set("key", "email_must_no_non_control_character") + })); + } + + if !validate_email(s) { + return Err(Error::new("Validation Error").extend_with(|_, e| { + e.set("key", "invalid_email_format") + })); + } + } + + Ok(()) + } +} +```