Skip to content

Commit

Permalink
fix: error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 6, 2024
1 parent 108870e commit 582a49f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 11 additions & 2 deletions serde_valid_derive/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::validate::{MetaListValidation, MetaNameValueValidation, MetaPathValidation};
use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
Expand Down Expand Up @@ -241,7 +242,15 @@ impl Error {
}

pub fn validate_type_required_error(attribute: &syn::Attribute) -> Self {
Self::new(attribute.span(), "#[validate(???)] needs validation type.")
let filterd_candidates: Vec<&str> = (MetaPathValidation::iter().map(|x| x.name()))
.chain(MetaNameValueValidation::iter().map(|x| x.name()))
.chain(MetaListValidation::iter().map(|x| x.name()))
.collect::<Vec<_>>();

Self::new(
attribute.meta.span(),
format!("#[validate(???)] needs validation type. Is it one of the following?\n{filterd_candidates:#?}"),
)
}

pub fn validate_unknown_type(path: &syn::Path, unknown: &str, candidates: &[&str]) -> Self {
Expand All @@ -250,7 +259,7 @@ impl Error {

Self::new(
path.span(),
format!("Unknown: `{unknown}`. Is it one of the following?\n{filterd_candidates:#?}"),
format!("Unknown: `{unknown}` validation type. Is it one of the following?\n{filterd_candidates:#?}"),
)
}

Expand Down
7 changes: 4 additions & 3 deletions serde_valid_derive/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ mod numeric;
mod object;
mod string;

pub use common::{
MetaListMessage, MetaListValidation, MetaNameValueValidation, MetaPathValidation,

Check failure on line 11 in serde_valid_derive/src/validate.rs

View workflow job for this annotation

GitHub Actions / Lints

unused import: `MetaListMessage`

Check warning on line 11 in serde_valid_derive/src/validate.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `MetaListMessage`

Check warning on line 11 in serde_valid_derive/src/validate.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `MetaListMessage`

Check failure on line 11 in serde_valid_derive/src/validate.rs

View workflow job for this annotation

GitHub Actions / Lints

unused import: `MetaListMessage`
};

pub use field::{FieldValidators, Validator};
pub use meta::extract_meta_validator;

#[cfg(feature = "fluent")]
pub use common::MetaListMessage;

0 comments on commit 582a49f

Please sign in to comment.