-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: validation error handling.
- Loading branch information
1 parent
9ef25ea
commit 9b13318
Showing
5 changed files
with
102 additions
and
136 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,24 @@ | ||
use crate::serde::rename::RenameMap; | ||
use crate::types::{Field, SingleIdentPath}; | ||
use crate::types::Field; | ||
use crate::validate::common::{CustomMessageToken, MetaListValidation}; | ||
use crate::validate::generic::{ | ||
extract_generic_custom_validator, extract_generic_enumerate_validator, | ||
}; | ||
use crate::validate::{MetaNameValueValidation, MetaPathValidation, Validator}; | ||
use std::str::FromStr; | ||
use crate::validate::Validator; | ||
|
||
pub fn extract_validator_from_nested_meta_list( | ||
field: &impl Field, | ||
validation_type: MetaListValidation, | ||
validation: &syn::MetaList, | ||
custom_message: CustomMessageToken, | ||
rename_map: &RenameMap, | ||
) -> Result<Validator, crate::Errors> { | ||
let mut errors = vec![]; | ||
|
||
let validation_ident = SingleIdentPath::new(&validation.path).ident(); | ||
let validation_name = validation_ident.to_string(); | ||
|
||
match MetaListValidation::from_str(&validation_name) { | ||
Ok(MetaListValidation::Enumerate) => { | ||
match validation_type { | ||
MetaListValidation::Enumerate => { | ||
extract_generic_enumerate_validator(field, validation, custom_message, rename_map) | ||
} | ||
Ok(MetaListValidation::Custom) => { | ||
MetaListValidation::Custom => { | ||
extract_generic_custom_validator(field, validation, rename_map) | ||
} | ||
Err(unknown) => { | ||
let error = if MetaNameValueValidation::from_str(&validation_name).is_ok() { | ||
crate::Error::validate_meta_name_value_need_value( | ||
&validation.path, | ||
&validation_name, | ||
) | ||
} else if MetaPathValidation::from_str(&validation_name).is_ok() { | ||
crate::Error::validate_meta_path_need_value(&validation.path, &validation_name) | ||
} else { | ||
crate::Error::validate_unknown_type( | ||
&validation.path, | ||
&unknown, | ||
&(MetaPathValidation::iter().map(|x| x.name())) | ||
.chain(MetaNameValueValidation::iter().map(|x| x.name())) | ||
.chain(MetaListValidation::iter().map(|x| x.name())) | ||
.collect::<Vec<_>>(), | ||
) | ||
}; | ||
errors.push(error); | ||
|
||
Err(errors) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,21 @@ | ||
use crate::serde::rename::RenameMap; | ||
use crate::types::{Field, SingleIdentPath}; | ||
use crate::types::Field; | ||
use crate::validate::array::extract_array_unique_items_validator; | ||
use crate::validate::common::{ | ||
CustomMessageToken, MetaListValidation, MetaNameValueValidation, MetaPathValidation, | ||
}; | ||
use crate::validate::common::{CustomMessageToken, MetaPathValidation}; | ||
use crate::validate::Validator; | ||
use std::str::FromStr; | ||
|
||
pub fn extract_validator_from_nested_meta_path( | ||
field: &impl Field, | ||
validation: &syn::Path, | ||
validation_type: MetaPathValidation, | ||
_validation: &syn::Path, | ||
custom_message: CustomMessageToken, | ||
rename_map: &RenameMap, | ||
) -> Result<Validator, crate::Errors> { | ||
let mut errors = vec![]; | ||
|
||
let validation_ident = SingleIdentPath::new(validation).ident(); | ||
let validation_name = validation_ident.to_string(); | ||
|
||
match MetaPathValidation::from_str(&validation_name) { | ||
Ok(MetaPathValidation::UniqueItems) => Ok(extract_array_unique_items_validator( | ||
match validation_type { | ||
MetaPathValidation::UniqueItems => Ok(extract_array_unique_items_validator( | ||
field, | ||
custom_message, | ||
rename_map, | ||
)), | ||
Err(unknown) => { | ||
let error = if MetaNameValueValidation::from_str(&validation_name).is_ok() { | ||
crate::Error::validate_meta_name_value_need_value(validation, &validation_name) | ||
} else if MetaListValidation::from_str(&validation_name).is_ok() { | ||
crate::Error::validate_meta_list_need_value(validation, &validation_name) | ||
} else { | ||
crate::Error::validate_unknown_type( | ||
validation, | ||
&unknown, | ||
&(MetaPathValidation::iter().map(|x| x.name())) | ||
.chain(MetaNameValueValidation::iter().map(|x| x.name())) | ||
.chain(MetaListValidation::iter().map(|x| x.name())) | ||
.collect::<Vec<_>>(), | ||
) | ||
}; | ||
errors.push(error); | ||
|
||
Err(errors) | ||
} | ||
} | ||
} |