|
4 | 4 | package errors |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "fmt" |
8 | | - "github.com/santhosh-tekuri/jsonschema/v5" |
| 7 | + "fmt" |
| 8 | + "github.com/santhosh-tekuri/jsonschema/v5" |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | // SchemaValidationFailure is a wrapper around the jsonschema.ValidationError object, to provide a more |
12 | 12 | // user-friendly way to break down what went wrong. |
13 | 13 | type SchemaValidationFailure struct { |
14 | | - // Reason is a human-readable message describing the reason for the error. |
15 | | - Reason string `json:"reason,omitempty" yaml:"reason,omitempty"` |
| 14 | + // Reason is a human-readable message describing the reason for the error. |
| 15 | + Reason string `json:"reason,omitempty" yaml:"reason,omitempty"` |
16 | 16 |
|
17 | | - // Location is the XPath-like location of the validation failure |
18 | | - Location string `json:"location,omitempty" yaml:"location,omitempty"` |
| 17 | + // Location is the XPath-like location of the validation failure |
| 18 | + Location string `json:"location,omitempty" yaml:"location,omitempty"` |
19 | 19 |
|
20 | | - // Line is the line number where the violation occurred. This may a local line number |
21 | | - // if the validation is a schema (only schemas are validated locally, so the line number will be relative to |
22 | | - // the Context object held by the ValidationError object). |
23 | | - Line int `json:"line,omitempty" yaml:"line,omitempty"` |
| 20 | + // Line is the line number where the violation occurred. This may a local line number |
| 21 | + // if the validation is a schema (only schemas are validated locally, so the line number will be relative to |
| 22 | + // the Context object held by the ValidationError object). |
| 23 | + Line int `json:"line,omitempty" yaml:"line,omitempty"` |
24 | 24 |
|
25 | | - // Column is the column number where the violation occurred. This may a local column number |
26 | | - // if the validation is a schema (only schemas are validated locally, so the column number will be relative to |
27 | | - // the Context object held by the ValidationError object). |
28 | | - Column int `json:"column,omitempty" yaml:"column,omitempty"` |
| 25 | + // Column is the column number where the violation occurred. This may a local column number |
| 26 | + // if the validation is a schema (only schemas are validated locally, so the column number will be relative to |
| 27 | + // the Context object held by the ValidationError object). |
| 28 | + Column int `json:"column,omitempty" yaml:"column,omitempty"` |
29 | 29 |
|
30 | | - // The original error object, which is a jsonschema.ValidationError object. |
31 | | - OriginalError *jsonschema.ValidationError `json:"-" yaml:"-"` |
| 30 | + // The original error object, which is a jsonschema.ValidationError object. |
| 31 | + OriginalError *jsonschema.ValidationError `json:"-" yaml:"-"` |
32 | 32 | } |
33 | 33 |
|
34 | 34 | // Error returns a string representation of the error |
35 | 35 | func (s *SchemaValidationFailure) Error() string { |
36 | | - return fmt.Sprintf("Reason: %s, Location: %s", s.Reason, s.Location) |
| 36 | + return fmt.Sprintf("Reason: %s, Location: %s", s.Reason, s.Location) |
37 | 37 | } |
38 | 38 |
|
39 | 39 | // ValidationError is a struct that contains all the information about a validation error. |
40 | 40 | type ValidationError struct { |
41 | 41 |
|
42 | | - // Message is a human-readable message describing the error. |
43 | | - Message string `json:"message" yaml:"message"` |
| 42 | + // Message is a human-readable message describing the error. |
| 43 | + Message string `json:"message" yaml:"message"` |
44 | 44 |
|
45 | | - // Reason is a human-readable message describing the reason for the error. |
46 | | - Reason string `json:"reason" yaml:"reason"` |
| 45 | + // Reason is a human-readable message describing the reason for the error. |
| 46 | + Reason string `json:"reason" yaml:"reason"` |
47 | 47 |
|
48 | | - // ValidationType is a string that describes the type of validation that failed. |
49 | | - ValidationType string `json:"validationType" yaml:"validationType"` |
| 48 | + // ValidationType is a string that describes the type of validation that failed. |
| 49 | + ValidationType string `json:"validationType" yaml:"validationType"` |
50 | 50 |
|
51 | | - // ValidationSubType is a string that describes the subtype of validation that failed. |
52 | | - ValidationSubType string `json:"validationSubType" yaml:"validationSubType"` |
| 51 | + // ValidationSubType is a string that describes the subtype of validation that failed. |
| 52 | + ValidationSubType string `json:"validationSubType" yaml:"validationSubType"` |
53 | 53 |
|
54 | | - // SpecLine is the line number in the spec where the error occurred. |
55 | | - SpecLine int `json:"specLine" yaml:"specLine"` |
| 54 | + // SpecLine is the line number in the spec where the error occurred. |
| 55 | + SpecLine int `json:"specLine" yaml:"specLine"` |
56 | 56 |
|
57 | | - // SpecCol is the column number in the spec where the error occurred. |
58 | | - SpecCol int `json:"specColumn" yaml:"specColumn"` |
| 57 | + // SpecCol is the column number in the spec where the error occurred. |
| 58 | + SpecCol int `json:"specColumn" yaml:"specColumn"` |
59 | 59 |
|
60 | | - // HowToFix is a human-readable message describing how to fix the error. |
61 | | - HowToFix string `json:"howToFix" yaml:"howToFix"` |
| 60 | + // HowToFix is a human-readable message describing how to fix the error. |
| 61 | + HowToFix string `json:"howToFix" yaml:"howToFix"` |
62 | 62 |
|
63 | | - // SchemaValidationErrors is a slice of SchemaValidationFailure objects that describe the validation errors |
64 | | - // This is only populated whe the validation type is against a schema. |
65 | | - SchemaValidationErrors []*SchemaValidationFailure `json:"validationErrors,omitempty" yaml:"validationErrors,omitempty"` |
| 63 | + // SchemaValidationErrors is a slice of SchemaValidationFailure objects that describe the validation errors |
| 64 | + // This is only populated whe the validation type is against a schema. |
| 65 | + SchemaValidationErrors []*SchemaValidationFailure `json:"validationErrors,omitempty" yaml:"validationErrors,omitempty"` |
66 | 66 |
|
67 | | - // Context is the object that the validation error occurred on. This is usually a pointer to a schema |
68 | | - // or a parameter object. |
69 | | - Context interface{} `json:"-" yaml:"-"` |
| 67 | + // Context is the object that the validation error occurred on. This is usually a pointer to a schema |
| 68 | + // or a parameter object. |
| 69 | + Context interface{} `json:"-" yaml:"-"` |
70 | 70 | } |
71 | 71 |
|
72 | 72 | // Error returns a string representation of the error |
73 | 73 | func (v *ValidationError) Error() string { |
74 | | - if v.SchemaValidationErrors != nil { |
75 | | - return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s, Line: %d, Column: %d", |
76 | | - v.Message, v.Reason, v.SchemaValidationErrors, v.SpecLine, v.SpecCol) |
77 | | - } else { |
78 | | - return fmt.Sprintf("Error: %s, Reason: %s, Line: %d, Column: %d", |
79 | | - v.Message, v.Reason, v.SpecLine, v.SpecCol) |
80 | | - } |
| 74 | + if v.SchemaValidationErrors != nil { |
| 75 | + return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s, Line: %d, Column: %d", |
| 76 | + v.Message, v.Reason, v.SchemaValidationErrors, v.SpecLine, v.SpecCol) |
| 77 | + } else { |
| 78 | + return fmt.Sprintf("Error: %s, Reason: %s, Line: %d, Column: %d", |
| 79 | + v.Message, v.Reason, v.SpecLine, v.SpecCol) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// IsPathMissingError returns true if the error has a ValidationType of "path" and a ValidationSubType of "missing" |
| 84 | +func (v *ValidationError) IsPathMissingError() bool { |
| 85 | + return v.ValidationType == "path" && v.ValidationSubType == "missing" |
81 | 86 | } |
0 commit comments