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 2 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
13 changes: 13 additions & 0 deletions internals/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ func SafeError(x error) string {
}
return x.Error()
}

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

oldFn := t.ValidateFunc
t.ValidateFunc = func(val any, ctx ParseCtx) bool {
return !oldFn(val, ctx)
}

return append(testArr, t)
}
60 changes: 31 additions & 29 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/Oudwins/zog/conf"
"github.com/Oudwins/zog/internals"
p "github.com/Oudwins/zog/internals"
Jh123x marked this conversation as resolved.
Show resolved Hide resolved
"github.com/Oudwins/zog/zconst"
)
Expand All @@ -25,6 +26,7 @@ type StringSchema struct {
required *p.Test
catch *string
coercer conf.CoercerFunc
isNot bool
}

// ! INTERNALS
Expand Down Expand Up @@ -157,8 +159,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 +168,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 +177,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 +186,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 +195,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 +213,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 +232,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 +252,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 +272,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 +292,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 +315,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 +340,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 +367,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 +385,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 +405,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 current fn
func (v *StringSchema) Not() *StringSchema {
v.isNot = !v.isNot
return v
}

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

return v
}
37 changes: 37 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,39 @@ 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: "len",
ParamsM: map[string]any{"len": 4},
Typ: "string",
Val: "test",
Msg: "string must be exactly 4 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)
})
}
}