-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
133 lines (119 loc) · 7.25 KB
/
errors.go
File metadata and controls
133 lines (119 loc) · 7.25 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package validation
import (
"errors"
"fmt"
"strings"
"github.com/muonsoft/validation/message"
)
var (
ErrInvalidDate = NewError("invalid date", message.InvalidDate)
ErrInvalidDateTime = NewError("invalid datetime", message.InvalidDateTime)
ErrInvalidEAN13 = NewError("invalid EAN-13", message.InvalidEAN13)
ErrInvalidEAN8 = NewError("invalid EAN-8", message.InvalidEAN8)
ErrInvalidEmail = NewError("invalid email", message.InvalidEmail)
ErrInvalidHostname = NewError("invalid hostname", message.InvalidHostname)
ErrInvalidIBAN = NewError("invalid IBAN", message.InvalidIBAN)
ErrInvalidBIC = NewError("invalid BIC", message.InvalidBIC)
ErrBICIBANCountryMismatch = NewError("BIC IBAN country mismatch", message.BICNotAssociatedWithIBAN)
ErrInvalidISIN = NewError("invalid ISIN", message.InvalidISIN)
ErrInvalidISSN = NewError("invalid ISSN", message.InvalidISSN)
ErrInvalidISBN = NewError("invalid ISBN", message.InvalidISBN)
ErrInvalidISBN10 = NewError("invalid ISBN-10", message.InvalidISBN10)
ErrInvalidISBN13 = NewError("invalid ISBN-13", message.InvalidISBN13)
ErrInvalidCurrency = NewError("invalid currency", message.InvalidCurrency)
ErrInvalidCIDR = NewError("invalid CIDR", message.InvalidCIDR)
ErrCIDRNetmaskOutOfRange = NewError("CIDR netmask out of range", message.CIDRNetmaskOutOfRange)
ErrInvalidIP = NewError("invalid IP address", message.InvalidIP)
ErrInvalidJSON = NewError("invalid JSON", message.InvalidJSON)
ErrInvalidLUHN = NewError("invalid LUHN", message.InvalidLUHN)
ErrInvalidMAC = NewError("invalid MAC address", message.InvalidMAC)
ErrInvalidTime = NewError("invalid time", message.InvalidTime)
ErrInvalidULID = NewError("invalid ULID", message.InvalidULID)
ErrInvalidUPCA = NewError("invalid UPC-A", message.InvalidUPCA)
ErrInvalidUPCE = NewError("invalid UPC-E", message.InvalidUPCE)
ErrInvalidURL = NewError("invalid URL", message.InvalidURL)
ErrInvalidUUID = NewError("invalid UUID", message.InvalidUUID)
ErrIsBlank = NewError("is blank", message.IsBlank)
ErrIsEqual = NewError("is equal", message.IsEqual)
ErrIsNil = NewError("is nil", message.IsNil)
ErrNoSuchChoice = NewError("no such choice", message.NoSuchChoice)
ErrNotBlank = NewError("is not blank", message.NotBlank)
ErrNotDivisible = NewError("is not divisible", message.NotDivisible)
ErrNotDivisibleCount = NewError("not divisible count", message.NotDivisibleCount)
ErrNotEqual = NewError("is not equal", message.NotEqual)
ErrNotExactCount = NewError("not exact count", message.NotExactCount)
ErrNotExactLength = NewError("not exact length", message.NotExactLength)
ErrNotFalse = NewError("is not false", message.NotFalse)
ErrNotInRange = NewError("is not in range", message.NotInRange)
ErrNotInteger = NewError("is not an integer", message.NotInteger)
ErrNotNegative = NewError("is not negative", message.NotNegative)
ErrNotNegativeOrZero = NewError("is not negative or zero", message.NotNegativeOrZero)
ErrNotNil = NewError("is not nil", message.NotNil)
ErrNotNumeric = NewError("is not numeric", message.NotNumeric)
ErrNotPositive = NewError("is not positive", message.NotPositive)
ErrNotPositiveOrZero = NewError("is not positive or zero", message.NotPositiveOrZero)
ErrNotTrue = NewError("is not true", message.NotTrue)
ErrNotUnique = NewError("is not unique", message.NotUnique)
ErrNotValid = NewError("is not valid", message.NotValid)
ErrProhibitedIP = NewError("is prohibited IP", message.ProhibitedIP)
ErrProhibitedURL = NewError("is prohibited URL", message.ProhibitedURL)
ErrTooEarly = NewError("is too early", message.TooEarly)
ErrTooEarlyOrEqual = NewError("is too early or equal", message.TooEarlyOrEqual)
ErrTooFewElements = NewError("too few elements", message.TooFewElements)
ErrTooHigh = NewError("is too high", message.TooHigh)
ErrTooHighOrEqual = NewError("is too high or equal", message.TooHighOrEqual)
ErrTooLate = NewError("is too late", message.TooLate)
ErrTooLateOrEqual = NewError("is too late or equal", message.TooLateOrEqual)
ErrTooLong = NewError("is too long", message.TooLong)
ErrTooLow = NewError("is too low", message.TooLow)
ErrTooLowOrEqual = NewError("is too low or equal", message.TooLowOrEqual)
ErrTooManyElements = NewError("too many elements", message.TooManyElements)
ErrTooShort = NewError("is too short", message.TooShort)
ErrSuspiciousInvisible = NewError("suspicious invisible characters", message.SuspiciousInvisible)
ErrSuspiciousMixedNumbers = NewError("suspicious mixed numbers", message.SuspiciousMixedNumbers)
ErrSuspiciousHiddenOverlay = NewError("suspicious hidden overlay", message.SuspiciousHiddenOverlay)
ErrSuspiciousCharactersRestriction = NewError("suspicious characters restriction", message.SuspiciousCharactersRestriction)
)
// Error is a base type for static validation error used as an underlying error for [Violation].
// It can be used to programmatically test for a specific violation.
// Error code values are protected by backward compatibility rules, message values are not protected.
type Error struct {
code string
message string
}
// NewError creates a static validation error. It should be used to create only package-level errors.
func NewError(code string, message string) *Error {
return &Error{code: code, message: message}
}
// Error returns error code. This code is protected by backward compatibility rules.
func (err *Error) Error() string { return err.code }
// Message returns message template that will be shown to the end user.
// Be aware. This message is not protected by backward compatibility rules and may be changed even in patch versions.
func (err *Error) Message() string { return err.message }
// ConstraintError is used to return critical error from constraint that immediately
// stops the validation process. It is recommended to use [Validator.CreateConstraintError] method
// to initiate an error from current validation context.
type ConstraintError struct {
ConstraintName string
Path *PropertyPath
Description string
}
func (err *ConstraintError) Error() string {
var s strings.Builder
s.WriteString("validate by " + err.ConstraintName)
if err.Path != nil {
s.WriteString(` at path "` + err.Path.String() + `"`)
}
s.WriteString(": " + err.Description)
return s.String()
}
// ConstraintNotFoundError is returned when trying to get a constraint
// from the validator store using a non-existent key.
type ConstraintNotFoundError struct {
Key string
Type string
}
func (err *ConstraintNotFoundError) Error() string {
return fmt.Sprintf(`constraint by key "%s" of type "%s" is not found`, err.Key, err.Type)
}
var errTranslatorOptionsDenied = errors.New("translation options denied when using custom translator")