validator
Validator Package
The validator package provides an extensible, customizable validation system built on top of validator/v10. It includes support for custom validation rules and error translations, making it ideal for applications that require flexible and descriptive error handling.
Features
Easy integration with
go-playground/validator.Support for custom validators with custom error messages.
Simplified API for struct validation.
Extensible design for adding more
custom validators.
Installation
go get github.com/kittipat1413/go-common/framework/validatorDocumentation
For detailed API documentation, examples, and usage patterns, visit the Go Package Documentation.
Usage
Basic Validation
Custom Validators
You can create application-specific validation logic beyond the built-in rules provided by validator/v10. This package provides an interface called CustomValidator, which allows you to define custom validation rules, tags, and error message translations.
To create a custom validator, you must implement the CustomValidator interface, which consists of three methods:
Each method in the CustomValidator interface has a specific purpose:
Tag(): Defines the unique identifier for your custom validation rule. This tag is used in struct field tags to apply the validator. For example, ifTag()returns"mytag", you can use it in a struct asvalidate:"mytag".Func(): Specifies the validation logic. This method returns a function that implements thevalidator.Functype, which is called by the validator during the validation process. The function receives aFieldLevelinstance representing the field to validate. You should returntrueif the field passes validation, andfalseotherwise.Translation(): Defines the error message and translation function for this validator. If you want to use a custom error message format, return it intranslation(using{0}for the field name and{1}, etc., for any parameters). ThecustomFuncparameter allows additional customization for translating the error message, providing control over how the message displays.If you return
""(empty string) andnilforTranslation(), the default error message will be used.
Example: Creating a Custom Validator
The following example demonstrates how to create a custom validator that always fails and provides a custom error message:
Explanation
Tag: The
Tag()method returns"mytag", which you can use in struct tags likevalidate:"mytag".Func: The
Func()method returns a function that always fails for demonstration purposes.Translation: The
Translation()method returns a custom error message and translation function The error message is"{0} failed custom validation", where{0}will be replaced with the field name (fe.Field()).
Using Custom Validators
To register a custom validator, you need to pass an instance of the custom validator to the NewValidator function using the WithCustomValidator option. The following example demonstrates how to use the custom validator created in the previous section:
Expected Output:
Note: The package is built to make adding your own validators straightforward. If you need a domain-specific custom validator, you can implement the
CustomValidatorinterface and use it directly in your codebase, without waiting for a merge intogo-common. If you feel your custom validator could benefit others, consider sharing it here via a pull request or issue.
Using Custom Field Names in Validation Errors
To make validation errors more readable, especially in APIs that use JSON serialization, you can customize field names in error messages to match the json struct tags. The package provides a convenient option for this with WithTagNameFunc.
JSON Tag Name Function: The package includes a predefined
JSONTagNameFuncto automatically use JSON field names in validation error messages.
Registering JSONTagNameFunc
To register JSONTagNameFunc when creating a Validator instance, use the WithTagNameFunc option:
With this setup, any validation error messages will use the names specified in the json tags. For example:
Expected Output:
Examples
You can find a complete working example in the repository under framework/validator/example.
You can find an implementation example of a custom validator in the repository under framework/validator/custom_validator.
Last updated