1
|
|
|
package validation |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"errors" |
5
|
|
|
"fmt" |
6
|
|
|
"strings" |
7
|
|
|
|
8
|
|
|
"github.com/muonsoft/validation/message" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
var ( |
12
|
|
|
ErrInvalidEAN13 = NewError("invalid EAN-13", message.InvalidEAN13) |
13
|
|
|
ErrInvalidEAN8 = NewError("invalid EAN-8", message.InvalidEAN8) |
14
|
|
|
ErrInvalidEmail = NewError("invalid email", message.InvalidEmail) |
15
|
|
|
ErrInvalidHostname = NewError("invalid hostname", message.InvalidHostname) |
16
|
|
|
ErrInvalidIP = NewError("invalid IP address", message.InvalidIP) |
17
|
|
|
ErrInvalidJSON = NewError("invalid JSON", message.InvalidJSON) |
18
|
|
|
ErrInvalidUPCA = NewError("invalid UPC-A", message.InvalidUPCA) |
19
|
|
|
ErrInvalidUPCE = NewError("invalid UPC-E", message.InvalidUPCE) |
20
|
|
|
ErrInvalidURL = NewError("invalid URL", message.InvalidURL) |
21
|
|
|
ErrIsBlank = NewError("is blank", message.IsBlank) |
22
|
1 |
|
ErrIsEqual = NewError("is equal", message.IsEqual) |
23
|
|
|
ErrIsNil = NewError("is nil", message.IsNil) |
24
|
|
|
ErrNoSuchChoice = NewError("no such choice", message.NoSuchChoice) |
25
|
|
|
ErrNotBlank = NewError("is not blank", message.NotBlank) |
26
|
|
|
ErrNotEqual = NewError("is not equal", message.NotEqual) |
27
|
|
|
ErrNotExactCount = NewError("not exact count", message.NotExactCount) |
28
|
|
|
ErrNotExactLength = NewError("not exact length", message.NotExactLength) |
29
|
|
|
ErrNotFalse = NewError("is not false", message.NotFalse) |
30
|
|
|
ErrNotInRange = NewError("is not in range", message.NotInRange) |
31
|
|
|
ErrNotInteger = NewError("is not an integer", message.NotInteger) |
32
|
|
|
ErrNotNegative = NewError("is not negative", message.NotNegative) |
33
|
|
|
ErrNotNegativeOrZero = NewError("is not negative or zero", message.NotNegativeOrZero) |
34
|
|
|
ErrNotNil = NewError("is not nil", message.NotNil) |
35
|
1 |
|
ErrNotNumeric = NewError("is not numeric", message.NotNumeric) |
36
|
|
|
ErrNotPositive = NewError("is not positive", message.NotPositive) |
37
|
|
|
ErrNotPositiveOrZero = NewError("is not positive or zero", message.NotPositiveOrZero) |
38
|
|
|
ErrNotTrue = NewError("is not true", message.NotTrue) |
39
|
|
|
ErrNotUnique = NewError("is not unique", message.NotUnique) |
40
|
|
|
ErrNotValid = NewError("is not valid", message.NotValid) |
41
|
|
|
ErrProhibitedIP = NewError("is prohibited IP", message.ProhibitedIP) |
42
|
|
|
ErrTooEarly = NewError("is too early", message.TooEarly) |
43
|
|
|
ErrTooEarlyOrEqual = NewError("is too early or equal", message.TooEarlyOrEqual) |
44
|
|
|
ErrTooFewElements = NewError("too few elements", message.TooFewElements) |
45
|
1 |
|
ErrTooHigh = NewError("is too high", message.TooHigh) |
46
|
|
|
ErrTooHighOrEqual = NewError("is too high or equal", message.TooHighOrEqual) |
47
|
|
|
ErrTooLate = NewError("is too late", message.TooLate) |
48
|
|
|
ErrTooLateOrEqual = NewError("is too late or equal", message.TooLateOrEqual) |
49
|
|
|
ErrTooLong = NewError("is too long", message.TooLong) |
50
|
|
|
ErrTooLow = NewError("is too low", message.TooLow) |
51
|
|
|
ErrTooLowOrEqual = NewError("is too low or equal", message.TooLowOrEqual) |
52
|
|
|
ErrTooManyElements = NewError("too many elements", message.TooManyElements) |
53
|
|
|
ErrTooShort = NewError("is too short", message.TooShort) |
54
|
|
|
) |
55
|
1 |
|
|
56
|
|
|
// Error is a base type for static validation error used as an underlying error for Violation. |
57
|
|
|
// It can be used to programmatically test for a specific violation. |
58
|
|
|
// Error code values are protected by backward compatibility rules, message values are not protected. |
59
|
|
|
type Error struct { |
60
|
|
|
code string |
61
|
|
|
message string |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// NewError creates a static validation error. It should be used to create only package-level errors. |
65
|
|
|
func NewError(code string, message string) *Error { |
66
|
|
|
return &Error{code: code, message: message} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Error returns error code. This code is protected by backward compatibility rules. |
70
|
|
|
func (err *Error) Error() string { return err.code } |
71
|
|
|
|
72
|
|
|
// Message returns message template that will be shown to the end user. |
73
|
|
|
// Be aware. This message is not protected by backward compatibility rules and may be changed even in patch versions. |
74
|
|
|
func (err *Error) Message() string { return err.message } |
75
|
|
|
|
76
|
|
|
// ConstraintError is used to return critical error from constraint that immediately |
77
|
|
|
// stops the validation process. It is recommended to use validator.CreateConstraintError() method |
78
|
|
|
// to initiate an error from current validation context. |
79
|
|
|
type ConstraintError struct { |
80
|
|
|
ConstraintName string |
81
|
|
|
Path *PropertyPath |
82
|
|
|
Description string |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
func (err *ConstraintError) Error() string { |
86
|
|
|
var s strings.Builder |
87
|
|
|
s.WriteString("failed to validate by " + err.ConstraintName) |
88
|
|
|
if err.Path != nil { |
89
|
|
|
s.WriteString(` at path "` + err.Path.String() + `"`) |
90
|
|
|
} |
91
|
|
|
s.WriteString(": " + err.Description) |
92
|
|
|
|
93
|
|
|
return s.String() |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// ConstraintAlreadyStoredError is returned when trying to put a constraint |
97
|
|
|
// in the validator store using an existing key. |
98
|
|
|
type ConstraintAlreadyStoredError struct { |
99
|
|
|
Key string |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
func (err *ConstraintAlreadyStoredError) Error() string { |
103
|
|
|
return fmt.Sprintf(`constraint with key "%s" already stored`, err.Key) |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// ConstraintNotFoundError is returned when trying to get a constraint |
107
|
|
|
// from the validator store using a non-existent key. |
108
|
|
|
type ConstraintNotFoundError struct { |
109
|
|
|
Key string |
110
|
|
|
Type string |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
func (err *ConstraintNotFoundError) Error() string { |
114
|
|
|
return fmt.Sprintf(`constraint by key "%s" of type "%s" is not found`, err.Key, err.Type) |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
var errTranslatorOptionsDenied = errors.New("translation options denied when using custom translator") |
118
|
|
|
|