-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidation_error.go
More file actions
43 lines (36 loc) · 938 Bytes
/
validation_error.go
File metadata and controls
43 lines (36 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package hvalid
import (
"fmt"
"strings"
)
// GetDefaultErrorMsg 获取默认错误信息
func GetDefaultErrorMsg(fieldName string, rule string) string {
return fmt.Sprintf("%s %s", fieldName, rule)
}
// ValidationError 表示验证错误
type ValidationError struct {
Field string // 字段名称
Errors []string // 错误信息列表
}
// Error 实现 error 接口
func (e *ValidationError) Error() string {
if len(e.Errors) == 0 {
return ""
}
return fmt.Sprintf("%s: %s", e.Field, strings.Join(e.Errors, "; "))
}
// NewValidationError 创建新的验证错误
func NewValidationError(field string) *ValidationError {
return &ValidationError{
Field: field,
Errors: make([]string, 0),
}
}
// AddError 添加错误信息
func (e *ValidationError) AddError(err string) {
e.Errors = append(e.Errors, err)
}
// HasError 检查是否有错误
func (e *ValidationError) HasError() bool {
return len(e.Errors) > 0
}