1
|
|
|
package validation |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"errors" |
5
|
|
|
"fmt" |
6
|
|
|
"reflect" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// InapplicableConstraintError occurs when trying to use constraint on not applicable values. |
10
|
|
|
// For example, if you are trying to compare slice with a number. |
11
|
|
|
type InapplicableConstraintError struct { |
12
|
|
|
Constraint Constraint |
13
|
|
|
ValueType string |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
func (err InapplicableConstraintError) Error() string { |
17
|
|
|
return fmt.Sprintf("%s cannot be applied to %s", err.Constraint.Name(), err.ValueType) |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// NewInapplicableConstraintError helps to create a error on trying to use constraint on not applicable values. |
21
|
|
|
func NewInapplicableConstraintError(constraint Constraint, valueType string) InapplicableConstraintError { |
22
|
1 |
|
return InapplicableConstraintError{ |
23
|
|
|
Constraint: constraint, |
24
|
|
|
ValueType: valueType, |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// NotValidatableError occurs when validator cannot determine type by reflection or it is not supported |
29
|
|
|
// by validator. |
30
|
|
|
type NotValidatableError struct { |
31
|
|
|
Value reflect.Value |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func (err NotValidatableError) Error() string { |
35
|
1 |
|
return fmt.Sprintf("value of type %v is not validatable", err.Value.Kind()) |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// ConstraintAlreadyStoredError is returned when trying to put a constraint |
39
|
|
|
// in the validator store using an existing key. |
40
|
|
|
type ConstraintAlreadyStoredError struct { |
41
|
|
|
Key string |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func (err ConstraintAlreadyStoredError) Error() string { |
45
|
1 |
|
return fmt.Sprintf(`constraint with key "%s" already stored`, err.Key) |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// ConstraintNotFoundError is returned when trying to get a constraint |
49
|
|
|
// from the validator store using a non-existent key. |
50
|
|
|
type ConstraintNotFoundError struct { |
51
|
|
|
Key string |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func (err ConstraintNotFoundError) Error() string { |
55
|
1 |
|
return fmt.Sprintf(`constraint with key "%s" is not stored in the validator`, err.Key) |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
var errDefaultLanguageNotLoaded = errors.New("default language is not loaded") |
59
|
|
|
|