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