forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-ozzo/ozzo-validation/v4/each.go

166 lines
2.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2016 Qiang Xue. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a MIT-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package validation
import (
"context"
"errors"
"reflect"
"strconv"
)
// Each returns a validation rule that loops through an iterable (map, slice or array)
2024-02-18 10:42:21 +00:00
// and validates each value inside with the provided rules.
2024-02-18 10:42:21 +00:00
// An empty iterable is considered valid. Use the Required rule to make sure the iterable is not empty.
2024-02-18 10:42:21 +00:00
func Each(rules ...Rule) EachRule {
2024-02-18 10:42:21 +00:00
return EachRule{
2024-02-18 10:42:21 +00:00
rules: rules,
}
2024-02-18 10:42:21 +00:00
}
// EachRule is a validation rule that validates elements in a map/slice/array using the specified list of rules.
2024-02-18 10:42:21 +00:00
type EachRule struct {
rules []Rule
}
// Validate loops through the given iterable and calls the Ozzo Validate() method for each value.
2024-02-18 10:42:21 +00:00
func (r EachRule) Validate(value interface{}) error {
2024-02-18 10:42:21 +00:00
return r.ValidateWithContext(nil, value)
2024-02-18 10:42:21 +00:00
}
// ValidateWithContext loops through the given iterable and calls the Ozzo ValidateWithContext() method for each value.
2024-02-18 10:42:21 +00:00
func (r EachRule) ValidateWithContext(ctx context.Context, value interface{}) error {
2024-02-18 10:42:21 +00:00
errs := Errors{}
v := reflect.ValueOf(value)
2024-02-18 10:42:21 +00:00
switch v.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
for _, k := range v.MapKeys() {
2024-02-18 10:42:21 +00:00
val := r.getInterface(v.MapIndex(k))
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if ctx == nil {
2024-02-18 10:42:21 +00:00
err = Validate(val, r.rules...)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
err = ValidateWithContext(ctx, val, r.rules...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
errs[r.getString(k)] = err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case reflect.Slice, reflect.Array:
2024-02-18 10:42:21 +00:00
for i := 0; i < v.Len(); i++ {
2024-02-18 10:42:21 +00:00
val := r.getInterface(v.Index(i))
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if ctx == nil {
2024-02-18 10:42:21 +00:00
err = Validate(val, r.rules...)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
err = ValidateWithContext(ctx, val, r.rules...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
errs[strconv.Itoa(i)] = err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return errors.New("must be an iterable (map, slice or array)")
2024-02-18 10:42:21 +00:00
}
if len(errs) > 0 {
2024-02-18 10:42:21 +00:00
return errs
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (r EachRule) getInterface(value reflect.Value) interface{} {
2024-02-18 10:42:21 +00:00
switch value.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Ptr, reflect.Interface:
2024-02-18 10:42:21 +00:00
if value.IsNil() {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return value.Elem().Interface()
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return value.Interface()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (r EachRule) getString(value reflect.Value) string {
2024-02-18 10:42:21 +00:00
switch value.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Ptr, reflect.Interface:
2024-02-18 10:42:21 +00:00
if value.IsNil() {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return value.Elem().String()
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return value.String()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}