Skip to content

Commit

Permalink
Merge pull request #95 from rezakhademix/remove-unnecessary-pointers
Browse files Browse the repository at this point in the history
feat: removed unneccessary pointer
  • Loading branch information
rezakhademix authored May 8, 2024
2 parents 03a3c4a + a61e8f8 commit 412bf34
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion after.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) After(t, u time.Time, field, msg string) *Validator {
func (v Validator) After(t, u time.Time, field, msg string) Validator {
v.Check(t.After(u), field, v.msg(After, msg, field, u))

return v
Expand Down
2 changes: 1 addition & 1 deletion before.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) Before(t, u time.Time, field, msg string) *Validator {
func (v Validator) Before(t, u time.Time, field, msg string) Validator {
v.Check(t.Before(u), field, v.msg(Before, msg, field, u))

return v
Expand Down
4 changes: 2 additions & 2 deletions between.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) BetweenInt(i, min, max int, field, msg string) *Validator {
func (v Validator) BetweenInt(i, min, max int, field, msg string) Validator {
v.Check(i >= min && i <= max, field, v.msg(Between, msg, field, min, max))

return v
Expand All @@ -31,7 +31,7 @@ func (v *Validator) BetweenInt(i, min, max int, field, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) BetweenFloat(f, min, max float64, field, msg string) *Validator {
func (v Validator) BetweenFloat(f, min, max float64, field, msg string) Validator {
v.Check(f >= min && f <= max, field, v.msg(Between, msg, field, min, max))

return v
Expand Down
2 changes: 1 addition & 1 deletion date.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) Date(layout, d, field, msg string) *Validator {
func (v Validator) Date(layout, d, field, msg string) Validator {
_, err := time.Parse(layout, d)
if err != nil {
v.Check(false, field, v.msg(Date, msg, field))
Expand Down
6 changes: 3 additions & 3 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package govalidator
// v := validator.New()
// var zeroKelvin int64
// v.DefaultInt(&zeroKelvin, -273)
func (v *Validator) DefaultInt(i *int, val int) *Validator {
func (v Validator) DefaultInt(i *int, val int) Validator {
if i == nil || *i == 0 {
*i = val
}
Expand All @@ -24,7 +24,7 @@ func (v *Validator) DefaultInt(i *int, val int) *Validator {
// v := validator.New()
// var f float64
// v.DefaultFloat(&f, 3.14)
func (v *Validator) DefaultFloat(f *float64, val float64) *Validator {
func (v Validator) DefaultFloat(f *float64, val float64) Validator {
if f == nil || *f == 0 {
*f = val
}
Expand All @@ -40,7 +40,7 @@ func (v *Validator) DefaultFloat(f *float64, val float64) *Validator {
// v := validator.New()
// var lang string
// v.DefaultString(&lang, "persian")
func (v *Validator) DefaultString(s *string, val string) *Validator {
func (v Validator) DefaultString(s *string, val string) Validator {
if s == nil || *s == "" {
*s = val
}
Expand Down
6 changes: 3 additions & 3 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_DefaultInt(t *testing.T) {
actual int
expected int
defaultValue int
setDefault func(*int, int) *Validator
setDefault func(*int, int) Validator
}{
{
name: "Test default int of '3' wont be used if value is greater than 0 or nil",
Expand Down Expand Up @@ -53,7 +53,7 @@ func Test_DefaultFloat(t *testing.T) {
actual float64
expected float64
defaultValue float64
setDefault func(*float64, float64) *Validator
setDefault func(*float64, float64) Validator
}{
{
name: "Test default float of '5.0' won't be used if value is greater than 0.0 or nil",
Expand Down Expand Up @@ -92,7 +92,7 @@ func Test_DefaultString(t *testing.T) {
actual string
expected string
defaultValue string
setDefault func(*string, string) *Validator
setDefault func(*string, string) Validator
}{
{
name: "Test default string of 'something' won't be used if a value is already valid",
Expand Down
2 changes: 1 addition & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) Email(s, field, msg string) *Validator {
func (v Validator) Email(s, field, msg string) Validator {
v.RegexMatches(s, EmailRegex, field, v.msg(Email, msg, field))

return v
Expand Down
2 changes: 1 addition & 1 deletion exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) Exists(value any, table, column, field, msg string) *Validator {
func (v Validator) Exists(value any, table, column, field, msg string) Validator {
v.Check(v.repo.Exists(value, table, column), field, v.msg(Exists, msg, field))

return v
Expand Down
2 changes: 1 addition & 1 deletion ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) IP4(s, field, msg string) *Validator {
func (v Validator) IP4(s, field, msg string) Validator {
ip := net.ParseIP(s)

v.Check(ip != nil && ip.To4() != nil, field, v.msg(IP4, msg, field))
Expand Down
6 changes: 3 additions & 3 deletions len.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) LenString(s string, size int, field, msg string) *Validator {
func (v Validator) LenString(s string, size int, field, msg string) Validator {
v.Check(utf8.RuneCountInString(strings.TrimSpace(s)) == size, field, v.msg(Len, msg, field, size))

return v
Expand All @@ -41,7 +41,7 @@ func (v *Validator) LenString(s string, size int, field, msg string) *Validator
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) LenInt(i, size int, field, msg string) *Validator {
func (v Validator) LenInt(i, size int, field, msg string) Validator {
v.Check(len(strconv.Itoa(i)) == size, field, v.msg(Len, msg, field, size))

return v
Expand All @@ -56,7 +56,7 @@ func (v *Validator) LenInt(i, size int, field, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) LenSlice(s []any, size int, field, msg string) *Validator {
func (v Validator) LenSlice(s []any, size int, field, msg string) Validator {
v.Check(len(s) == size, field, v.msg(LenList, msg, field, size))

return v
Expand Down
6 changes: 3 additions & 3 deletions max.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MaxInt(i, max int, field, msg string) *Validator {
func (v Validator) MaxInt(i, max int, field, msg string) Validator {
v.Check(i <= max, field, v.msg(Max, msg, field, max))

return v
Expand All @@ -40,7 +40,7 @@ func (v *Validator) MaxInt(i, max int, field, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MaxFloat(f, max float64, field, msg string) *Validator {
func (v Validator) MaxFloat(f, max float64, field, msg string) Validator {
v.Check(f <= max, field, v.msg(Max, msg, field, max))

return v
Expand All @@ -55,7 +55,7 @@ func (v *Validator) MaxFloat(f, max float64, field, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MaxString(s string, maxLen int, field, msg string) *Validator {
func (v Validator) MaxString(s string, maxLen int, field, msg string) Validator {
v.Check(utf8.RuneCountInString(strings.TrimSpace(s)) <= maxLen, field, v.msg(MaxString, msg, field, maxLen))

return v
Expand Down
6 changes: 3 additions & 3 deletions min.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MinInt(i, min int, field, msg string) *Validator {
func (v Validator) MinInt(i, min int, field, msg string) Validator {
v.Check(i >= min, field, v.msg(Min, msg, field, min))

return v
Expand All @@ -40,7 +40,7 @@ func (v *Validator) MinInt(i, min int, field, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MinFloat(f, min float64, field, msg string) *Validator {
func (v Validator) MinFloat(f, min float64, field, msg string) Validator {
v.Check(f >= min, field, v.msg(Min, msg, field, min))

return v
Expand All @@ -55,7 +55,7 @@ func (v *Validator) MinFloat(f, min float64, field, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MinString(s string, minLen int, field, msg string) *Validator {
func (v Validator) MinString(s string, minLen int, field, msg string) Validator {
v.Check(utf8.RuneCountInString(strings.TrimSpace(s)) >= minLen, field, v.msg(MinString, msg, field, minLen))

return v
Expand Down
2 changes: 1 addition & 1 deletion notexists.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) NotExists(value any, table, column, field, msg string) *Validator {
func (v Validator) NotExists(value any, table, column, field, msg string) Validator {
v.Check(!v.repo.Exists(value, table, column), field, v.msg(NotExists, msg, field))

return v
Expand Down
2 changes: 1 addition & 1 deletion regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RegexMatches(s string, pattern string, field, msg string) *Validator {
func (v Validator) RegexMatches(s string, pattern string, field, msg string) Validator {
r := regexp.MustCompile(pattern)

v.Check(r.Match([]byte(s)), field, v.msg(Regex, msg))
Expand Down
8 changes: 4 additions & 4 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredString(s, field string, msg string) *Validator {
func (v Validator) RequiredString(s, field string, msg string) Validator {
v.Check(strings.TrimSpace(s) != "", field, v.msg(Required, msg, field))

return v
Expand All @@ -33,7 +33,7 @@ func (v *Validator) RequiredString(s, field string, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredInt(i int, field string, msg string) *Validator {
func (v Validator) RequiredInt(i int, field string, msg string) Validator {
v.Check(i != 0, field, v.msg(Required, msg, field))

return v
Expand All @@ -48,7 +48,7 @@ func (v *Validator) RequiredInt(i int, field string, msg string) *Validator {
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator {
func (v Validator) RequiredSlice(s []any, field string, msg string) Validator {
v.Check(len(s) > 0, field, v.msg(Required, msg, field))

return v
Expand All @@ -63,7 +63,7 @@ func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator {
func (v Validator) RequiredFloat(f float64, field string, msg string) Validator {
v.Check(f != 0.0, field, v.msg(Required, msg, field))

return v
Expand Down
2 changes: 1 addition & 1 deletion url.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) URL(s, field, msg string) *Validator {
func (v Validator) URL(s, field, msg string) Validator {
u, err := url.Parse(s)

v.Check(err == nil && u.Scheme != "" && u.Host != "", field, v.msg(URL, msg, field))
Expand Down
2 changes: 1 addition & 1 deletion uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) UUID(u, field, msg string) *Validator {
func (v Validator) UUID(u, field, msg string) Validator {
_, err := uuid.Parse(u)
if err != nil {
v.Check(false, field, v.msg(UUID, msg, field))
Expand Down
18 changes: 9 additions & 9 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ var (
)

// New will return a new validator
func New() *Validator {
return &Validator{
func New() Validator {
return Validator{
errs: make(map[string]string),
}
}
Expand All @@ -59,45 +59,45 @@ func New() *Validator {
// Example:
//
// validator := New().WithRepo(myRepository)
func (v *Validator) WithRepo(r Repository) *Validator {
func (v Validator) WithRepo(r Repository) Validator {
v.repo = r

return v
}

// IsPassed checks if the validator result has passed or not.
func (v *Validator) IsPassed() bool {
func (v Validator) IsPassed() bool {
return len(v.Errors()) == 0
}

// IsFailed checks if the validator result has failed or not.
func (v *Validator) IsFailed() bool {
func (v Validator) IsFailed() bool {
return !v.IsPassed()
}

// Errors returns a map of all validator rule errors.
func (v *Validator) Errors() map[string]string {
func (v Validator) Errors() map[string]string {
return v.errs
}

// Check is a dynamic method to define any custom validator rule by passing a rule as a function or expression
// which will return a boolean.
func (v *Validator) Check(ok bool, field, msg string) {
func (v Validator) Check(ok bool, field, msg string) {
if !ok {
v.addError(field, msg)
}
}

// addError fills the errors map and prevents duplicate fields from being added to validator errors.
func (v *Validator) addError(field, msg string) {
func (v Validator) addError(field, msg string) {
if _, exists := v.Errors()[field]; !exists {
v.Errors()[field] = msg
}
}

// msg returns the error message. If a custom error message is set, it returns the formatted custom message;
// otherwise, it returns the default message for the rule which has been set on the validator.
func (v *Validator) msg(method, msg string, fieldArgs ...any) string {
func (v Validator) msg(method, msg string, fieldArgs ...any) string {
if msg != "" {
return msg
}
Expand Down
2 changes: 1 addition & 1 deletion when.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package govalidator
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) When(condition bool, f func()) *Validator {
func (v Validator) When(condition bool, f func()) Validator {
if condition {
f()
}
Expand Down

0 comments on commit 412bf34

Please sign in to comment.