github.com/blend/go-sdk@v1.20220411.3/validate/int8.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  // Int8 errors
    13  const (
    14  	ErrInt8Min      ex.Class = "int8 should be above a minimum value"
    15  	ErrInt8Max      ex.Class = "int8 should be below a maximum value"
    16  	ErrInt8Positive ex.Class = "int8 should be positive"
    17  	ErrInt8Negative ex.Class = "int8 should be negative"
    18  	ErrInt8Zero     ex.Class = "int8 should be zero"
    19  	ErrInt8NotZero  ex.Class = "int8 should not be zero"
    20  )
    21  
    22  // Int8 returns validators for int8s.
    23  func Int8(value *int8) Int8Validators {
    24  	return Int8Validators{value}
    25  }
    26  
    27  // Int8Validators implements int8 validators.
    28  type Int8Validators struct {
    29  	Value *int8
    30  }
    31  
    32  // Min returns a validator that an int8 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 Int8Validators) Min(min int8) 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(ErrInt8Min, nil, "min: %d", min)
    40  		}
    41  		if *i.Value < min {
    42  			return Errorf(ErrInt8Min, *i.Value, "min: %d", min)
    43  		}
    44  		return nil
    45  	}
    46  }
    47  
    48  // Max returns a validator that an int8 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 Int8Validators) Max(max int8) 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(ErrInt8Max, *i.Value, "max: %d", max)
    59  		}
    60  		return nil
    61  	}
    62  }
    63  
    64  // Between returns a validator that an int8 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 Int8Validators) Between(min, max int8) 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(ErrInt8Min, nil, "min: %d", min)
    72  		}
    73  		if *i.Value < min {
    74  			return Errorf(ErrInt8Min, *i.Value, "min: %d", min)
    75  		}
    76  		if *i.Value > max {
    77  			return Errorf(ErrInt8Max, *i.Value, "max: %d", max)
    78  		}
    79  		return nil
    80  	}
    81  }
    82  
    83  // Positive returns a validator that an int8 is positive.
    84  func (i Int8Validators) Positive() Validator {
    85  	return func() error {
    86  		if i.Value == nil {
    87  			// an unset value cannot be positive
    88  			return Error(ErrInt8Positive, nil)
    89  		}
    90  		if *i.Value < 0 {
    91  			return Error(ErrInt8Positive, *i.Value)
    92  		}
    93  		return nil
    94  	}
    95  }
    96  
    97  // Negative returns a validator that an int8 is negative.
    98  func (i Int8Validators) Negative() Validator {
    99  	return func() error {
   100  		if i.Value == nil {
   101  			// an unset value cannot be negative
   102  			return Error(ErrInt8Negative, nil)
   103  		}
   104  		if *i.Value > 0 {
   105  			return Error(ErrInt8Negative, *i.Value)
   106  		}
   107  		return nil
   108  	}
   109  }
   110  
   111  // Zero returns a validator that an int8 is zero.
   112  func (i Int8Validators) Zero() Validator {
   113  	return func() error {
   114  		if i.Value == nil {
   115  			// an unset value cannot be zero
   116  			return Error(ErrInt8Zero, nil)
   117  		}
   118  		if *i.Value != 0 {
   119  			return Error(ErrInt8Zero, *i.Value)
   120  		}
   121  		return nil
   122  	}
   123  }
   124  
   125  // NotZero returns a validator that an int8 is not zero.
   126  func (i Int8Validators) NotZero() Validator {
   127  	return func() error {
   128  		if i.Value == nil {
   129  			// an unset value cannot be not zero
   130  			return Error(ErrInt8NotZero, nil)
   131  		}
   132  		if *i.Value == 0 {
   133  			return Error(ErrInt8NotZero, *i.Value)
   134  		}
   135  		return nil
   136  	}
   137  }