Fix the custom validator cannot work on `Option<Vec<T>>`.

This commit is contained in:
Sunli 2021-11-30 13:46:23 +08:00
parent 5fcf552a93
commit 86f10c5397
4 changed files with 73 additions and 5 deletions

View File

@ -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<Vec<T>>`.
## [3.0.9] 2021-11-30
- Fix the validator cannot work on `Option<Vec<T>>`.

View File

@ -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 ?;
}
}
}
});

View File

@ -1,4 +1,4 @@
# Poem
# Actix-web
## Request example

View File

@ -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>,
) -> i32 {
values.into_iter().sum()
}
async fn value3(
&self,
#[graphql(validator(list, custom = "MyValidator::new(100)"))] values: Option<Vec<i32>>,
) -> 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]