Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 14 additions & 8 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")

Expand Down