admin page updates
This commit is contained in:
124
internal/validation/stringlist.go
Normal file
124
internal/validation/stringlist.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// StringList represents a list of StringFields for validating multiple string values
|
||||
type StringList struct {
|
||||
FieldBase
|
||||
Fields []*StringField
|
||||
}
|
||||
|
||||
// newStringList creates a new StringList from form/query values
|
||||
func newStringList(key string, g Getter) *StringList {
|
||||
items := g.GetList(key)
|
||||
list := &StringList{
|
||||
FieldBase: newField(key, g),
|
||||
Fields: make([]*StringField, 0, len(items)),
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
if item == "" {
|
||||
continue // Skip empty values
|
||||
}
|
||||
// Create a StringField directly with the value
|
||||
field := &StringField{
|
||||
Value: item,
|
||||
FieldBase: newField(key, g),
|
||||
}
|
||||
list.Fields = append(list.Fields, field)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// Values returns all string values in the list
|
||||
func (l *StringList) Values() []string {
|
||||
values := make([]string, len(l.Fields))
|
||||
for i, field := range l.Fields {
|
||||
values[i] = field.Value
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// Required enforces at least one non-empty value in the list
|
||||
func (l *StringList) Required() *StringList {
|
||||
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 *StringList) MinItems(min int) *StringList {
|
||||
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 *StringList) MaxItems(max int) *StringList {
|
||||
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
|
||||
}
|
||||
|
||||
// MaxLength enforces a maximum string length for each item
|
||||
func (l *StringList) MaxLength(length int) *StringList {
|
||||
for _, field := range l.Fields {
|
||||
field.MaxLength(length)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// MinLength enforces a minimum string length for each item
|
||||
func (l *StringList) MinLength(length int) *StringList {
|
||||
for _, field := range l.Fields {
|
||||
field.MinLength(length)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// AllowedValues enforces each item must be in the allowed list
|
||||
func (l *StringList) AllowedValues(allowed []string) *StringList {
|
||||
for _, field := range l.Fields {
|
||||
field.AllowedValues(allowed)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// ToUpper transforms all strings to uppercase
|
||||
func (l *StringList) ToUpper() *StringList {
|
||||
for _, field := range l.Fields {
|
||||
field.ToUpper()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// ToLower transforms all strings to lowercase
|
||||
func (l *StringList) ToLower() *StringList {
|
||||
for _, field := range l.Fields {
|
||||
field.ToLower()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// TrimSpace removes leading and trailing whitespace from all items
|
||||
func (l *StringList) TrimSpace() *StringList {
|
||||
for _, field := range l.Fields {
|
||||
field.TrimSpace()
|
||||
}
|
||||
return l
|
||||
}
|
||||
Reference in New Issue
Block a user