Skip to content

Commit

Permalink
feat: include Pattern when encountering NoRegexMatchError
Browse files Browse the repository at this point in the history
  • Loading branch information
paragon committed Nov 11, 2024
1 parent b82cdfd commit 44a0996
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
17 changes: 15 additions & 2 deletions internal/integration/all_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"context"
"fmt"
"net/http/httptest"
"strings"
"testing"
Expand Down Expand Up @@ -50,15 +51,27 @@ func TestAllof(t *testing.T) {
require.NoError(t, err)
}

regexPattern := "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"

ctx := context.Background()
t.Run("nullableStrings", func(t *testing.T) {
err := client.NullableStrings(ctx, api.NilString{})
_, ok := errors.Into[*validate.NoRegexMatchError](err)
regexMatchErr, ok := errors.Into[*validate.NoRegexMatchError](err)
require.True(t, ok, "validate: string: no regex match")
require.ErrorContains(
t,
regexMatchErr,
fmt.Sprintf("no regex match: %s", regexPattern),
)

err = client.NullableStrings(ctx, api.NewNilString("foo"))
_, ok = errors.Into[*validate.NoRegexMatchError](err)
regexMatchErr, ok = errors.Into[*validate.NoRegexMatchError](err)
require.True(t, ok, "validate: string: no regex match")
require.ErrorContains(
t,
regexMatchErr,
fmt.Sprintf("no regex match: %s", regexPattern),
)

err = client.NullableStrings(ctx, api.NewNilString("127.0.0.1"))
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions jsonschema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,17 @@ func (p *Parser) parseSchema(schema *RawSchema, ctx *jsonpointer.ResolveCtx, hoo
return p.wrapField(field, p.file(ctx), schema.Common.Locator, err)
}

validateMinMax := func(prop string, min, max *uint64) (rerr error) {
if min == nil || max == nil {
validateMinMax := func(prop string, minVal *uint64, maxVal *uint64) (rerr error) {
if minVal == nil || maxVal == nil {
return nil
}
if *min > *max {
msg := fmt.Sprintf("min%s (%d) is greater than max%s (%d)", prop, *min, prop, *max)
if *minVal > *maxVal {
msg := fmt.Sprintf("minVal%s (%d) is greater than maxVal%s (%d)", prop, *minVal, prop, *maxVal)
ptr := schema.Common.Locator.Pointer(p.file(ctx))

me := new(location.MultiError)
me.ReportPtr(ptr.Field("min"+prop), msg)
me.ReportPtr(ptr.Field("max"+prop), "")
me.ReportPtr(ptr.Field("minVal"+prop), msg)
me.ReportPtr(ptr.Field("maxVal"+prop), "")
return me
}
return nil
Expand Down
9 changes: 6 additions & 3 deletions validate/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validate

import (
"fmt"
"github.com/ogen-go/ogen/ogenregex"
"strings"

"github.com/go-faster/errors"
Expand Down Expand Up @@ -102,9 +103,11 @@ func (e *MaxLengthError) Error() string {
}

// NoRegexMatchError reports that value have no regexp match.
type NoRegexMatchError struct{}
type NoRegexMatchError struct {
Pattern ogenregex.Regexp
}

// MaxLengthError implements error.
func (*NoRegexMatchError) Error() string {
return "no regex match"
func (e *NoRegexMatchError) Error() string {
return fmt.Sprintf("no regex match: %s", e.Pattern.String())
}
4 changes: 3 additions & 1 deletion validate/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func (t String) Validate(v string) error {
return errors.Wrap(err, "execute regex")
}
if !match {
return &NoRegexMatchError{}
return &NoRegexMatchError{
Pattern: r,
}
}
}
return nil
Expand Down

0 comments on commit 44a0996

Please sign in to comment.