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

refactor: packages zconst & primitives -> internals #27

Merged
merged 3 commits into from
Sep 17, 2024
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,9 @@ conf.ErrorFormatter = func(e p.ZogError, p z.ParseCtx) {
}

// override specific error messages
// For this you will need to import `zog/primitives` package. Which you should use with caution since it is an internal package
conf.DefaultErrMsgMap["string"]["my_custom_error_code"] = "my custom error message"
// For this I recommend you import `zod/zconst` which contains zog constants
conf.DefaultErrMsgMap[zconst.TypeString]["my_custom_error_code"] = "my custom error message"
conf.DefaultErrMsgMap[zconst.TypeString][zconst.ErrCodeRequired] = "Now all required errors will get this message"
```

## Zog Schema Parsign Execution Structure
Expand Down
2 changes: 1 addition & 1 deletion boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zog

import (
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/primitives"
p "github.com/Oudwins/zog/internals"
)

type boolProcessor struct {
Expand Down
87 changes: 45 additions & 42 deletions conf/Errors.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
package conf

// Default error messages for all schemas. Replace the text with your own messages to customize the error messages for all zog schemas
// As a general rule of thumb, if an error message only has one parameter, the parameter name will be the same as the error code
import (
"fmt"
"strings"

p "github.com/Oudwins/zog/primitives"
p "github.com/Oudwins/zog/internals"
zconst "github.com/Oudwins/zog/zconst"
)

// Default error messages for all schemas. Replace the text with your own messages to customize the error messages for all zog schemas
// As a general rule of thumb, if an error message only has one parameter, the parameter name will be the same as the error code
var DefaultErrMsgMap = map[p.ZogType]map[p.ZogErrCode]string{
p.TypeString: {
p.ErrCodeRequired: "is required",
p.ErrCodeMin: "string must contain at least {{min}} character(s)",
p.ErrCodeMax: "string must contain at most {{min}} character(s)",
p.ErrCodeLen: "string must be exactly {{len}} character(s)",
p.ErrCodeEmail: "must be a valid email",
p.ErrCodeURL: "must be a valid URL",
p.ErrCodeHasPrefix: "string must start with {{prefix}}",
p.ErrCodeHasSuffix: "string must end with {{suffix}}",
p.ErrCodeContains: "string must contain {{contained}}",
p.ErrCodeContainsDigit: "string must contain at least one digit",
p.ErrCodeContainsUpper: "string must contain at least one uppercase letter",
p.ErrCodeContainsLower: "string must contain at least one lowercase letter",
p.ErrCodeContainsSpecial: "string must contain at least one special character",
p.ErrCodeOneOf: "string must be one of {{one_of_options}}",
var DefaultErrMsgMap = map[zconst.ZogType]map[zconst.ZogErrCode]string{
zconst.TypeString: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeMin: "string must contain at least {{min}} character(s)",
zconst.ErrCodeMax: "string must contain at most {{min}} character(s)",
zconst.ErrCodeLen: "string must be exactly {{len}} character(s)",
zconst.ErrCodeEmail: "must be a valid email",
zconst.ErrCodeURL: "must be a valid URL",
zconst.ErrCodeHasPrefix: "string must start with {{prefix}}",
zconst.ErrCodeHasSuffix: "string must end with {{suffix}}",
zconst.ErrCodeContains: "string must contain {{contained}}",
zconst.ErrCodeContainsDigit: "string must contain at least one digit",
zconst.ErrCodeContainsUpper: "string must contain at least one uppercase letter",
zconst.ErrCodeContainsLower: "string must contain at least one lowercase letter",
zconst.ErrCodeContainsSpecial: "string must contain at least one special character",
zconst.ErrCodeOneOf: "string must be one of {{one_of_options}}",
},
p.TypeBool: {
p.ErrCodeRequired: "is required",
p.ErrCodeTrue: "must be true",
p.ErrCodeFalse: "must be false",
zconst.TypeBool: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeTrue: "must be true",
zconst.ErrCodeFalse: "must be false",
},
p.TypeNumber: {
p.ErrCodeRequired: "is required",
p.ErrCodeLTE: "number must be less than or equal to {{lte}}",
p.ErrCodeLT: "number must be less than {{lt}}",
p.ErrCodeGTE: "number must be greater than or equal to {{gte}}",
p.ErrCodeGT: "number must be greater than {{gt}}",
p.ErrCodeEQ: "number must be equal to {{eq}}",
p.ErrCodeOneOf: "number must be one of {{options}}",
zconst.TypeNumber: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeLTE: "number must be less than or equal to {{lte}}",
zconst.ErrCodeLT: "number must be less than {{lt}}",
zconst.ErrCodeGTE: "number must be greater than or equal to {{gte}}",
zconst.ErrCodeGT: "number must be greater than {{gt}}",
zconst.ErrCodeEQ: "number must be equal to {{eq}}",
zconst.ErrCodeOneOf: "number must be one of {{options}}",
},
p.TypeTime: {
p.ErrCodeRequired: "is required",
p.ErrCodeAfter: "time must be after {{after}}",
p.ErrCodeBefore: "time must be before {{before}}",
p.ErrCodeEQ: "time must be equal to {{eq}}",
zconst.TypeTime: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeAfter: "time must be after {{after}}",
zconst.ErrCodeBefore: "time must be before {{before}}",
zconst.ErrCodeEQ: "time must be equal to {{eq}}",
},
p.TypeSlice: {
p.ErrCodeRequired: "is required",
p.ErrCodeMin: "slice must contain at least {{min}} items",
p.ErrCodeMax: "slice must contain at most {{max}} items",
p.ErrCodeLen: "slice must contain exactly {{len}} items",
p.ErrCodeContains: "slice must contain {{contained}}",
zconst.TypeSlice: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeMin: "slice must contain at least {{min}} items",
zconst.ErrCodeMax: "slice must contain at most {{max}} items",
zconst.ErrCodeLen: "slice must contain exactly {{len}} items",
zconst.ErrCodeContains: "slice must contain {{contained}}",
},
p.TypeStruct: {
p.ErrCodeRequired: "is required",
zconst.TypeStruct: {
zconst.ErrCodeRequired: "is required",
},
}

Expand Down
9 changes: 5 additions & 4 deletions conf/Errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package conf
import (
"testing"

p "github.com/Oudwins/zog/primitives"
p "github.com/Oudwins/zog/internals"
zconst "github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)

Expand All @@ -12,9 +13,9 @@ func TestDefaultErrorFormatter(t *testing.T) {
input p.ZogError
want string
}{
{input: &p.ZogErr{C: p.ErrCodeRequired, Typ: p.TypeString}, want: DefaultErrMsgMap[p.TypeString][p.ErrCodeRequired]},
{input: &p.ZogErr{C: p.ErrCodeRequired, Typ: p.TypeString, Msg: "DON'T OVERRIDE ME"}, want: "DON'T OVERRIDE ME"},
{input: &p.ZogErr{C: "INVALID_ERR_CODE", Typ: p.TypeString}, want: "string is invalid"},
{input: &p.ZogErr{C: zconst.ErrCodeRequired, Typ: zconst.TypeString}, want: DefaultErrMsgMap[zconst.TypeString][zconst.ErrCodeRequired]},
{input: &p.ZogErr{C: zconst.ErrCodeRequired, Typ: zconst.TypeString, Msg: "DON'T OVERRIDE ME"}, want: "DON'T OVERRIDE ME"},
{input: &p.ZogErr{C: "INVALID_ERR_CODE", Typ: zconst.TypeString}, want: "string is invalid"},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion primitives/DataProviders.go → internals/DataProviders.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package primitives
package internals

import "reflect"

Expand Down
2 changes: 1 addition & 1 deletion primitives/PathBuilder.go → internals/PathBuilder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package primitives
package internals

import "strings"

Expand Down
18 changes: 11 additions & 7 deletions primitives/errors.go → internals/errors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package primitives
package internals

import (
zconst "github.com/Oudwins/zog/zconst"
)

// Error interface returned from all processors
type ZogError interface {
Code() ZogErrCode
Code() zconst.ZogErrCode
Value() any // the error value
Dtype() string // destination type
Params() map[string]any
Expand All @@ -19,16 +23,16 @@ type ErrFmtFunc = func(e ZogError, p ParseCtx)

// Error implementation
type ZogErr struct {
C ZogErrCode // error code
ParamsM map[string]any // params for the error (e.g. min, max, len, etc)
Typ string // destination type
Val any // value that caused the error
C zconst.ZogErrCode // error code
ParamsM map[string]any // params for the error (e.g. min, max, len, etc)
Typ string // destination type
Val any // value that caused the error
Msg string
Err error // the underlying error
}

// error code, err uuid
func (e *ZogErr) Code() ZogErrCode {
func (e *ZogErr) Code() zconst.ZogErrCode {
return e.C
}

Expand Down
41 changes: 21 additions & 20 deletions primitives/genericTests.go → internals/genericTests.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package primitives
package internals

import (
"reflect"

zconst "github.com/Oudwins/zog/zconst"
"golang.org/x/exp/constraints"
)

func Required() Test {
t := Test{
ErrCode: ErrCodeRequired,
ErrCode: zconst.ErrCodeRequired,
ValidateFunc: func(val any, ctx ParseCtx) bool {
return !IsZeroValue(val)
},
Expand All @@ -22,20 +23,20 @@ type LengthCapable[K any] interface {

func LenMin[T LengthCapable[any]](n int) Test {
t := Test{
ErrCode: ErrCodeMin,
ErrCode: zconst.ErrCodeMin,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
x := val.(T)
return len(x) >= n
},
}
t.Params[ErrCodeMin] = n
t.Params[zconst.ErrCodeMin] = n
return t
}

func LenMax[T LengthCapable[any]](n int) Test {
t := Test{
ErrCode: ErrCodeMax,
ErrCode: zconst.ErrCodeMax,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(T)
Expand All @@ -45,13 +46,13 @@ func LenMax[T LengthCapable[any]](n int) Test {
return len(val) <= n
},
}
t.Params[ErrCodeMax] = n
t.Params[zconst.ErrCodeMax] = n
return t
}

func Len[T LengthCapable[any]](n int) Test {
t := Test{
ErrCode: ErrCodeLen,
ErrCode: zconst.ErrCodeLen,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(T)
Expand All @@ -61,13 +62,13 @@ func Len[T LengthCapable[any]](n int) Test {
return len(val) == n
},
}
t.Params[ErrCodeLen] = n
t.Params[zconst.ErrCodeLen] = n
return t
}

func In[T any](values []T) Test {
t := Test{
ErrCode: ErrCodeOneOf,
ErrCode: zconst.ErrCodeOneOf,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
for _, value := range values {
Expand All @@ -79,13 +80,13 @@ func In[T any](values []T) Test {
return false
},
}
t.Params[ErrCodeOneOf] = values
t.Params[zconst.ErrCodeOneOf] = values
return t
}

func EQ[T comparable](n T) Test {
t := Test{
ErrCode: ErrCodeEQ,
ErrCode: zconst.ErrCodeEQ,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
v, ok := val.(T)
Expand All @@ -95,13 +96,13 @@ func EQ[T comparable](n T) Test {
return v == n
},
}
t.Params[ErrCodeEQ] = n
t.Params[zconst.ErrCodeEQ] = n
return t
}

func LTE[T constraints.Ordered](n T) Test {
t := Test{
ErrCode: ErrCodeLTE,
ErrCode: zconst.ErrCodeLTE,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
v, ok := val.(T)
Expand All @@ -111,13 +112,13 @@ func LTE[T constraints.Ordered](n T) Test {
return v <= n
},
}
t.Params[ErrCodeLTE] = n
t.Params[zconst.ErrCodeLTE] = n
return t
}

func GTE[T constraints.Ordered](n T) Test {
t := Test{
ErrCode: ErrCodeGTE,
ErrCode: zconst.ErrCodeGTE,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
v, ok := val.(T)
Expand All @@ -127,13 +128,13 @@ func GTE[T constraints.Ordered](n T) Test {
return v >= n
},
}
t.Params[ErrCodeGTE] = n
t.Params[zconst.ErrCodeGTE] = n
return t
}

func LT[T constraints.Ordered](n T) Test {
t := Test{
ErrCode: ErrCodeLT,
ErrCode: zconst.ErrCodeLT,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
v, ok := val.(T)
Expand All @@ -143,13 +144,13 @@ func LT[T constraints.Ordered](n T) Test {
return v < n
},
}
t.Params[ErrCodeLT] = n
t.Params[zconst.ErrCodeLT] = n
return t
}

func GT[T constraints.Ordered](n T) Test {
t := Test{
ErrCode: ErrCodeGT,
ErrCode: zconst.ErrCodeGT,
Params: make(map[string]any, 1),
ValidateFunc: func(val any, ctx ParseCtx) bool {
v, ok := val.(T)
Expand All @@ -159,6 +160,6 @@ func GT[T constraints.Ordered](n T) Test {
return v > n
},
}
t.Params[ErrCodeGT] = n
t.Params[zconst.ErrCodeGT] = n
return t
}
6 changes: 4 additions & 2 deletions primitives/parsing.go → internals/parsing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package primitives
package internals

import zconst "github.com/Oudwins/zog/zconst"

type ZogParseCtx struct {
Fmter ErrFmtFunc
Expand Down Expand Up @@ -49,7 +51,7 @@ func NewParseCtx(errs ZogErrors, fmter ErrFmtFunc) *ZogParseCtx {
type TestFunc = func(val any, ctx ParseCtx) bool

type Test struct {
ErrCode ZogErrCode
ErrCode zconst.ZogErrCode
Params map[string]any
ErrFmt ErrFmtFunc
ValidateFunc TestFunc
Expand Down
2 changes: 1 addition & 1 deletion primitives/types.go → internals/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package primitives
package internals

import "time"

Expand Down
2 changes: 1 addition & 1 deletion primitives/utils.go → internals/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package primitives
package internals

import "reflect"

Expand Down
2 changes: 1 addition & 1 deletion numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zog

import (
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/primitives"
p "github.com/Oudwins/zog/internals"
)

type Numeric interface {
Expand Down
Loading
Loading