Built-in Range Validators
Range Validators
MinLength(min int)
Checks if a string meets a minimum length requirement.
Usage Example:
MaxLength(max int)
Checks if a string meets a maximum length requirement.
Usage Example:
Length(min, max int)
Checks if a string meets a length requirement within a range.
Usage Example:
Min(min float64)
Checks if a numeric value is greater than or equal to a minimum value.
Usage Example:
Max(max float64)
Checks if a numeric value is less than or equal to a maximum value.
Usage Example:
Each(validatorFunc ValidatorFunc)
Checks if every element in a slice or array satisfies the provided validator function.
Usage Example:
Complete Example for Range Validators
Here’s an example demonstrating the use of range validators:
Explanation of Range Validators
-
MinLength
andMaxLength
:*These functions validate the length of a string.
*Useful for fields like usernames, passwords, or descriptions.
-
Length
:*Combines
MinLength
andMaxLength
to validate that a string is within a specific range.*Ideal for fields with strict length requirements.
-
Min
andMax
:*Validate numeric values (integers or floats) against minimum and maximum thresholds.
*Commonly used for age, scores, or other numeric inputs.
-
Each
:*Validates every element in a slice or array using a provided validator function.
*Useful for ensuring all elements in a list meet specific criteria.