github.com/blend/go-sdk@v1.20220411.3/validate/float64.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 (
    11  	"math"
    12  
    13  	"github.com/blend/go-sdk/ex"
    14  )
    15  
    16  // Float64 errors
    17  const (
    18  	ErrFloat64Min      ex.Class = "float64 should be above a minimum value"
    19  	ErrFloat64Max      ex.Class = "float64 should be below a maximum value"
    20  	ErrFloat64Positive ex.Class = "float64 should be positive"
    21  	ErrFloat64Negative ex.Class = "float64 should be negative"
    22  	ErrFloat64Epsilon  ex.Class = "float64 should be within an epsilon of a value"
    23  	ErrFloat64Zero     ex.Class = "float64 should be zero"
    24  	ErrFloat64NotZero  ex.Class = "float64 should not be zero"
    25  )
    26  
    27  // Float64 returns validators for float64s.
    28  func Float64(value *float64) Float64Validators {
    29  	return Float64Validators{value}
    30  }
    31  
    32  // Float64Validators implements float64 validators.
    33  type Float64Validators struct {
    34  	Value *float64
    35  }
    36  
    37  // Min returns a validator that a float64 is above a minimum value inclusive.
    38  // Min will pass for a value 1 if the min is set to 1, that is no error
    39  // would be returned.
    40  func (f Float64Validators) Min(min float64) Validator {
    41  	return func() error {
    42  		if f.Value == nil {
    43  			// an unset value cannot satisfy a minimum because it has no value.
    44  			return Errorf(ErrFloat64Min, nil, "min: %v", min)
    45  		}
    46  		if *f.Value < min {
    47  			return Errorf(ErrFloat64Min, *f.Value, "min: %v", min)
    48  		}
    49  		return nil
    50  	}
    51  }
    52  
    53  // Max returns a validator that a float64 is below a max value inclusive.
    54  // Max will pass for a value 10 if the max is set to 10, that is no error
    55  // would be returned.
    56  func (f Float64Validators) Max(max float64) Validator {
    57  	return func() error {
    58  		if f.Value == nil {
    59  			// an unset value cannot _violate_ a maximum because it has no value.
    60  			return nil
    61  		}
    62  		if *f.Value > max {
    63  			return Errorf(ErrFloat64Max, *f.Value, "max: %v", max)
    64  		}
    65  		return nil
    66  	}
    67  }
    68  
    69  // Between returns a validator that a float64 is between a given min and max inclusive,
    70  // that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass
    71  // for [1] and [5].
    72  func (f Float64Validators) Between(min, max float64) Validator {
    73  	return func() error {
    74  		if f.Value == nil {
    75  			// an unset value cannot satisfy a minimum because it has no value.
    76  			return Errorf(ErrFloat64Min, nil, "min: %v", min)
    77  		}
    78  		if *f.Value < min {
    79  			return Errorf(ErrFloat64Min, *f.Value, "min: %v", min)
    80  		}
    81  		if *f.Value > max {
    82  			return Errorf(ErrFloat64Max, *f.Value, "max: %v", max)
    83  		}
    84  		return nil
    85  	}
    86  }
    87  
    88  // Positive returns a validator that a float64 is positive.
    89  func (f Float64Validators) Positive() Validator {
    90  	return func() error {
    91  		if f.Value == nil {
    92  			// an unset value cannot be positive
    93  			return Error(ErrFloat64Positive, nil)
    94  		}
    95  		if *f.Value < 0 {
    96  			return Error(ErrFloat64Positive, *f.Value)
    97  		}
    98  		return nil
    99  	}
   100  }
   101  
   102  // Negative returns a validator that a float64 is negative.
   103  func (f Float64Validators) Negative() Validator {
   104  	return func() error {
   105  		if f.Value == nil {
   106  			// an unset value cannot be negative
   107  			return Error(ErrFloat64Negative, nil)
   108  		}
   109  		if *f.Value > 0 {
   110  			return Error(ErrFloat64Negative, *f.Value)
   111  		}
   112  		return nil
   113  	}
   114  }
   115  
   116  // Epsilon returns if a value is comparable to another value within an epsilon.
   117  // It will return a failure if the absolute difference between the target value
   118  // and a given value is greater than the given epsilon.
   119  func (f Float64Validators) Epsilon(value, epsilon float64) Validator {
   120  	return func() error {
   121  		if f.Value == nil {
   122  			// an unset value cannot be negative
   123  			return Errorf(ErrFloat64Epsilon, nil, "value: %v epsilon: %v", value, epsilon)
   124  		}
   125  		if math.Abs(*f.Value-value) > epsilon {
   126  			return Errorf(ErrFloat64Epsilon, *f.Value, "value: %v epsilon: %v", value, epsilon)
   127  		}
   128  		return nil
   129  	}
   130  }
   131  
   132  // Zero returns a validator that a float64 is zero.
   133  func (f Float64Validators) Zero() Validator {
   134  	return func() error {
   135  		if f.Value == nil {
   136  			// an unset value cannot be zero
   137  			return Error(ErrFloat64Zero, nil)
   138  		}
   139  		if *f.Value != 0 {
   140  			return Error(ErrFloat64Zero, *f.Value)
   141  		}
   142  		return nil
   143  	}
   144  }
   145  
   146  // NotZero returns a validator that a float64 is not zero.
   147  func (f Float64Validators) NotZero() Validator {
   148  	return func() error {
   149  		if f.Value == nil {
   150  			// an unset value cannot be not zero
   151  			return Error(ErrFloat64NotZero, nil)
   152  		}
   153  		if *f.Value == 0 {
   154  			return Error(ErrFloat64NotZero, *f.Value)
   155  		}
   156  		return nil
   157  	}
   158  }