@@ -16,14 +16,15 @@ import (
1616 "sync"
1717 "testing"
1818
19+ "github.com/dlclark/regexp2"
1920 "github.com/pb33f/libopenapi"
21+ "github.com/pb33f/libopenapi-validator/config"
2022 "github.com/santhosh-tekuri/jsonschema/v6"
2123 "github.com/stretchr/testify/assert"
2224 "github.com/stretchr/testify/require"
2325
2426 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
2527
26- "github.com/pb33f/libopenapi-validator/config"
2728 "github.com/pb33f/libopenapi-validator/helpers"
2829)
2930
@@ -142,25 +143,30 @@ func TestNewValidator_ValidateDocument(t *testing.T) {
142143 assert .Len (t , errs , 0 )
143144}
144145
145- type alwaysMatchesRegex jsonschema. RegexpEngine
146+ type dlclarkRegexp regexp2. Regexp
146147
147- func (dr * alwaysMatchesRegex ) MatchString (s string ) bool {
148- return true
148+ func (re * dlclarkRegexp ) MatchString (s string ) bool {
149+ matched , err := (* regexp2 .Regexp )(re ).MatchString (s )
150+ return err == nil && matched
149151}
150152
151- func (dr * alwaysMatchesRegex ) String () string {
152- return ""
153+ func (re * dlclarkRegexp ) String () string {
154+ return ( * regexp2 . Regexp )( re ). String ()
153155}
154156
155- func fakeRegexEngine (s string ) (jsonschema.Regexp , error ) {
156- return (* alwaysMatchesRegex )(nil ), nil
157+ func dlclarkCompile (s string ) (jsonschema.Regexp , error ) {
158+ re , err := regexp2 .Compile (s , regexp2 .ECMAScript )
159+ if err != nil {
160+ return nil , err
161+ }
162+ return (* dlclarkRegexp )(re ), nil
157163}
158164
159165func TestNewValidator_WithRegex (t * testing.T ) {
160166 doc , err := libopenapi .NewDocument (petstoreBytes )
161167 require .Nil (t , err , "Failed to load spec" )
162168
163- v , errs := NewValidator (doc , config .WithRegexEngine (fakeRegexEngine ))
169+ v , errs := NewValidator (doc , config .WithRegexEngine (dlclarkCompile ))
164170 require .Empty (t , errs , "Failed to build validator" )
165171 require .NotNil (t , v , "Failed to build validator" )
166172
0 commit comments