Skip to content

Commit 10a05ab

Browse files
committed
Allow passthrough for existing options.
Handles #144
1 parent 2b72777 commit 10a05ab

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func NewValidationOptions(opts ...Option) *ValidationOptions {
3333
return o
3434
}
3535

36+
// WithExistingOpts returns an Option that will copy the values from the supplied ValidationOptions instance
37+
func WithExistingOpts(options *ValidationOptions) Option {
38+
return func(o *ValidationOptions) {
39+
o.RegexEngine = options.RegexEngine
40+
o.FormatAssertions = options.FormatAssertions
41+
o.ContentAssertions = options.ContentAssertions
42+
}
43+
}
44+
3645
// WithRegexEngine Assigns a custom regular-expression engine to be used during validation.
3746
func WithRegexEngine(engine jsonschema.RegexpEngine) Option {
3847
return func(o *ValidationOptions) {

requests/validate_body.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ func (v *requestBodyValidator) ValidateRequestBodyWithPathItem(request *http.Req
108108
})
109109
}
110110

111-
// render the schema, to be used for validation
112-
validationSucceeded, validationErrors := ValidateRequestSchema(request, schema, renderedInline, renderedJSON, config.WithRegexEngine(v.options.RegexEngine))
111+
validationSucceeded, validationErrors := ValidateRequestSchema(request, schema, renderedInline, renderedJSON, config.WithExistingOpts(v.options))
113112

114113
errors.PopulateValidationErrors(validationErrors, request, pathValue)
115114

requests/validate_body_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"encoding/json"
99
"fmt"
10+
"github.com/pb33f/libopenapi-validator/config"
1011
"net/http"
1112
"testing"
1213

@@ -1444,3 +1445,52 @@ func TestValidateBody_SchemaNoType_Issue75(t *testing.T) {
14441445
assert.Len(t, valErrs, 1)
14451446
assert.Equal(t, "PUT request body is empty for '/path1'", valErrs[0].Message)
14461447
}
1448+
1449+
// https://github.com/pb33f/libopenapi-validator/issues/144
1450+
func TestValidateBody_InvalidSchema_EnsureOptionsPassthrough(t *testing.T) {
1451+
spec := `openapi: 3.1.0
1452+
paths:
1453+
/burgers/createBurger:
1454+
post:
1455+
requestBody:
1456+
content:
1457+
application/json:
1458+
schema:
1459+
$ref: '#/components/schema_validation/V1_UserRequest'
1460+
components:
1461+
schema_validation:
1462+
V1_UserRequest:
1463+
type: object
1464+
properties:
1465+
email:
1466+
type: string
1467+
format: email
1468+
minLength: 1
1469+
maxLength: 320`
1470+
1471+
doc, _ := libopenapi.NewDocument([]byte(spec))
1472+
1473+
m, _ := doc.BuildV3Model()
1474+
v := NewRequestBodyValidator(&m.Model, config.WithFormatAssertions())
1475+
1476+
items := make(map[string]interface{})
1477+
items["email"] = "test"
1478+
1479+
bodyBytes, _ := json.Marshal(items)
1480+
1481+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
1482+
bytes.NewBuffer(bodyBytes))
1483+
request.Header.Set("Content-Type", "application/json")
1484+
1485+
valid, errors := v.ValidateRequestBody(request)
1486+
1487+
assert.False(t, valid)
1488+
assert.Len(t, errors, 1)
1489+
assert.Len(t, errors[0].SchemaValidationErrors, 1)
1490+
assert.Equal(t, "POST request body for '/burgers/createBurger' failed to validate schema", errors[0].Message)
1491+
1492+
assert.False(t, valid)
1493+
assert.Len(t, errors, 1)
1494+
assert.Len(t, errors[0].SchemaValidationErrors, 1)
1495+
assert.Equal(t, "'test' is not valid email: missing @", errors[0].SchemaValidationErrors[0].Reason)
1496+
}

0 commit comments

Comments
 (0)