Fix the validator cannot work on `Option<Vec<T>>`.
This commit is contained in:
parent
28a17a9a03
commit
c0a257dc7d
|
@ -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.9] 2021-11-30
|
||||
|
||||
- Fix the validator cannot work on `Option<Vec<T>>`.
|
||||
|
||||
## [3.0.8] 2021-11-30
|
||||
|
||||
- `#[graphql(validator(list))]` no longer applies to `max_items` and `min_items`.
|
||||
|
|
|
@ -164,9 +164,11 @@ impl Validators {
|
|||
if !elem_validators.is_empty() {
|
||||
if self.list {
|
||||
codes.push(quote! {
|
||||
for __item in #value {
|
||||
if let ::std::option::Option::Some(__raw_value) = #crate_name::InputType::as_raw_value(__item) {
|
||||
#(#elem_validators #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) {
|
||||
#(#elem_validators #map_err ?;)*
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -618,6 +618,13 @@ pub async fn test_list_both_max_items_and_max_length() {
|
|||
) -> String {
|
||||
values.into_iter().collect()
|
||||
}
|
||||
|
||||
async fn value2(
|
||||
&self,
|
||||
#[graphql(validator(list, max_length = 3, max_items = 2))] values: Option<Vec<String>>,
|
||||
) -> String {
|
||||
values.into_iter().flatten().collect()
|
||||
}
|
||||
}
|
||||
|
||||
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
||||
|
@ -662,4 +669,61 @@ pub async fn test_list_both_max_items_and_max_length() {
|
|||
"value": "ab"
|
||||
})
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
schema
|
||||
.execute(r#"{ value2(values: ["a", "b", "cdef"])}"#)
|
||||
.await
|
||||
.into_result()
|
||||
.unwrap_err(),
|
||||
vec![ServerError {
|
||||
message: r#"Failed to parse "[String!]": the value length is 3, must be less than or equal to 2"#.to_string(),
|
||||
source: None,
|
||||
locations: vec![Pos { column: 18, line: 1}],
|
||||
path: vec![PathSegment::Field("value2".to_string())],
|
||||
extensions: None
|
||||
}]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
schema
|
||||
.execute(r#"{ value2(values: ["a", "cdef"])}"#)
|
||||
.await
|
||||
.into_result()
|
||||
.unwrap_err(),
|
||||
vec![ServerError {
|
||||
message: r#"Failed to parse "String": the string length is 4, must be less than or equal to 3"#.to_string(),
|
||||
source: None,
|
||||
locations: vec![Pos { column: 18, line: 1}],
|
||||
path: vec![PathSegment::Field("value2".to_string())],
|
||||
extensions: None
|
||||
}]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
schema
|
||||
.execute(r#"{ value2(values: ["a", "b", "cdef"])}"#)
|
||||
.await
|
||||
.into_result()
|
||||
.unwrap_err(),
|
||||
vec![ServerError {
|
||||
message: r#"Failed to parse "[String!]": the value length is 3, must be less than or equal to 2"#.to_string(),
|
||||
source: None,
|
||||
locations: vec![Pos { column: 18, line: 1}],
|
||||
path: vec![PathSegment::Field("value2".to_string())],
|
||||
extensions: None
|
||||
}]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
schema
|
||||
.execute(r#"{ value2(values: null)}"#)
|
||||
.await
|
||||
.into_result()
|
||||
.unwrap()
|
||||
.data,
|
||||
value!({
|
||||
"value2": ""
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue