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 8 commits into
base: master
Choose a base branch
from
Open
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
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.ZogErrCode]string{
zconst.TypeString: {
zconst.ErrCodeRequired: "is required",
zconst.ErrCodeNotNil: "must not be empty",
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.ErrCodeUUID: "must be a valid UUID",
zconst.ErrCodeMatch: "string is invalid",
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.ErrCodeRequired: "is required",
internals.NotErrCode(zconst.ErrCodeRequired): "is not required",
zconst.ErrCodeNotNil: "must not be empty",
internals.NotErrCode(zconst.ErrCodeNotNil): "must be empty",
zconst.ErrCodeMin: "string must contain at least {{min}} character(s)",
internals.NotErrCode(zconst.ErrCodeMin): "string must contain less than {{min}} character(s)",
zconst.ErrCodeMax: "string must contain at most {{max}} character(s)",
internals.NotErrCode(zconst.ErrCodeMax): "string must contain more than {{max}} character(s)",
zconst.ErrCodeLen: "string must be exactly {{len}} character(s)",
internals.NotErrCode(zconst.ErrCodeLen): "string must not be exactly {{len}} character(s)",
zconst.ErrCodeEmail: "must be a valid email",
internals.NotErrCode(zconst.ErrCodeEmail): "must not be a valid email",
zconst.ErrCodeUUID: "must be a valid UUID",
internals.NotErrCode(zconst.ErrCodeUUID): "must not be a valid UUID",
zconst.ErrCodeMatch: "string must match the pattern",
internals.NotErrCode(zconst.ErrCodeMatch): "string must not match the pattern",
zconst.ErrCodeURL: "must be a valid URL",
internals.NotErrCode(zconst.ErrCodeURL): "must not be a valid URL",
zconst.ErrCodeHasPrefix: "string must start with {{prefix}}",
internals.NotErrCode(zconst.ErrCodeHasPrefix): "string must not start with {{prefix}}",
zconst.ErrCodeHasSuffix: "string must end with {{suffix}}",
internals.NotErrCode(zconst.ErrCodeHasSuffix): "string must not end with {{suffix}}",
zconst.ErrCodeContains: "string must contain {{contained}}",
internals.NotErrCode(zconst.ErrCodeContains): "string must not contain {{contained}}",
zconst.ErrCodeContainsDigit: "string must contain at least one digit",
internals.NotErrCode(zconst.ErrCodeContainsDigit): "string must not contain any digits",
zconst.ErrCodeContainsUpper: "string must contain at least one uppercase letter",
internals.NotErrCode(zconst.ErrCodeContainsUpper): "string must not contain any uppercase letters",
zconst.ErrCodeContainsLower: "string must contain at least one lowercase letter",
internals.NotErrCode(zconst.ErrCodeContainsLower): "string must not contain any lowercase letters",
zconst.ErrCodeContainsSpecial: "string must contain at least one special character",
internals.NotErrCode(zconst.ErrCodeContainsSpecial): "string must not contain any special character",
zconst.ErrCodeOneOf: "string must be one of {{one_of_options}}",
internals.NotErrCode(zconst.ErrCodeOneOf): "string must not be one of {{one_of_options}}",
zconst.ErrCodeFallback: "string is invalid",
},
zconst.TypeBool: {
zconst.ErrCodeRequired: "is required",
Expand Down
25 changes: 25 additions & 0 deletions internals/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package internals

import (
"fmt"
"strings"

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

func SafeString(x any) string {
Expand All @@ -17,3 +20,25 @@ func SafeError(x error) string {
}
return x.Error()
}

func AddTest(testArr []Test, t Test, isNot bool) []Test {
if !isNot {
return append(testArr, t)
}

// Saving old functions required here to prevent recursive call during assignment.
oldFn := t.ValidateFunc
t.ValidateFunc = func(val any, ctx ParseCtx) bool {
return !oldFn(val, ctx)
}
t.ErrCode = NotErrCode(t.ErrCode)

return append(testArr, t)
}

func NotErrCode(e zconst.ZogErrCode) string {
if strings.HasPrefix(e, "not_") {
return zconst.ZogErrCode(strings.TrimPrefix(e, "not_"))
}
return zconst.ZogErrCode(fmt.Sprintf("not_%s", e))
}
59 changes: 30 additions & 29 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type StringSchema struct {
required *p.Test
catch *string
coercer conf.CoercerFunc
isNot bool
}

// ! INTERNALS
Expand Down Expand Up @@ -157,8 +158,7 @@ func (v *StringSchema) Test(t p.Test, opts ...TestOption) *StringSchema {
for _, opt := range opts {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is one of the enum values
Expand All @@ -167,8 +167,7 @@ func (v *StringSchema) OneOf(enum []string, options ...TestOption) *StringSchema
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is at least n characters long
Expand All @@ -177,8 +176,7 @@ func (v *StringSchema) Min(n int, options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is at most n characters long
Expand All @@ -187,8 +185,7 @@ func (v *StringSchema) Max(n int, options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is exactly n characters long
Expand All @@ -197,8 +194,7 @@ func (v *StringSchema) Len(n int, options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is a valid email address
Expand All @@ -216,8 +212,7 @@ func (v *StringSchema) Email(options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is a valid URL
Expand All @@ -236,8 +231,7 @@ func (v *StringSchema) URL(options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value has the prefix
Expand All @@ -257,8 +251,7 @@ func (v *StringSchema) HasPrefix(s string, options ...TestOption) *StringSchema
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value has the suffix
Expand All @@ -278,8 +271,7 @@ func (v *StringSchema) HasSuffix(s string, options ...TestOption) *StringSchema
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value contains the substring
Expand All @@ -299,8 +291,7 @@ func (v *StringSchema) Contains(sub string, options ...TestOption) *StringSchema
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value contains an uppercase letter
Expand All @@ -323,8 +314,7 @@ func (v *StringSchema) ContainsUpper(options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value contains a digit
Expand All @@ -349,8 +339,7 @@ func (v *StringSchema) ContainsDigit(options ...TestOption) *StringSchema {
opt(&t)
}

v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value contains a special character
Expand All @@ -377,8 +366,7 @@ func (v *StringSchema) ContainsSpecial(options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that the value is a valid uuid
Expand All @@ -396,8 +384,7 @@ func (v *StringSchema) UUID(options ...TestOption) *StringSchema {
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
return v.addTest(t)
}

// Test: checks that value matches to regex
Expand All @@ -417,6 +404,20 @@ func (v *StringSchema) Match(regex *regexp.Regexp, options ...TestOption) *Strin
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v.addTest(t)
}

// Test: nots the next test fn
func (v *StringSchema) Not() *StringSchema {
v.isNot = !v.isNot
return v
}

func (v *StringSchema) addTest(t p.Test) *StringSchema {
v.tests = p.AddTest(v.tests, t, v.isNot)
if v.isNot {
v.isNot = false
}

return v
}
90 changes: 90 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"regexp"
"testing"

"github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -377,3 +378,92 @@ func TestStringGetType(t *testing.T) {
s := String()
assert.Equal(t, zconst.TypeString, s.getType())
}

func TestStringNot(t *testing.T) {
tests := map[string]struct {
schema *StringSchema
strVal string
expectedErrMap internals.ZogErrList
}{
"not len success": {
schema: String().Not().Len(10).Contains("test"),
strVal: "test",
expectedErrMap: nil,
},
"not len fail": {
schema: String().Not().Len(4).Contains("t"),
strVal: "test",
expectedErrMap: internals.ZogErrList{
&internals.ZogErr{
C: "not_len",
ParamsM: map[string]any{"len": 4},
Typ: "string",
Val: "test",
Msg: "string must not be exactly 4 character(s)",
Err: nil,
},
},
},
"double not success": {
schema: String().Not().Not().Len(4),
strVal: "test",
expectedErrMap: nil,
},
"double not fail": {
schema: String().Not().Not().Len(4),
strVal: "testing",
expectedErrMap: internals.ZogErrList{
&internals.ZogErr{
C: "len",
ParamsM: map[string]any{"len": 4},
Typ: "string",
Val: "testing",
Msg: "string must be exactly 4 character(s)",
Err: nil,
},
},
},
"not email": {
schema: String().Not().Email(),
strVal: "not-an-email",
expectedErrMap: nil,
},

"not email failure": {
schema: String().Not().Email(),
strVal: "test@test.com",
expectedErrMap: internals.ZogErrList{
&internals.ZogErr{
C: "not_email",
ParamsM: nil,
Typ: "string",
Val: "test@test.com",
Msg: "must not be a valid email",
Err: nil,
},
},
},
"not with empty": {
schema: String().Not().Len(1),
strVal: "a",
expectedErrMap: internals.ZogErrList{
&internals.ZogErr{
C: "not_len",
ParamsM: map[string]any{"len": 1},
Typ: "string",
Val: "a",
Msg: "string must not be exactly 1 character(s)",
Err: nil,
},
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var dest string
errMap := tc.schema.Parse(tc.strVal, &dest)
assert.Equal(t, tc.expectedErrMap, errMap)
})
}
}