Skip to content

Commit

Permalink
Allow empty #[validate] attributes.
Browse files Browse the repository at this point in the history
Fixes #109
  • Loading branch information
GREsau committed Sep 20, 2021
1 parent 00e482c commit de7314f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions schemars/tests/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Struct {
#[validate(required)]
required_option: Option<bool>,
#[validate(required)]
#[validate]
#[serde(flatten)]
required_flattened: Option<Inner>,
}
Expand Down
11 changes: 8 additions & 3 deletions schemars_derive/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Attrs {

for meta_item in attrs
.iter()
.flat_map(|attr| get_meta_items(attr, attr_type, errors))
.flat_map(|attr| get_meta_items(attr, attr_type, errors, ignore_errors))
.flatten()
{
match &meta_item {
Expand Down Expand Up @@ -201,6 +201,7 @@ fn get_meta_items(
attr: &syn::Attribute,
attr_type: &'static str,
errors: &Ctxt,
ignore_errors: bool,
) -> Result<Vec<syn::NestedMeta>, ()> {
if !attr.path.is_ident(attr_type) {
return Ok(Vec::new());
Expand All @@ -209,11 +210,15 @@ fn get_meta_items(
match attr.parse_meta() {
Ok(List(meta)) => Ok(meta.nested.into_iter().collect()),
Ok(other) => {
errors.error_spanned_by(other, format!("expected #[{}(...)]", attr_type));
if !ignore_errors {
errors.error_spanned_by(other, format!("expected #[{}(...)]", attr_type))
}
Err(())
}
Err(err) => {
errors.error_spanned_by(attr, err);
if !ignore_errors {
errors.error_spanned_by(attr, err)
}
Err(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion schemars_derive/src/attr/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl ValidationAttrs {

for meta_item in attrs
.iter()
.flat_map(|attr| get_meta_items(attr, attr_type, errors))
.flat_map(|attr| get_meta_items(attr, attr_type, errors, ignore_errors))
.flatten()
{
match &meta_item {
Expand Down

0 comments on commit de7314f

Please sign in to comment.