Built-in Regex Validator And Custom Validators
Regex Validator
Regex(pattern string)
Validates a string against a regular expression.
Purpose: Validates a string against a regular expression.
Usage: Provide a valid regex pattern as a string.
Example Patterns:
Alphanumeric: ^[A-Za-z0-9]+$
Zip Code (5 digits): ^\d{5}$
Email: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Usage Example:
Custom Validators
The go-validator
package allows you to define and use custom validators tailored to your specific validation needs. Custom validators are created using the CreateValidator
helper function, which takes a validation function and an error message as arguments.
How to Add a Custom Validator
-
Define the Validation Logic Write a function that implements the
ValidatorFunc
type. This function should:- Accept a value of type
interface{}
. - Return an
error
if the validation fails, ornil
if it passes.
- Accept a value of type
-
Create the Validator Use the
CreateValidator
function to wrap your validation logic with a custom error message. -
Add the Validator to Your Validation Rules Include the custom validator in the
Validators
field of aValidationOption
.
Step-by-Step Example
Step 1: Define the Validation Logic
Suppose you want to create a custom validator that ensures a string starts with a specific prefix (e.g., "prefix-"
).
Step 2: Create the Validator
Use the CreateValidator
helper function to create a validator with a custom error message.
Step 3: Add the Validator to Your Validation Rules
Include the custom validator in the Validators
field of a ValidationOption
.
Step 4: Use the Validator
Pass the validationOptions to the Validate function or use it in a framework adapter like Gin.
Explanation of the Code
-
Custom Validation Function
- The
StartsWithPrefix
function is a factory function that returns aValidatorFunc
. - It checks if the input value is a string and whether it starts with the specified prefix.
- The
-
CreateValidator
- The
CreateValidator
function wraps the validation logic with a custom error message. - This makes it easy to reuse the same validation logic with different error messages.
- The
-
ValidationOption
- The
Validators
field of aValidationOption
accepts a slice ofValidator
objects. - You can include multiple validators for a single field.
- The
-
Validate Function
- The
Validate
function iterates over theValidationOption
rules and applies the validators to the corresponding fields in the request body.
- The
Advanced Example: Combining Multiple Validators
You can combine multiple validators for a single field. For example, ensure that a field is both alphanumeric and starts with a specific prefix.
Tips for Writing Custom Validators
-
Handle Type Assertions Carefully
- Always check the type of the input value using type assertions (
value.(type)
). - Return an appropriate error if the value is not of the expected type.
- Always check the type of the input value using type assertions (
-
Keep Validators Focused
- Each validator should focus on a single validation rule. This makes them reusable and easier to test.
-
Provide Clear Error Messages
- Use descriptive error messages that explain why the validation failed.
-
Test Your Validators
- Write unit tests for your custom validators to ensure they work as expected.
By following these steps, you can easily extend the functionality of the go-validator
package with your own custom validation rules.