|
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 | | - // DeepLocation is the path to the validation failure as exposed by the jsonschema library. |
21 | | - DeepLocation string `json:"deepLocation,omitempty" yaml:"deepLocation,omitempty"` |
| 20 | + // DeepLocation is the path to the validation failure as exposed by the jsonschema library. |
| 21 | + DeepLocation string `json:"deepLocation,omitempty" yaml:"deepLocation,omitempty"` |
22 | 22 |
|
23 | | - // AbsoluteLocation is the absolute path to the validation failure as exposed by the jsonschema library. |
24 | | - AbsoluteLocation string `json:"absoluteLocation,omitempty" yaml:"absoluteLocation,omitempty"` |
| 23 | + // AbsoluteLocation is the absolute path to the validation failure as exposed by the jsonschema library. |
| 24 | + AbsoluteLocation string `json:"absoluteLocation,omitempty" yaml:"absoluteLocation,omitempty"` |
25 | 25 |
|
26 | | - // Line is the line number where the violation occurred. This may a local line number |
27 | | - // if the validation is a schema (only schemas are validated locally, so the line number will be relative to |
28 | | - // the Context object held by the ValidationError object). |
29 | | - Line int `json:"line,omitempty" yaml:"line,omitempty"` |
| 26 | + // Line is the line number where the violation occurred. This may a local line number |
| 27 | + // if the validation is a schema (only schemas are validated locally, so the line number will be relative to |
| 28 | + // the Context object held by the ValidationError object). |
| 29 | + Line int `json:"line,omitempty" yaml:"line,omitempty"` |
30 | 30 |
|
31 | | - // Column is the column number where the violation occurred. This may a local column number |
32 | | - // if the validation is a schema (only schemas are validated locally, so the column number will be relative to |
33 | | - // the Context object held by the ValidationError object). |
34 | | - Column int `json:"column,omitempty" yaml:"column,omitempty"` |
| 31 | + // Column is the column number where the violation occurred. This may a local column number |
| 32 | + // if the validation is a schema (only schemas are validated locally, so the column number will be relative to |
| 33 | + // the Context object held by the ValidationError object). |
| 34 | + Column int `json:"column,omitempty" yaml:"column,omitempty"` |
35 | 35 |
|
36 | | - // ReferenceSchema is the schema that was referenced in the validation failure. |
37 | | - ReferenceSchema string `json:"referenceSchema,omitempty" yaml:"referenceSchema,omitempty"` |
| 36 | + // ReferenceSchema is the schema that was referenced in the validation failure. |
| 37 | + ReferenceSchema string `json:"referenceSchema,omitempty" yaml:"referenceSchema,omitempty"` |
38 | 38 |
|
39 | | - // ReferenceObject is the object that was referenced in the validation failure. |
40 | | - ReferenceObject string `json:"referenceObject,omitempty" yaml:"referenceObject,omitempty"` |
| 39 | + // ReferenceObject is the object that was referenced in the validation failure. |
| 40 | + ReferenceObject string `json:"referenceObject,omitempty" yaml:"referenceObject,omitempty"` |
41 | 41 |
|
42 | | - // The original error object, which is a jsonschema.ValidationError object. |
43 | | - OriginalError *jsonschema.ValidationError `json:"-" yaml:"-"` |
| 42 | + // The original error object, which is a jsonschema.ValidationError object. |
| 43 | + OriginalError *jsonschema.ValidationError `json:"-" yaml:"-"` |
44 | 44 | } |
45 | 45 |
|
46 | 46 | // Error returns a string representation of the error |
47 | 47 | func (s *SchemaValidationFailure) Error() string { |
48 | | - return fmt.Sprintf("Reason: %s, Location: %s", s.Reason, s.Location) |
| 48 | + return fmt.Sprintf("Reason: %s, Location: %s", s.Reason, s.Location) |
49 | 49 | } |
50 | 50 |
|
51 | 51 | // ValidationError is a struct that contains all the information about a validation error. |
52 | 52 | type ValidationError struct { |
53 | 53 |
|
54 | | - // Message is a human-readable message describing the error. |
55 | | - Message string `json:"message" yaml:"message"` |
| 54 | + // Message is a human-readable message describing the error. |
| 55 | + Message string `json:"message" yaml:"message"` |
56 | 56 |
|
57 | | - // Reason is a human-readable message describing the reason for the error. |
58 | | - Reason string `json:"reason" yaml:"reason"` |
| 57 | + // Reason is a human-readable message describing the reason for the error. |
| 58 | + Reason string `json:"reason" yaml:"reason"` |
59 | 59 |
|
60 | | - // ValidationType is a string that describes the type of validation that failed. |
61 | | - ValidationType string `json:"validationType" yaml:"validationType"` |
| 60 | + // ValidationType is a string that describes the type of validation that failed. |
| 61 | + ValidationType string `json:"validationType" yaml:"validationType"` |
62 | 62 |
|
63 | | - // ValidationSubType is a string that describes the subtype of validation that failed. |
64 | | - ValidationSubType string `json:"validationSubType" yaml:"validationSubType"` |
| 63 | + // ValidationSubType is a string that describes the subtype of validation that failed. |
| 64 | + ValidationSubType string `json:"validationSubType" yaml:"validationSubType"` |
65 | 65 |
|
66 | | - // SpecLine is the line number in the spec where the error occurred. |
67 | | - SpecLine int `json:"specLine" yaml:"specLine"` |
| 66 | + // SpecLine is the line number in the spec where the error occurred. |
| 67 | + SpecLine int `json:"specLine" yaml:"specLine"` |
68 | 68 |
|
69 | | - // SpecCol is the column number in the spec where the error occurred. |
70 | | - SpecCol int `json:"specColumn" yaml:"specColumn"` |
| 69 | + // SpecCol is the column number in the spec where the error occurred. |
| 70 | + SpecCol int `json:"specColumn" yaml:"specColumn"` |
71 | 71 |
|
72 | | - // HowToFix is a human-readable message describing how to fix the error. |
73 | | - HowToFix string `json:"howToFix" yaml:"howToFix"` |
| 72 | + // HowToFix is a human-readable message describing how to fix the error. |
| 73 | + HowToFix string `json:"howToFix" yaml:"howToFix"` |
74 | 74 |
|
75 | | - // SchemaValidationErrors is a slice of SchemaValidationFailure objects that describe the validation errors |
76 | | - // This is only populated whe the validation type is against a schema. |
77 | | - SchemaValidationErrors []*SchemaValidationFailure `json:"validationErrors,omitempty" yaml:"validationErrors,omitempty"` |
| 75 | + // SchemaValidationErrors is a slice of SchemaValidationFailure objects that describe the validation errors |
| 76 | + // This is only populated whe the validation type is against a schema. |
| 77 | + SchemaValidationErrors []*SchemaValidationFailure `json:"validationErrors,omitempty" yaml:"validationErrors,omitempty"` |
78 | 78 |
|
79 | | - // Context is the object that the validation error occurred on. This is usually a pointer to a schema |
80 | | - // or a parameter object. |
81 | | - Context interface{} `json:"-" yaml:"-"` |
| 79 | + // Context is the object that the validation error occurred on. This is usually a pointer to a schema |
| 80 | + // or a parameter object. |
| 81 | + Context interface{} `json:"-" yaml:"-"` |
82 | 82 | } |
83 | 83 |
|
84 | 84 | // Error returns a string representation of the error |
85 | 85 | func (v *ValidationError) Error() string { |
86 | | - if v.SchemaValidationErrors != nil { |
87 | | - return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s, Line: %d, Column: %d", |
88 | | - v.Message, v.Reason, v.SchemaValidationErrors, v.SpecLine, v.SpecCol) |
89 | | - } else { |
90 | | - return fmt.Sprintf("Error: %s, Reason: %s, Line: %d, Column: %d", |
91 | | - v.Message, v.Reason, v.SpecLine, v.SpecCol) |
92 | | - } |
| 86 | + if v.SchemaValidationErrors != nil { |
| 87 | + if v.SpecLine > 0 && v.SpecCol > 0 { |
| 88 | + return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s, Line: %d, Column: %d", |
| 89 | + v.Message, v.Reason, v.SchemaValidationErrors, v.SpecLine, v.SpecCol) |
| 90 | + } else { |
| 91 | + return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s", |
| 92 | + v.Message, v.Reason, v.SchemaValidationErrors) |
| 93 | + } |
| 94 | + } else { |
| 95 | + if v.SpecLine > 0 && v.SpecCol > 0 { |
| 96 | + return fmt.Sprintf("Error: %s, Reason: %s, Line: %d, Column: %d", |
| 97 | + v.Message, v.Reason, v.SpecLine, v.SpecCol) |
| 98 | + } else { |
| 99 | + return fmt.Sprintf("Error: %s, Reason: %s", |
| 100 | + v.Message, v.Reason) |
| 101 | + } |
| 102 | + } |
93 | 103 | } |
94 | 104 |
|
95 | 105 | // IsPathMissingError returns true if the error has a ValidationType of "path" and a ValidationSubType of "missing" |
96 | 106 | func (v *ValidationError) IsPathMissingError() bool { |
97 | | - return v.ValidationType == "path" && v.ValidationSubType == "missing" |
| 107 | + return v.ValidationType == "path" && v.ValidationSubType == "missing" |
98 | 108 | } |
0 commit comments