github.com/blend/go-sdk@v1.20220411.3/validate/float32.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 // Float32 errors 17 const ( 18 ErrFloat32Min ex.Class = "float32 should be above a minimum value" 19 ErrFloat32Max ex.Class = "float32 should be below a maximum value" 20 ErrFloat32Positive ex.Class = "float32 should be positive" 21 ErrFloat32Negative ex.Class = "float32 should be negative" 22 ErrFloat32Epsilon ex.Class = "float32 should be within an epsilon of a value" 23 ErrFloat32Zero ex.Class = "float32 should be zero" 24 ErrFloat32NotZero ex.Class = "float32 should not be zero" 25 ) 26 27 // Float32 returns validators for float32s. 28 func Float32(value *float32) Float32Validators { 29 return Float32Validators{value} 30 } 31 32 // Float32Validators implements float32 validators. 33 type Float32Validators struct { 34 Value *float32 35 } 36 37 // Min returns a validator that a float32 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 Float32Validators) Min(min float32) 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(ErrFloat32Min, nil, "min: %v", min) 45 } 46 if *f.Value < min { 47 return Errorf(ErrFloat32Min, *f.Value, "min: %v", min) 48 } 49 return nil 50 } 51 } 52 53 // Max returns a validator that a float32 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 Float32Validators) Max(max float32) 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(ErrFloat32Max, *f.Value, "max: %v", max) 64 } 65 return nil 66 } 67 } 68 69 // Between returns a validator that a float32 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 Float32Validators) Between(min, max float32) 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(ErrFloat32Min, nil, "min: %v", min) 77 } 78 if *f.Value < min { 79 return Errorf(ErrFloat32Min, *f.Value, "min: %v", min) 80 } 81 if *f.Value > max { 82 return Errorf(ErrFloat32Max, *f.Value, "max: %v", max) 83 } 84 return nil 85 } 86 } 87 88 // Positive returns a validator that a float32 is positive. 89 func (f Float32Validators) Positive() Validator { 90 return func() error { 91 if f.Value == nil { 92 // an unset value cannot be positive 93 return Error(ErrFloat32Positive, nil) 94 } 95 if *f.Value < 0 { 96 return Error(ErrFloat32Positive, *f.Value) 97 } 98 return nil 99 } 100 } 101 102 // Negative returns a validator that a float32 is negative. 103 func (f Float32Validators) Negative() Validator { 104 return func() error { 105 if f.Value == nil { 106 // an unset value cannot be negative 107 return Error(ErrFloat32Negative, nil) 108 } 109 if *f.Value > 0 { 110 return Error(ErrFloat32Negative, *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 Float32Validators) Epsilon(value, epsilon float32) Validator { 120 return func() error { 121 if f.Value == nil { 122 // an unset value cannot be negative 123 return Errorf(ErrFloat32Epsilon, nil, "value: %v epsilon: %v", value, epsilon) 124 } 125 if math.Abs(float64(*f.Value-value)) > float64(epsilon) { 126 return Errorf(ErrFloat32Epsilon, *f.Value, "value: %v epsilon: %v", value, epsilon) 127 } 128 return nil 129 } 130 } 131 132 // Zero returns a validator that a float32 is zero. 133 func (f Float32Validators) Zero() Validator { 134 return func() error { 135 if f.Value == nil { 136 // an unset value cannot be zero 137 return Error(ErrFloat32Zero, nil) 138 } 139 if *f.Value != 0 { 140 return Error(ErrFloat32Zero, *f.Value) 141 } 142 return nil 143 } 144 } 145 146 // NotZero returns a validator that a float32 is not zero. 147 func (f Float32Validators) NotZero() Validator { 148 return func() error { 149 if f.Value == nil { 150 // an unset value cannot be not zero 151 return Error(ErrFloat32NotZero, nil) 152 } 153 if *f.Value == 0 { 154 return Error(ErrFloat32NotZero, *f.Value) 155 } 156 return nil 157 } 158 }