File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -283,7 +283,16 @@ fn named_struct_custom_closure_vec_errors_is_err() {
283283}
284284
285285#[ test]
286- fn filed_custom_validation_using_self ( ) {
286+ fn filed_custom_validation_complex_closure ( ) {
287+ fn kind_validation ( kind : & str ) -> Result < ( ) , serde_valid:: validation:: Error > {
288+ match kind {
289+ "Cat" | "Dog" => Ok ( ( ) ) ,
290+ _ => Err ( serde_valid:: validation:: Error :: Custom (
291+ "Kind should be Cat or Dog." . to_string ( ) ,
292+ ) ) ,
293+ }
294+ }
295+
287296 fn food_validation ( kind : & str , food : & str ) -> Result < ( ) , serde_valid:: validation:: Error > {
288297 match kind {
289298 "Cat" => {
@@ -310,10 +319,16 @@ fn filed_custom_validation_using_self() {
310319
311320 #[ derive( Validate ) ]
312321 struct Pet {
313- #[ validate( enumerate = [ "Cat" , "Dog" ] ) ]
322+ #[ validate( custom = kind_validation ) ]
314323 kind : String ,
315324
316- #[ validate( custom = |food| food_validation( & self . kind, food) ) ]
325+ #[ validate( custom = |food| {
326+ if kind_validation( & self . kind) . is_ok( ) {
327+ food_validation( & self . kind, food)
328+ } else {
329+ Ok ( ( ) )
330+ }
331+ } ) ]
317332 food : String ,
318333 }
319334
You can’t perform that action at this time.
0 commit comments