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

145 lines
1.9 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 (
"encoding/json"
"fmt"
"sort"
)
type (
2024-02-18 10:42:21 +00:00
// Errors represents the validation errors that are indexed by struct field names, map or slice keys.
2024-02-18 10:42:21 +00:00
Errors map[string]error
// InternalError represents an error that should NOT be treated as a validation error.
2024-02-18 10:42:21 +00:00
InternalError interface {
error
2024-02-18 10:42:21 +00:00
InternalError() error
}
internalError struct {
error
}
)
// NewInternalError wraps a given error into an InternalError.
2024-02-18 10:42:21 +00:00
func NewInternalError(err error) InternalError {
2024-02-18 10:42:21 +00:00
return &internalError{error: err}
2024-02-18 10:42:21 +00:00
}
// InternalError returns the actual error that it wraps around.
2024-02-18 10:42:21 +00:00
func (e *internalError) InternalError() error {
2024-02-18 10:42:21 +00:00
return e.error
2024-02-18 10:42:21 +00:00
}
// Error returns the error string of Errors.
2024-02-18 10:42:21 +00:00
func (es Errors) Error() string {
2024-02-18 10:42:21 +00:00
if len(es) == 0 {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
keys := []string{}
2024-02-18 10:42:21 +00:00
for key := range es {
2024-02-18 10:42:21 +00:00
keys = append(keys, key)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sort.Strings(keys)
s := ""
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
if i > 0 {
2024-02-18 10:42:21 +00:00
s += "; "
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if errs, ok := es[key].(Errors); ok {
2024-02-18 10:42:21 +00:00
s += fmt.Sprintf("%v: (%v)", key, errs)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s += fmt.Sprintf("%v: %v", key, es[key].Error())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return s + "."
2024-02-18 10:42:21 +00:00
}
// MarshalJSON converts the Errors into a valid JSON.
2024-02-18 10:42:21 +00:00
func (es Errors) MarshalJSON() ([]byte, error) {
2024-02-18 10:42:21 +00:00
errs := map[string]interface{}{}
2024-02-18 10:42:21 +00:00
for key, err := range es {
2024-02-18 10:42:21 +00:00
if ms, ok := err.(json.Marshaler); ok {
2024-02-18 10:42:21 +00:00
errs[key] = ms
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
errs[key] = err.Error()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return json.Marshal(errs)
2024-02-18 10:42:21 +00:00
}
// Filter removes all nils from Errors and returns back the updated Errors as an error.
2024-02-18 10:42:21 +00:00
// If the length of Errors becomes 0, it will return nil.
2024-02-18 10:42:21 +00:00
func (es Errors) Filter() error {
2024-02-18 10:42:21 +00:00
for key, value := range es {
2024-02-18 10:42:21 +00:00
if value == nil {
2024-02-18 10:42:21 +00:00
delete(es, key)
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(es) == 0 {
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 es
2024-02-18 10:42:21 +00:00
}