github.com/blend/go-sdk@v1.20220411.3/validate/uint32.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 // Uint32 errors 13 const ( 14 ErrUint32Min ex.Class = "uint32 should be above a minimum value" 15 ErrUint32Max ex.Class = "uint32 should be below a maximum value" 16 ErrUint32Zero ex.Class = "uint32 should be zero" 17 ErrUint32NotZero ex.Class = "uint32 should not be zero" 18 ) 19 20 // Uint32 returns validators for uint32s. 21 func Uint32(value *uint32) Uint32Validators { 22 return Uint32Validators{value} 23 } 24 25 // Uint32Validators implements uint32 validators. 26 type Uint32Validators struct { 27 Value *uint32 28 } 29 30 // Min returns a validator that an uint32 is above a minimum value inclusive. 31 // Min will pass for a value 1 if the min is set to 1, that is no error 32 // would be returned. 33 func (i Uint32Validators) Min(min uint32) Validator { 34 return func() error { 35 if i.Value == nil { 36 // an unset value cannot satisfy a minimum because it has no value. 37 return Errorf(ErrUint32Min, nil, "min: %d", min) 38 } 39 if *i.Value < min { 40 return Errorf(ErrUint32Min, *i.Value, "min: %d", min) 41 } 42 return nil 43 } 44 } 45 46 // Max returns a validator that an uint32 is below a max value inclusive. 47 // Max will pass for a value 10 if the max is set to 10, that is no error 48 // would be returned. 49 func (i Uint32Validators) Max(max uint32) Validator { 50 return func() error { 51 if i.Value == nil { 52 // an unset value cannot _violate_ a maximum because it has no value. 53 return nil 54 } 55 if *i.Value > max { 56 return Errorf(ErrUint32Max, *i.Value, "max: %d", max) 57 } 58 return nil 59 } 60 } 61 62 // Between returns a validator that an uint32 is between a given min and max inclusive, 63 // that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass 64 // for [1] and [5]. 65 func (i Uint32Validators) Between(min, max uint32) Validator { 66 return func() error { 67 if i.Value == nil { 68 // an unset value cannot satisfy a minimum because it has no value. 69 return Errorf(ErrUint32Min, nil, "min: %d", min) 70 } 71 if *i.Value < min { 72 return Errorf(ErrUint32Min, *i.Value, "min: %d", min) 73 } 74 if *i.Value > max { 75 return Errorf(ErrUint32Max, *i.Value, "max: %d", max) 76 } 77 return nil 78 } 79 } 80 81 // Zero returns a validator that an uint32 is zero. 82 func (i Uint32Validators) Zero() Validator { 83 return func() error { 84 if i.Value == nil { 85 // an unset value cannot be zero 86 return Error(ErrUint32Zero, nil) 87 } 88 if *i.Value != 0 { 89 return Error(ErrUint32Zero, *i.Value) 90 } 91 return nil 92 } 93 } 94 95 // NotZero returns a validator that an uint32 is not zero. 96 func (i Uint32Validators) NotZero() Validator { 97 return func() error { 98 if i.Value == nil { 99 // an unset value cannot be not zero 100 return Error(ErrUint32NotZero, nil) 101 } 102 if *i.Value == 0 { 103 return Error(ErrUint32NotZero, *i.Value) 104 } 105 return nil 106 } 107 }