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

289 lines
3.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 (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"text/template"
)
type (
2024-02-18 10:42:21 +00:00
// Error interface represents an validation error
2024-02-18 10:42:21 +00:00
Error interface {
Error() string
2024-02-18 10:42:21 +00:00
Code() string
2024-02-18 10:42:21 +00:00
Message() string
2024-02-18 10:42:21 +00:00
SetMessage(string) Error
2024-02-18 10:42:21 +00:00
Params() map[string]interface{}
2024-02-18 10:42:21 +00:00
SetParams(map[string]interface{}) Error
}
// ErrorObject is the default validation error
2024-02-18 10:42:21 +00:00
// that implements the Error interface.
2024-02-18 10:42:21 +00:00
ErrorObject struct {
code string
2024-02-18 10:42:21 +00:00
message string
params map[string]interface{}
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
// values are Error or Errors (for map, slice and array error value is Errors).
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
}
// SetCode set the error's translation code.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) SetCode(code string) Error {
2024-02-18 10:42:21 +00:00
e.code = code
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
// Code get the error's translation code.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) Code() string {
2024-02-18 10:42:21 +00:00
return e.code
2024-02-18 10:42:21 +00:00
}
// SetParams set the error's params.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) SetParams(params map[string]interface{}) Error {
2024-02-18 10:42:21 +00:00
e.params = params
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
// AddParam add parameter to the error's parameters.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) AddParam(name string, value interface{}) Error {
2024-02-18 10:42:21 +00:00
if e.params == nil {
2024-02-18 10:42:21 +00:00
e.params = make(map[string]interface{})
2024-02-18 10:42:21 +00:00
}
e.params[name] = value
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
// Params returns the error's params.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) Params() map[string]interface{} {
2024-02-18 10:42:21 +00:00
return e.params
2024-02-18 10:42:21 +00:00
}
// SetMessage set the error's message.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) SetMessage(message string) Error {
2024-02-18 10:42:21 +00:00
e.message = message
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
// Message return the error's message.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) Message() string {
2024-02-18 10:42:21 +00:00
return e.message
2024-02-18 10:42:21 +00:00
}
// Error returns the error message.
2024-02-18 10:42:21 +00:00
func (e ErrorObject) Error() string {
2024-02-18 10:42:21 +00:00
if len(e.params) == 0 {
2024-02-18 10:42:21 +00:00
return e.message
2024-02-18 10:42:21 +00:00
}
res := bytes.Buffer{}
2024-02-18 10:42:21 +00:00
_ = template.Must(template.New("err").Parse(e.message)).Execute(&res, e.params)
return res.String()
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 := make([]string, len(es))
2024-02-18 10:42:21 +00:00
i := 0
2024-02-18 10:42:21 +00:00
for key := range es {
2024-02-18 10:42:21 +00:00
keys[i] = key
2024-02-18 10:42:21 +00:00
i++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sort.Strings(keys)
var s strings.Builder
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.WriteString("; ")
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
_, _ = fmt.Fprintf(&s, "%v: (%v)", key, errs)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
_, _ = fmt.Fprintf(&s, "%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
s.WriteString(".")
2024-02-18 10:42:21 +00:00
return s.String()
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
}
// NewError create new validation error.
2024-02-18 10:42:21 +00:00
func NewError(code, message string) Error {
2024-02-18 10:42:21 +00:00
return ErrorObject{
code: code,
2024-02-18 10:42:21 +00:00
message: message,
}
2024-02-18 10:42:21 +00:00
}
// Assert that our ErrorObject implements the Error interface.
2024-02-18 10:42:21 +00:00
var _ Error = ErrorObject{}