gitlab.com/evatix-go/core@v1.3.55/corevalidator/SliceValidators.go (about) 1 package corevalidator 2 3 import ( 4 "fmt" 5 6 "gitlab.com/evatix-go/core/constants" 7 "gitlab.com/evatix-go/core/coredata/corestr" 8 ) 9 10 type SliceValidators struct { 11 Validators []*SliceValidator 12 } 13 14 func (it SliceValidators) Length() int { 15 return len(it.Validators) 16 } 17 18 func (it SliceValidators) IsEmpty() bool { 19 return len(it.Validators) == 0 20 } 21 22 func (it *SliceValidators) IsValid( 23 isCaseSensitive bool, 24 ) bool { 25 return it.IsMatch(isCaseSensitive) 26 } 27 28 func (it *SliceValidators) IsMatch( 29 isCaseSensitive bool, 30 ) bool { 31 if it.IsEmpty() { 32 return true 33 } 34 35 for _, sliceValidator := range it.Validators { 36 if !sliceValidator.IsValid(isCaseSensitive) { 37 return false 38 } 39 } 40 41 return true 42 } 43 44 func (it *SliceValidators) VerifyAll( 45 header string, 46 params *ValidatorParamsBase, 47 isPrintError bool, 48 ) error { 49 if it.IsEmpty() { 50 return nil 51 } 52 53 errs := corestr.New.SimpleSlice.Cap(constants.Capacity2) 54 55 for _, sliceValidator := range it.Validators { 56 err := sliceValidator.AllVerifyError(params) 57 58 errs.AddError(err) 59 } 60 61 if errs.IsEmpty() { 62 return nil 63 } 64 65 errs.InsertAt(0, header) 66 err := errs.AsDefaultError() 67 68 if isPrintError { 69 fmt.Println(err) 70 } 71 72 return err 73 } 74 75 func (it *SliceValidators) VerifyFirst( 76 header string, 77 params *ValidatorParamsBase, 78 isPrintError bool, 79 ) error { 80 if it.IsEmpty() { 81 return nil 82 } 83 84 errs := corestr.New.SimpleSlice.Cap(constants.Capacity2) 85 86 for _, sliceValidator := range it.Validators { 87 err := sliceValidator.VerifyFirstError(params) 88 89 errs.AddError(err) 90 } 91 92 if errs.IsEmpty() { 93 return nil 94 } 95 96 errs.InsertAt(0, header) 97 err := errs.AsDefaultError() 98 99 if isPrintError { 100 fmt.Println(err) 101 } 102 103 return err 104 } 105 106 func (it *SliceValidators) VerifyUpto( 107 isPrintErr, 108 isFirstOnly bool, 109 length int, 110 header string, 111 params *ValidatorParamsBase, 112 ) error { 113 if it.IsEmpty() { 114 return nil 115 } 116 117 errs := corestr.New.SimpleSlice.Cap(constants.Capacity2) 118 119 for _, sliceValidator := range it.Validators { 120 err := sliceValidator.AllVerifyErrorUptoLength( 121 isFirstOnly, 122 params, 123 length) 124 125 errs.AddError(err) 126 } 127 128 if errs.IsEmpty() { 129 return nil 130 } 131 132 errs.InsertAt(0, header) 133 err := errs.AsDefaultError() 134 135 if isPrintErr { 136 fmt.Println(err) 137 } 138 139 return err 140 }