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

216 lines
3.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 provides configurable and extensible rules for validating data of various types.
2024-02-18 10:42:21 +00:00
package validation
import (
"fmt"
"reflect"
"strconv"
)
type (
2024-02-18 10:42:21 +00:00
// Validatable is the interface indicating the type implementing it supports data validation.
2024-02-18 10:42:21 +00:00
Validatable interface {
2024-02-18 10:42:21 +00:00
// Validate validates the data and returns an error if validation fails.
2024-02-18 10:42:21 +00:00
Validate() error
}
// Rule represents a validation rule.
2024-02-18 10:42:21 +00:00
Rule interface {
2024-02-18 10:42:21 +00:00
// Validate validates a value and returns a value if validation fails.
2024-02-18 10:42:21 +00:00
Validate(value interface{}) error
}
// RuleFunc represents a validator function.
2024-02-18 10:42:21 +00:00
// You may wrap it as a Rule by calling By().
2024-02-18 10:42:21 +00:00
RuleFunc func(value interface{}) error
)
var (
2024-02-18 10:42:21 +00:00
// ErrorTag is the struct tag name used to customize the error field name for a struct field.
2024-02-18 10:42:21 +00:00
ErrorTag = "json"
// Skip is a special validation rule that indicates all rules following it should be skipped.
2024-02-18 10:42:21 +00:00
Skip = &skipRule{}
validatableType = reflect.TypeOf((*Validatable)(nil)).Elem()
)
// Validate validates the given value and returns the validation error, if any.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Validate performs validation using the following steps:
2024-02-18 10:42:21 +00:00
// - validate the value against the rules passed in as parameters
2024-02-18 10:42:21 +00:00
// - if the value is a map and the map values implement `Validatable`, call `Validate` of every map value
2024-02-18 10:42:21 +00:00
// - if the value is a slice or array whose values implement `Validatable`, call `Validate` of every element
2024-02-18 10:42:21 +00:00
func Validate(value interface{}, rules ...Rule) error {
2024-02-18 10:42:21 +00:00
for _, rule := range rules {
2024-02-18 10:42:21 +00:00
if _, ok := rule.(*skipRule); ok {
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
if err := rule.Validate(value); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
rv := reflect.ValueOf(value)
2024-02-18 10:42:21 +00:00
if (rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface) && rv.IsNil() {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
if v, ok := value.(Validatable); ok {
2024-02-18 10:42:21 +00:00
return v.Validate()
2024-02-18 10:42:21 +00:00
}
switch rv.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
if rv.Type().Elem().Implements(validatableType) {
2024-02-18 10:42:21 +00:00
return validateMap(rv)
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
if rv.Type().Elem().Implements(validatableType) {
2024-02-18 10:42:21 +00:00
return validateSlice(rv)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case reflect.Ptr, reflect.Interface:
2024-02-18 10:42:21 +00:00
return Validate(rv.Elem().Interface())
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
// validateMap validates a map of validatable elements
2024-02-18 10:42:21 +00:00
func validateMap(rv reflect.Value) error {
2024-02-18 10:42:21 +00:00
errs := Errors{}
2024-02-18 10:42:21 +00:00
for _, key := range rv.MapKeys() {
2024-02-18 10:42:21 +00:00
if mv := rv.MapIndex(key).Interface(); mv != nil {
2024-02-18 10:42:21 +00:00
if err := mv.(Validatable).Validate(); err != nil {
2024-02-18 10:42:21 +00:00
errs[fmt.Sprintf("%v", key.Interface())] = 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
}
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
}
// validateMap validates a slice/array of validatable elements
2024-02-18 10:42:21 +00:00
func validateSlice(rv reflect.Value) error {
2024-02-18 10:42:21 +00:00
errs := Errors{}
2024-02-18 10:42:21 +00:00
l := rv.Len()
2024-02-18 10:42:21 +00:00
for i := 0; i < l; i++ {
2024-02-18 10:42:21 +00:00
if ev := rv.Index(i).Interface(); ev != nil {
2024-02-18 10:42:21 +00:00
if err := ev.(Validatable).Validate(); 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
}
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
}
type skipRule struct{}
func (r *skipRule) Validate(interface{}) error {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
type inlineRule struct {
f RuleFunc
}
func (r *inlineRule) Validate(value interface{}) error {
2024-02-18 10:42:21 +00:00
return r.f(value)
2024-02-18 10:42:21 +00:00
}
// By wraps a RuleFunc into a Rule.
2024-02-18 10:42:21 +00:00
func By(f RuleFunc) Rule {
2024-02-18 10:42:21 +00:00
return &inlineRule{f}
2024-02-18 10:42:21 +00:00
}