|
@@ -7,15 +7,14 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-// validation errors
|
|
|
+/* validation errors */
|
|
|
var (
|
|
|
EInvalidInteger = errors.New("not a valid integer value")
|
|
|
EInvalidFloat = errors.New("not a valid float value")
|
|
|
ERequired = errors.New("this field is required")
|
|
|
)
|
|
|
|
|
|
-// ValidLettersGeneric is a validator generator for checking
|
|
|
-// for valid letters in field
|
|
|
+/* ValidLettersGeneric is a validator generator for checking for valid letters in field */
|
|
|
func ValidLettersGeneric(Letters string, Error error) ValidatorFunc {
|
|
|
Callback := func(field FormField, ctx context.Context) error {
|
|
|
for _, Rune := range field.GetString() {
|
|
@@ -28,8 +27,7 @@ func ValidLettersGeneric(Letters string, Error error) ValidatorFunc {
|
|
|
return Callback
|
|
|
}
|
|
|
|
|
|
-// ValidRequired returns nil if there is text in the field.
|
|
|
-// If the field is empty it returns error.
|
|
|
+/* ValidRequired makes sure field is not empty. */
|
|
|
func ValidRequired(field FormField, ctx context.Context) error {
|
|
|
if field.GetString() == "" {
|
|
|
return ERequired
|
|
@@ -37,8 +35,7 @@ func ValidRequired(field FormField, ctx context.Context) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// ValidLength returns a string field validator that verifies if a string
|
|
|
-// length is between specified min and max values.
|
|
|
+/* ValidLength makes sure that a string length is between specified min and max values. */
|
|
|
func ValidLength(min, max int) ValidatorFunc {
|
|
|
var ELength = errors.New(
|
|
|
fmt.Sprintf("must be a string between %d and %d characters in length", min, max))
|
|
@@ -50,7 +47,7 @@ func ValidLength(min, max int) ValidatorFunc {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// ValidFieldIn verifies if item is within the list of items
|
|
|
+/* ValidFieldIn verifies if item is within the list of items */
|
|
|
func ValidFieldIn(list []string) ValidatorFunc {
|
|
|
var EInvalidValue = errors.New(
|
|
|
fmt.Sprintf(
|
|
@@ -68,7 +65,7 @@ func ValidFieldIn(list []string) ValidatorFunc {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// ValidInt returns error if field does not contain a valid integer value
|
|
|
+/* ValidInt returns error if field does not contain a valid integer value */
|
|
|
func ValidInt(field FormField, ctx context.Context) error {
|
|
|
_, err := field.GetInt()
|
|
|
if err != nil {
|
|
@@ -77,8 +74,7 @@ func ValidInt(field FormField, ctx context.Context) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// ValidBetween is a validator generator that makes sure field is
|
|
|
-// integer value within the specified range.
|
|
|
+/* ValidBetween makes sure that field is integer value within the specified range. */
|
|
|
func ValidBetween(min, max int) ValidatorFunc {
|
|
|
var EInvalidInterval = errors.New(
|
|
|
fmt.Sprintf("must be integer between %d and %d", min, max))
|
|
@@ -94,7 +90,7 @@ func ValidBetween(min, max int) ValidatorFunc {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// ValidFloat returns error if field does not contain a valid integer value
|
|
|
+/* ValidFloat returns error if field does not contain a valid integer value */
|
|
|
func ValidFloat(field FormField, ctx context.Context) error {
|
|
|
_, err := field.GetFloat()
|
|
|
if err != nil {
|
|
@@ -103,8 +99,7 @@ func ValidFloat(field FormField, ctx context.Context) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// ValidBetweenFloat32 is a v alidator generator that makes sure field is
|
|
|
-// float64 value within the specified range.
|
|
|
+/* ValidBetweenFloat32 makes sure field is float64 value within the specified range. */
|
|
|
func ValidBetweenFloat(min, max float64) ValidatorFunc {
|
|
|
var EInvalidInterval = errors.New(
|
|
|
fmt.Sprintf("must be float value between %.2f and %.2f", min, max))
|
|
@@ -120,8 +115,7 @@ func ValidBetweenFloat(min, max float64) ValidatorFunc {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// ValidFieldEqualTo is a general purpose validator that checks
|
|
|
-// if two fields have the same value for verification purposes.
|
|
|
+/* ValidFieldEqualTo is a validator that checks if two fields have the same value. */
|
|
|
func ValidFieldEqualTo(Other *FormField, err error) ValidatorFunc {
|
|
|
return func(field FormField, ctx context.Context) error {
|
|
|
if field.GetString() != Other.GetString() {
|