validator

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2025 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindJSON

func BindJSON(ctx context.Context, body io.ReadCloser, s any) error

BindJSON binds body into a struct instance. Additionally it runs the struct through mold transformer.

func BindPathValues added in v1.3.0

func BindPathValues(ctx context.Context, r *http.Request, s any) error

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

func BindUrlValues(ctx context.Context, v url.Values, s any) error

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

func RegisterModifier(tag string, fn mold.Func)

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

func RegisterScrubber(tag string, fn mold.Func)

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

func RegisterValidator(tag string, fn vd.Func, callValidationEvenIfNull ...bool)

RegisterValidator register custiom validation function.

func ValidateStruct

func ValidateStruct(ctx context.Context, s any) error

ValidateStruct validates the incoming structure.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL