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 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( diff --git a/validator_test.go b/validator_test.go index e7d5b21..d0f5808 100644 --- a/validator_test.go +++ b/validator_test.go @@ -16,6 +16,7 @@ import ( "sync" "testing" + "github.com/dlclark/regexp2" "github.com/pb33f/libopenapi" "github.com/santhosh-tekuri/jsonschema/v6" "github.com/stretchr/testify/assert" @@ -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")