diff --git a/serde_valid/src/lib.rs b/serde_valid/src/lib.rs index 5b2c4d9..e3af35d 100644 --- a/serde_valid/src/lib.rs +++ b/serde_valid/src/lib.rs @@ -541,18 +541,18 @@ pub mod error; mod features; pub mod json; mod traits; +pub mod utils; pub mod validation; -use std::collections::HashMap; - -use indexmap::IndexMap; - pub use error::{ EnumerateError, Error, ExclusiveMaximumError, ExclusiveMinimumError, MaxItemsError, MaxLengthError, MaxPropertiesError, MaximumError, MinItemsError, MinLengthError, MinPropertiesError, MinimumError, MultipleOfError, PatternError, UniqueItemsError, }; -pub mod utils; +#[allow(unused_imports)] +pub use features::*; +use indexmap::IndexMap; +use std::collections::HashMap; pub use validation::{ ValidateEnumerate, ValidateExclusiveMaximum, ValidateExclusiveMinimum, ValidateMaxItems, ValidateMaxLength, ValidateMaxProperties, ValidateMaximum, ValidateMinItems, ValidateMinLength, @@ -560,9 +560,6 @@ pub use validation::{ ValidateUniqueItems, }; -#[allow(unused_imports)] -pub use features::*; - pub mod export { #[cfg(feature = "fluent")] pub use fluent; diff --git a/serde_valid/tests/deprecated_custom_test.rs b/serde_valid/tests/deprecated_custom_test.rs deleted file mode 100644 index a0127c8..0000000 --- a/serde_valid/tests/deprecated_custom_test.rs +++ /dev/null @@ -1,297 +0,0 @@ -#[allow(deprecated)] -mod test { - use serde_json::json; - use serde_valid::Validate; - - mod parenthesized { - pub fn meta_path_validation(_val: &[i32]) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - } - - #[test] - fn custom_validation_is_ok() { - fn user_validation(_val: &i32) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - #[derive(Validate)] - struct TestStruct { - #[validate(custom(user_validation))] - val: i32, - } - - let s = TestStruct { val: 1 }; - assert!(s.validate().is_ok()); - } - - #[test] - fn custom_parenthesized_path_validation_is_ok() { - #[derive(Validate)] - struct TestStruct { - #[validate(custom(parenthesized::meta_path_validation))] - val: Vec, - } - - let s = TestStruct { - val: vec![1, 2, 3, 4], - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn custom_clouser_validation_is_ok() { - fn user_validation( - _val: &[i32], - _maximum: i32, - ) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - #[derive(Validate)] - struct TestStruct { - #[validate(custom(|x| user_validation(x, 10)))] - val: Vec, - } - - let s = TestStruct { - val: vec![1, 2, 3, 4], - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn custom_clouser_validation_is_err() { - fn user_validation( - val: &[i32], - maximum: i32, - ) -> Result<(), serde_valid::validation::Error> { - if val.iter().all(|v| v <= &maximum) { - Ok(()) - } else { - Err(serde_valid::validation::Error::Custom( - "this is custom message.".to_string(), - )) - } - } - - #[derive(Validate)] - struct TestStruct { - #[validate(custom(|x| user_validation(x, 10)))] - val: Vec, - } - - let s = TestStruct { - val: vec![1, 2, 3, 11], - }; - assert!(s.validate().is_err()); - } - - #[test] - fn custom_validation_error() { - fn user_validation(_val: &[i32]) -> Result<(), serde_valid::validation::Error> { - Err(serde_valid::validation::Error::Custom( - "this is custom message.".to_string(), - )) - } - - #[derive(Validate)] - struct TestStruct { - #[validate(custom(user_validation))] - val: Vec, - } - - let s = TestStruct { - val: vec![1, 2, 3, 4], - }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn named_struct_custom_is_ok() { - fn sample_struct_validation( - _val: &TestStruct, - ) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - #[derive(Validate)] - #[validate(custom(sample_struct_validation))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert!(s.validate().is_ok()); - } - - #[test] - fn named_struct_custom_closure_is_ok() { - fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert!(s.validate().is_ok()); - } - - #[test] - fn unnamed_struct_custom_is_ok() { - fn sample_struct_validation( - _val: &TestStruct, - ) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - #[derive(Validate)] - #[validate(custom(sample_struct_validation))] - struct TestStruct(i32); - - let s = TestStruct(5); - assert_eq!(s.0, 5); - assert!(s.validate().is_ok()); - } - - #[test] - fn unnamed_struct_custom_closure_is_ok() { - fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.0)))] - struct TestStruct(i32); - - let s = TestStruct(5); - assert_eq!(s.0, 5); - assert!(s.validate().is_ok()); - } - - #[test] - fn unnamed_struct_custom_closure_is_err() { - fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { - Err(serde_valid::validation::Error::Custom( - "Struct Validation Error.".to_owned(), - )) - } - - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.0)))] - struct TestStruct(i32); - - let s = TestStruct(5); - assert_eq!(s.0, 5); - assert!(s.validate().is_err()); - } - - #[test] - fn named_struct_custom_vec_errors_is_ok() { - fn validation(_val: &TestStruct) -> Result<(), Vec> { - Ok(()) - } - - #[derive(Validate)] - #[validate(custom(validation))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert!(s.validate().is_ok()); - } - - #[test] - fn named_struct_custom_vec_errors_is_err() { - fn validation(_val: &TestStruct) -> Result<(), Vec> { - Err(vec![ - serde_valid::validation::Error::Custom("Error 1".to_owned()), - serde_valid::validation::Error::Custom("Error 2".to_owned()), - ]) - } - - #[derive(Validate)] - #[validate(custom(validation))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - - assert_eq!(s.val, 5); - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": ["Error 1", "Error 2"], - "properties": {} - }) - .to_string() - ); - } - - #[test] - fn named_struct_custom_closure_vec_errors_is_ok() { - fn sample_struct_validation(_val: i32) -> Result<(), Vec> { - Ok(()) - } - - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert!(s.validate().is_ok()); - } - - #[test] - fn named_struct_custom_closure_vec_errors_is_err() { - fn sample_struct_validation(_val: i32) -> Result<(), Vec> { - Err(vec![ - serde_valid::validation::Error::Custom("Error 1".to_owned()), - serde_valid::validation::Error::Custom("Error 2".to_owned()), - ]) - } - - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": ["Error 1", "Error 2"], - "properties": {} - }) - .to_string() - ); - } -} diff --git a/serde_valid/tests/deprecated_enumerate_test.rs b/serde_valid/tests/deprecated_enumerate_test.rs deleted file mode 100644 index 792b6c9..0000000 --- a/serde_valid/tests/deprecated_enumerate_test.rs +++ /dev/null @@ -1,256 +0,0 @@ -#[allow(deprecated)] -mod test { - use serde_json::json; - use serde_valid::{Validate, ValidateEnumerate}; - - #[test] - fn enumerate_integer_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3))] - val: i32, - } - - let s = TestStruct { val: 1 }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_float_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(0.3, 1.2, 1.5))] - val: f32, - } - - let s = TestStruct { val: 0.3 }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_str_type() { - #[derive(Validate)] - struct TestStruct<'a> { - #[validate(enumerate("a", "b"))] - val: &'a str, - } - - let s = TestStruct { val: "a" }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_string_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate("a", "b"))] - val: String, - } - - let s = TestStruct { - val: "a".to_string(), - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_vec_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3, 4, 5))] - val: Vec, - } - - let s = TestStruct { val: vec![3, 4] }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_vec_str_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate("1", "2", "3", "4", "5"))] - val: Vec<&'static str>, - } - - let s = TestStruct { - val: vec!["3", "4"], - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_vec_string_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate("1", "2", "3", "4", "5"))] - val: Vec, - } - - let s = TestStruct { - val: vec!["3".to_owned(), "4".to_owned()], - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_option_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3))] - val: Option, - } - - let s = TestStruct { val: Some(3) }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_vec_option_type() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(3))] - val: Vec>, - } - - let s = TestStruct { val: vec![Some(3)] }; - assert!(s.validate().is_ok()); - } - - #[test] - fn enumerate_is_err() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(0.3, 1.2, 1.5))] - val: f32, - } - - let s = TestStruct { val: 0.1 }; - assert!(s.validate().is_err()); - } - - #[test] - fn enumerate_err_message() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3))] - val: i32, - } - - let s = TestStruct { val: 4 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "The value must be in [1, 2, 3]." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn enumerate_custom_err_message_fn() { - fn error_message(_params: &serde_valid::error::EnumerateError) -> String { - "this is custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3), message_fn = error_message)] - val: i32, - } - - let s = TestStruct { val: 4 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn enumerate_custom_err_message() { - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3), message = "this is custom message.")] - val: i32, - } - - let s = TestStruct { val: 4 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn enumerate_numeric_trait() { - struct MyType(i32); - - impl PartialEq for MyType { - fn eq(&self, other: &i32) -> bool { - self.0.eq(other) - } - } - - impl ValidateEnumerate for MyType { - fn validate_enumerate( - &self, - enumerate: &[i32], - ) -> Result<(), serde_valid::EnumerateError> { - self.0.validate_enumerate(enumerate) - } - } - - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3), message = "this is custom message.")] - val: MyType, - } - - let s = TestStruct { val: MyType(4) }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } -} diff --git a/serde_valid/tests/deprecated_message_fn_test.rs b/serde_valid/tests/deprecated_message_fn_test.rs deleted file mode 100644 index bd78180..0000000 --- a/serde_valid/tests/deprecated_message_fn_test.rs +++ /dev/null @@ -1,315 +0,0 @@ -#[allow(deprecated)] -mod test { - - use serde_json::json; - use serde_valid::Validate; - - use serde::Deserialize; - - #[test] - fn deprecated_enumerate_custom_err_message_fn() { - fn error_message(_params: &serde_valid::error::EnumerateError) -> String { - "this is custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate(1, 2, 3), message_fn(error_message))] - val: i32, - } - - let s = TestStruct { val: 4 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn enumerate_custom_err_message_fn() { - fn error_message(_params: &serde_valid::error::EnumerateError) -> String { - "this is custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(enumerate = [1, 2, 3], message_fn(error_message))] - val: i32, - } - - let s = TestStruct { val: 4 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn items_custom_err_message_fn() { - #[inline] - fn min_error_message(_params: &serde_valid::MinItemsError) -> String { - "this is min custom message.".to_string() - } - #[inline] - fn max_error_message(_params: &serde_valid::MaxItemsError) -> String { - "this is max custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(min_items = 4, message_fn(min_error_message))] - #[validate(max_items = 2, message_fn(max_error_message))] - val: Vec, - } - - let s = TestStruct { val: vec![1, 2, 3] }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is min custom message.", - "this is max custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn length_custom_err_message_fn() { - fn custom_min_error_message(_params: &serde_valid::MinLengthError) -> String { - "this is min custom message.".to_string() - } - - fn custom_max_error_message(_params: &serde_valid::MaxLengthError) -> String { - "this is max custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(min_length = 5, message_fn(custom_min_error_message))] - #[validate(max_length = 3, message_fn(custom_max_error_message))] - val: String, - } - - let s = TestStruct { - val: String::from("test"), - }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is min custom message.", - "this is max custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn multiple_of_custom_err_message_fn() { - fn error_message(_params: &serde_valid::MultipleOfError) -> String { - "this is custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(multiple_of = 5, message_fn(error_message))] - val: i32, - } - - let s = TestStruct { val: 14 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn pattern_custom_err_message_fn() { - fn error_message(_params: &serde_valid::PatternError) -> String { - "this is custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(pattern = r"^\d{4}-\d{2}-\d{2}$", message_fn = error_message)] - val: String, - } - - let s = TestStruct { - val: String::from("2020/09/10"), - }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn properties_custom_err_message_fn() { - fn min_custom_error_message(_params: &serde_valid::MinPropertiesError) -> String { - "this is min custom message.".to_string() - } - - fn max_custom_error_message(_params: &serde_valid::MaxPropertiesError) -> String { - "this is max custom message.".to_string() - } - - #[derive(Deserialize, Validate)] - struct TestStruct { - #[validate(min_properties = 3, message_fn(min_custom_error_message))] - #[validate(max_properties = 1, message_fn(max_custom_error_message))] - val: serde_json::Map, - } - - let s: TestStruct = serde_json::from_value(json!({ - "val": { - "key1": "value1", - "key2": "value2", - } - })) - .unwrap(); - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is min custom message.", - "this is max custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn range_custom_err_message_fn() { - fn custom_min_error_message(_params: &serde_valid::MinimumError) -> String { - "this is min custom message.".to_string() - } - - fn custom_max_error_message(_params: &serde_valid::MaximumError) -> String { - "this is max custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(minimum = 5, message_fn(custom_min_error_message))] - #[validate(maximum = 3, message_fn(custom_max_error_message))] - val: i32, - } - - let s = TestStruct { val: 4 }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is min custom message.", - "this is max custom message." - ] - } - } - }) - .to_string() - ); - } - - #[test] - fn unique_items_custom_err_message_fn() { - fn error_message(_params: &serde_valid::UniqueItemsError) -> String { - "this is custom message.".to_string() - } - - #[derive(Validate)] - struct TestStruct { - #[validate(unique_items, message_fn(error_message))] - val: Vec, - } - - let s = TestStruct { - val: vec![1, 2, 3, 2], - }; - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": [], - "properties": { - "val": { - "errors": [ - "this is custom message." - ] - } - } - }) - .to_string() - ); - } -} diff --git a/serde_valid_derive/src/attribute.rs b/serde_valid_derive/src/attribute.rs index f9067bd..c48fbe1 100644 --- a/serde_valid_derive/src/attribute.rs +++ b/serde_valid_derive/src/attribute.rs @@ -100,7 +100,6 @@ enum_str! { enum_str! { pub enum MetaListFieldValidation { - Enumerate = "enumerate", Custom = "custom", } } @@ -132,14 +131,12 @@ enum_str! { #[cfg(not(feature = "fluent"))] enum_str! { pub enum MetaListCustomMessage { - MessageFn = "message_fn", } } #[cfg(feature = "fluent")] enum_str! { pub enum MetaListCustomMessage { - MessageFn = "message_fn", I18n = "i18n", Fluent = "fluent", } diff --git a/serde_valid_derive/src/attribute/common/message_format.rs b/serde_valid_derive/src/attribute/common/message_format.rs index f59cb6f..6a0ddd9 100644 --- a/serde_valid_derive/src/attribute/common/message_format.rs +++ b/serde_valid_derive/src/attribute/common/message_format.rs @@ -1,10 +1,11 @@ use crate::attribute::{MetaListCustomMessage, MetaNameValueCustomMessage, MetaPathCustomMessage}; -use crate::types::{CommaSeparatedNestedMetas, NestedMeta, SingleIdentPath}; -use crate::warning::{Warning, WithWarnings}; +use crate::types::SingleIdentPath; +#[cfg(feature = "fluent")] +use crate::types::{CommaSeparatedNestedMetas, NestedMeta}; +use crate::warning::WithWarnings; use proc_macro2::TokenStream; use quote::quote; use std::str::FromStr; -use syn::spanned::Spanned; use super::lit::get_str; pub type MessageFormat = TokenStream; @@ -61,31 +62,25 @@ pub fn extract_custom_message_format( } } +#[allow(unused_variables)] fn extract_custom_message_format_from_meta_list( custom_message_type: &MetaListCustomMessage, meta_list: &syn::MetaList, ) -> Result, crate::Errors> { - let path = &meta_list.path; - let path_ident = SingleIdentPath::new(path).ident(); - let message_fn_define = meta_list - .parse_args_with(CommaSeparatedNestedMetas::parse_terminated) - .map_err(|error| vec![crate::Error::custom_message_parse_error(path_ident, &error)])?; - match custom_message_type { - MetaListCustomMessage::MessageFn => get_message_fn_from_meta_list(path, &message_fn_define) - .map(|message_fn| { - WithWarnings::new_with_warnings( - message_fn, - vec![Warning::new_message_fn_list_deprecated( - path_ident, - path.span(), - )], - ) - }), #[cfg(feature = "fluent")] message_type @ (MetaListCustomMessage::I18n | MetaListCustomMessage::Fluent) => { + let path = &meta_list.path; + let path_ident = SingleIdentPath::new(path).ident(); + let message_fn_define = meta_list + .parse_args_with(CommaSeparatedNestedMetas::parse_terminated) + .map_err(|error| { + vec![crate::Error::custom_message_parse_error(path_ident, &error)] + })?; get_fluent_message_from_meta(message_type, path, &message_fn_define) } + #[allow(unreachable_patterns)] + _ => unreachable!("This should not be called"), } } @@ -104,31 +99,6 @@ fn extract_custom_message_format_from_name_value( } } -fn get_message_fn_from_meta_list( - path: &syn::Path, - fn_define: &CommaSeparatedNestedMetas, -) -> Result { - let fn_name = match fn_define.len() { - 0 => Err(vec![crate::Error::message_fn_meta_list_need_item(path)]), - 1 => match &fn_define[0] { - NestedMeta::Meta(syn::Meta::Path(fn_name)) => Some(quote!(#fn_name)), - _ => None, - } - .ok_or_else(|| { - vec![crate::Error::message_fn_meta_list_allow_name_path( - &fn_define[0], - )] - }), - _ => Err(fn_define - .iter() - .skip(1) - .map(crate::Error::message_fn_meta_list_tail_error) - .collect()), - }?; - - Ok(quote!(::serde_valid::validation::error::Format::MessageFn(#fn_name))) -} - fn get_message_fn_from_meta_name_value( meta_name_value: &syn::MetaNameValue, ) -> Result, crate::Errors> { diff --git a/serde_valid_derive/src/attribute/field_validate/generic.rs b/serde_valid_derive/src/attribute/field_validate/generic.rs index fbfae0a..983eda9 100644 --- a/serde_valid_derive/src/attribute/field_validate/generic.rs +++ b/serde_valid_derive/src/attribute/field_validate/generic.rs @@ -6,8 +6,5 @@ pub use custom::{ extract_generic_custom_validator_from_meta_list, extract_generic_custom_validator_from_meta_name_value, }; -pub use enumerate::{ - extract_generic_enumerate_validator_from_list, - extract_generic_enumerate_validator_from_name_value, -}; +pub use enumerate::extract_generic_enumerate_validator_from_name_value; pub use validate::extract_generic_validate_validator; diff --git a/serde_valid_derive/src/attribute/field_validate/generic/enumerate.rs b/serde_valid_derive/src/attribute/field_validate/generic/enumerate.rs index ad3e063..fdbee13 100644 --- a/serde_valid_derive/src/attribute/field_validate/generic/enumerate.rs +++ b/serde_valid_derive/src/attribute/field_validate/generic/enumerate.rs @@ -6,16 +6,6 @@ use quote::quote; type Lits<'a> = syn::punctuated::Punctuated; -pub fn extract_generic_enumerate_validator_from_list( - field: &impl Field, - item_list: &syn::MetaList, - message_format: MessageFormat, - rename_map: &RenameMap, -) -> Result { - let lits = get_enumerate_from_list(item_list)?; - inner_extract_generic_enumerate_validator(field, &lits, message_format, rename_map) -} - pub fn extract_generic_enumerate_validator_from_name_value( field: &impl Field, name_value: &syn::MetaNameValue, @@ -54,37 +44,6 @@ fn inner_extract_generic_enumerate_validator( )) } -fn get_enumerate_from_list(meta_list: &syn::MetaList) -> Result { - let mut errors = vec![]; - let mut enumerate = Lits::new(); - let nested = meta_list - .parse_args_with(crate::types::CommaSeparatedNestedMetas::parse_terminated) - .map_err(|error| { - vec![crate::Error::validate_enumerate_parse_error( - meta_list, &error, - )] - })?; - - if nested.is_empty() { - errors.push(crate::Error::validate_enumerate_need_item(&meta_list.path)); - } - for item in nested { - match &item { - crate::types::NestedMeta::Lit(lit) => enumerate.push(lit.clone()), - crate::types::NestedMeta::Meta(meta) => errors.push(crate::Error::literal_only(meta)), - crate::types::NestedMeta::Closure(closure) => { - errors.push(crate::Error::closure_not_supported(closure)) - } - } - } - - if errors.is_empty() { - Ok(enumerate) - } else { - Err(errors) - } -} - fn get_enumerate_from_name_value(name_value: &syn::MetaNameValue) -> Result { if let syn::Expr::Array(array) = &name_value.value { let mut enumerate = Lits::new(); diff --git a/serde_valid_derive/src/attribute/field_validate/meta/meta_list.rs b/serde_valid_derive/src/attribute/field_validate/meta/meta_list.rs index 746d0d5..4b54be0 100644 --- a/serde_valid_derive/src/attribute/field_validate/meta/meta_list.rs +++ b/serde_valid_derive/src/attribute/field_validate/meta/meta_list.rs @@ -1,13 +1,8 @@ -use syn::spanned::Spanned; - use crate::attribute::common::message_format::MessageFormat; -use crate::attribute::field_validate::generic::{ - extract_generic_custom_validator_from_meta_list, extract_generic_enumerate_validator_from_list, -}; +use crate::attribute::field_validate::generic::extract_generic_custom_validator_from_meta_list; use crate::attribute::{MetaListFieldValidation, Validator}; use crate::serde::rename::RenameMap; use crate::types::Field; -use crate::warning::Warning; use crate::warning::WithWarnings; pub fn extract_field_validator_from_meta_list( @@ -18,19 +13,6 @@ pub fn extract_field_validator_from_meta_list( rename_map: &RenameMap, ) -> Result, crate::Errors> { match validation_type { - MetaListFieldValidation::Enumerate => extract_generic_enumerate_validator_from_list( - field, - validation, - message_format, - rename_map, - ) - .map(|data| WithWarnings { - data, - warnings: vec![Warning::new_enumerate_meta_list_deprecated( - field.ident(), - validation.span(), - )], - }), MetaListFieldValidation::Custom => extract_generic_custom_validator_from_meta_list( field, validation, diff --git a/serde_valid_derive/src/error.rs b/serde_valid_derive/src/error.rs index 65cdcd0..0c5042e 100644 --- a/serde_valid_derive/src/error.rs +++ b/serde_valid_derive/src/error.rs @@ -5,7 +5,7 @@ use crate::attribute::{ }; use itertools::Itertools; use proc_macro2::TokenStream; -use quote::{quote, ToTokens}; +use quote::quote; use syn::spanned::Spanned; pub fn object_errors_tokens() -> TokenStream { @@ -361,17 +361,6 @@ impl Error { ) } - pub fn validate_enumerate_parse_error(meta_list: &syn::MetaList, error: &syn::Error) -> Self { - Self::new( - meta_list.span(), - format!("#[validate(enumerate(???))] parse error: {error}"), - ) - } - - pub fn validate_enumerate_need_item(path: &syn::Path) -> Self { - Self::new(path.span(), "#[validate(enumerate(???))] needs items.") - } - pub fn validate_enumerate_need_array(path: impl Spanned) -> Self { Self::new( path.span(), @@ -407,27 +396,6 @@ impl Error { ) } - pub fn message_fn_meta_list_need_item(path: &syn::Path) -> Self { - Self::new( - path.span(), - "#[validate(..., message_fn(???))] needs function.", - ) - } - - pub fn message_fn_meta_list_allow_name_path(nested_meta: &crate::types::NestedMeta) -> Self { - Self::new( - nested_meta.span(), - "#[validate(..., message_fn(???))] allows only function name path.", - ) - } - - pub fn message_fn_meta_list_tail_error(nested_meta: &crate::types::NestedMeta) -> Self { - Self::new( - nested_meta.span(), - "#[validate(..., message_fn(???))] allows only 1 item.", - ) - } - pub fn message_fn_meta_name_value_needs_function_or_closure( meta_name_value: &syn::MetaNameValue, ) -> Self { @@ -526,13 +494,6 @@ impl Error { Self::new(lit.span(), "Allow str literal only.") } - pub fn closure_not_supported(closure: &syn::ExprClosure) -> Self { - Self::new( - closure.or1_token.span(), - format!("Closure not supported. {}", closure.to_token_stream()), - ) - } - pub fn too_many_list_items(nested_meta: &syn::Meta) -> Self { Self::new(nested_meta.span(), "Too many list items.") } diff --git a/serde_valid_derive/src/warning.rs b/serde_valid_derive/src/warning.rs index 4295ec9..85a7527 100644 --- a/serde_valid_derive/src/warning.rs +++ b/serde_valid_derive/src/warning.rs @@ -101,15 +101,6 @@ impl Warning { } } - pub fn new_enumerate_meta_list_deprecated(ident: &syn::Ident, span: Span) -> Self { - Self::Deprecated { - ident: ident.clone(), - note: "#[validate(enumerate(...))] is deprecated, please use #[validate(enumerate = [...])] instead." - .to_string(), - span, - } - } - pub fn new_custom_meta_list_deprecated(ident: &syn::Ident, span: Span) -> Self { Self::Deprecated { ident: ident.clone(), @@ -119,15 +110,6 @@ impl Warning { } } - pub fn new_message_fn_list_deprecated(ident: &syn::Ident, span: Span) -> Self { - Self::Deprecated { - ident: ident.clone(), - note: "#[validate(message_fn(...))] is deprecated, please use #[validate(message_fn = ...)] instead." - .to_string(), - span, - } - } - pub fn add_index(&self, index: usize) -> Self { match self { Self::Deprecated { ident, note, span } => Self::Deprecated {