File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed
serde_valid_derive/src/rule Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 1+ use serde_valid:: Validate ;
2+
3+ #[ test]
4+ fn test_raw_type_field ( ) {
5+ #[ derive( Validate ) ]
6+ // `"r#type"` is not a valid identifier.
7+ // It's proc_macro2 specification.
8+ // To avoid this, indicate `type`.
9+ #[ rule( sample_rule( type ) ) ]
10+ struct MyStruct {
11+ #[ validate( maximum = 10 ) ]
12+ pub r#type : i32 ,
13+ }
14+
15+ fn sample_rule ( _type : & i32 ) -> Result < ( ) , serde_valid:: validation:: Error > {
16+ Ok ( ( ) )
17+ }
18+
19+ let my_struct = MyStruct { r#type : 1 } ;
20+ assert ! ( my_struct. validate( ) . is_ok( ) ) ;
21+ }
Original file line number Diff line number Diff line change @@ -104,11 +104,14 @@ fn extract_rule_from_meta_list(
104104 . filter_map ( |nested_meta| {
105105 let arg = match nested_meta {
106106 syn:: NestedMeta :: Meta ( syn:: Meta :: Path ( path) ) => {
107- arg_idents. insert ( syn:: Ident :: new (
108- & path. to_token_stream ( ) . to_string ( ) ,
109- path. span ( ) ,
110- ) ) ;
111- Some ( quote ! ( #path) )
107+ let ident = path. to_token_stream ( ) . to_string ( ) ;
108+ if ident == "type" {
109+ arg_idents. insert ( syn:: Ident :: new_raw ( "type" , path. span ( ) ) ) ;
110+ Some ( quote ! ( r#type) )
111+ } else {
112+ arg_idents. insert ( syn:: Ident :: new ( & ident, path. span ( ) ) ) ;
113+ Some ( quote ! ( #path) )
114+ }
112115 }
113116 _ => None ,
114117 } ;
You can’t perform that action at this time.
0 commit comments