From 40a71929195b051fc192800289a4cd96e67cbb73 Mon Sep 17 00:00:00 2001 From: Sunli Date: Tue, 30 Nov 2021 13:46:23 +0800 Subject: [PATCH] Fix the custom validator cannot work on `Option>`. --- CHANGELOG.md | 4 ++ derive/src/validators.rs | 10 ++-- docs/en/src/integrations_to_actix_web.md | 2 +- tests/validators.rs | 62 ++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1004a55..5a6f4793 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.0.10] 2021-11-30 + +- Fix the custom validator cannot work on `Option>`. + ## [3.0.9] 2021-11-30 - Fix the validator cannot work on `Option>`. diff --git a/derive/src/validators.rs b/derive/src/validators.rs index 620989e6..17eb7dfe 100644 --- a/derive/src/validators.rs +++ b/derive/src/validators.rs @@ -188,10 +188,12 @@ impl Validators { if self.list { codes.push(quote! { let __custom_validator = #__create_custom_validator; - for __item in #value { - if let ::std::option::Option::Some(__raw_value) = #crate_name::InputType::as_raw_value(__item) { - #crate_name::CustomValidator::check(&__custom_validator, __raw_value) - .map_err(|err_msg| #crate_name::InputValueError::<#ty>::custom(err_msg)) #map_err ?; + if let ::std::option::Option::Some(value) = #crate_name::InputType::as_raw_value(#value) { + for __item in value { + if let ::std::option::Option::Some(__raw_value) = #crate_name::InputType::as_raw_value(__item) { + #crate_name::CustomValidator::check(&__custom_validator, __raw_value) + .map_err(|err_msg| #crate_name::InputValueError::<#ty>::custom(err_msg)) #map_err ?; + } } } }); diff --git a/docs/en/src/integrations_to_actix_web.md b/docs/en/src/integrations_to_actix_web.md index 8c78d070..e572bf69 100644 --- a/docs/en/src/integrations_to_actix_web.md +++ b/docs/en/src/integrations_to_actix_web.md @@ -1,4 +1,4 @@ -# Poem +# Actix-web ## Request example diff --git a/tests/validators.rs b/tests/validators.rs index af7ecef4..655e4aed 100644 --- a/tests/validators.rs +++ b/tests/validators.rs @@ -341,6 +341,20 @@ pub async fn test_custom_validator() { async fn input(&self, input: MyInput) -> i32 { input.n } + + async fn value2( + &self, + #[graphql(validator(list, custom = "MyValidator::new(100)"))] values: Vec, + ) -> i32 { + values.into_iter().sum() + } + + async fn value3( + &self, + #[graphql(validator(list, custom = "MyValidator::new(100)"))] values: Option>, + ) -> i32 { + values.into_iter().flatten().sum() + } } struct Subscription; @@ -444,6 +458,54 @@ pub async fn test_custom_validator() { extensions: None }] ); + + assert_eq!( + schema + .execute("{ value2(values: [77, 88] ) }") + .await + .into_result() + .unwrap_err(), + vec![ServerError { + message: r#"Failed to parse "[Int!]": expect 100, actual 77"#.to_string(), + source: None, + locations: vec![Pos { + line: 1, + column: 18 + }], + path: vec![PathSegment::Field("value2".to_string())], + extensions: None + }] + ); + + assert_eq!( + schema + .execute("{ value3(values: [77, 88] ) }") + .await + .into_result() + .unwrap_err(), + vec![ServerError { + message: r#"Failed to parse "[Int!]": expect 100, actual 77"#.to_string(), + source: None, + locations: vec![Pos { + line: 1, + column: 18 + }], + path: vec![PathSegment::Field("value3".to_string())], + extensions: None + }] + ); + + assert_eq!( + schema + .execute("{ value3(values: null ) }") + .await + .into_result() + .unwrap() + .data, + value!({ + "value3": 0 + }) + ); } #[tokio::test]