Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add regexp error for handle in app #1100

Merged
merged 3 commits into from
Nov 21, 2023
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
8 changes: 6 additions & 2 deletions internal/integration/all_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"strings"
"testing"

"github.com/go-faster/errors"
"github.com/stretchr/testify/require"

api "github.com/ogen-go/ogen/internal/integration/test_allof"
"github.com/ogen-go/ogen/validate"
)

type allofTestServer struct {
Expand Down Expand Up @@ -51,10 +53,12 @@ func TestAllof(t *testing.T) {
ctx := context.Background()
t.Run("nullableStrings", func(t *testing.T) {
err := client.NullableStrings(ctx, api.NilString{})
require.EqualError(t, err, "validate: string: no regex match")
_, ok := errors.Into[*validate.NoRegexMatchError](err)
require.True(t, ok, "validate: string: no regex match")

err = client.NullableStrings(ctx, api.NewNilString("foo"))
require.EqualError(t, err, "validate: string: no regex match")
_, ok = errors.Into[*validate.NoRegexMatchError](err)
require.True(t, ok, "validate: string: no regex match")

err = client.NullableStrings(ctx, api.NewNilString("127.0.0.1"))
require.NoError(t, err)
Expand Down
8 changes: 8 additions & 0 deletions validate/error.go → validate/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ type MaxLengthError struct {
func (e *MaxLengthError) Error() string {
return fmt.Sprintf("len %d greater than maximum %d", e.Len, e.MaxLength)
}

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

// MaxLengthError implements error.
func (*NoRegexMatchError) Error() string {
return "no regex match"
}
2 changes: 1 addition & 1 deletion validate/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (t String) Validate(v string) error {
return errors.Wrap(err, "execute regex")
}
if !match {
return errors.New("no regex match")
return &NoRegexMatchError{}
}
}
return nil
Expand Down