github.com/blend/go-sdk@v1.20220411.3/validate/int.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - 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  import "github.com/blend/go-sdk/ex"
    11  
    12  // Int errors
    13  const (
    14  	ErrIntMin      ex.Class = "int should be above a minimum value"
    15  	ErrIntMax      ex.Class = "int should be below a maximum value"
    16  	ErrIntPositive ex.Class = "int should be positive"
    17  	ErrIntNegative ex.Class = "int should be negative"
    18  	ErrIntZero     ex.Class = "int should be zero"
    19  	ErrIntNotZero  ex.Class = "int should not be zero"
    20  )
    21  
    22  // Int returns validators for ints.
    23  func Int(value *int) IntValidators {
    24  	return IntValidators{value}
    25  }
    26  
    27  // IntValidators implements int validators.
    28  type IntValidators struct {
    29  	Value *int
    30  }
    31  
    32  // Min returns a validator that an int is above a minimum value inclusive.
    33  // Min will pass for a value 1 if the min is set to 1, that is no error
    34  // would be returned.
    35  func (i IntValidators) Min(min int) Validator {
    36  	return func() error {
    37  		if i.Value == nil {
    38  			// an unset value cannot satisfy a minimum because it has no value.
    39  			return Errorf(ErrIntMin, nil, "min: %d", min)
    40  		}
    41  		if *i.Value < min {
    42  			return Errorf(ErrIntMin, *i.Value, "min: %d", min)
    43  		}
    44  		return nil
    45  	}
    46  }
    47  
    48  // Max returns a validator that an int is below a max value inclusive.
    49  // Max will pass for a value 10 if the max is set to 10, that is no error
    50  // would be returned.
    51  func (i IntValidators) Max(max int) Validator {
    52  	return func() error {
    53  		if i.Value == nil {
    54  			// an unset value cannot _violate_ a maximum because it has no value.
    55  			return nil
    56  		}
    57  		if *i.Value > max {
    58  			return Errorf(ErrIntMax, *i.Value, "max: %d", max)
    59  		}
    60  		return nil
    61  	}
    62  }
    63  
    64  // Between returns a validator that an int is between a given min and max inclusive,
    65  // that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass
    66  // for [1] and [5].
    67  func (i IntValidators) Between(min, max int) Validator {
    68  	return func() error {
    69  		if i.Value == nil {
    70  			// an unset value cannot satisfy a minimum because it has no value.
    71  			return Errorf(ErrIntMin, nil, "min: %d", min)
    72  		}
    73  		if *i.Value < min {
    74  			return Errorf(ErrIntMin, *i.Value, "min: %d", min)
    75  		}
    76  		if *i.Value > max {
    77  			return Errorf(ErrIntMax, *i.Value, "max: %d", max)
    78  		}
    79  		return nil
    80  	}
    81  }
    82  
    83  // Positive returns a validator that an int is positive.
    84  func (i IntValidators) Positive() Validator {
    85  	return func() error {
    86  		if i.Value == nil {
    87  			// an unset value cannot be positive
    88  			return Error(ErrIntPositive, nil)
    89  		}
    90  		if *i.Value < 0 {
    91  			return Error(ErrIntPositive, *i.Value)
    92  		}
    93  		return nil
    94  	}
    95  }
    96  
    97  // Negative returns a validator that an int is negative.
    98  func (i IntValidators) Negative() Validator {
    99  	return func() error {
   100  		if i.Value == nil {
   101  			// an unset value cannot be negative
   102  			return Error(ErrIntNegative, nil)
   103  		}
   104  		if *i.Value > 0 {
   105  			return Error(ErrIntNegative, *i.Value)
   106  		}
   107  		return nil
   108  	}
   109  }
   110  
   111  // Zero returns a validator that an int is zero.
   112  func (i IntValidators) Zero() Validator {
   113  	return func() error {
   114  		if i.Value == nil {
   115  			// an unset value cannot be zero
   116  			return Error(ErrIntZero, nil)
   117  		}
   118  		if *i.Value != 0 {
   119  			return Error(ErrIntZero, *i.Value)
   120  		}
   121  		return nil
   122  	}
   123  }
   124  
   125  // NotZero returns a validator that an int is not zero.
   126  func (i IntValidators) NotZero() Validator {
   127  	return func() error {
   128  		if i.Value == nil {
   129  			// an unset value cannot be not zero
   130  			return Error(ErrIntNotZero, nil)
   131  		}
   132  		if *i.Value == 0 {
   133  			return Error(ErrIntNotZero, *i.Value)
   134  		}
   135  		return nil
   136  	}
   137  }