Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
v "github.com/miniscruff/vaddie"
)
type User struct {
FirstName string
LastName string
Age int
Email string
FavoriteColor string
Hobbies []string
Addresses []*Address
}
func (u *User) Validate() error {
return v.Join(
v.AllOf(u.FirstName, "first_name", v.StrMin(2), v.StrMax(64)),
v.AllOf(u.LastName, "last_name", v.StrMin(2), v.StrMax(64)),
v.AllOf(u.Age, "age", v.OrderedGte(0), v.OrderedLte(130)),
v.AllOf(u.Email, "email", v.StrNotEmpty()), // no email check
v.AllOf(u.FavoriteColor, "favorite_color",
v.StrNotEmpty(),
),
// For some reason SliceMinLength cannot infer *Address
v.All(u.Addresses, "addresses", v.SliceMinLength[*Address](1)),
v.All(u.Hobbies, "hobbies",
v.SliceMinLength[string](1),
v.Dive(v.StrMin(3), v.StrMax(64)),
),
)
}
type Address struct {
Street string
City string
Planet string
Phone string
}
func (a *Address) Validate() error {
return v.Join(
v.AllOf(a.Street, "street", v.StrNotEmpty()),
v.AllOf(a.City, "city", v.StrNotEmpty()),
v.AllOf(a.Planet, "planet", v.StrNotEmpty()),
v.AllOf(a.Phone, "phone", v.StrNotEmpty()),
)
}
func main() {
address := &Address{
Street: "Eavesdown Docks",
Planet: "Persphone",
Phone: "none",
City: "Unknown",
}
user := &User{
FirstName: "Badger",
LastName: "Smith",
Age: 45,
Email: "Badger.Smith@gmail",
FavoriteColor: "#000",
Addresses: []*Address{address},
Hobbies: []string{"RC Cars"},
}
fmt.Printf("validating user:\n%v\n", user.Validate())
// Output
// validating user:
// <nil>
}
Index ¶
- func All[T any](values []T, key string, validateSlice ...ValidateSlice[T]) error
- func AllOf[T any](value T, key string, validateValues ...ValidateValue[T]) error
- func Join(errs ...error) error
- func JoinAnd(errs ...error) error
- func OneOf[T any](value T, key string, validateValues ...ValidateValue[T]) error
- func Optional[T any](value *T, key string, validateValues ...ValidateValue[T]) error
- func Required[T any](value *T, key string, validateValues ...ValidateValue[T]) error
- type ValidateSlice
- type ValidateValue
- func And[T any](validateValues ...ValidateValue[T]) ValidateValue[T]
- func ComparableContains[T comparable](values ...T) ValidateValue[T]
- func ComparableEq[T comparable](eq T) ValidateValue[T]
- func ComparableNe[T comparable](eq T) ValidateValue[T]
- func Or[T any](validateValues ...ValidateValue[T]) ValidateValue[T]
- func OrderedEq[T cmp.Ordered](eq T) ValidateValue[T]
- func OrderedGt[T cmp.Ordered](minValue T) ValidateValue[T]
- func OrderedGte[T cmp.Ordered](minValue T) ValidateValue[T]
- func OrderedLt[T cmp.Ordered](maxValue T) ValidateValue[T]
- func OrderedLte[T cmp.Ordered](maxValue T) ValidateValue[T]
- func OrderedNe[T cmp.Ordered](eq T) ValidateValue[T]
- func StrAscii() ValidateValue[string]
- func StrContains(substr string) ValidateValue[string]
- func StrContainsAny(chars string) ValidateValue[string]
- func StrHasPrefix(prefix string) ValidateValue[string]
- func StrHasSuffix(suffix string) ValidateValue[string]
- func StrLetters() ValidateValue[string]
- func StrMax(maxLength int) ValidateValue[string]
- func StrMin(minLength int) ValidateValue[string]
- func StrNotContains(substr string) ValidateValue[string]
- func StrNotContainsAny(chars string) ValidateValue[string]
- func StrNotEmpty() ValidateValue[string]
- func StrNotHasPrefix(prefix string) ValidateValue[string]
- func StrNotHasSuffix(suffix string) ValidateValue[string]
- func TimeAfter(after time.Time) ValidateValue[time.Time]
- func TimeBefore(before time.Time) ValidateValue[time.Time]
- func TimeEq(value time.Time) ValidateValue[time.Time]
- type ValidationError
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All[T any](values []T, key string, validateSlice ...ValidateSlice[T]) error
All can be used to validate all the items of a slice. If T implements the Validator interface, each value will also run that validation.
func AllOf ¶
func AllOf[T any](value T, key string, validateValues ...ValidateValue[T]) error
AllOf validates that our value meets all of the validaton rules. If T implements the Validator interface, it is validated first.
func OneOf ¶
func OneOf[T any](value T, key string, validateValues ...ValidateValue[T]) error
OneOf validates that our value meets at least one of the validaton rules. If T implements the Validator interface, it is validated first but does not immediately return nil if it passes. Instead we will still go through the validate funcs until one other check passes.
func Optional ¶
func Optional[T any](value *T, key string, validateValues ...ValidateValue[T]) error
Optional will validate a value meets our rules if and only if it is not nil. A nil value will always meet validation. If T implements the Validator interface, and is not nil, that is run first.
func Required ¶ added in v0.5.0
func Required[T any](value *T, key string, validateValues ...ValidateValue[T]) error
Required will validate a value meets our rules if and only if it is not nil. A nil value will error immediately. If T implements the Validator interface, and is not nil, that is run first.
Types ¶
type ValidateSlice ¶
ValidateSlice can be used to validate a slice of values.
func Dive ¶
func Dive[T any](validateValues ...ValidateValue[T]) ValidateSlice[T]
Dive can be used to dive into a slice validating the values within.
func SliceMinLength ¶
func SliceMinLength[T any](minLength int) ValidateSlice[T]
SliceMinLength validates that a slice has at least a minimum amount of values.
type ValidateValue ¶
ValidateValue can be used to validate a field value.
func And ¶
func And[T any](validateValues ...ValidateValue[T]) ValidateValue[T]
And combines many validation rules into one. All validations must be true for the validation to be successful.
func ComparableContains ¶ added in v0.4.0
func ComparableContains[T comparable](values ...T) ValidateValue[T]
ComparableContains validates a comparable value is part of a slice.
func ComparableEq ¶ added in v0.4.0
func ComparableEq[T comparable](eq T) ValidateValue[T]
ComparableEq validates that two comparable values are equal.
func ComparableNe ¶ added in v0.4.0
func ComparableNe[T comparable](eq T) ValidateValue[T]
ComparableNe validates that two comparable values are not equal.
func Or ¶
func Or[T any](validateValues ...ValidateValue[T]) ValidateValue[T]
Or combines many validation rules into one.
func OrderedEq ¶
func OrderedEq[T cmp.Ordered](eq T) ValidateValue[T]
OrderedEq validates that an ordered value is equal to another value.
func OrderedGt ¶
func OrderedGt[T cmp.Ordered](minValue T) ValidateValue[T]
OrderedGt validates that an ordered value as greater than a minimum value.
func OrderedGte ¶
func OrderedGte[T cmp.Ordered](minValue T) ValidateValue[T]
OrderedGte validates that an ordered value as greater than or equal to a minimum value.
func OrderedLt ¶
func OrderedLt[T cmp.Ordered](maxValue T) ValidateValue[T]
OrderedLt validates that an ordered value as less than to a maximum value.
func OrderedLte ¶
func OrderedLte[T cmp.Ordered](maxValue T) ValidateValue[T]
OrderedLt validates that an ordered value as less than or equal to a maximum value.
func OrderedNe ¶ added in v0.4.0
func OrderedNe[T cmp.Ordered](eq T) ValidateValue[T]
OrderedNe validates that an ordered value is not equal to another value.
func StrAscii ¶
func StrAscii() ValidateValue[string]
StrAscii validates every rune is an ascii value.
func StrContains ¶
func StrContains(substr string) ValidateValue[string]
StrContains validates our string contains the provided substring.
func StrContainsAny ¶
func StrContainsAny(chars string) ValidateValue[string]
StrContainsAny validates whether any Unicode code points in chars are within value.
func StrHasPrefix ¶
func StrHasPrefix(prefix string) ValidateValue[string]
StrHasPrefix validates our string has the provided prefix.
func StrHasSuffix ¶
func StrHasSuffix(suffix string) ValidateValue[string]
StrHasSuffix validates our string has the provided suffix.
func StrLetters ¶
func StrLetters() ValidateValue[string]
StrLetters validates every rune is a letter.
func StrMax ¶
func StrMax(maxLength int) ValidateValue[string]
StrMax validates our value is no more then a maximum length.
func StrMin ¶
func StrMin(minLength int) ValidateValue[string]
StrMin validates our value is at least a minimum length.
func StrNotContains ¶
func StrNotContains(substr string) ValidateValue[string]
StrNotContains validates our string does not contain the provided substring.
func StrNotContainsAny ¶
func StrNotContainsAny(chars string) ValidateValue[string]
StrNotContainsAny validates whether all Unicode code points in chars are not within value.
func StrNotEmpty ¶
func StrNotEmpty() ValidateValue[string]
StrNotEmpty validates that a given string is not empty.
func StrNotHasPrefix ¶
func StrNotHasPrefix(prefix string) ValidateValue[string]
StrNotHasPrefix validates our string does not have the provided prefix.
func StrNotHasSuffix ¶
func StrNotHasSuffix(suffix string) ValidateValue[string]
StrNotHasSuffix validates our string does not have the provided suffix.
func TimeAfter ¶
func TimeAfter(after time.Time) ValidateValue[time.Time]
TimeAfter validates our time comes after the provided time.
func TimeBefore ¶
func TimeBefore(before time.Time) ValidateValue[time.Time]
TimeBefore validates our time comes before the provided time.
type ValidationError ¶
ValidationError is what we return on invalid validations. Output of the error is a single string of: `Key [Index] Message [(Help)]`
func (*ValidationError) Error ¶
func (v *ValidationError) Error() string