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 not operator #76

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1ff3383
feat: add not for strings
Jh123x Feb 7, 2025
81ced9c
feat: update not operator
Jh123x Feb 8, 2025
2ceedfa
feat: add i18n
Jh123x Feb 8, 2025
bf5e2d1
improve test coverage
Jh123x Feb 8, 2025
18c8073
fix: update i18n wrong sentences
Jh123x Feb 8, 2025
1ce2005
fix: fix i18n according to coderabbitai
Jh123x Feb 8, 2025
00a28a2
chore: update comments
Jh123x Feb 9, 2025
ce034a2
fix: use current alias
Jh123x Feb 9, 2025
4dcc9ab
feat: restrict not functions
Jh123x Mar 5, 2025
06878b2
Merge remote-tracking branch 'upstream/master' into feat/add_not
Jh123x Mar 5, 2025
8a7aca7
fix: update issue code fn
Jh123x Mar 5, 2025
b74ffa6
docs: update docs
Jh123x Mar 5, 2025
b409aa3
test: improve test coverage
Jh123x Mar 5, 2025
b746181
Merge remote-tracking branch 'origin/master' into feat/add_not
Jh123x Mar 15, 2025
8742063
Merge remote-tracking branch 'origin/master' into feat/add_not
Jh123x Apr 2, 2025
ff91b28
fix: fix not string schema
Jh123x Apr 2, 2025
95396f1
test: update test cases
Jh123x Apr 2, 2025
ecad838
refactor: make string const
Jh123x Apr 2, 2025
614e0a1
test: add string custom test
Jh123x Apr 2, 2025
99efe74
test: add more test cases
Jh123x Apr 2, 2025
53c3e59
Merge remote-tracking branch 'origin/master' into feat/add_not
Jh123x Apr 3, 2025
eb4c297
fix: fix compatibility issue
Jh123x Apr 3, 2025
aa0531b
fix: remove Test compatibility
Jh123x Apr 3, 2025
f81804f
fix: add nil check
Jh123x Apr 3, 2025
28e815b
fix: pass through options
Jh123x Apr 3, 2025
74afdc0
doc: add comment for not function
Jh123x Apr 3, 2025
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
54 changes: 36 additions & 18 deletions i18n/en/en.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
package en

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

