-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
179 lines (155 loc) · 4.84 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2023 Christoph Fichtmüller. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package jug
import (
"fmt"
"regexp"
"strings"
)
type Validator struct {
errors strings.Builder
}
func NewValidator() *Validator {
return &Validator{
errors: strings.Builder{},
}
}
// V invokes a validation function on the validator
func (v *Validator) V(fun func(*Validator)) *Validator {
fun(v)
return v
}
// Require requires a condition to be truthy
func (v *Validator) Require(condition bool, message string) *Validator {
if !condition {
v.append(message)
}
return v
}
// RequireEnum requires a value to be found in a given enum
func (v *Validator) RequireEnum(s string, message string, values ...string) *Validator {
if len(s) == 0 {
return v
}
for _, value := range values {
if s == value {
return v
}
}
v.append(message)
return v
}
// RequireStringSliceMinLength requires the given slice to have at least min elements
//
// Deprecated: use RequireSliceMinLength instead.
func (v *Validator) RequireStringSliceMinLength(s []string, min int, message string) *Validator {
return v.RequireSliceMinLength(s, min, message)
}
// RequireSliceMinLength requires the given slice to have at least min elements
func (v *Validator) RequireSliceMinLength(s []string, min int, message string) *Validator {
if len(s) < min {
v.append(message)
}
return v
}
// RequireStringSliceNotEmpty requires the given slice not to be empty
//
// Deprecated: use RequireSliceNotEmpty instead.
func (v *Validator) RequireStringSliceNotEmpty(s []string, message string) *Validator {
return v.RequireSliceNotEmpty(s, message)
}
// RequireSliceNotEmpty requires the given slice not to be empty
func (v *Validator) RequireSliceNotEmpty(s []string, message string) *Validator {
if len(s) == 0 {
v.append(message)
}
return v
}
// RequireStringSliceEnum requires the given slice to only contain elements from values...
//
// Deprecated: use RequireSliceEnum instead.
func (v *Validator) RequireStringSliceEnum(s []string, message string, values ...string) *Validator {
return v.RequireSliceEnum(s, message, values...)
}
// RequireSliceEnum requires the given slice to only contain elements from values...
func (v *Validator) RequireSliceEnum(s []string, message string, values ...string) *Validator {
if len(s) == 0 {
return v
}
m := make(map[string]bool)
for _, v := range values {
m[v] = true
}
for _, i := range s {
_, ok := m[i]
if !ok {
v.append(message)
return v
}
}
return v
}
// RequireMatchesRegex requires a value to match a given regular expression
func (v *Validator) RequireMatchesRegex(s string, regex *regexp.Regexp, message string) *Validator {
if len(s) > 0 && !regex.MatchString(s) {
v.append(message)
}
return v
}
// RequireStringMinLength requires a value to have a given minimum length
//
// Deprecated: use RequireMinLength instead.
func (v *Validator) RequireStringMinLength(s string, min int, message string) *Validator {
return v.RequireMinLength(s, min, message)
}
// RequireMinLength requires a value to have a given minimum length
func (v *Validator) RequireMinLength(s string, min int, message string) *Validator {
return v.Require(len(s) >= min, message)
}
// RequireStringMaxLength requires a value to have a given maximum length
//
// Deprecated: use RequireMaxLength instead.
func (v *Validator) RequireStringMaxLength(s string, max int, message string) *Validator {
return v.RequireMaxLength(s, max, message)
}
// RequireMaxLength requires a value to have a given maximum length
func (v *Validator) RequireMaxLength(s string, max int, message string) *Validator {
return v.Require(len(s) < max, message)
}
// RequireStringNotEmpty requires a string not to be empty
//
// Deprecated: use RequireNotEmpty instead
func (v *Validator) RequireStringNotEmpty(s string, message string) *Validator {
return v.RequireNotEmpty(s, message)
}
// RequireNotEmpty requires a string not to be empty
func (v *Validator) RequireNotEmpty(s string, message string) *Validator {
return v.Require(len(s) > 0, message)
}
// RequireStringLengthBetween requires a string TODO
func (v *Validator) RequireStringLengthBetween(s string, min int, max int, message string) *Validator {
return v.Require(len(s) >= min && len(s) < max, message)
}
// Validate performs the validation
func (v *Validator) Validate() error {
if v.errors.Len() > 0 {
return fmt.Errorf(v.errors.String())
}
return nil
}
func (v *Validator) append(msg string) {
if v.errors.Len() > 0 {
v.errors.WriteString(", ")
}
v.errors.WriteString(msg)
}
// ValidateSub performs validation on a sub item.
func ValidateSub[T Validatable](v *Validator, key string, items []T) *Validator {
for i, item := range items {
if err := item.Validate(); err != nil {
v.append(fmt.Sprintf("%s[%d]: %s", key, i, err.Error()))
}
}
return v
}