Skip to content

Commit

Permalink
refactor: move default map to i18n/en package
Browse files Browse the repository at this point in the history
  • Loading branch information
Oudwins committed Sep 19, 2024
1 parent 9553766 commit d1b398f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 59 deletions.
61 changes: 3 additions & 58 deletions conf/Errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,16 @@ import (
"fmt"
"strings"

"github.com/Oudwins/zog/i18n/en"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)

// Map used to format errors in Zog. Both ZogType & ZogErrCode are just strings
type LangMap = map[zconst.ZogType]map[zconst.ZogErrCode]string

// 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 LangMap = 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}}",
zconst.ErrCodeFallback: "string is invalid",
},
zconst.TypeBool: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeTrue: "must be true",
zconst.ErrCodeFalse: "must be false",
zconst.ErrCodeFallback: "value is invalid",
},
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}}",
zconst.ErrCodeFallback: "number is invalid",
},
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}}",
zconst.ErrCodeFallback: "time is invalid",
},
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}}",
zconst.ErrCodeFallback: "slice is invalid",
},
zconst.TypeStruct: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeFallback: "struct is invalid",
},
}
var DefaultErrMsgMap zconst.LangMap = en.Map

func NewDefaultFormatter(m LangMap) p.ErrFmtFunc {
func NewDefaultFormatter(m zconst.LangMap) p.ErrFmtFunc {
return func(e p.ZogError, p p.ParseCtx) {
if e.Message() != "" {
return
Expand Down
60 changes: 60 additions & 0 deletions i18n/en/en.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package en

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

var Map zconst.LangMap = 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 {{max}} 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}}",
zconst.ErrCodeFallback: "string is invalid",
},
zconst.TypeBool: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeTrue: "must be true",
zconst.ErrCodeFalse: "must be false",
zconst.ErrCodeFallback: "value is invalid",
},
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}}",
zconst.ErrCodeFallback: "number is invalid",
},
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}}",
zconst.ErrCodeFallback: "time is invalid",
},
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}}",
zconst.ErrCodeFallback: "slice is invalid",
},
zconst.TypeStruct: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeFallback: "struct is invalid",
},
}
3 changes: 2 additions & 1 deletion i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package i18n
import (
"github.com/Oudwins/zog/conf"
"github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)

// Takes a map[langKey]conf.LangMap
func SetLanguagesErrsMap(m map[string]conf.LangMap, defaultLang string) {
func SetLanguagesErrsMap(m map[string]zconst.LangMap, defaultLang string) {
langKey := "lang"

conf.ErrorFormatter = func(e internals.ZogError, ctx internals.ParseCtx) {
Expand Down
3 changes: 3 additions & 0 deletions zconst/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const (
ZogTag = "zog"
)

// Map used to format errors in Zog. Both ZogType & ZogErrCode are just strings
type LangMap = map[ZogType]map[ZogErrCode]string

type ZogType = string

const (
Expand Down

0 comments on commit d1b398f

Please sign in to comment.