From bb983b6dc7da99799c32de1fad24b447ba69eabc Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Sat, 22 Jun 2024 00:30:31 +0900 Subject: [PATCH] feat: add warnings. --- serde_valid/tests/deprecated_custom_test.rs | 465 +++++++++--------- .../field_validate/meta/meta_list.rs | 2 +- .../src/attribute/struct_validate/meta.rs | 6 +- .../struct_validate/meta/meta_list.rs | 27 +- .../struct_validate/meta/meta_name_value.rs | 14 +- .../struct_validate/meta/meta_path.rs | 7 +- .../src/attribute/variant_validate.rs | 15 +- .../src/attribute/variant_validate/meta.rs | 9 +- .../variant_validate/meta/meta_list.rs | 27 +- .../variant_validate/meta/meta_name_value.rs | 14 +- .../variant_validate/meta/meta_path.rs | 7 +- serde_valid_derive/src/derive/enum_derive.rs | 14 +- serde_valid_derive/src/warning.rs | 15 +- 13 files changed, 351 insertions(+), 271 deletions(-) diff --git a/serde_valid/tests/deprecated_custom_test.rs b/serde_valid/tests/deprecated_custom_test.rs index c72b50c..a0127c8 100644 --- a/serde_valid/tests/deprecated_custom_test.rs +++ b/serde_valid/tests/deprecated_custom_test.rs @@ -1,284 +1,297 @@ -use serde_json::json; -use serde_valid::Validate; +#[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(()) + 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(()) - } + #[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, + } - #[derive(Validate)] - struct TestStruct { - #[validate(custom(user_validation))] - val: i32, + let s = TestStruct { val: 1 }; + assert!(s.validate().is_ok()); } - 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, + } -#[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()); } - 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(()) + } -#[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, + } - #[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()); } - 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(), + )) + } + } -#[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 { + #[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(|x| user_validation(x, 10)))] - val: Vec, - } - let s = TestStruct { - val: vec![1, 2, 3, 11], - }; - assert!(s.validate().is_err()); -} + #[derive(Validate)] + struct TestStruct { + #[validate(custom(user_validation))] + val: Vec, + } -#[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(), - )) + 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() + ); } - #[derive(Validate)] - struct TestStruct { - #[validate(custom(user_validation))] - val: Vec, - } + #[test] + fn named_struct_custom_is_ok() { + fn sample_struct_validation( + _val: &TestStruct, + ) -> Result<(), serde_valid::validation::Error> { + Ok(()) + } - 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() - ); -} + #[derive(Validate)] + #[validate(custom(sample_struct_validation))] + struct TestStruct { + val: i32, + } -#[test] -fn named_struct_custom_is_ok() { - fn sample_struct_validation(_val: &TestStruct) -> Result<(), serde_valid::validation::Error> { - Ok(()) + let s = TestStruct { val: 5 }; + assert_eq!(s.val, 5); + assert!(s.validate().is_ok()); } - #[derive(Validate)] - #[validate(custom(sample_struct_validation))] - struct TestStruct { - val: i32, - } + #[test] + fn named_struct_custom_closure_is_ok() { + fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { + Ok(()) + } - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert!(s.validate().is_ok()); -} + #[derive(Validate)] + #[validate(custom(|s| sample_struct_validation(s.val)))] + struct TestStruct { + val: i32, + } -#[test] -fn named_struct_custom_closure_is_ok() { - fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { - Ok(()) + let s = TestStruct { val: 5 }; + assert_eq!(s.val, 5); + assert!(s.validate().is_ok()); } - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] - struct TestStruct { - val: i32, - } + #[test] + fn unnamed_struct_custom_is_ok() { + fn sample_struct_validation( + _val: &TestStruct, + ) -> Result<(), serde_valid::validation::Error> { + Ok(()) + } - let s = TestStruct { val: 5 }; - assert_eq!(s.val, 5); - assert!(s.validate().is_ok()); -} + #[derive(Validate)] + #[validate(custom(sample_struct_validation))] + struct TestStruct(i32); -#[test] -fn unnamed_struct_custom_is_ok() { - fn sample_struct_validation(_val: &TestStruct) -> Result<(), serde_valid::validation::Error> { - Ok(()) + let s = TestStruct(5); + assert_eq!(s.0, 5); + assert!(s.validate().is_ok()); } - #[derive(Validate)] - #[validate(custom(sample_struct_validation))] - struct TestStruct(i32); + #[test] + fn unnamed_struct_custom_closure_is_ok() { + fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { + Ok(()) + } - let s = TestStruct(5); - assert_eq!(s.0, 5); - assert!(s.validate().is_ok()); -} + #[derive(Validate)] + #[validate(custom(|s| sample_struct_validation(s.0)))] + struct TestStruct(i32); -#[test] -fn unnamed_struct_custom_closure_is_ok() { - fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { - Ok(()) + let s = TestStruct(5); + assert_eq!(s.0, 5); + assert!(s.validate().is_ok()); } - #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.0)))] - struct TestStruct(i32); + #[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(), + )) + } - let s = TestStruct(5); - assert_eq!(s.0, 5); - assert!(s.validate().is_ok()); -} + #[derive(Validate)] + #[validate(custom(|s| sample_struct_validation(s.0)))] + struct TestStruct(i32); -#[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(), - )) + let s = TestStruct(5); + assert_eq!(s.0, 5); + assert!(s.validate().is_err()); } - #[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(()) + } -#[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, + } - #[derive(Validate)] - #[validate(custom(validation))] - struct TestStruct { - val: i32, + let s = TestStruct { val: 5 }; + assert_eq!(s.val, 5); + assert!(s.validate().is_ok()); } - 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()), + ]) + } -#[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, + } - #[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() + ); } - 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(()) + } -#[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, + } - #[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()); } - 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()), + ]) + } -#[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, + } - #[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() + ); } - - 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_derive/src/attribute/field_validate/meta/meta_list.rs b/serde_valid_derive/src/attribute/field_validate/meta/meta_list.rs index 860c6a7..746d0d5 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 @@ -26,7 +26,7 @@ pub fn extract_field_validator_from_meta_list( ) .map(|data| WithWarnings { data, - warnings: vec![Warning::new_enumerate_path_deprecated( + warnings: vec![Warning::new_enumerate_meta_list_deprecated( field.ident(), validation.span(), )], diff --git a/serde_valid_derive/src/attribute/struct_validate/meta.rs b/serde_valid_derive/src/attribute/struct_validate/meta.rs index 7636781..b4eb9e5 100644 --- a/serde_valid_derive/src/attribute/struct_validate/meta.rs +++ b/serde_valid_derive/src/attribute/struct_validate/meta.rs @@ -25,9 +25,7 @@ pub fn extract_struct_validator( ) -> Result, crate::Errors> { match &attribute.meta { syn::Meta::Path(_) => Ok(WithWarnings::new(quote!())), - syn::Meta::List(list) => { - inner_extract_struct_validator(attribute, list).map(WithWarnings::new) - } + syn::Meta::List(list) => inner_extract_struct_validator(attribute, list), syn::Meta::NameValue(name_value) => { Err(vec![crate::Error::validate_meta_name_value_not_supported( name_value, @@ -39,7 +37,7 @@ pub fn extract_struct_validator( fn inner_extract_struct_validator( attribute: &syn::Attribute, meta_list: &syn::MetaList, -) -> Result { +) -> Result, crate::Errors> { let mut errors = vec![]; let nested = meta_list .parse_args_with(crate::types::CommaSeparatedMetas::parse_terminated) diff --git a/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs b/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs index 71c36d4..883debc 100644 --- a/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs +++ b/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs @@ -1,17 +1,32 @@ -use crate::attribute::{ - common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, - MetaListStructValidation, Validator, +use syn::spanned::Spanned; + +use crate::{ + attribute::{ + common::message_format::MessageFormat, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, + MetaListStructValidation, Validator, + }, + warning::{Warning, WithWarnings}, }; pub fn extract_struct_validator_from_meta_list( validation_type: MetaListStructValidation, validation: &syn::MetaList, message_format: MessageFormat, -) -> Result { +) -> Result, crate::Errors> { match validation_type { MetaListStructValidation::Custom => { - extract_generic_struct_custom_validator_from_meta_list(validation, message_format) + extract_generic_struct_custom_validator_from_meta_list(validation, message_format).map( + |validator| { + WithWarnings::new_with_warnings( + validator, + vec![Warning::new_custom_meta_list_deprecated( + validation.path.get_ident().unwrap(), + validation.span(), + )], + ) + }, + ) } } } diff --git a/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs b/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs index d70cd3c..f216b6c 100644 --- a/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs +++ b/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs @@ -1,7 +1,10 @@ -use crate::attribute::{ - common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value, - MetaNameValueStructValidation, Validator, +use crate::{ + attribute::{ + common::message_format::MessageFormat, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value, + MetaNameValueStructValidation, Validator, + }, + warning::WithWarnings, }; #[inline] @@ -9,10 +12,11 @@ pub fn extract_struct_validator_from_meta_name_value( validation_type: MetaNameValueStructValidation, validation: &syn::MetaNameValue, message_format: MessageFormat, -) -> Result { +) -> Result, crate::Errors> { match validation_type { MetaNameValueStructValidation::Custom => { extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format) } } + .map(WithWarnings::new) } diff --git a/serde_valid_derive/src/attribute/struct_validate/meta/meta_path.rs b/serde_valid_derive/src/attribute/struct_validate/meta/meta_path.rs index 32ee315..600fe22 100644 --- a/serde_valid_derive/src/attribute/struct_validate/meta/meta_path.rs +++ b/serde_valid_derive/src/attribute/struct_validate/meta/meta_path.rs @@ -1,5 +1,6 @@ -use crate::attribute::{ - common::message_format::MessageFormat, MetaPathStructValidation, Validator, +use crate::{ + attribute::{common::message_format::MessageFormat, MetaPathStructValidation, Validator}, + warning::WithWarnings, }; #[inline] @@ -7,6 +8,6 @@ pub fn extract_struct_validator_from_meta_path( validation_type: MetaPathStructValidation, _validation: &syn::Path, _message_format: MessageFormat, -) -> Result { +) -> Result, crate::Errors> { match validation_type {} } diff --git a/serde_valid_derive/src/attribute/variant_validate.rs b/serde_valid_derive/src/attribute/variant_validate.rs index f623c7d..29ee9b4 100644 --- a/serde_valid_derive/src/attribute/variant_validate.rs +++ b/serde_valid_derive/src/attribute/variant_validate.rs @@ -1,20 +1,24 @@ mod meta; -use crate::attribute::Validator; +use crate::{attribute::Validator, warning::WithWarnings}; use self::meta::extract_variant_validator; pub fn collect_variant_custom_from_variant( attributes: &[syn::Attribute], -) -> Result { +) -> Result, crate::Errors> { let mut errors = vec![]; + let mut warnings = vec![]; let validations = attributes .iter() .filter_map(|attribute| { if attribute.path().is_ident("validate") { match extract_variant_validator(attribute) { - Ok(validator) => Some(validator), + Ok(validator) => { + warnings.extend(validator.warnings); + Some(validator.data) + } Err(validator_error) => { errors.extend(validator_error); None @@ -27,7 +31,10 @@ pub fn collect_variant_custom_from_variant( .collect::>(); if errors.is_empty() { - Ok(Validator::from_iter(validations)) + Ok(WithWarnings::new_with_warnings( + Validator::from_iter(validations), + warnings, + )) } else { Err(errors) } diff --git a/serde_valid_derive/src/attribute/variant_validate/meta.rs b/serde_valid_derive/src/attribute/variant_validate/meta.rs index 972b326..fc10b99 100644 --- a/serde_valid_derive/src/attribute/variant_validate/meta.rs +++ b/serde_valid_derive/src/attribute/variant_validate/meta.rs @@ -9,6 +9,7 @@ use crate::{ Validator, }, types::SingleIdentPath, + warning::WithWarnings, }; use quote::quote; use std::str::FromStr; @@ -19,9 +20,11 @@ use self::{ meta_path::extract_variant_validator_from_meta_path, }; -pub fn extract_variant_validator(attribute: &syn::Attribute) -> Result { +pub fn extract_variant_validator( + attribute: &syn::Attribute, +) -> Result, crate::Errors> { match &attribute.meta { - syn::Meta::Path(_) => Ok(quote!()), + syn::Meta::Path(_) => Ok(WithWarnings::new(quote!())), syn::Meta::List(list) => inner_extract_variant_validator(attribute, list), syn::Meta::NameValue(name_value) => { Err(vec![crate::Error::validate_meta_name_value_not_supported( @@ -34,7 +37,7 @@ pub fn extract_variant_validator(attribute: &syn::Attribute) -> Result Result { +) -> Result, crate::Errors> { let mut errors = vec![]; let nested = meta_list .parse_args_with(crate::types::CommaSeparatedMetas::parse_terminated) diff --git a/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs b/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs index 4c80dff..de447e6 100644 --- a/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs +++ b/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs @@ -1,17 +1,32 @@ -use crate::attribute::{ - common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, - MetaListStructValidation, Validator, +use syn::spanned::Spanned; + +use crate::{ + attribute::{ + common::message_format::MessageFormat, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, + MetaListStructValidation, Validator, + }, + warning::{Warning, WithWarnings}, }; pub fn extract_variant_validator_from_meta_list( validation_type: MetaListStructValidation, validation: &syn::MetaList, message_format: MessageFormat, -) -> Result { +) -> Result, crate::Errors> { match validation_type { MetaListStructValidation::Custom => { - extract_generic_struct_custom_validator_from_meta_list(validation, message_format) + extract_generic_struct_custom_validator_from_meta_list(validation, message_format).map( + |validator| { + WithWarnings::new_with_warnings( + validator, + vec![Warning::new_custom_meta_list_deprecated( + validation.path.get_ident().unwrap(), + validation.span(), + )], + ) + }, + ) } } } diff --git a/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs b/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs index 09a44d4..3fca084 100644 --- a/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs +++ b/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs @@ -1,7 +1,10 @@ -use crate::attribute::{ - common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value, - MetaNameValueStructValidation, Validator, +use crate::{ + attribute::{ + common::message_format::MessageFormat, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value, + MetaNameValueStructValidation, Validator, + }, + warning::WithWarnings, }; #[inline] @@ -9,10 +12,11 @@ pub fn extract_variant_validator_from_meta_name_value( validation_type: MetaNameValueStructValidation, validation: &syn::MetaNameValue, message_format: MessageFormat, -) -> Result { +) -> Result, crate::Errors> { match validation_type { MetaNameValueStructValidation::Custom => { extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format) } } + .map(WithWarnings::new) } diff --git a/serde_valid_derive/src/attribute/variant_validate/meta/meta_path.rs b/serde_valid_derive/src/attribute/variant_validate/meta/meta_path.rs index bec8f8c..a047b02 100644 --- a/serde_valid_derive/src/attribute/variant_validate/meta/meta_path.rs +++ b/serde_valid_derive/src/attribute/variant_validate/meta/meta_path.rs @@ -1,5 +1,6 @@ -use crate::attribute::{ - common::message_format::MessageFormat, MetaPathStructValidation, Validator, +use crate::{ + attribute::{common::message_format::MessageFormat, MetaPathStructValidation, Validator}, + warning::WithWarnings, }; #[inline] @@ -7,6 +8,6 @@ pub fn extract_variant_validator_from_meta_path( validation_type: MetaPathStructValidation, _validation: &syn::Path, _message_format: MessageFormat, -) -> Result { +) -> Result, crate::Errors> { match validation_type {} } diff --git a/serde_valid_derive/src/derive/enum_derive.rs b/serde_valid_derive/src/derive/enum_derive.rs index 2099a70..f9a6511 100644 --- a/serde_valid_derive/src/derive/enum_derive.rs +++ b/serde_valid_derive/src/derive/enum_derive.rs @@ -102,7 +102,7 @@ fn expand_enum_variant_named_fields_validation( rule_fields, WithWarnings { data: rules, - warnings, + mut warnings, }, ) = match collect_rules_from_named_struct(&variant.ident, &variant.attrs) { Ok(field_rules) => field_rules, @@ -113,7 +113,10 @@ fn expand_enum_variant_named_fields_validation( }; let enum_validates = match collect_variant_custom_from_variant(&input.attrs) { - Ok(validations) => TokenStream::from_iter(validations), + Ok(validations) => { + warnings.extend(validations.warnings); + TokenStream::from_iter(validations.data) + } Err(rule_errors) => { errors.extend(rule_errors); quote!() @@ -184,7 +187,7 @@ fn expand_enum_variant_unnamed_fields_varidation( rule_fields, WithWarnings { data: rules, - warnings, + mut warnings, }, ) = match collect_rules_from_unnamed_struct(&variant.ident, &variant.attrs) { Ok(field_rules) => field_rules, @@ -195,7 +198,10 @@ fn expand_enum_variant_unnamed_fields_varidation( }; let enum_validates = match collect_variant_custom_from_variant(&input.attrs) { - Ok(validations) => TokenStream::from_iter(validations), + Ok(validations) => { + warnings.extend(validations.warnings); + TokenStream::from_iter(validations.data) + } Err(rule_errors) => { errors.extend(rule_errors); quote!() diff --git a/serde_valid_derive/src/warning.rs b/serde_valid_derive/src/warning.rs index b634cc6..29579f9 100644 --- a/serde_valid_derive/src/warning.rs +++ b/serde_valid_derive/src/warning.rs @@ -18,6 +18,10 @@ impl WithWarnings { } } + pub fn new_with_warnings(data: T, warnings: Vec) -> Self { + Self { data, warnings } + } + pub fn from_iter(data: impl IntoIterator>) -> WithWarnings> { let mut warnings = vec![]; let data = data @@ -103,7 +107,7 @@ impl Warning { } } - pub fn new_enumerate_path_deprecated(ident: &syn::Ident, span: Span) -> Self { + 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." @@ -112,6 +116,15 @@ impl Warning { } } + pub fn new_custom_meta_list_deprecated(ident: &syn::Ident, span: Span) -> Self { + Self::Deprecated { + ident: ident.clone(), + note: "#[validate(custom(...))] is deprecated, please use #[validate(custom = ...)] instead." + .to_string(), + span, + } + } + pub fn add_index(&self, index: usize) -> Self { match self { Self::Deprecated { ident, note, span } => Self::Deprecated {