Documentation ¶
Overview ¶
Package validate contains field validations for Go structs, appropriated to use with [*http.Request].
There are two ways a struct can be validated:
- Explicitly, by calling Fields and passing the validations required;
- Implicitly, by making the struct implement Validatable with a pointer receiver and using the binding functions.
When a validation fails, Fields use the Message and Fields attributes of the Field struct to build the validation error.
Custom validations ¶
A validation is simply an instance of the struct Field. In order to create a new validation, is enough to just create your own instance, passing the arguments. You could also change only the Message field, if that meets your use case. Check the package-level examples.
Index ¶
- func Fields[T any](target *T, validations ...Field) error
- type Error
- type Field
- func After(target *time.Time, value time.Time) Field
- func Before(target *time.Time, value time.Time) Field
- func Between[T constraints.Ordered](target *T, min, max T) Field
- func BetweenLength[T any](target *T, min, max int) Field
- func BetweenTime(target *time.Time, min, max time.Time) Field
- func DateTime(target *string, layout string) Field
- func Email(target *string) Field
- func Empty[T any](target *T) Field
- func Equal[T comparable](target *T, value T) Field
- func EqualField[T comparable](target *T, field *T) Field
- func Greater[T constraints.Ordered](target *T, value T) Field
- func GreaterField[T constraints.Ordered](target *T, field *T) Field
- func GreaterOrEqual[T constraints.Ordered](target *T, value T) Field
- func GreaterOrEqualField[T constraints.Ordered](target *T, field *T) Field
- func HasPrefix(target *string, prefix string) Field
- func HasSuffix(target *string, suffix string) Field
- func IPAddress(target *string) Field
- func IPv4Address(target *string) Field
- func IPv6Address(target *string) Field
- func Length[T any](target *T, value int) Field
- func Less[T constraints.Ordered](target *T, value T) Field
- func LessField[T constraints.Ordered](target *T, field *T) Field
- func LessOrEqual[T constraints.Ordered](target *T, value T) Field
- func LessOrEqualField[T constraints.Ordered](target *T, field *T) Field
- func Lowercase(target *string) Field
- func MaxLength[T any](target *T, value int) Field
- func MinLength[T any](target *T, value int) Field
- func NotEmpty[T any](target *T) Field
- func NotEqual[T comparable](target *T, value T) Field
- func NotEqualField[T comparable](target *T, field *T) Field
- func OneOf[T comparable](target *T, values ...T) Field
- func Required[T any](target *T) Field
- func Substring(target *string, substring string) Field
- func UUID(target *string) Field
- func Uppercase(target *string) Field
- type Validatable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fields ¶
Fields validates the fields of a struct of type T.
It uses the tags of the binding functions (such as "uri", "query" or "json") from the fields to build a message for the user in case the validation fails. If none of these tags are set or are set as the empty string, Fields uses the field's name instead.
If T is not a struct type, Fields panics.
Types ¶
type Error ¶
type Error struct { // Validations that have failed. Violations []Field }
Error occurs when at least one Field validation fails in validation steps.
type Field ¶
type Field struct { // Determines if this validation has succeeded. Valid bool // A user-friendly message that can be displayed if the validation fails. In this case, // "{i}" placeholders, where i is the index of the replacement in Fields, are replaced by the field's tag values. Message string // Pointers to the fields involved in this validation. It is a slice because the validation // can have multiple fields as arguments. Fields []any }
Field represents a field validation.
func Between ¶
func Between[T constraints.Ordered](target *T, min, max T) Field
Between validates if target is greater than min and less than max.
func BetweenLength ¶
BetweenLength validates if target has a length greater or equal than min and less or equal than max.
If target is not a pointer to a slice, array, string or map, BetweenLength panics.
func BetweenTime ¶
BetweenTime validates if target is after min and before max.
func DateTime ¶
DateTime validates if target is a valid date-time string.
Note that binding functions bind strings to fields with type time.Time if the layout is time.RFC3339, validating them in the process.
func Empty ¶
Empty validates if target is empty.
If target is not a pointer to a slice, array, string or map, Empty panics.
func Equal ¶
func Equal[T comparable](target *T, value T) Field
Equal validates if target is equal to value.
func EqualField ¶
func EqualField[T comparable](target *T, field *T) Field
EqualField validates if target is equal to the value of field.
func Greater ¶
func Greater[T constraints.Ordered](target *T, value T) Field
Greater validates if target is greater than value.
func GreaterField ¶
func GreaterField[T constraints.Ordered](target *T, field *T) Field
GreaterField validates if target is greater than the value of field.
func GreaterOrEqual ¶
func GreaterOrEqual[T constraints.Ordered](target *T, value T) Field
GreaterOrEqual validates if target is greater or equal than value.
func GreaterOrEqualField ¶
func GreaterOrEqualField[T constraints.Ordered](target *T, field *T) Field
GreaterOrEqualField validates if target is greater or equal than the value of field.
func IPv4Address ¶
IPv4Address validates if target is a valid IPv4 address.
func IPv6Address ¶
IPv6Address validates if target is a valid IPv6 address.
func Length ¶
Length validates if target has a length of value.
If target is not a pointer to a slice, array, string or map, Length panics.
func Less ¶
func Less[T constraints.Ordered](target *T, value T) Field
Less validates if target is less than value.
func LessField ¶
func LessField[T constraints.Ordered](target *T, field *T) Field
LessField validates if target is less than the value of field.
func LessOrEqual ¶
func LessOrEqual[T constraints.Ordered](target *T, value T) Field
LessOrEqual validates if target is less or equal than value.
func LessOrEqualField ¶
func LessOrEqualField[T constraints.Ordered](target *T, field *T) Field
LessOrEqualField validates if target is less or equal than the value of field.
func MaxLength ¶
MaxLength validates if target has a length less or equal than value.
If target is not a pointer to a slice, array, string or map, MaxLength panics.
func MinLength ¶
MinLength validates if target has a length greater or equal than value.
If target is not a pointer to a slice, array, string or map, MinLength panics.
func NotEmpty ¶
NotEmpty validates if target is not empty.
If target is not a pointer to a slice, array, string or map, NotEmpty panics.
func NotEqual ¶
func NotEqual[T comparable](target *T, value T) Field
NotEqual validates if target is not equal to value.
func NotEqualField ¶
func NotEqualField[T comparable](target *T, field *T) Field
NotEqualField validates if target is not equal to the value of field.
func OneOf ¶
func OneOf[T comparable](target *T, values ...T) Field
OneOf validates if target is one of values.
func Required ¶
Required validates that target is not nil. Suited for when target is a pointer field.
type Validatable ¶
type Validatable interface { // Validate returns a list of field validations. Validate() []Field }
Validatable structs can be validated.