Skip to content

Commit ee8b645

Browse files
committed
refactored some copy pasta.
1 parent a5e3571 commit ee8b645

File tree

7 files changed

+81
-54
lines changed

7 files changed

+81
-54
lines changed

helpers/path_finder.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,18 @@ func ExtractJSONPathFromStringLocation(instanceLocation string) string {
159159

160160
return ExtractJSONPathFromInstanceLocation(segments)
161161
}
162+
163+
// ConvertStringLocationToPathSegments converts a string-based instance location to path segments array
164+
// Handles edge cases like empty strings and root-only paths
165+
func ConvertStringLocationToPathSegments(instanceLocation string) []string {
166+
if instanceLocation == "" {
167+
return []string{}
168+
}
169+
170+
segments := strings.Split(strings.Trim(instanceLocation, "/"), "/")
171+
if len(segments) == 1 && segments[0] == "" {
172+
return []string{}
173+
}
174+
175+
return segments
176+
}

helpers/path_finder_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,64 @@ func TestExtractJSONPathFromStringLocation(t *testing.T) {
526526
})
527527
}
528528
}
529+
530+
func TestConvertStringLocationToPathSegments(t *testing.T) {
531+
testCases := []struct {
532+
name string
533+
instancePath string
534+
expected []string
535+
}{
536+
{
537+
name: "Empty string",
538+
instancePath: "",
539+
expected: []string{},
540+
},
541+
{
542+
name: "Root path only",
543+
instancePath: "/",
544+
expected: []string{},
545+
},
546+
{
547+
name: "Single field",
548+
instancePath: "/name",
549+
expected: []string{"name"},
550+
},
551+
{
552+
name: "Multiple fields",
553+
instancePath: "/user/profile/email",
554+
expected: []string{"user", "profile", "email"},
555+
},
556+
{
557+
name: "Array index",
558+
instancePath: "/users/0/name",
559+
expected: []string{"users", "0", "name"},
560+
},
561+
{
562+
name: "Multiple array indices",
563+
instancePath: "/matrix/0/1/value",
564+
expected: []string{"matrix", "0", "1", "value"},
565+
},
566+
{
567+
name: "Field with special characters",
568+
instancePath: "/user/email-address",
569+
expected: []string{"user", "email-address"},
570+
},
571+
{
572+
name: "Complex nested path",
573+
instancePath: "/data/items/1/properties/field-name",
574+
expected: []string{"data", "items", "1", "properties", "field-name"},
575+
},
576+
{
577+
name: "Leading and trailing slashes",
578+
instancePath: "///user/name///",
579+
expected: []string{"user", "name"},
580+
},
581+
}
582+
583+
for _, tc := range testCases {
584+
t.Run(tc.name, func(t *testing.T) {
585+
result := ConvertStringLocationToPathSegments(tc.instancePath)
586+
assert.Equal(t, tc.expected, result)
587+
})
588+
}
589+
}

parameters/validate_parameter.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,12 @@ func formatJsonSchemaValidationError(schema *base.Schema, scErrs *jsonschema.Val
225225
continue // ignore this error, it's not useful
226226
}
227227

