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 7, 2024
1 parent f756530 commit c05f1a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 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
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 c05f1a5

Please sign in to comment.