Skip to content

Commit

Permalink
improved: method comment examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rezakhademix committed Apr 2, 2024
1 parent 70aebb1 commit b83eb8c
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 24 deletions.
12 changes: 10 additions & 2 deletions between.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const (
//
// Example:
//
// validator.BetweenInt(21, 1, 10, "age", "age must be between 1 and 10.")
// v := validator.New()
// v.BetweenInt(21, 1, 10, "age", "age must be between 1 and 10.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
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))

Expand All @@ -22,7 +26,11 @@ func (v *Validator) BetweenInt(i, min, max int, field, msg string) *Validator {
//
// Example:
//
// validator.BetweenFloat(3.5, 2.0, 5.0, "height", "height must be between 2.0 and 5.0 meters.")
// v := validator.New()
// v.BetweenFloat(3.5, 2.0, 5.0, "height", "height must be between 2.0 and 5.0 meters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
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))

Expand Down
6 changes: 5 additions & 1 deletion date.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const (
//
// Example:
//
// validator.Date("2006-01-02", "2024-03-09","birthdate", "birthdate must be a valid date in the format YYYY-MM-DD.")
// v := validator.New()
// v.Date("2006-01-02", "2024-03-09","birthdate", "birthdate must be a valid date in the format YYYY-MM-DD.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) Date(layout, d, field, msg string) *Validator {
_, err := time.Parse(layout, d)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const (
//
// Example:
//
// validator.Email("john.doe@example.com", "email", "email address is not valid.")
// v := validator.New()
// v.Email("john.doe@example.com", "email", "email address is not valid.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) Email(s, field, msg string) *Validator {
v.RegexMatches(s, EmailRegex, field, v.msg(Email, msg, field))

Expand Down
6 changes: 5 additions & 1 deletion exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const (
//
// Example:
//
// validator.Exists(42, "users", "id", "user_id", "user with id 42 does not exist.")
// v := validator.New()
// v.Exists(42, "users", "id", "user_id", "user with id 42 does not exist.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
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))

Expand Down
2 changes: 1 addition & 1 deletion in.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package govalidator
//
// Example:
//
// result := In("apple", "banana", "orange", "apple")
// result := validator.In("apple", "banana", "orange", "apple")
// // result will be true because "apple" is included in the list of acceptable values.
func In[T comparable](value T, acceptableValues ...T) bool {
for i := range acceptableValues {
Expand Down
18 changes: 15 additions & 3 deletions len.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const (
//
// Example:
//
// validator.LenString("rez", 5, "username", "username must be 5 characters.")
// v := validator.New()
// v.LenString("rez", 5, "username", "username must be 5 characters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) LenString(s string, size int, field, msg string) *Validator {
v.Check(len(strings.TrimSpace(s)) == size, field, v.msg(Len, msg, field, size))

Expand All @@ -31,7 +35,11 @@ func (v *Validator) LenString(s string, size int, field, msg string) *Validator
//
// Example:
//
// validator.LenInt(12345, 5, "zipcode", "Zip code must be 5 digits long.")
// v := validator.New()
// v.LenInt(12345, 5, "zipcode", "Zip code must be 5 digits long.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
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))

Expand All @@ -42,7 +50,11 @@ func (v *Validator) LenInt(i, size int, field, msg string) *Validator {
//
// Example:
//
// validator.LenSlice([]int{1, 2, 3, 4, 5}, 5, "numbers", "the list must contain exactly 5 numbers.")
// v := validator.New()
// v.LenSlice([]int{1, 2, 3, 4, 5}, 5, "numbers", "the list must contain exactly 5 numbers.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) LenSlice(s []any, size int, field, msg string) *Validator {
v.Check(len(s) == size, field, v.msg(LenList, msg, field, size))

Expand Down
18 changes: 15 additions & 3 deletions max.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const (
//
// Example:
//
// validator.MaxInt(10, 100, "age", "age must be less than 100.")
// v := validator.New()
// v.MaxInt(10, 100, "age", "age must be less than 100.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MaxInt(i, max int, field, msg string) *Validator {
v.Check(i <= max, field, v.msg(Max, msg, field, max))

Expand All @@ -28,7 +32,11 @@ func (v *Validator) MaxInt(i, max int, field, msg string) *Validator {
//
// Example:
//
// validator.MaxFloat(3.5, 5.0, "height", "height must be less than 5.0 meters.")
// v := validator.New()
// v.MaxFloat(3.5, 5.0, "height", "height must be less than 5.0 meters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MaxFloat(f, max float64, field, msg string) *Validator {
v.Check(f <= max, field, v.msg(Max, msg, field, max))

Expand All @@ -39,7 +47,11 @@ func (v *Validator) MaxFloat(f, max float64, field, msg string) *Validator {
//
// Example:
//
// validator.MaxString("rey", 5, "name", "name should has less than 5 characters.")
// v := validator.New()
// v.MaxString("rey", 5, "name", "name should has less than 5 characters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MaxString(s string, maxLen int, field, msg string) *Validator {
v.Check(len(strings.TrimSpace(s)) <= maxLen, field, v.msg(MaxString, msg, field, maxLen))

Expand Down
18 changes: 15 additions & 3 deletions min.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const (
//
// Example:
//
// validator.MinInt(18, 0, "age", "age must be at least 0.")
// v := validator.New()
// v.MinInt(18, 0, "age", "age must be at least 0.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MinInt(i, min int, field, msg string) *Validator {
v.Check(i >= min, field, v.msg(Min, msg, field, min))

Expand All @@ -28,7 +32,11 @@ func (v *Validator) MinInt(i, min int, field, msg string) *Validator {
//
// Example:
//
// validator.MinFloat(5.0, 0.0, "height", "height must be at least 0.0 meters.")
// v := validator.New()
// v.MinFloat(5.0, 0.0, "height", "height must be at least 0.0 meters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MinFloat(f, min float64, field, msg string) *Validator {
v.Check(f >= min, field, v.msg(Min, msg, field, min))

Expand All @@ -39,7 +47,11 @@ func (v *Validator) MinFloat(f, min float64, field, msg string) *Validator {
//
// Example:
//
// validator.MinString("rey", 5, "name", "name should has more than 5 characters.")
// v := validator.New()
// v.MinString("rey", 5, "name", "name should has more than 5 characters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) MinString(s string, minLen int, field, msg string) *Validator {
v.Check(len(strings.TrimSpace(s)) >= minLen, field, v.msg(MinString, msg, field, minLen))

Expand Down
6 changes: 5 additions & 1 deletion notexists.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const (
//
// Example:
//
// validator.NotExists(42, "users", "id", "user_id", "user with id 42 already exists.")
// v := validator.New()
// v.NotExists(42, "users", "id", "user_id", "user with id 42 already exists.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
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))

Expand Down
6 changes: 5 additions & 1 deletion regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const (
//
// Example:
//
// validator.RegexMatches("example123", "[a-z]+[0-9]+", "input", "input must contain letters followed by numbers.")
// v := validator.New()
// v.RegexMatches("example123", "[a-z]+[0-9]+", "input", "input must contain letters followed by numbers.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RegexMatches(s string, pattern string, field, msg string) *Validator {
r := regexp.MustCompile(pattern)

Expand Down
24 changes: 20 additions & 4 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const (
//
// Example:
//
// validator.RequiredString("hello", "username", "username is required.")
// v := validator.New()
// v.RequiredString("hello", "username", "username is required.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredString(s, field string, msg string) *Validator {
v.Check(strings.TrimSpace(s) != "", field, v.msg(Required, msg, field))

Expand All @@ -24,7 +28,11 @@ func (v *Validator) RequiredString(s, field string, msg string) *Validator {
//
// Example:
//
// validator.RequiredInt(42, "age", "age is required.")
// v := validator.New()
// v.RequiredInt(42, "age", "age is required.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredInt(i int, field string, msg string) *Validator {
v.Check(i != 0, field, v.msg(Required, msg, field))

Expand All @@ -35,7 +43,11 @@ func (v *Validator) RequiredInt(i int, field string, msg string) *Validator {
//
// Example:
//
// validator.RequiredSlice([]string{"apple", "banana", "orange"}, "fruits", "at least one fruit must be provided.")
// v := validator.New()
// v.RequiredSlice([]string{"apple", "banana", "orange"}, "fruits", "at least one fruit must be provided.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator {
v.Check(len(s) > 0, field, v.msg(Required, msg, field))

Expand All @@ -46,7 +58,11 @@ func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator
//
// Example:
//
// validator.RequiredFloat(3.5, "weight", "weight is required.")
// v := validator.New()
// v.RequiredFloat(3.5, "weight", "weight is required.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator {
v.Check(f != 0.0, field, v.msg(Required, msg, field))

Expand Down
2 changes: 1 addition & 1 deletion unqiue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package govalidator
// Example:
//
// values := []int{1, 2, 3, 4, 5}
// result := Unique(values)
// result := validator.Unique(values)
// // result will be true because all values in the slice are unique.
func Unique[T comparable](values []T) bool {
uniqueValues := make(map[T]bool)
Expand Down
6 changes: 5 additions & 1 deletion uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const (
//
// Example:
//
// validator.UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479", "uuid", "Invalid UUID format.")
// v := validator.New()
// v.UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479", "uuid", "Invalid UUID format.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) UUID(u, field, msg string) *Validator {
_, err := uuid.Parse(u)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion when.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ package govalidator
//
// Example:
//
// validator.When(len(username) > 0, func() {
// v := validator.New()
// v.When(len(username) > 0, func() {
// validator.RequiredString(username, "username", "username is required.")
// })
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v *Validator) When(condition bool, f func()) *Validator {
if condition {
f()
Expand Down

0 comments on commit b83eb8c

Please sign in to comment.