|
1 | 1 | package errors
|
2 | 2 |
|
3 |
| -import "fmt" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
4 | 7 |
|
5 | 8 | const (
|
6 |
| - invalidType = `%s is an invalid type name` |
7 |
| - typeFail = `%s in %s must be of type %s` |
8 |
| - typeFailWithData = `%s in %s must be of type %s: %q` |
9 |
| - typeFailWithError = `%s in %s must be of type %s, because: %s` |
10 |
| - requiredFail = `%s in %s is required` |
11 |
| - tooLongMessage = `%s in %s should be at most %d chars long` |
12 |
| - tooShortMessage = `%s in %s should be at least %d chars long` |
13 |
| - patternFail = `%s in %s should match '%s'` |
14 |
| - enumFail = `%s in %s should be one of %v` |
15 |
| - mulitpleOfFail = `%s in %s should be a multiple of %v` |
16 |
| - maxIncFail = `%s in %s should be less than or equal to %v` |
17 |
| - maxExcFail = `%s in %s should be less than %v` |
18 |
| - minIncFail = `%s in %s should be greater than or equal to %v` |
19 |
| - minExcFail = `%s in %s should be greater than %v` |
20 |
| - uniqueFail = `%s in %s shouldn't contain duplicates` |
21 |
| - maxItemsFail = `%s in %s should have at most %d items` |
22 |
| - minItemsFail = `%s in %s should have at least %d items` |
23 |
| - typeFailNoIn = `%s must be of type %s` |
24 |
| - typeFailWithDataNoIn = `%s must be of type %s: %q` |
25 |
| - typeFailWithErrorNoIn = `%s must be of type %s, because: %s` |
26 |
| - requiredFailNoIn = `%s is required` |
27 |
| - tooLongMessageNoIn = `%s should be at most %d chars long` |
28 |
| - tooShortMessageNoIn = `%s should be at least %d chars long` |
29 |
| - patternFailNoIn = `%s should match '%s'` |
30 |
| - enumFailNoIn = `%s should be one of %v` |
31 |
| - mulitpleOfFailNoIn = `%s should be a multiple of %v` |
32 |
| - maxIncFailNoIn = `%s should be less than or equal to %v` |
33 |
| - maxExcFailNoIn = `%s should be less than %v` |
34 |
| - minIncFailNoIn = `%s should be greater than or equal to %v` |
35 |
| - minExcFailNoIn = `%s should be greater than %v` |
36 |
| - uniqueFailNoIn = `%s shouldn't contain duplicates` |
37 |
| - maxItemsFailNoIn = `%s should have at most %d items` |
38 |
| - minItemsFailNoIn = `%s should have at least %d items` |
39 |
| - noAdditionalItems = "%s in %s can't have additional items" |
40 |
| - noAdditionalItemsNoIn = "%s can't have additional items" |
| 9 | + invalidType = "%s is an invalid type name" |
| 10 | + typeFail = "%s in %s must be of type %s" |
| 11 | + typeFailWithData = "%s in %s must be of type %s: %q" |
| 12 | + typeFailWithError = "%s in %s must be of type %s, because: %s" |
| 13 | + requiredFail = "%s in %s is required" |
| 14 | + tooLongMessage = "%s in %s should be at most %d chars long" |
| 15 | + tooShortMessage = "%s in %s should be at least %d chars long" |
| 16 | + patternFail = "%s in %s should match '%s'" |
| 17 | + enumFail = "%s in %s should be one of %v" |
| 18 | + mulitpleOfFail = "%s in %s should be a multiple of %v" |
| 19 | + maxIncFail = "%s in %s should be less than or equal to %v" |
| 20 | + maxExcFail = "%s in %s should be less than %v" |
| 21 | + minIncFail = "%s in %s should be greater than or equal to %v" |
| 22 | + minExcFail = "%s in %s should be greater than %v" |
| 23 | + uniqueFail = "%s in %s shouldn't contain duplicates" |
| 24 | + maxItemsFail = "%s in %s should have at most %d items" |
| 25 | + minItemsFail = "%s in %s should have at least %d items" |
| 26 | + typeFailNoIn = "%s must be of type %s" |
| 27 | + typeFailWithDataNoIn = "%s must be of type %s: %q" |
| 28 | + typeFailWithErrorNoIn = "%s must be of type %s, because: %s" |
| 29 | + requiredFailNoIn = "%s is required" |
| 30 | + tooLongMessageNoIn = "%s should be at most %d chars long" |
| 31 | + tooShortMessageNoIn = "%s should be at least %d chars long" |
| 32 | + patternFailNoIn = "%s should match '%s'" |
| 33 | + enumFailNoIn = "%s should be one of %v" |
| 34 | + mulitpleOfFailNoIn = "%s should be a multiple of %v" |
| 35 | + maxIncFailNoIn = "%s should be less than or equal to %v" |
| 36 | + maxExcFailNoIn = "%s should be less than %v" |
| 37 | + minIncFailNoIn = "%s should be greater than or equal to %v" |
| 38 | + minExcFailNoIn = "%s should be greater than %v" |
| 39 | + uniqueFailNoIn = "%s shouldn't contain duplicates" |
| 40 | + maxItemsFailNoIn = "%s should have at most %d items" |
| 41 | + minItemsFailNoIn = "%s should have at least %d items" |
| 42 | + noAdditionalItems = "%s in %s can't have additional items" |
| 43 | + noAdditionalItemsNoIn = "%s can't have additional items" |
| 44 | + tooFewProperties = "%s in %s should have at least %d properties" |
| 45 | + tooFewPropertiesNoIn = "%s should have at least %d properties" |
| 46 | + tooManyProperties = "%s in %s should have at most %d properties" |
| 47 | + tooManyPropertiesNoIn = "%s should have at most %d properties" |
| 48 | + unallowedProperty = "%s.%s in %s is a forbidden property" |
| 49 | + unallowedPropertyNoIn = "%s.%s is a forbidden property" |
| 50 | + failedAllPatternProps = "%s.%s in %s failed all pattern properties" |
| 51 | + failedAllPatternPropsNoIn = "%s.%s failed all pattern properties" |
41 | 52 | )
|
42 | 53 |
|
| 54 | +// CompositeError is an error that groups several errors together |
| 55 | +type CompositeError struct { |
| 56 | + Errors []error |
| 57 | + code int32 |
| 58 | + message string |
| 59 | +} |
| 60 | + |
| 61 | +func (c *CompositeError) Code() int32 { |
| 62 | + return c.code |
| 63 | +} |
| 64 | + |
| 65 | +func (c *CompositeError) Error() string { |
| 66 | + if len(c.Errors) > 0 { |
| 67 | + msgs := []string{c.message + ":"} |
| 68 | + for _, e := range c.Errors { |
| 69 | + msgs = append(msgs, e.Error()) |
| 70 | + } |
| 71 | + return strings.Join(msgs, "\n") |
| 72 | + } |
| 73 | + return c.message |
| 74 | +} |
| 75 | + |
43 | 76 | // CompositeValidationError an error to wrap a bunch of other errors
|
44 |
| -func CompositeValidationError(errors ...Error) *Validation { |
45 |
| - return &Validation{ |
| 77 | +func CompositeValidationError(errors ...error) *CompositeError { |
| 78 | + return &CompositeError{ |
46 | 79 | code: 422,
|
47 |
| - Value: append([]Error{}, errors...), |
| 80 | + Errors: append([]error{}, errors...), |
48 | 81 | message: "validation failure list",
|
49 | 82 | }
|
50 | 83 | }
|
51 | 84 |
|
| 85 | +// FailedAllPatternProperties an error for when the property doesn't match a pattern |
| 86 | +func FailedAllPatternProperties(name, in, key string) *Validation { |
| 87 | + msg := fmt.Sprintf(failedAllPatternProps, name, key, in) |
| 88 | + if in == "" { |
| 89 | + msg = fmt.Sprintf(failedAllPatternPropsNoIn, name, key) |
| 90 | + } |
| 91 | + return &Validation{ |
| 92 | + code: 422, |
| 93 | + Name: name, |
| 94 | + In: in, |
| 95 | + Value: key, |
| 96 | + message: msg, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// PropertyNotAllowed an error for when the property doesn't match a pattern |
| 101 | +func PropertyNotAllowed(name, in, key string) *Validation { |
| 102 | + msg := fmt.Sprintf(unallowedProperty, name, key, in) |
| 103 | + if in == "" { |
| 104 | + msg = fmt.Sprintf(unallowedPropertyNoIn, name, key) |
| 105 | + } |
| 106 | + return &Validation{ |
| 107 | + code: 422, |
| 108 | + Name: name, |
| 109 | + In: in, |
| 110 | + Value: key, |
| 111 | + message: msg, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// TooFewProperties an error for an object with too few properties |
| 116 | +func TooFewProperties(name, in string, n int64) *Validation { |
| 117 | + msg := fmt.Sprintf(tooFewProperties, name, in, n) |
| 118 | + if in == "" { |
| 119 | + msg = fmt.Sprintf(tooFewPropertiesNoIn, name, n) |
| 120 | + } |
| 121 | + return &Validation{ |
| 122 | + code: 422, |
| 123 | + Name: name, |
| 124 | + In: in, |
| 125 | + Value: n, |
| 126 | + message: msg, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// TooManyProperties an error for an object with too many properties |
| 131 | +func TooManyProperties(name, in string, n int64) *Validation { |
| 132 | + msg := fmt.Sprintf(tooManyProperties, name, in, n) |
| 133 | + if in == "" { |
| 134 | + msg = fmt.Sprintf(tooManyPropertiesNoIn, name, n) |
| 135 | + } |
| 136 | + return &Validation{ |
| 137 | + code: 422, |
| 138 | + Name: name, |
| 139 | + In: in, |
| 140 | + Value: n, |
| 141 | + message: msg, |
| 142 | + } |
| 143 | +} |
| 144 | + |
52 | 145 | // AdditionalItemsNotAllowed an error for invalid additional items
|
53 | 146 | func AdditionalItemsNotAllowed(name, in string) *Validation {
|
54 | 147 | msg := fmt.Sprintf(noAdditionalItems, name, in)
|
|
0 commit comments