validator
Last updated
Last updated
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.
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
.
Tag()
: Defines the unique identifier for your custom validation rule. This tag is used in struct field tags to apply the validator. For example, if Tag()
returns "mytag"
, you can use it in a struct as validate:"mytag"
.
Func()
: Specifies the validation logic. This method returns a function that implements the validator.Func
type, which is called by the validator during the validation process. The function receives a FieldLevel
instance representing the field to validate. You should return true
if the field passes validation, and false
otherwise.
Translation()
: Defines the error message and translation function for this validator. If you want to use a custom error message format, return it in translation
(using {0}
for the field name and {1}
, etc., for any parameters). The customFunc
parameter allows additional customization for translating the error message, providing control over how the message displays.
If you return ""
(empty string) and nil
for Translation()
, the default error message will be used.
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 like validate:"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()
).
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
CustomValidator
interface 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.
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 JSONTagNameFunc
to automatically use JSON field names in validation error messages.
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:
You can create application-specific validation logic beyond the built-in rules provided by validator/v10
. This package provides an interface called , which allows you to define custom validation rules, tags, and error message translations.
To create a custom validator, you must implement the interface, which consists of three methods:
Each method in the interface has a specific purpose:
You can find a complete working example in the repository under .
You can find an implementation example of a custom validator in the repository under .