228-
// Convert string location to path segments for InstancePath
229-
var instancePathSegments []string
230-
if er.InstanceLocation != "" {
231-
instancePathSegments = strings.Split(strings.Trim(er.InstanceLocation, "/"), "/")
232-
if len(instancePathSegments) == 1 && instancePathSegments[0] == "" {
233-
instancePathSegments = []string{}
234-
}
235-
}
236-
237228
fail := &errors.SchemaValidationFailure{
238229
Reason: errMsg,
239230
Location: er.KeywordLocation,
240231
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
241232
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
242-
InstancePath: instancePathSegments,
233+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
243234
OriginalError: scErrs,
244235
}
245236
if schema != nil {

requests/validate_request.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"reflect"
1313
"regexp"
1414
"strconv"
15-
"strings"
1615

1716
"github.com/pb33f/libopenapi/datamodel/high/base"
1817
"github.com/santhosh-tekuri/jsonschema/v6"
@@ -167,21 +166,12 @@ func ValidateRequestSchema(
167166

168167
errMsg := er.Error.Kind.LocalizedString(message.NewPrinter(language.Tag{}))
169168

170-
// Convert string location to path segments for InstancePath
171-
var instancePathSegments []string
172-
if er.InstanceLocation != "" {
173-
instancePathSegments = strings.Split(strings.Trim(er.InstanceLocation, "/"), "/")
174-
if len(instancePathSegments) == 1 && instancePathSegments[0] == "" {
175-
instancePathSegments = []string{}
176-
}
177-
}
178-
179169
violation := &errors.SchemaValidationFailure{
180170
Reason: errMsg,
181171
Location: er.KeywordLocation,
182172
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
183173
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
184-
InstancePath: instancePathSegments,
174+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
185175
ReferenceSchema: string(renderedSchema),
186176
ReferenceObject: referenceObject,
187177
OriginalError: jk,

responses/validate_response.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"reflect"
1313
"regexp"
1414
"strconv"
15-
"strings"
1615

1716
"github.com/pb33f/libopenapi/datamodel/high/base"
1817
"github.com/santhosh-tekuri/jsonschema/v6"
@@ -194,21 +193,12 @@ func ValidateResponseSchema(
194193
referenceObject = string(responseBody)
195194
}
196195

197-
// Convert string location to path segments for InstancePath
198-
var instancePathSegments []string
199-
if er.InstanceLocation != "" {
200-
instancePathSegments = strings.Split(strings.Trim(er.InstanceLocation, "/"), "/")
201-
if len(instancePathSegments) == 1 && instancePathSegments[0] == "" {
202-
instancePathSegments = []string{}
203-
}
204-
}
205-
206196
violation := &errors.SchemaValidationFailure{
207197
Reason: errMsg,
208198
Location: er.KeywordLocation,
209199
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
210200
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
211-
InstancePath: instancePathSegments,
201+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
212202
ReferenceSchema: string(renderedSchema),
213203
ReferenceObject: referenceObject,
214204
OriginalError: jk,

schema_validation/validate_document.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"strings"
1110

1211
"github.com/pb33f/libopenapi"
1312
"github.com/santhosh-tekuri/jsonschema/v6"
@@ -84,21 +83,12 @@ func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bo
8483

8584
// locate the violated property in the schema
8685
located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.InstanceLocation)
87-
// Convert string location to path segments for InstancePath
88-
var instancePathSegments []string
89-
if er.InstanceLocation != "" {
90-
instancePathSegments = strings.Split(strings.Trim(er.InstanceLocation, "/"), "/")
91-
if len(instancePathSegments) == 1 && instancePathSegments[0] == "" {
92-
instancePathSegments = []string{}
93-
}
94-
}
95-
9686
violation := &liberrors.SchemaValidationFailure{
9787
Reason: errMsg,
9888
Location: er.InstanceLocation,
9989
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
10090
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
101-
InstancePath: instancePathSegments,
91+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
10292
DeepLocation: er.KeywordLocation,
10393
AbsoluteLocation: er.AbsoluteKeywordLocation,
10494
OriginalError: jk,

schema_validation/validate_schema.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"reflect"
1313
"regexp"
1414
"strconv"
15-
"strings"
1615
"sync"
1716

1817
"github.com/pb33f/libopenapi/datamodel/high/base"
@@ -242,21 +241,12 @@ func extractBasicErrors(schFlatErrs []jsonschema.OutputUnit,
242241
referenceObject = string(payload)
243242
}
244243

245-
// Convert string location to path segments for InstancePath
246-
var instancePathSegments []string
247-
if er.InstanceLocation != "" {
248-
instancePathSegments = strings.Split(strings.Trim(er.InstanceLocation, "/"), "/")
249-
if len(instancePathSegments) == 1 && instancePathSegments[0] == "" {
250-
instancePathSegments = []string{}
251-
}
252-
}
253-
254244
violation := &liberrors.SchemaValidationFailure{
255245
Reason: errMsg,
256246
Location: er.InstanceLocation,
257247
FieldName: helpers.ExtractFieldNameFromStringLocation(er.InstanceLocation),
258248
FieldPath: helpers.ExtractJSONPathFromStringLocation(er.InstanceLocation),
259-
InstancePath: instancePathSegments,
249+
InstancePath: helpers.ConvertStringLocationToPathSegments(er.InstanceLocation),
260250
DeepLocation: er.KeywordLocation,
261251
AbsoluteLocation: er.AbsoluteKeywordLocation,
262252
ReferenceSchema: string(renderedSchema),

0 commit comments

Comments
 (0)