github.com/blend/go-sdk@v1.20240719.1/validate/when_else.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package validate
     9  
    10  // WhenElse returns the result of the "passes" validator if the predicate returns true,
    11  // otherwise it returns the result of the "fails" validator.
    12  func WhenElse(predicate func() bool, passes, fails Validator) Validator {
    13  	return func() error {
    14  		if predicate() {
    15  			if passes != nil {
    16  				return passes()
    17  			}
    18  			return nil
    19  		}
    20  		if fails != nil {
    21  			return fails()
    22  		}
    23  		return nil
    24  	}
    25  }