-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.go
More file actions
64 lines (52 loc) · 1.47 KB
/
basic.go
File metadata and controls
64 lines (52 loc) · 1.47 KB
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
package validate
import "reflect"
// NotNil will return an error if value is nil.
func NotNil[T any](value T) error {
// Do a reflect check to see if the value is nil.
vof := reflect.ValueOf(value)
switch vof.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
if vof.IsNil() {
return &Violation{Code: CodeNotNil}
}
}
return nil
}
// Not will validate that the value is not the given value.
func Not[T comparable](not T) Validator[T] {
return func(value T) error {
if value == not {
return &Violation{Code: CodeNot, Args: Args{"not": not}}
}
return nil
}
}
// Required will validate that the value is not the zero value for the type.
func Required[T comparable](value T) error {
var x T // Create the nullable value for the type
if value == x {
return &Violation{Code: CodeRequired}
}
return nil
}
// Equal will validate that the value is equal to the expected value.
// This will not do a deep comparison.
func Equal[T comparable](expected T) Validator[T] {
return func(value T) error {
if value != expected {
return &Violation{Code: CodeEqual, Args: Args{"expected": expected}}
}
return nil
}
}
// OneOf will validate that the value is one of the accepted values.
func OneOf[T comparable](accepted ...T) Validator[T] {
return func(value T) error {
for _, a := range accepted {
if value == a {
return nil
}
}
return &Violation{Code: CodeOneOf, Args: Args{"accepted": accepted}}
}
}