Skip to content

Commit

Permalink
fix: required custom z.Message
Browse files Browse the repository at this point in the history
  • Loading branch information
Oudwins committed Sep 16, 2024
1 parent 143e3cf commit 3aeebf0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (v *sliceProcessor) process(val any, dest any, path p.PathBuilder, ctx p.Pa
return
} else {
// REQUIRED & ZERO VALUE
ctx.NewError(path, Errors.Required(val, destType))
ctx.NewError(path, Errors.FromTest(val, destType, v.required, ctx))
return
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zog

import (
"net/url"
"regexp"
"strings"

Expand All @@ -10,7 +11,6 @@ import (

var (
emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
urlRegex = regexp.MustCompile(`^(http(s)?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/?$`)
)

type stringProcessor struct {
Expand Down Expand Up @@ -166,12 +166,12 @@ func (v *stringProcessor) URL(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: p.ErrCodeURL,
ValidateFunc: func(v any, ctx p.ParseCtx) bool {
u, ok := v.(string)
s, ok := v.(string)
if !ok {
return false
}
isOk := urlRegex.MatchString(u)
return isOk
u, err := url.Parse(s)
return err == nil && u.Scheme != "" && u.Host != ""
},
}
for _, opt := range options {
Expand Down
3 changes: 2 additions & 1 deletion string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func TestStringLength(t *testing.T) {
}

func TestStringRequired(t *testing.T) {
field := String().Required()
field := String().Required(Message("a"))
var dest string

errs := field.Parse("", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, errs[0].Message(), "a")

errs = field.Parse("foo", &dest)
assert.Empty(t, errs)
Expand Down
2 changes: 1 addition & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (v *structProcessor) process(data any, dest any, path p.PathBuilder, ctx p.
if v.required == nil {
return
} else {
ctx.NewError(path, Errors.Required(data, destType))
ctx.NewError(path, Errors.FromTest(data, destType, v.required, ctx))
return
}
}
Expand Down
6 changes: 1 addition & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func (e *errHelpers) WrapUnknown(o any, destType p.ZogType, err error) p.ZogErro
return zerr
}

func (e *errHelpers) Required(o any, destType p.ZogType) p.ZogError {
return e.New(p.ErrCodeRequired, o, destType, nil, "", nil)
}

func (e *errHelpers) New(code p.ZogErrCode, o any, destType p.ZogType, params map[string]any, msg string, err error) p.ZogError {
return &p.ZogErr{
C: code,
Expand Down Expand Up @@ -178,7 +174,7 @@ func primitiveProcessor[T p.ZogPrimitive](val any, dest any, path p.PathBuilder,
*destPtr = *catch
hasCatched = true
} else {
ctx.NewError(path, Errors.Required(val, destType))
ctx.NewError(path, Errors.FromTest(val, destType, required, ctx))
return
}
}
Expand Down

0 comments on commit 3aeebf0

Please sign in to comment.