Test Failed
Push — main ( 747293...5fe0dd )
by Igor
02:44
created

validation.*ConstraintAlreadyStoredError.Error   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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