github.com/blend/go-sdk@v1.20220411.3/validate/uint16.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 // Uint16 errors 13 const ( 14 ErrUint16Min ex.Class = "uint16 should be above a minimum value" 15 ErrUint16Max ex.Class = "uint16 should be below a maximum value" 16 ErrUint16Zero ex.Class = "uint16 should be zero" 17 ErrUint16NotZero ex.Class = "uint16 should not be zero" 18 ) 19 20 // Uint16 returns validators for uint16s. 21 func Uint16(value *uint16) Uint16Validators { 22 return Uint16Validators{value} 23 } 24 25 // Uint16Validators implements uint16 validators. 26 type Uint16Validators struct { 27 Value *uint16 28 } 29 30 // Min returns a validator that an uint16 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 Uint16Validators) Min(min uint16) 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(ErrUint16Min, nil, "min: %d", min) 38 } 39 if *i.Value < min { 40 return Errorf(ErrUint16Min, *i.Value, "min: %d", min) 41 } 42 return nil 43 } 44 } 45 46 // Max returns a validator that an uint16 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 Uint16Validators) Max(max uint16) 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(ErrUint16Max, *i.Value, "max: %d", max) 57 } 58 return nil 59 } 60 } 61 62 // Between returns a validator that an uint16 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 Uint16Validators) Between(min, max uint16) 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(ErrUint16Min, nil, "min: %d", min) 70 } 71 if *i.Value < min { 72 return Errorf(ErrUint16Min, *i.Value, "min: %d", min) 73 } 74 if *i.Value > max { 75 return Errorf(ErrUint16Max, *i.Value, "max: %d", max) 76 } 77 return nil 78 } 79 } 80 81 // Zero returns a validator that an uint16 is zero. 82 func (i Uint16Validators) Zero() Validator { 83 return func() error { 84 if i.Value == nil { 85 // an unset value cannot be zero 86 return Error(ErrUint16Zero, nil) 87 } 88 if *i.Value != 0 { 89 return Error(ErrUint16Zero, *i.Value) 90 } 91 return nil 92 } 93 } 94 95 // NotZero returns a validator that an uint16 is not zero. 96 func (i Uint16Validators) NotZero() Validator { 97 return func() error { 98 if i.Value == nil { 99 // an unset value cannot be not zero 100 return Error(ErrUint16NotZero, nil) 101 } 102 if *i.Value == 0 { 103 return Error(ErrUint16NotZero, *i.Value) 104 } 105 return nil 106 } 107 }