Created
November 8, 2021 00:02
-
-
Save jayhuang75/b2614b0d22f7a758d34b140bd773ee54 to your computer and use it in GitHub Desktop.
go train delay rust api json body validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub fn new(q: &str) -> Result<Self, AppError> { | |
| // get the input search string | |
| let raw_query: SearchRawQuery = serde_json::from_str(q)?; | |
| let mut query_keys_validation_list: Vec<&Option<Vec<Value>>> = vec![]; | |
| // check if have must keyword | |
| if let Some(must) = &raw_query.query.must { | |
| query_keys_validation_list.extend_from_slice(&[ | |
| &must.ranges, | |
| ]); | |
| } | |
| // all valid then pass the key validation | |
| let is_valid_query = query_keys_validation_list | |
| .par_iter() | |
| .all(|item| SearchRawQuery::has_valid_keys(item)); | |
| // if the query is invalid return error | |
| if !is_valid_query { | |
| return Err(AppError { | |
| message: Some(format!("query string is invalid: {:?}", q.to_string())), | |
| cause: Some("invalid query string".to_string()), | |
| error_type: AppErrorType::SearchError, | |
| }); | |
| } | |
| Ok(raw_query) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment