Skip to content

Commit e296c65

Browse files
JemDaydaveshanley
authored andcommitted
- Address Linting issues.
1 parent 7fd7aeb commit e296c65

14 files changed

+59
-56
lines changed

config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ func NewValidationOptions(opts ...Option) *ValidationOptions {
2424

2525
// Apply any supplied overrides
2626
for _, opt := range opts {
27-
opt(o)
27+
// Sanity
28+
if opt != nil {
29+
opt(o)
30+
}
2831
}
2932

3033
// Done

helpers/schema_compiler.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111

1212
// ConfigureCompiler configures a JSON Schema compiler with the desired behavior.
1313
func ConfigureCompiler(c *jsonschema.Compiler, o *config.ValidationOptions) {
14+
// Sanity
15+
if o == nil {
16+
return
17+
}
18+
1419
// nil is the default so this is OK.
1520
c.UseRegexpEngine(o.RegexEngine)
1621

@@ -38,18 +43,32 @@ func NewCompilerWithOptions(o *config.ValidationOptions) *jsonschema.Compiler {
3843
}
3944

4045
// NewCompiledSchema establishes a programmatic representation of a JSON Schema document that is used for validation.
41-
func NewCompiledSchema(name string, jsonSchema []byte, o *config.ValidationOptions) *jsonschema.Schema {
46+
func NewCompiledSchema(name string, jsonSchema []byte, o *config.ValidationOptions) (*jsonschema.Schema, error) {
47+
// Fake-Up a resource name for the schema
48+
resourceName := fmt.Sprintf("%s.json", name)
49+
4250
// Establish a compiler with the desired configuration
4351
compiler := NewCompilerWithOptions(o)
4452
compiler.UseLoader(NewCompilerLoader())
4553

4654
// Decode the JSON Schema into a JSON blob.
47-
decodedSchema, _ := jsonschema.UnmarshalJSON(bytes.NewReader(jsonSchema))
48-
_ = compiler.AddResource(fmt.Sprintf("%s.json", name), decodedSchema)
55+
decodedSchema, err := jsonschema.UnmarshalJSON(bytes.NewReader(jsonSchema))
56+
if err != nil {
57+
return nil, fmt.Errorf("failed to unmarshal JSON schema: %w", err)
58+
}
59+
60+
// Give our schema to the compiler.
61+
err = compiler.AddResource(resourceName, decodedSchema)
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to add resource to schema compiler: %w", err)
64+
}
4965

5066
// Try to compile it.
51-
jsch, _ := compiler.Compile(fmt.Sprintf("%s.json", name))
67+
jsch, err := compiler.Compile(resourceName)
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to compile JSON schema: %w", err)
70+
}
5271

5372
// Done.
54-
return jsch
73+
return jsch, nil
5574
}

helpers/schema_compiler_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package helpers
33
import (
44
"testing"
55

6-
"github.com/pb33f/libopenapi-validator/config"
76
"github.com/stretchr/testify/require"
7+
8+
"github.com/pb33f/libopenapi-validator/config"
89
)
910

1011
// A few simple JSON Schemas

parameters/cookie_parameters.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (v *paramValidator) ValidateCookieParamsWithPathItem(request *http.Request,
9393
"The cookie parameter",
9494
p.Name,
9595
helpers.ParameterValidation,
96-
helpers.ParameterValidationQuery)...)
96+
helpers.ParameterValidationQuery,
97+
v.options)...)
9798
}
9899
}
99100
case helpers.Array:

parameters/header_parameters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (v *paramValidator) ValidateHeaderParamsWithPathItem(request *http.Request,
115115
"The header parameter",
116116
p.Name,
117117
helpers.ParameterValidation,
118-
helpers.ParameterValidationQuery)...)
118+
helpers.ParameterValidationQuery, v.options)...)
119119
}
120120

121121
case helpers.Array:

parameters/path_parameters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (v *paramValidator) ValidatePathParamsWithPathItem(request *http.Request, p
223223
"The path parameter",
224224
p.Name,
225225
helpers.ParameterValidation,
226-
helpers.ParameterValidationPath)...)
226+
helpers.ParameterValidationPath, v.options)...)
227227
}
228228

229229
case helpers.Array:

parameters/query_parameters.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ doneLooking:
173173
"The query parameter",
174174
params[p].Name,
175175
helpers.ParameterValidation,
176-
helpers.ParameterValidationQuery)...)
176+
helpers.ParameterValidationQuery, v.options)...)
177177
if len(validationErrors) > numErrors {
178178
// we've already added an error for this, so we can skip the rest of the values
179179
break skipValues
@@ -185,7 +185,7 @@ doneLooking:
185185
// only check if items is a schema, not a boolean
186186
if sch.Items != nil && sch.Items.IsA() {
187187
validationErrors = append(validationErrors,
188-
ValidateQueryArray(sch, params[p], ef, contentWrapped)...)
188+
ValidateQueryArray(sch, params[p], ef, contentWrapped, v.options)...)
189189
}
190190
}
191191
}
@@ -209,7 +209,7 @@ doneLooking:
209209
"The query parameter (which is an array)",
210210
params[p].Name,
211211
helpers.ParameterValidation,
212-
helpers.ParameterValidationQuery)...)
212+
helpers.ParameterValidationQuery, v.options)...)
213213
break doneLooking
214214
}
215215
}

parameters/query_parameters_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,7 +2701,7 @@ paths:
27012701
}
27022702

27032703
errs := ValidateParameterSchema(s, rawObject, "cake", "burger", "lemons",
2704-
"pizza", "rice", "herbs")
2704+
"pizza", "rice", "herbs", nil)
27052705

27062706
assert.Len(t, errs, 1)
27072707
assert.Equal(t, "lemons 'pizza' is defined as an object, "+
@@ -2945,7 +2945,7 @@ paths:
29452945
}
29462946

29472947
errs := ValidateParameterSchema(s, rawObject, "cake", "burger", "lemons",
2948-
"pizza", "rice", "herbs")
2948+
"pizza", "rice", "herbs", nil)
29492949

29502950
assert.Len(t, errs, 1)
29512951
assert.Equal(t, "lemons 'pizza' is defined as an object, "+

parameters/validate_parameter.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"net/url"
1010
"reflect"
11-
"strings"
1211

1312
"github.com/pb33f/libopenapi/datamodel/high/base"
1413
"github.com/pb33f/libopenapi/utils"
@@ -33,8 +32,13 @@ func ValidateSingleParameterSchema(
3332
subValType string,
3433
o *config.ValidationOptions,
3534
) (validationErrors []*errors.ValidationError) {
36-
jsch := helpers.NewCompiledSchema(name, buildJsonRender(schema), o)
35+
// Attempt to compile the JSON Schema
36+
jsch, err := helpers.NewCompiledSchema(name, buildJsonRender(schema), o)
37+
if err != nil {
38+
return
39+
}
3740

41+
// Validate the object and report any errors.
3842
scErrs := jsch.Validate(rawObject)
3943
var werras *jsonschema.ValidationError
4044
if stdError.As(scErrs, &werras) {
@@ -70,6 +74,7 @@ func ValidateParameterSchema(
7074
name,
7175
validationType,
7276
subValType string,
77+
validationOptions *config.ValidationOptions,
7378
) []*errors.ValidationError {
7479
var validationErrors []*errors.ValidationError
7580

@@ -100,11 +105,7 @@ func ValidateParameterSchema(
100105
validEncoding = true
101106
}
102107
// 3. create a new json schema compiler and add the schema to it
103-
compiler := jsonschema.NewCompiler()
104-
105-
decodedSchema, _ := jsonschema.UnmarshalJSON(strings.NewReader(string(jsonSchema)))
106-
_ = compiler.AddResource(fmt.Sprintf("%s.json", name), decodedSchema)
107-
jsch, _ := compiler.Compile(fmt.Sprintf("%s.json", name))
108+
jsch, _ := helpers.NewCompiledSchema(name, jsonSchema, validationOptions)
108109

109110
// 4. validate the object against the schema
110111
var scErrs error

parameters/validation_functions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/pb33f/libopenapi/datamodel/high/base"
1313
"github.com/pb33f/libopenapi/datamodel/high/v3"
1414

15+
"github.com/pb33f/libopenapi-validator/config"
1516
"github.com/pb33f/libopenapi-validator/errors"
1617
"github.com/pb33f/libopenapi-validator/helpers"
1718
)
@@ -98,7 +99,7 @@ func ValidateHeaderArray(
9899

99100
// ValidateQueryArray will validate a query parameter that is an array
100101
func ValidateQueryArray(
101-
sch *base.Schema, param *v3.Parameter, ef string, contentWrapped bool,
102+
sch *base.Schema, param *v3.Parameter, ef string, contentWrapped bool, validationOptions *config.ValidationOptions,
102103
) []*errors.ValidationError {
103104
var validationErrors []*errors.ValidationError
104105
itemsSchema := sch.Items.A.Schema()
@@ -174,7 +175,7 @@ func ValidateQueryArray(
174175
"The query parameter (which is an array)",
175176
param.Name,
176177
helpers.ParameterValidation,
177-
helpers.ParameterValidationQuery)...)
178+
helpers.ParameterValidationQuery, validationOptions)...)
178179

179180
case helpers.String:
180181

0 commit comments

Comments
 (0)