diff --git a/between.go b/between.go index 1f578c1..40f7552 100644 --- a/between.go +++ b/between.go @@ -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)) @@ -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)) diff --git a/date.go b/date.go index 3d43a35..1d80cc0 100644 --- a/date.go +++ b/date.go @@ -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 { diff --git a/email.go b/email.go index 82ffda8..da175be 100644 --- a/email.go +++ b/email.go @@ -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)) diff --git a/exists.go b/exists.go index 4bfb947..18e07ee 100644 --- a/exists.go +++ b/exists.go @@ -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)) diff --git a/in.go b/in.go index 1ae4d49..60afdf1 100644 --- a/in.go +++ b/in.go @@ -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 { diff --git a/len.go b/len.go index 7fa4ff8..ce747bd 100644 --- a/len.go +++ b/len.go @@ -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)) @@ -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)) @@ -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)) diff --git a/max.go b/max.go index 60d0ef8..10d17fd 100644 --- a/max.go +++ b/max.go @@ -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)) @@ -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)) @@ -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)) diff --git a/min.go b/min.go index b4e1197..f3c10bc 100644 --- a/min.go +++ b/min.go @@ -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)) @@ -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)) @@ -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)) diff --git a/notexists.go b/notexists.go index 85bf8b7..f31da3d 100644 --- a/notexists.go +++ b/notexists.go @@ -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)) diff --git a/regex.go b/regex.go index 75ac57c..444fe6d 100644 --- a/regex.go +++ b/regex.go @@ -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) diff --git a/required.go b/required.go index 236dec6..a3ac0ca 100644 --- a/required.go +++ b/required.go @@ -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)) @@ -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)) @@ -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)) @@ -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)) diff --git a/unqiue.go b/unqiue.go index 21694bc..c274139 100644 --- a/unqiue.go +++ b/unqiue.go @@ -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) diff --git a/uuid.go b/uuid.go index a9581cf..8106cb4 100644 --- a/uuid.go +++ b/uuid.go @@ -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 { diff --git a/when.go b/when.go index cb9ceac..651286d 100644 --- a/when.go +++ b/when.go @@ -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()