Skip to content

Commit 503a619

Browse files
committed
update unit test to pass custom regex validator
1 parent 06f971a commit 503a619

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/pb33f/libopenapi-validator
33
go 1.23.0
44

55
require (
6+
github.com/dlclark/regexp2 v1.11.0
67
github.com/pb33f/libopenapi v0.19.1
78
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
89
github.com/stretchr/testify v1.10.0

validator_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

159165
func 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

Comments
 (0)