Fix the custom validator cannot work on Option<Vec<T>>
.
This commit is contained in:
parent
fcad1006ef
commit
40a7192919
|
@ -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/),
|
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).
|
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
|
## [3.0.9] 2021-11-30
|
||||||
|
|
||||||
- Fix the validator cannot work on `Option<Vec<T>>`.
|
- Fix the validator cannot work on `Option<Vec<T>>`.
|
||||||
|
|
|
@ -188,10 +188,12 @@ impl Validators {
|
||||||
if self.list {
|
if self.list {
|
||||||
codes.push(quote! {
|
codes.push(quote! {
|
||||||
let __custom_validator = #__create_custom_validator;
|
let __custom_validator = #__create_custom_validator;
|
||||||
for __item in #value {
|
if let ::std::option::Option::Some(value) = #crate_name::InputType::as_raw_value(#value) {
|
||||||
if let ::std::option::Option::Some(__raw_value) = #crate_name::InputType::as_raw_value(__item) {
|
for __item in value {
|
||||||
#crate_name::CustomValidator::check(&__custom_validator, __raw_value)
|
if let ::std::option::Option::Some(__raw_value) = #crate_name::InputType::as_raw_value(__item) {
|
||||||
.map_err(|err_msg| #crate_name::InputValueError::<#ty>::custom(err_msg)) #map_err ?;
|
#crate_name::CustomValidator::check(&__custom_validator, __raw_value)
|
||||||
|
.map_err(|err_msg| #crate_name::InputValueError::<#ty>::custom(err_msg)) #map_err ?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Poem
|
# Actix-web
|
||||||
|
|
||||||
## Request example
|
## Request example
|
||||||
|
|
||||||
|
|
|
@ -341,6 +341,20 @@ pub async fn test_custom_validator() {
|
||||||
async fn input(&self, input: MyInput) -> i32 {
|
async fn input(&self, input: MyInput) -> i32 {
|
||||||
input.n
|
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;
|
struct Subscription;
|
||||||
|
@ -444,6 +458,54 @@ pub async fn test_custom_validator() {
|
||||||
extensions: None
|
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]
|
#[tokio::test]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user