/
validator_interface.go
200 lines (159 loc) · 8.36 KB
/
validator_interface.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
/*
This interface reflects the kinds of validations provided by the validator
package (gopkg.in/go-playground/validator.v9).
This interface is kept distinct from the rest for the sake of code
organization and documentation.
Some of the material in the comments is from the validator package (above)
and other material is from the JSON schema validation RFC
(http://json-schema.org/latest/json-schema-validation.html). The material is
annotated to avoid confusion, and some modification have been made for the
same reason.
For compatibility with the Go JSON spec library (github.com/go-openapi/spec),
the return types they are what they are.
The site,
https://spacetelescope.github.io/understanding-json-schema/reference/string.html,
may have clues to creating more interesting validations (email, urls, etc.).
*/
type Validator interface {
/*
Validator Package Documentation:
This validates that the value is not the data types default zero
value. For numbers ensures value is not zero. For strings ensures
value is not "". For slices, maps, pointers, interfaces, channels
and functions ensures the value is not nil.
JSON Schema Validation RFC:
6.17. required
The value of this keyword MUST be an array. Elements of this array, if any, MUST be strings, and MUST be unique.
An object instance is valid against this keyword if every item in the array is the name of a property in the instance.
Omitting this keyword has the same behavior as an empty array.
Returns -1 when no length is enforced.
*/
IsRequired() bool
/*
Validator Package Documentation:
For numbers, length will ensure that the value is equal to the
parameter given. For strings, it checks that the string length is
exactly that number of characters. For slices, arrays, and maps,
validates the number of items.
There doesn't appear to be a JSON schema equivalent to Length() for
string types. Thus, using this with strings should result in the
equivalent of both a Min() and Max() returning the same value.
*/
Length() float64
/*
Validator Package Documentation:
For numbers, max will ensure that the value is less than or equal to
the parameter given. For strings, it checks that the string length
is at most that number of characters. For slices, arrays, and maps,
validates the number of items.
JSON Schema Validation RFC:
6.2. maximum
The value of "maximum" MUST be a number, representing an inclusive upper limit for a numeric instance.
If the instance is a number, then this keyword validates only if the instance is less than or exactly equal to "maximum".
6.6. maxLength
The value of this keyword MUST be a non-negative integer.
A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.
The length of a string instance is defined as the number of its characters as defined by RFC 7159 [RFC7159].
6.11. maxItems
The value of this keyword MUST be a non-negative integer.
An array instance is valid against "maxItems" if its size is less than, or equal to, the value of this keyword.
Returns -1 when this validation is not enforced.
*/
Max() float64
/*
Validator Package Documentation:
For numbers, min will ensure that the value is greater or equal to
the parameter given. For strings, it checks that the string length
is at least that number of characters. For slices, arrays, and maps,
validates the number of items.
JSON Schema Validation RFC:
6.4. minimum
The value of "minimum" MUST be a number, representing an inclusive upper limit for a numeric instance.
If the instance is a number, then this keyword validates only if the instance is greater than or exactly equal to "minimum".
6.7. minLength
The value of this keyword MUST be a non-negative integer.
A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.
The length of a string instance is defined as the number of its characters as defined by RFC 7159 [RFC7159].
Omitting this keyword has the same behavior as a value of 0.
6.12. minItems
The value of this keyword MUST be a non-negative integer.
An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword.
Omitting this keyword has the same behavior as a value of 0.
Returns -1 when this validation is not enforced.
*/
Min() float64
/*
Validator Package Documentation:
For strings & numbers, eq will ensure that the value is equal to the
parameter given. For slices, arrays, and maps, validates the number
of items.
The closest JSON schema equivalent to Equals() for string types is the
regex pattern. Consequently, be careful what you equals for. For numeric
values, using this should have the equivalent result of setting Min()
and Max() with the same value.
The returned boolean indicates if the validation is present from the
source. A string is returned containing the raw value from the source.
No guarantees are made about the parsability of the returned value in
the case of numeric and plural types.
*/
Equals() (string, bool)
/*
Validator Package Documentation:
For strings & numbers, this will ensure that the value is not equal
to the parameter given. For slices, arrays, and maps, validates the
number of items.
There doesn't appear to be a JSON schema equivalent to NotEqual(). I
don't know of a way to emulate the behavior without a complex boolean
expression. Therefore, this method will not be implemented.
*/
// NotEqual() string
/*
Validator Package Documentation:
For numbers, this will ensure that the value is greater than the
parameter given. For strings, it checks that the string length is
greater than that number of characters. For slices, arrays and maps
it validates the number of items.
JSON Schema Validation RFC:
6.5. exclusiveMinimum
The value of "exclusiveMinimum" MUST be number, representing an exclusive upper limit for a numeric instance.
If the instance is a number, then the instance is valid only if it has a value strictly greater than (not equal to) "exclusiveMinimum".
6.7. minLength
The value of this keyword MUST be a non-negative integer.
A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.
The length of a string instance is defined as the number of its characters as defined by RFC 7159 [RFC7159].
Omitting this keyword has the same behavior as a value of 0.
6.12. minItems
The value of this keyword MUST be a non-negative integer.
An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword.
Omitting this keyword has the same behavior as a value of 0.
Returns -1 when this validation is not enforced.
*/
GreaterThan() float64
/*
Validator Package Documentation:
For numbers, this will ensure that the value is less than the
parameter given. For strings, it checks that the string length is
less than that number of characters. For slices, arrays, and maps it
validates the number of items.
JSON Schema Validation RFC:
6.3. exclusiveMaximum
The value of "exclusiveMaximum" MUST be number, representing an exclusive upper limit for a numeric instance.
If the instance is a number, then the instance is valid only if it has a value strictly less than (not equal to) "exclusiveMaximum".
6.6. maxLength
The value of this keyword MUST be a non-negative integer.
A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.
The length of a string instance is defined as the number of its characters as defined by RFC 7159 [RFC7159].
6.11. maxItems
The value of this keyword MUST be a non-negative integer.
An array instance is valid against "maxItems" if its size is less than, or equal to, the value of this keyword.
Returns -1 when this validation is not enforced.
*/
LessThan() float64
// The following are redundant, and their equivalent expressions in the
// Validator package will be interpreted and returned with the Min() and
// Max() accessors.
//
// GreaterThanOrEqual() string // Same as 'min' above. Kept both to make terminology with 'len' easier.
// LessThanOrEqual() string // Same as 'max' above. Kept both to make terminology with 'len' easier.
}