Documentation
¶
Index ¶
- func BindJSON(ctx context.Context, body io.ReadCloser, s any) error
- func BindPathValues(ctx context.Context, r *http.Request, s any) error
- func BindUrlValues(ctx context.Context, v url.Values, s any) error
- func FormatErrors(err vd.ValidationErrors) map[string]any
- func RegisterModifier(tag string, fn mold.Func)
- func RegisterScrubber(tag string, fn mold.Func)
- func RegisterTranslation(t Translation)
- func RegisterValidator(tag string, fn vd.Func, callValidationEvenIfNull ...bool)
- func ValidateStruct(ctx context.Context, s any) error
- type Translation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindJSON ¶
BindJSON binds body into a struct instance. Additionally it runs the struct through mold transformer.
func BindPathValues ¶ added in v1.3.0
BindPathValues binds values from the URL path to the struct fields based on the "path" tag. It uses reflection to dynamically set the field values.
func BindUrlValues ¶
BindUrlValues binds url.Values into a struct instance. Additionally it runs the struct through mold transformer.
func FormatErrors ¶
func FormatErrors(err vd.ValidationErrors) map[string]any
FormatErrors formats ValidationErrors into a map of error strings similar to the source data type.
Example validation error -
{
"Data.Addresses[0].Street":"Street is a required field",
"Data.Books[0]":"Books[0] must be at least 10 characters in length",
"Data.Books[1]":"Books[1] must be at least 10 characters in length",
"Data.Books[2]":"Books[2] is a required field",
"Data.Email":"Email is a required field",
"Data.Id":"Id is a required field",
"Data.UserName":"UserName is a required field"
}
Corresponding error message after formatting -
{
"addresses": [
{
"street": "Street is a required field"
}
],
"books": [
"Books[0] must be at least 10 characters in length",
"Books[1] must be at least 10 characters in length",
"Books[2] is a required field"
],
"email": "Email is a required field",
"id": "Id is a required field",
"userName": "UserName is a required field"
}
func RegisterModifier ¶
RegisterModifier registers a new modifier or replaces an existing one with tag. The modifier can be used using the tag inside struct tag mod.
func RegisterScrubber ¶
RegisterScrubber registers a new scrubber or replaces existing one with tag.
func RegisterTranslation ¶
func RegisterTranslation(t Translation)
RegisterTranslation registers a translation or overrides an existing one.
Example of such translation -
{
Tag: "required",
Translation: "{0} is a required field",
Override: false,
}
func RegisterValidator ¶
RegisterValidator register custiom validation function.
Types ¶
type Translation ¶
type Translation struct {
// Tag is the validator's tag
Tag string
// Translation is the translation template.
// i.e "{0} is a required field"
Translation string
// Override will override any existing translation for the tag.
Override bool
// CustomRegisFunc custom registration function for validate.
//
// See the implementations in here -
// https://github.com/go-playground/validator/blob/v10.23.0/translations/en/en.go
// It can be left empty/nil if there's no variation of a validation tag or
// no need to specify different registration functions.
CustomRegisFunc vd.RegisterTranslationsFunc
// CustomTransFunc is custiom translator function.
//
// See the implementations in here -
// https://github.com/go-playground/validator/blob/v10.23.0/translations/en/en.go
// It can be left empty/nil if there's no variation of a validation tag or
// no need to specify different translation functions.
CustomTransFunc vd.TranslationFunc
}
Translation defines a custom translation for a specific tag