Skip to content

Commit b87cec9

Browse files
author
Michael Bonifacio
committed
Refactor SchemaValidationFailure struct fields
1 parent c440fec commit b87cec9

File tree

6 files changed

+57
-58
lines changed

6 files changed

+57
-58
lines changed

errors/validation_error.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,27 @@ import (
99
"github.com/santhosh-tekuri/jsonschema/v6"
1010
)
1111

12-
// SchemaValidationFailure is a wrapper around the jsonschema.ValidationError object, to provide a more
13-
// user-friendly way to break down what went wrong.
12+
// SchemaValidationFailure describes any failure that occurs when validating data
13+
// against either an OpenAPI or JSON Schema. It aims to be a more user-friendly
14+
// representation of the error than what is provided by the jsonschema library.
1415
type SchemaValidationFailure struct {
1516
// Reason is a human-readable message describing the reason for the error.
1617
Reason string `json:"reason,omitempty" yaml:"reason,omitempty"`
1718

18-
// Location is the XPath-like location of the validation failure
19-
Location string `json:"location,omitempty" yaml:"location,omitempty"`
19+
// InstancePath is the raw path segments from the root to the failing field
20+
InstancePath []string `json:"instancePath,omitempty" yaml:"instancePath,omitempty"`
2021

2122
// FieldName is the name of the specific field that failed validation (last segment of the path)
2223
FieldName string `json:"fieldName,omitempty" yaml:"fieldName,omitempty"`
2324

24-
// FieldPath is the JSONPath representation of the field location (e.g., "$.user.email")
25+
// FieldPath is the JSONPath representation of the field location that failed validation (e.g., "$.user.email")
2526
FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
2627

27-
// InstancePath is the raw path segments from the root to the failing field
28-
InstancePath []string `json:"instancePath,omitempty" yaml:"instancePath,omitempty"`
29-
30-
// DeepLocation is the path to the validation failure as exposed by the jsonschema library.
31-
DeepLocation string `json:"deepLocation,omitempty" yaml:"deepLocation,omitempty"`
28+
// KeywordLocation is the relative path to the JsonSchema keyword that failed validation
29+
KeywordLocation string `json:"keywordLocation,omitempty" yaml:"keywordLocation,omitempty"`
3230

33-
// AbsoluteLocation is the absolute path to the validation failure as exposed by the jsonschema library.
34-
AbsoluteLocation string `json:"absoluteLocation,omitempty" yaml:"absoluteLocation,omitempty"`
31+
// AbsoluteKeywordLocation is the absolute path to the validation failure as exposed by the jsonschema library.
32+
AbsoluteKeywordLocation string `json:"absoluteKeywordLocation,omitempty" yaml:"absoluteKeywordLocation,omitempty"`
3533

3634
// Line is the line number where the violation occurred. This may a local line number
3735
// if the validation is a schema (only schemas are validated locally, so the line number will be relative to
@@ -46,14 +44,15 @@ type SchemaValidationFailure struct {
4644
// ReferenceSchema is the schema that was referenced in the validation failure.
4745
ReferenceSchema string `json:"referenceSchema,omitempty" yaml:"referenceSchema,omitempty"`
4846

49-
// ReferenceObject is the object that was referenced in the validation failure.
47+
// ReferenceObject is the object that failed schema validation
5048
ReferenceObject string `json:"referenceObject,omitempty" yaml:"referenceObject,omitempty"`
5149

52-
// ReferenceExample is an example object generated from the schema that was referenced in the validation failure.
53-
ReferenceExample string `json:"referenceExample,omitempty" yaml:"referenceExample,omitempty"`
50+
// The original jsonschema.ValidationError object, if the schema failure originated from the jsonschema library.
51+
OriginalJsonSchemaError *jsonschema.ValidationError `json:"-" yaml:"-"`
5452

55-
// The original error object, which is a jsonschema.ValidationError object.
56-
OriginalError *jsonschema.ValidationError `json:"-" yaml:"-"`
53+
// DEPRECATED in favor of explicit use of FieldPath & InstancePath
54+
// Location is the XPath-like location of the validation failure
55+
Location string `json:"location,omitempty" yaml:"location,omitempty"`
5756
}
5857

5958
// Error returns a string representation of the error
@@ -97,7 +96,7 @@ type ValidationError struct {
9796
ParameterName string `json:"parameterName,omitempty" yaml:"parameterName,omitempty"`
9897

9998
// SchemaValidationErrors is a slice of SchemaValidationFailure objects that describe the validation errors
100-
// This is only populated whe the validation type is against a schema.
99+
// This is only populated when the validation type is against a schema.
101100
SchemaValidationErrors []*SchemaValidationFailure `json:"validationErrors,omitempty" yaml:"validationErrors,omitempty"`
102101

103102
// Context is the object that the validation error occurred on. This is usually a pointer to a schema

parameters/validate_parameter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ func formatJsonSchemaValidationError(schema *base.Schema, scErrs *jsonschema.Val
226226
}
227227

228228
fail := &errors.SchemaValidationFailure{
229-
Reason: errMsg,
230-
Location: er.KeywordLocation,
231-
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
232-
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
233-
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
234-
OriginalError: scErrs,
229+
Reason: errMsg,
230+
Location: er.KeywordLocation,
231+
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
232+
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
233+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
234+
OriginalJsonSchemaError: scErrs,
235235
}
236236
if schema != nil {
237237
rendered, err := schema.RenderInline()

requests/validate_request.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ func ValidateRequestSchema(
167167
errMsg := er.Error.Kind.LocalizedString(message.NewPrinter(language.Tag{}))
168168

169169
violation := &errors.SchemaValidationFailure{
170-
Reason: errMsg,
171-
Location: er.KeywordLocation,
172-
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
173-
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
174-
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
175-
ReferenceSchema: string(renderedSchema),
176-
ReferenceObject: referenceObject,
177-
OriginalError: jk,
170+
Reason: errMsg,
171+
Location: er.KeywordLocation,
172+
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
173+
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
174+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
175+
ReferenceSchema: string(renderedSchema),
176+
ReferenceObject: referenceObject,
177+
OriginalJsonSchemaError: jk,
178178
}
179179
// if we have a location within the schema, add it to the error
180180
if located != nil {

responses/validate_response.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ func ValidateResponseSchema(
194194
}
195195

196196
violation := &errors.SchemaValidationFailure{
197-
Reason: errMsg,
198-
Location: er.KeywordLocation,
199-
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
200-
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
201-
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
202-
ReferenceSchema: string(renderedSchema),
203-
ReferenceObject: referenceObject,
204-
OriginalError: jk,
197+
Reason: errMsg,
198+
Location: er.KeywordLocation,
199+
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
200+
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
201+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
202+
ReferenceSchema: string(renderedSchema),
203+
ReferenceObject: referenceObject,
204+
OriginalJsonSchemaError: jk,
205205
}
206206
// if we have a location within the schema, add it to the error
207207
if located != nil {

schema_validation/validate_document.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bo
8484
// locate the violated property in the schema
8585
located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
8686
violation := &liberrors.SchemaValidationFailure{
87-
Reason: errMsg,
88-
Location: er.InstanceLocation,
89-
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
90-
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
91-
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
92-
DeepLocation: er.KeywordLocation,
93-
AbsoluteLocation: er.AbsoluteKeywordLocation,
94-
OriginalError: jk,
87+
Reason: errMsg,
88+
Location: er.InstanceLocation,
89+
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
90+
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
91+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
92+
KeywordLocation: er.KeywordLocation,
93+
AbsoluteKeywordLocation: er.AbsoluteKeywordLocation,
94+
OriginalJsonSchemaError: jk,
9595
}
9696

9797
// if we have a location within the schema, add it to the error

schema_validation/validate_schema.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,16 @@ func extractBasicErrors(schFlatErrs []jsonschema.OutputUnit,
300300
}
301301

302302
violation := &liberrors.SchemaValidationFailure{
303-
Reason: errMsg,
304-
Location: er.InstanceLocation,
305-
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
306-
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
307-
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
308-
DeepLocation: er.KeywordLocation,
309-
AbsoluteLocation: er.AbsoluteKeywordLocation,
310-
ReferenceSchema: string(renderedSchema),
311-
ReferenceObject: referenceObject,
312-
OriginalError: jk,
303+
Reason: errMsg,
304+
Location: er.InstanceLocation,
305+
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
306+
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
307+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
308+
KeywordLocation: er.KeywordLocation,
309+
AbsoluteKeywordLocation: er.AbsoluteKeywordLocation,
310+
ReferenceSchema: string(renderedSchema),
311+
ReferenceObject: referenceObject,
312+
OriginalJsonSchemaError: jk,
313313
}
314314
// if we have a location within the schema, add it to the error
315315
if located != nil {

0 commit comments

Comments
 (0)