Skip to content
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
1 change: 1 addition & 0 deletions boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (v *BoolSchema) Test(t p.Test, options ...TestOption) *BoolSchema {
for _, opt := range options {
opt(&t)
}
t.ValidateFunc = customTestBackwardsCompatWrapper(t.ValidateFunc)
v.tests = append(v.tests, t)
return v
}
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/examples-of-use/rest-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ sidebar_position: 2

# Using Zog in a REST API

Zog providers a helper function called `z.Errors.SanitizeMap(errsMap)` that will return a map of strings of the error messages (stripping out the internal error). So, if you do not mind sending errors to your users in the same form zog returns them, you can do something like this:
Zog providers two helper functions called `z.Issues.SanitizeMap(issueMap)` and `z.Issues.SanitizeList(issueList)` that will return a map of strings of the issue messages (stripping out the internal error). So, if you do not mind sending issue messages to your users in the same form zog returns them, you can do something like this:

```go
errs := schema.Parse(zhttp.Request(r), &userFormData)

if errs != nil {
sanitized := z.Errors.SanitizeMap(errs)
sanitized := z.Issues.SanitizeMap(errs)
// sanitize will be map[string][]string
// for example:
// {"name": []string{"min length is 5", "max length is 10"}, "email": []string{"is not a valid email"}}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/packages/zenv.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Parse() env {
errs := envSchema.Parse(zenv.NewDataProvider(), &e)
if errs != nil {
fmt.Println("FAILURE TO PARSE ENV VARIABLES")
log.Fatal(z.Errors.SanitizeMap(errs))
log.Fatal(z.Issues.SanitizeMap(errs))
}
return e
}
Expand Down
2 changes: 1 addition & 1 deletion error_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func TestErrorMessages(t *testing.T) {
var u Msgs

errs := schema.Parse(map[string]any{"name": "0", "age": 0, "time": "2020-01-01T00:00:00Z", "bool": false, "slice": []string{"x"}}, &u)
sanitized := Errors.SanitizeMap(errs)
sanitized := Issues.SanitizeMapAndCollect(errs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify the migration from Errors to Issues package.

The change from Errors.SanitizeMap to Issues.SanitizeMapAndCollect suggests a package migration. Let's verify that all usages have been updated consistently.


🏁 Script executed:

#!/bin/bash
# Search for any remaining usages of the old Errors.SanitizeMap
rg "Errors\.SanitizeMap"

# Search for all usages of the new Issues.SanitizeMapAndCollect to ensure consistent adoption
rg "Issues\.SanitizeMapAndCollect"

Length of output: 600


Migration Verification: Inconsistent Package References

  • In error_msgs_test.go (line 30), the call to Issues.SanitizeMapAndCollect(errs) is correctly updated.
  • However, residual references to Errors.SanitizeMap remain in the documentation (e.g., in docs/docs/examples-of-use/rest-apis.md and docs/docs/packages/zenv.md). Please verify if these need to be updated for consistent migration across the project.

fmt.Println(sanitized)
}
8 changes: 8 additions & 0 deletions internals/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ type TestCtx struct {
func (c *SchemaCtx) Issue() ZogIssue {
// TODO handle catch here
e := ZogIssuePool.Get().(*ZogErr)
e.C = ""
e.EPath = c.Path.String()
e.Err = nil
e.Msg = ""
e.ParamsM = nil
e.Typ = c.DType
e.Val = c.Val
return e
Expand All @@ -121,6 +125,8 @@ func (c *SchemaCtx) IssueFromTest(test *Test, val any) ZogIssue {
e := ZogIssuePool.Get().(*ZogErr)
e.C = test.IssueCode
e.EPath = c.Path.String()
e.Err = nil
e.Msg = ""
e.Typ = c.DType
e.Val = val
e.ParamsM = test.Params
Expand All @@ -138,6 +144,8 @@ func (c *SchemaCtx) IssueFromCoerce(err error) ZogIssue {
e := ZogIssuePool.Get().(*ZogErr)
e.C = zconst.IssueCodeCoerce
e.EPath = c.Path.String()
e.Err = nil
e.Msg = ""
e.Typ = c.DType
e.Val = c.Val
e.Err = err
Expand Down
36 changes: 18 additions & 18 deletions internals/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func LenMin[T LengthCapable[any]](n int) Test {
IssueCode: zconst.IssueCodeMin,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
x := val.(T)
return len(x) >= n
x := val.(*T)
return len(*x) >= n
},
}
t.Params[zconst.IssueCodeMin] = n
Expand All @@ -51,11 +51,11 @@ func LenMax[T LengthCapable[any]](n int) Test {
IssueCode: zconst.IssueCodeMax,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx Ctx) bool {
val, ok := v.(T)
val, ok := v.(*T)
if !ok {
return false
}
return len(val) <= n
return len(*val) <= n
},
}
t.Params[zconst.IssueCodeMax] = n
Expand All @@ -67,11 +67,11 @@ func Len[T LengthCapable[any]](n int) Test {
IssueCode: zconst.IssueCodeLen,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx Ctx) bool {
val, ok := v.(T)
val, ok := v.(*T)
if !ok {
return false
}
return len(val) == n
return len(*val) == n
},
}
t.Params[zconst.IssueCodeLen] = n
Expand All @@ -84,8 +84,8 @@ func In[T any](values []T) Test {
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
for _, value := range values {
v := val.(T)
if reflect.DeepEqual(v, value) {
v := val.(*T)
if reflect.DeepEqual(*v, value) {
return true
}
}
Expand All @@ -101,11 +101,11 @@ func EQ[T comparable](n T) Test {
IssueCode: zconst.IssueCodeEQ,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
v, ok := val.(T)
v, ok := val.(*T)
if !ok {
return false
}
return v == n
return *v == n
},
}
t.Params[zconst.IssueCodeEQ] = n
Expand All @@ -117,11 +117,11 @@ func LTE[T constraints.Ordered](n T) Test {
IssueCode: zconst.IssueCodeLTE,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
v, ok := val.(T)
v, ok := val.(*T)
if !ok {
return false
}
return v <= n
return *v <= n
},
}
t.Params[zconst.IssueCodeLTE] = n
Expand All @@ -133,11 +133,11 @@ func GTE[T constraints.Ordered](n T) Test {
IssueCode: zconst.IssueCodeGTE,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
v, ok := val.(T)
v, ok := val.(*T)
if !ok {
return false
}
return v >= n
return *v >= n
},
}
t.Params[zconst.IssueCodeGTE] = n
Expand All @@ -149,11 +149,11 @@ func LT[T constraints.Ordered](n T) Test {
IssueCode: zconst.IssueCodeLT,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
v, ok := val.(T)
v, ok := val.(*T)
if !ok {
return false
}
return v < n
return *v < n
},
}
t.Params[zconst.IssueCodeLT] = n
Expand All @@ -165,11 +165,11 @@ func GT[T constraints.Ordered](n T) Test {
IssueCode: zconst.IssueCodeGT,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx Ctx) bool {
v, ok := val.(T)
v, ok := val.(*T)
if !ok {
return false
}
return v > n
return *v > n
},
}
t.Params[zconst.IssueCodeGT] = n
Expand Down
1 change: 1 addition & 0 deletions numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (v *NumberSchema[T]) Test(t p.Test, opts ...TestOption) *NumberSchema[T] {
for _, opt := range opts {
opt(&t)
}
t.ValidateFunc = customTestBackwardsCompatWrapper(t.ValidateFunc)
v.tests = append(v.tests, t)
return v
}
Expand Down
41 changes: 21 additions & 20 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (v *StringSchema) Test(t p.Test, opts ...TestOption) *StringSchema {
for _, opt := range opts {
opt(&t)
}
t.ValidateFunc = customTestBackwardsCompatWrapper(t.ValidateFunc)
v.tests = append(v.tests, t)
return v
}
Expand Down Expand Up @@ -218,11 +219,11 @@ func (v *StringSchema) Email(options ...TestOption) *StringSchema {
t := p.Test{
IssueCode: zconst.IssueCodeEmail,
ValidateFunc: func(v any, ctx ParseCtx) bool {
email, ok := v.(string)
email, ok := v.(*string)
if !ok {
return false
}
return emailRegex.MatchString(email)
return emailRegex.MatchString(*email)
},
}
for _, opt := range options {
Expand All @@ -237,11 +238,11 @@ func (v *StringSchema) URL(options ...TestOption) *StringSchema {
t := p.Test{
IssueCode: zconst.IssueCodeURL,
ValidateFunc: func(v any, ctx ParseCtx) bool {
s, ok := v.(string)
s, ok := v.(*string)
if !ok {
return false
}
u, err := url.Parse(s)
u, err := url.Parse(*s)
return err == nil && u.Scheme != "" && u.Host != ""
},
}
Expand All @@ -258,11 +259,11 @@ func (v *StringSchema) HasPrefix(s string, options ...TestOption) *StringSchema
IssueCode: zconst.IssueCodeHasPrefix,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
val, ok := v.(*string)
if !ok {
return false
}
return strings.HasPrefix(val, s)
return strings.HasPrefix(*val, s)
},
}
t.Params[zconst.IssueCodeHasPrefix] = s
Expand All @@ -279,11 +280,11 @@ func (v *StringSchema) HasSuffix(s string, options ...TestOption) *StringSchema
IssueCode: zconst.IssueCodeHasSuffix,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
val, ok := v.(*string)
if !ok {
return false
}
return strings.HasSuffix(val, s)
return strings.HasSuffix(*val, s)
},
}
t.Params[zconst.IssueCodeHasSuffix] = s
Expand All @@ -300,11 +301,11 @@ func (v *StringSchema) Contains(sub string, options ...TestOption) *StringSchema
IssueCode: zconst.IssueCodeContains,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
val, ok := v.(*string)
if !ok {
return false
}
return strings.Contains(val, sub)
return strings.Contains(*val, sub)
},
}
t.Params[zconst.IssueCodeContains] = sub
Expand All @@ -320,11 +321,11 @@ func (v *StringSchema) ContainsUpper(options ...TestOption) *StringSchema {
t := p.Test{
IssueCode: zconst.IssueCodeContainsUpper,
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
val, ok := v.(*string)
if !ok {
return false
}
for _, r := range val {
for _, r := range *val {
if r >= 'A' && r <= 'Z' {
return true
}
Expand All @@ -344,11 +345,11 @@ func (v *StringSchema) ContainsDigit(options ...TestOption) *StringSchema {
t := p.Test{
IssueCode: zconst.IssueCodeContainsDigit,
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
val, ok := v.(*string)
if !ok {
return false
}
for _, r := range val {
for _, r := range *val {
if r >= '0' && r <= '9' {
return true
}
Expand All @@ -371,11 +372,11 @@ func (v *StringSchema) ContainsSpecial(options ...TestOption) *StringSchema {
p.Test{
IssueCode: zconst.IssueCodeContainsSpecial,
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
val, ok := v.(*string)
if !ok {
return false
}
for _, r := range val {
for _, r := range *val {
if (r >= '!' && r <= '/') ||
(r >= ':' && r <= '@') ||
(r >= '[' && r <= '`') ||
Expand All @@ -398,11 +399,11 @@ func (v *StringSchema) UUID(options ...TestOption) *StringSchema {
t := p.Test{
IssueCode: zconst.IssueCodeUUID,
ValidateFunc: func(v any, ctx ParseCtx) bool {
uuid, ok := v.(string)
uuid, ok := v.(*string)
if !ok {
return false
}
return uuidRegex.MatchString(uuid)
return uuidRegex.MatchString(*uuid)
},
}
for _, opt := range options {
Expand All @@ -418,11 +419,11 @@ func (v *StringSchema) Match(regex *regexp.Regexp, options ...TestOption) *Strin
IssueCode: zconst.IssueCodeMatch,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
s, ok := v.(string)
s, ok := v.(*string)
if !ok {
return false
}
return regex.MatchString(s)
return regex.MatchString(*s)
},
}
t.Params[zconst.IssueCodeMatch] = regex.String()
Expand Down
7 changes: 4 additions & 3 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (v *TimeSchema) Test(t p.Test, opts ...TestOption) *TimeSchema {
for _, opt := range opts {
opt(&t)
}
t.ValidateFunc = customTestBackwardsCompatWrapper(t.ValidateFunc)
v.tests = append(v.tests, t)
return v
}
Expand All @@ -182,7 +183,7 @@ func (v *TimeSchema) After(t time.Time, opts ...TestOption) *TimeSchema {
IssueCode: zconst.IssueCodeAfter,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(time.Time)
val, ok := v.(*time.Time)
if !ok {
return false
}
Expand All @@ -204,7 +205,7 @@ func (v *TimeSchema) Before(t time.Time, opts ...TestOption) *TimeSchema {
IssueCode: zconst.IssueCodeBefore,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(time.Time)
val, ok := v.(*time.Time)
if !ok {
return false
}
Expand All @@ -226,7 +227,7 @@ func (v *TimeSchema) EQ(t time.Time, opts ...TestOption) *TimeSchema {
IssueCode: zconst.IssueCodeEQ,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(time.Time)
val, ok := v.(*time.Time)
if !ok {
return false
}
Expand Down
Loading
Loading