/
file.go
156 lines (125 loc) 路 4.76 KB
/
file.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
package validation
import (
"strconv"
"goyave.dev/goyave/v5/util/fsutil"
)
// FileValidator validates the field under validation must be a file.
// Multi-files are supported.
type FileValidator struct {
BaseValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *FileValidator) Validate(ctx *Context) bool {
_, ok := ctx.Value.([]fsutil.File)
return ok
}
// Name returns the string name of the validator.
func (v *FileValidator) Name() string { return "file" }
// IsType returns true.
func (v *FileValidator) IsType() bool { return true }
// File the field under validation must be a file. Multi-files are supported.
func File() *FileValidator {
return &FileValidator{}
}
//------------------------------
// FileCountValidator validates the field under validation must be a multi-files
// with exactly the specified number of files.
type FileCountValidator struct {
BaseValidator
Count uint
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *FileCountValidator) Validate(ctx *Context) bool {
files, ok := ctx.Value.([]fsutil.File)
return ok && uint(len(files)) == v.Count
}
// Name returns the string name of the validator.
func (v *FileCountValidator) Name() string { return "file_count" }
// MessagePlaceholders returns the ":value" placeholder.
func (v *FileCountValidator) MessagePlaceholders(_ *Context) []string {
return []string{
":value", strconv.FormatUint(uint64(v.Count), 10),
}
}
// FileCount the field under validation must be a multi-files
// with exactly the specified number of files.
func FileCount(count uint) *FileCountValidator {
return &FileCountValidator{Count: count}
}
//------------------------------
// MinFileCountValidator validates the field under validation must be a multi-files
// with at least the specified number of files.
type MinFileCountValidator struct {
BaseValidator
Min uint
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *MinFileCountValidator) Validate(ctx *Context) bool {
files, ok := ctx.Value.([]fsutil.File)
return ok && uint(len(files)) >= v.Min
}
// Name returns the string name of the validator.
func (v *MinFileCountValidator) Name() string { return "min_file_count" }
// MessagePlaceholders returns the ":min" placeholder.
func (v *MinFileCountValidator) MessagePlaceholders(_ *Context) []string {
return []string{
":min", strconv.FormatUint(uint64(v.Min), 10),
}
}
// MinFileCount the field under validation must be a multi-files
// with at least the specified number of files.
func MinFileCount(min uint) *MinFileCountValidator {
return &MinFileCountValidator{Min: min}
}
//------------------------------
// MaxFileCountValidator validates the field under validation must be a multi-files
// with at most the specified number of files.
type MaxFileCountValidator struct {
BaseValidator
Max uint
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *MaxFileCountValidator) Validate(ctx *Context) bool {
files, ok := ctx.Value.([]fsutil.File)
return ok && uint(len(files)) <= v.Max
}
// Name returns the string name of the validator.
func (v *MaxFileCountValidator) Name() string { return "max_file_count" }
// MessagePlaceholders returns the ":max" placeholder.
func (v *MaxFileCountValidator) MessagePlaceholders(_ *Context) []string {
return []string{
":max", strconv.FormatUint(uint64(v.Max), 10),
}
}
// MaxFileCount the field under validation must be a multi-files
// with at most the specified number of files.
func MaxFileCount(max uint) *MaxFileCountValidator {
return &MaxFileCountValidator{Max: max}
}
//------------------------------
// FileCountBetweenValidator validates the field under validation must be a multi-files
// with a number of files between the specified min and max.
type FileCountBetweenValidator struct {
BaseValidator
Min uint
Max uint
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *FileCountBetweenValidator) Validate(ctx *Context) bool {
files, ok := ctx.Value.([]fsutil.File)
return ok && uint(len(files)) >= v.Min && uint(len(files)) <= v.Max
}
// Name returns the string name of the validator.
func (v *FileCountBetweenValidator) Name() string { return "file_count_between" }
// MessagePlaceholders returns the ":min" and ":max" placeholders.
func (v *FileCountBetweenValidator) MessagePlaceholders(_ *Context) []string {
return []string{
":min", strconv.FormatUint(uint64(v.Min), 10),
":max", strconv.FormatUint(uint64(v.Max), 10),
}
}
// FileCountBetween the field under validation must be a multi-files
// with a number of files between the specified min and max.
func FileCountBetween(min, max uint) *FileCountBetweenValidator {
return &FileCountBetweenValidator{Min: min, Max: max}
}