From 834e109e9d84dea8b7d41ee9f7f976ea58138e17 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 1 Apr 2025 18:27:15 -0400 Subject: [PATCH 1/5] fix: pass config validator options when validation openapi document --- validator.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/validator.go b/validator.go index d96436c..7e0ce44 100644 --- a/validator.go +++ b/validator.go @@ -105,7 +105,11 @@ func (v *validator) GetResponseBodyValidator() responses.ResponseBodyValidator { } func (v *validator) ValidateDocument() (bool, []*errors.ValidationError) { - return schema_validation.ValidateOpenAPIDocument(v.document) + var validationOpts []config.Option + if v.options != nil { + validationOpts = append(validationOpts, config.WithRegexEngine(v.options.RegexEngine)) + } + return schema_validation.ValidateOpenAPIDocument(v.document, validationOpts...) } func (v *validator) ValidateHttpResponse( From 717828b95c586bb94a85582fd06069cdd7a89b99 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Mon, 7 Apr 2025 13:21:37 -0400 Subject: [PATCH 2/5] update unit test to pass custom regex validator --- validator_test.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/validator_test.go b/validator_test.go index e7d5b21..d1ec314 100644 --- a/validator_test.go +++ b/validator_test.go @@ -16,14 +16,15 @@ import ( "sync" "testing" + "github.com/dlclark/regexp2" "github.com/pb33f/libopenapi" + "github.com/pb33f/libopenapi-validator/config" "github.com/santhosh-tekuri/jsonschema/v6" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" v3 "github.com/pb33f/libopenapi/datamodel/high/v3" - "github.com/pb33f/libopenapi-validator/config" "github.com/pb33f/libopenapi-validator/helpers" ) @@ -142,25 +143,30 @@ func TestNewValidator_ValidateDocument(t *testing.T) { assert.Len(t, errs, 0) } -type alwaysMatchesRegex jsonschema.RegexpEngine +type dlclarkRegexp regexp2.Regexp -func (dr *alwaysMatchesRegex) MatchString(s string) bool { - return true +func (re *dlclarkRegexp) MatchString(s string) bool { + matched, err := (*regexp2.Regexp)(re).MatchString(s) + return err == nil && matched } -func (dr *alwaysMatchesRegex) String() string { - return "" +func (re *dlclarkRegexp) String() string { + return (*regexp2.Regexp)(re).String() } -func fakeRegexEngine(s string) (jsonschema.Regexp, error) { - return (*alwaysMatchesRegex)(nil), nil +func dlclarkCompile(s string) (jsonschema.Regexp, error) { + re, err := regexp2.Compile(s, regexp2.ECMAScript) + if err != nil { + return nil, err + } + return (*dlclarkRegexp)(re), nil } func TestNewValidator_WithRegex(t *testing.T) { doc, err := libopenapi.NewDocument(petstoreBytes) require.Nil(t, err, "Failed to load spec") - v, errs := NewValidator(doc, config.WithRegexEngine(fakeRegexEngine)) + v, errs := NewValidator(doc, config.WithRegexEngine(dlclarkCompile)) require.Empty(t, errs, "Failed to build validator") require.NotNil(t, v, "Failed to build validator") From 5d008bbab9cc92c527cc2ae64bfb261067aba9f4 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 8 Apr 2025 10:50:41 -0400 Subject: [PATCH 3/5] fix lint errors --- validator.go | 4 +--- validator_test.go | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/validator.go b/validator.go index 7e0ce44..a58833b 100644 --- a/validator.go +++ b/validator.go @@ -8,9 +8,6 @@ import ( "sync" "github.com/pb33f/libopenapi" - - v3 "github.com/pb33f/libopenapi/datamodel/high/v3" - "github.com/pb33f/libopenapi-validator/config" "github.com/pb33f/libopenapi-validator/errors" "github.com/pb33f/libopenapi-validator/parameters" @@ -18,6 +15,7 @@ import ( "github.com/pb33f/libopenapi-validator/requests" "github.com/pb33f/libopenapi-validator/responses" "github.com/pb33f/libopenapi-validator/schema_validation" + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" ) // Validator provides a coarse grained interface for validating an OpenAPI 3+ documents. diff --git a/validator_test.go b/validator_test.go index d1ec314..3cb4e99 100644 --- a/validator_test.go +++ b/validator_test.go @@ -19,13 +19,11 @@ import ( "github.com/dlclark/regexp2" "github.com/pb33f/libopenapi" "github.com/pb33f/libopenapi-validator/config" + "github.com/pb33f/libopenapi-validator/helpers" + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" "github.com/santhosh-tekuri/jsonschema/v6" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - v3 "github.com/pb33f/libopenapi/datamodel/high/v3" - - "github.com/pb33f/libopenapi-validator/helpers" ) func TestNewValidator(t *testing.T) { From 8441339b9a33eb1b77a76b3cccdcb13fffa01641 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 8 Apr 2025 10:56:39 -0400 Subject: [PATCH 4/5] fix lint errors --- validator.go | 4 +++- validator_test.go | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/validator.go b/validator.go index a58833b..7e0ce44 100644 --- a/validator.go +++ b/validator.go @@ -8,6 +8,9 @@ import ( "sync" "github.com/pb33f/libopenapi" + + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" + "github.com/pb33f/libopenapi-validator/config" "github.com/pb33f/libopenapi-validator/errors" "github.com/pb33f/libopenapi-validator/parameters" @@ -15,7 +18,6 @@ import ( "github.com/pb33f/libopenapi-validator/requests" "github.com/pb33f/libopenapi-validator/responses" "github.com/pb33f/libopenapi-validator/schema_validation" - v3 "github.com/pb33f/libopenapi/datamodel/high/v3" ) // Validator provides a coarse grained interface for validating an OpenAPI 3+ documents. diff --git a/validator_test.go b/validator_test.go index 3cb4e99..d0f5808 100644 --- a/validator_test.go +++ b/validator_test.go @@ -18,12 +18,14 @@ import ( "github.com/dlclark/regexp2" "github.com/pb33f/libopenapi" - "github.com/pb33f/libopenapi-validator/config" - "github.com/pb33f/libopenapi-validator/helpers" - v3 "github.com/pb33f/libopenapi/datamodel/high/v3" "github.com/santhosh-tekuri/jsonschema/v6" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" + + "github.com/pb33f/libopenapi-validator/config" + "github.com/pb33f/libopenapi-validator/helpers" ) func TestNewValidator(t *testing.T) { From 510d30df672db161947c3af411d5c8ddecfe4628 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 8 Apr 2025 17:26:27 -0400 Subject: [PATCH 5/5] fix merge conflicts --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 8aa2b0e..1aba6f5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/pb33f/libopenapi-validator go 1.23.0 require ( + github.com/dlclark/regexp2 v1.11.0 github.com/pb33f/libopenapi v0.21.8 github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 github.com/stretchr/testify v1.10.0