-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
113 lines (93 loc) · 4.73 KB
/
validator.go
File metadata and controls
113 lines (93 loc) · 4.73 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
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
package vogen
import "fmt"
// Validator represents a validator.
type Validator interface {
write(vo *Vogen, structName string, field Field)
}
// PositiveValueValidator is a Positive Value Validator.
type PositiveValueValidator struct{}
// NewPositiveValueValidator returns a new Positive Value Validator.
func NewPositiveValueValidator() Validator {
return &PositiveValueValidator{}
}
// write writes the Positive Value Validator to the code.
func (pvv *PositiveValueValidator) write(vo *Vogen, structName string, field Field) {
vo.code = append(vo.code, "\tif o."+field.lowerCamelCase()+" < 0 {\n")
vo.code = append(vo.code, "\t\treturn fmt.Errorf(\"struct '"+structName+"' field '"+field.Name+"' value is negative: %d\", "+field.lowerCamelCase()+")\n")
vo.code = append(vo.code, "\t}\n")
}
// NegativeValueValidator is a Negative Value Validator.
type NegativeValueValidator struct{}
// NewNegativeValueValidator returns a new Negative Value Validator.
// NegativeValueValidator checks if the value is negative.
func NewNegativeValueValidator() Validator {
return &NegativeValueValidator{}
}
// write writes the Negative Value Validator to the code.
func (nvv *NegativeValueValidator) write(vo *Vogen, structName string, field Field) {
vo.code = append(vo.code, "\tif o."+field.lowerCamelCase()+" >= 0 {\n")
vo.code = append(vo.code, "\t\treturn fmt.Errorf(\"struct '"+structName+"' field '"+field.Name+"' value is positive: %d\", "+field.lowerCamelCase()+")\n")
vo.code = append(vo.code, "\t}\n")
}
// MaxValueValidator is a Max Value Validator.
type MaxValueValidator struct {
maxValue int
}
// NewMaxValueValidator returns a new Max Value Validator.
// MaxValueValidator checks if the value exceeds the maximum value.
func NewMaxValueValidator(m int) Validator {
return &MaxValueValidator{maxValue: m}
}
// write writes the Max Value Validator to the code.
func (mvv *MaxValueValidator) write(vo *Vogen, structName string, field Field) {
vo.code = append(vo.code, "\tif o."+field.lowerCamelCase()+" > "+fmt.Sprintf("%d", mvv.maxValue)+" {\n")
vo.code = append(vo.code, "\t\treturn fmt.Errorf(\"struct '"+structName+"' field '"+field.Name+"' value exceeds the maximum value: %d\", "+field.lowerCamelCase()+")\n")
vo.code = append(vo.code, "\t}\n")
}
// MinValueValidator is a Min Value Validator.
type MinValueValidator struct {
minValue int
}
// NewMinValueValidator returns a new Min Value Validator.
// MinValueValidator checks if the value is less than the minimum value.
func NewMinValueValidator(m int) Validator {
return &MinValueValidator{minValue: m}
}
// write writes the Min Value Validator to the code.
func (miv *MinValueValidator) write(vo *Vogen, structName string, field Field) {
vo.code = append(vo.code, "\tif o."+field.lowerCamelCase()+" < "+fmt.Sprintf("%d", miv.minValue)+" {\n")
vo.code = append(vo.code, "\t\treturn fmt.Errorf(\"struct '"+structName+"' field '"+field.Name+"' value is less than the minimum value: %d\", "+field.lowerCamelCase()+")\n")
vo.code = append(vo.code, "\t}\n")
}
// RangeValueValidator is a Range Value Validator.
type RangeValueValidator struct {
minValue int
maxValue int
}
// NewRangeValueValidator returns a new Range Value Validator.
// RangeValueValidator checks if the value is out of range.
func NewRangeValueValidator(minV, maxV int) Validator {
return &RangeValueValidator{minValue: minV, maxValue: maxV}
}
// write writes the Range Value Validator to the code.
func (rvv *RangeValueValidator) write(vo *Vogen, structName string, field Field) {
vo.code = append(vo.code, "\tif o."+field.lowerCamelCase()+" < "+fmt.Sprintf("%d", rvv.minValue)+" || o."+field.lowerCamelCase()+" > "+fmt.Sprintf("%d", rvv.maxValue)+" {\n")
vo.code = append(vo.code, "\t\treturn fmt.Errorf(\"struct '"+structName+"' field '"+field.Name+"' value is out of range: %d\", "+field.lowerCamelCase()+")\n")
vo.code = append(vo.code, "\t}\n")
}
// StringLengthValidator is a String Length Validator.
type StringLengthValidator struct {
minLength int
maxLength int
}
// NewStringLengthValidator returns a new String Length Validator.
// StringLengthValidator checks if the string length is out of range.
func NewStringLengthValidator(minL, maxL int) Validator {
return &StringLengthValidator{minLength: minL, maxLength: maxL}
}
// write writes the String Length Validator to the code.
func (slv *StringLengthValidator) write(vo *Vogen, structName string, field Field) {
vo.code = append(vo.code, "\tif len(o."+field.lowerCamelCase()+") < "+fmt.Sprintf("%d", slv.minLength)+" || len(o."+field.lowerCamelCase()+") > "+fmt.Sprintf("%d", slv.maxLength)+" {\n")
vo.code = append(vo.code, "\t\treturn fmt.Errorf(\"struct '"+structName+"' field '"+field.Name+"' length is out of range: %d\", len(o."+field.lowerCamelCase()+"))\n")
vo.code = append(vo.code, "\t}\n")
}