@@ -12,9 +12,12 @@ pub(crate) fn into_rule(
1212 content_types : & HashMap < ContentTypeTemplate , ContentTypeSpec > ,
1313 field : & signed_doc_spec:: content_type:: ContentType ,
1414) -> anyhow:: Result < TokenStream > {
15+ let is_field_empty =
16+ field. value . is_none ( ) || field. value . as_ref ( ) . is_some_and ( std:: string:: String :: is_empty) ;
17+
1518 if matches ! ( field. required, IsRequired :: Excluded ) {
1619 anyhow:: ensure!(
17- field . value . is_empty ( ) ,
20+ is_field_empty ,
1821 "'value' field must not exist when 'required' is 'excluded'"
1922 ) ;
2023
@@ -24,18 +27,24 @@ pub(crate) fn into_rule(
2427 }
2528
2629 if matches ! ( field. required, IsRequired :: Yes ) {
27- anyhow:: ensure!( !field . value . is_empty ( ) , "'value' field must exist" ) ;
30+ anyhow:: ensure!( !is_field_empty , "'value' field must exist" ) ;
2831 }
2932
30- let template = ContentTypeTemplate ( field. value . clone ( ) ) ;
31- let Some ( _) = content_types. get ( & template) else {
32- return Err ( anyhow:: anyhow!( "Unsupported Content Type: {}" , field. value) ) ;
33- } ;
34- let ident = template. ident ( ) ;
35-
36- Ok ( quote ! {
37- crate :: validator:: rules:: ContentTypeRule :: Specified {
38- exp: ContentType :: #ident,
39- }
40- } )
33+ if let Some ( value) = & field. value {
34+ let template = ContentTypeTemplate ( value. clone ( ) ) ;
35+ let Some ( _) = content_types. get ( & template) else {
36+ return Err ( anyhow:: anyhow!( "Unsupported Content Type: {}" , value) ) ;
37+ } ;
38+ let ident = template. ident ( ) ;
39+
40+ Ok ( quote ! {
41+ crate :: validator:: rules:: ContentTypeRule :: Specified {
42+ exp: ContentType :: #ident,
43+ }
44+ } )
45+ } else {
46+ Ok ( quote ! {
47+ crate :: validator:: rules:: ContentTypeRule :: NotSpecified
48+ } )
49+ }
4150}
0 commit comments