var Map zconst.LangMap = map[zconst.ZogType]map[zconst.ZogIssueCode]string{
zconst.TypeString: {
zconst.IssueCodeRequired: "is required",
zconst.IssueCodeNotNil: "must not be empty",
zconst.IssueCodeMin: "string must contain at least {{min}} character(s)",
zconst.IssueCodeMax: "string must contain at most {{max}} character(s)",
zconst.IssueCodeLen: "string must be exactly {{len}} character(s)",
zconst.IssueCodeEmail: "must be a valid email",
zconst.IssueCodeUUID: "must be a valid UUID",
zconst.IssueCodeMatch: "string is invalid",
zconst.IssueCodeURL: "must be a valid URL",
zconst.IssueCodeHasPrefix: "string must start with {{prefix}}",
zconst.IssueCodeHasSuffix: "string must end with {{suffix}}",
zconst.IssueCodeContains: "string must contain {{contained}}",
zconst.IssueCodeContainsDigit: "string must contain at least one digit",
zconst.IssueCodeContainsUpper: "string must contain at least one uppercase letter",
zconst.IssueCodeContainsLower: "string must contain at least one lowercase letter",
zconst.IssueCodeContainsSpecial: "string must contain at least one special character",
zconst.IssueCodeOneOf: "string must be one of {{one_of_options}}",
zconst.IssueCodeFallback: "string is invalid",
internals.NotIssueCode(zconst.IssueCodeRequired): "is not required",
internals.NotIssueCode(zconst.IssueCodeNotNil): "must be empty",
internals.NotIssueCode(zconst.IssueCodeMin): "string must contain less than {{min}} character(s)",
internals.NotIssueCode(zconst.IssueCodeMax): "string must contain more than {{max}} character(s)",
internals.NotIssueCode(zconst.IssueCodeLen): "string must not be exactly {{len}} character(s)",
internals.NotIssueCode(zconst.IssueCodeEmail): "must not be a valid email",
internals.NotIssueCode(zconst.IssueCodeUUID): "must not be a valid UUID",
internals.NotIssueCode(zconst.IssueCodeMatch): "string must not match the pattern",
internals.NotIssueCode(zconst.IssueCodeURL): "must not be a valid URL",
internals.NotIssueCode(zconst.IssueCodeHasPrefix): "string must not start with {{prefix}}",
internals.NotIssueCode(zconst.IssueCodeHasSuffix): "string must not end with {{suffix}}",
internals.NotIssueCode(zconst.IssueCodeContains): "string must not contain {{contained}}",
internals.NotIssueCode(zconst.IssueCodeContainsDigit): "string must not contain any digits",
internals.NotIssueCode(zconst.IssueCodeContainsUpper): "string must not contain any uppercase letters",
internals.NotIssueCode(zconst.IssueCodeContainsLower): "string must not contain any lowercase letters",
internals.NotIssueCode(zconst.IssueCodeContainsSpecial): "string must not contain any special character",
internals.NotIssueCode(zconst.IssueCodeOneOf): "string must not be one of {{one_of_options}}",
zconst.IssueCodeRequired: "is required",
zconst.IssueCodeNotNil: "must not be empty",
zconst.IssueCodeMin: "string must contain at least {{min}} character(s)",
zconst.IssueCodeMax: "string must contain at most {{max}} character(s)",
zconst.IssueCodeLen: "string must be exactly {{len}} character(s)",
zconst.IssueCodeEmail: "must be a valid email",
zconst.IssueCodeUUID: "must be a valid UUID",
zconst.IssueCodeMatch: "string is invalid",
zconst.IssueCodeURL: "must be a valid URL",
zconst.IssueCodeHasPrefix: "string must start with {{prefix}}",
zconst.IssueCodeHasSuffix: "string must end with {{suffix}}",
zconst.IssueCodeContains: "string must contain {{contained}}",
zconst.IssueCodeContainsDigit: "string must contain at least one digit",
zconst.IssueCodeContainsUpper: "string must contain at least one uppercase letter",
zconst.IssueCodeContainsLower: "string must contain at least one lowercase letter",
zconst.IssueCodeContainsSpecial: "string must contain at least one special character",
zconst.IssueCodeOneOf: "string must be one of {{one_of_options}}",
zconst.IssueCodeFallback: "string is invalid",
},
zconst.TypeBool: {
zconst.IssueCodeRequired: "is required",
Expand Down
11 changes: 11 additions & 0 deletions internals/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ func TestFuncFromBool(fn BoolTFunc, test *Test) {
}
}

func TestNotFuncFromBool(fn BoolTFunc, test *Test) {
test.Func = func(val any, ctx Ctx) {
if fn(val, ctx) {
c := ctx.(*SchemaCtx)
issue := c.IssueFromTest(c.Test, val)
issue.Code = NotIssueCode(issue.Code)
ctx.AddIssue(issue)
}
}
}

func NewTestFunc(IssueCode zconst.ZogIssueCode, fn BoolTFunc, options ...TestOption) *Test {
t := &Test{
IssueCode: IssueCode,
Expand Down
23 changes: 21 additions & 2 deletions internals/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@ package internals

import (
"fmt"
"strings"

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

const (
notPrefix = "not_"
defaultString = "<nil>"
)

func SafeString(x any) string {
if x == nil {
return "<nil>"
return defaultString
}
return fmt.Sprintf("%v", x)
}

func SafeError(x error) string {
if x == nil {
return "<nil>"
return defaultString
}
return x.Error()
}

func NotIssueCode(e zconst.ZogIssueCode) string {
if strings.HasPrefix(e, notPrefix) {
return zconst.ZogIssueCode(strings.TrimPrefix(e, notPrefix))
}
return zconst.ZogIssueCode(notPrefix + e)
}

func PtrOf[T any](v T) *T {
return &v
}
Loading