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

Add string regex and uuid validators #40

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ String().Max(10)
String().Len(5)
String().Email()
String().URL()
String().UUID()
String().Regex()
String().Contains(string)
String().ContainsUpper()
String().ContainsDigit()
Expand Down
2 changes: 2 additions & 0 deletions i18n/en/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var Map zconst.LangMap = map[zconst.ZogType]map[zconst.ZogErrCode]string{
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.ErrCodeRegex: "string is invalid",
zconst.ErrCodeURL: "must be a valid URL",
zconst.ErrCodeHasPrefix: "string must start with {{prefix}}",
zconst.ErrCodeHasSuffix: "string must end with {{suffix}}",
Expand Down
2 changes: 2 additions & 0 deletions i18n/es/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var Map zconst.LangMap = map[zconst.ZogType]map[zconst.ZogErrCode]string{
zconst.ErrCodeMax: "Cadena debe contener como máximo {{max}} caracter(es)",
zconst.ErrCodeLen: "Cadena debe tener exactamente {{len}} caracter(es)",
zconst.ErrCodeEmail: "Debe ser un correo electrónico válido",
zconst.ErrCodeUUID: "Debe ser un UUID válido",
zconst.ErrCodeRegex: "Cadena no es válida",
zconst.ErrCodeURL: "Debe ser una URL válida",
zconst.ErrCodeHasPrefix: "Cadena debe comenzar con {{prefix}}",
zconst.ErrCodeHasSuffix: "Cadena debe terminar con {{suffix}}",
Expand Down
39 changes: 39 additions & 0 deletions string.go
Oudwins marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forgot to mention. My bad. I would like just the string as the param. So users can add it to the error message very easily if they want to by just modifying the error message to "String does not match {{match}}".

You can get the string out of the regex by doing regex.String()

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var (
emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
uuidRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$`)
)

type stringProcessor struct {
Expand Down Expand Up @@ -316,3 +317,41 @@ func (v *stringProcessor) ContainsSpecial(options ...TestOption) *stringProcesso
v.tests = append(v.tests, t)
return v
}

// checks that the value is a valid uuid
func (v *stringProcessor) UUID(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeUUID,
ValidateFunc: func(v any, ctx ParseCtx) bool {
uuid, ok := v.(string)
if !ok {
return false
}
return uuidRegex.MatchString(uuid)
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}

// checks that value matches to regex
func (v *stringProcessor) Regex(regex *regexp.Regexp, options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeRegex,
ValidateFunc: func(v any, ctx ParseCtx) bool {
s, ok := v.(string)
if !ok {
return false
}
return regex.MatchString(s)
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
32 changes: 32 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zog

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -315,3 +316,34 @@ func TestStringOneOf(t *testing.T) {
assert.NotEmpty(t, errs)
assert.Equal(t, "custom required", errs[0].Message())
}

func TestStringUUID(t *testing.T) {
field := String().UUID(Message("custom uuid msg"))
var dest string

errs := field.Parse("f81d4fae-7dec-11d0-a765-00a0c91e", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom uuid msg", errs[0].Message())

errs = field.Parse("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", &dest)
assert.Empty(t, errs)
assert.Equal(t, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", dest)

errs = field.Parse("F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6", &dest)
assert.Empty(t, errs)
assert.Equal(t, "F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6", dest)
}

func TestStringRegex(t *testing.T) {
r := regexp.MustCompile("^[0-9]{2}$")
field := String().Regex(r, Message("custom regex msg"))
var dest string

errs := field.Parse("f81d4fae-7dec-11d0-a765-00a0c91e", &dest)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom regex msg", errs[0].Message())

errs = field.Parse("00", &dest)
assert.Empty(t, errs)
assert.Equal(t, "00", dest)
}
2 changes: 2 additions & 0 deletions zconst/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (

// string only
ErrCodeEmail ZogErrCode = "email"
ErrCodeUUID ZogErrCode = "uuid"
ErrCodeRegex ZogErrCode = "regex"
ErrCodeURL ZogErrCode = "url"
ErrCodeHasPrefix ZogErrCode = "prefix"
ErrCodeHasSuffix ZogErrCode = "suffix"
Expand Down
Loading