admin page updates
This commit is contained in:
105
internal/validation/intlist.go
Normal file
105
internal/validation/intlist.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// IntList represents a list of IntFields for validating multiple integer values
|
||||
type IntList struct {
|
||||
FieldBase
|
||||
Fields []*IntField
|
||||
}
|
||||
|
||||
// newIntList creates a new IntList from form/query values
|
||||
func newIntList(key string, g Getter) *IntList {
|
||||
items := g.GetList(key)
|
||||
list := &IntList{
|
||||
FieldBase: newField(key, g),
|
||||
Fields: make([]*IntField, 0, len(items)),
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
if item == "" {
|
||||
continue // Skip empty values
|
||||
}
|
||||
|
||||
var val int
|
||||
var err error
|
||||
val, err = strconv.Atoi(item)
|
||||
if err != nil {
|
||||
g.AddCheck(newFailedCheck(
|
||||
"Value is not a number",
|
||||
fmt.Sprintf("%s contains invalid integer: %s", key, item),
|
||||
))
|
||||
continue
|
||||
}
|
||||
|
||||
// Create an IntField directly with the value
|
||||
field := &IntField{
|
||||
Value: val,
|
||||
FieldBase: newField(key, g),
|
||||
}
|
||||
list.Fields = append(list.Fields, field)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// Values returns all int values in the list
|
||||
func (l *IntList) Values() []int {
|
||||
values := make([]int, len(l.Fields))
|
||||
for i, field := range l.Fields {
|
||||
values[i] = field.Value
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// Required enforces at least one value in the list
|
||||
func (l *IntList) Required() *IntList {
|
||||
if len(l.Fields) == 0 {
|
||||
l.getter.AddCheck(newFailedCheck(
|
||||
"Field not provided",
|
||||
fmt.Sprintf("%s is required", l.Key),
|
||||
))
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// MinItems enforces a minimum number of items in the list
|
||||
func (l *IntList) MinItems(min int) *IntList {
|
||||
if len(l.Fields) < min {
|
||||
l.getter.AddCheck(newFailedCheck(
|
||||
"Too few items",
|
||||
fmt.Sprintf("%s requires at least %d item(s)", l.Key, min),
|
||||
))
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// MaxItems enforces a maximum number of items in the list
|
||||
func (l *IntList) MaxItems(max int) *IntList {
|
||||
if len(l.Fields) > max {
|
||||
l.getter.AddCheck(newFailedCheck(
|
||||
"Too many items",
|
||||
fmt.Sprintf("%s allows at most %d item(s)", l.Key, max),
|
||||
))
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// Max enforces a maximum value for each item
|
||||
func (l *IntList) Max(max int) *IntList {
|
||||
for _, field := range l.Fields {
|
||||
field.Max(max)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// Min enforces a minimum value for each item
|
||||
func (l *IntList) Min(min int) *IntList {
|
||||
for _, field := range l.Fields {
|
||||
field.Min(min)
|
||||
}
|
||||
return l
|
||||
}
|
||||
Reference in New Issue
Block a user