vaddie

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 7 Imported by: 0

README

Vaddie

Go Packge GitHub release GitHub License

Validation library using type safe, developer friendly and extendible functions.

[!WARNING] This library is strictly around validations, parsing or building is otherwise left up to you. Validations that would otherwise fail parsing, are not handled or expected to be supported.

Support

Currently experimental, valdiations will be added, changed or removed. New validations may be added with Exp prefixes indicating they are still experimental.

Examples

package main

import (
	"fmt"

	v "github.com/miniscruff/vaddie"
)

type User struct {
	FirstName     string     `json:"firstName"`
	LastName      string     `json:"lastName"`
	Age           uint8      `json:"age"`
}

func main() {
	user := &User{
		FirstName:     "Badger",
		LastName:      "Smith",
		Age:           45,
    }
    err := 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(uint8(0)), v.OrderedLte(uint8(130))),
    )

	fmt.Printf("validating user:\n%v\n", err)
}

Benchmarks

Coming soon

[!NOTE] If you have a particularly large or complex validation requirement, please share.

Need help?

Use the discussions page for help requests and how-to questions.

Please open GitHub issues for bugs and feature requests. File an issue before creating a pull request, unless it is something simple like a typo.

Want to Contribute?

If you want to contribute through code or documentation, the Contributing guide is the place to start. If you need additional help create an issue or post on discussions.

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>
}
Output:

Index

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.

func Join

func Join(errs ...error) error

Join combines all errors into one.

func JoinAnd

func JoinAnd(errs ...error) error

JoinAnd will return the first non-nil error, or nil if all errors are nil.

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.

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.

Types

type ValidateSlice

type ValidateSlice[T any] func(values []T) error

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

type ValidateValue[T any] func(value T) error

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 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 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.

func TimeEq

func TimeEq(value time.Time) ValidateValue[time.Time]

TimeEq validates our time is equal to the provided time.

type ValidationError

type ValidationError struct {
	Key     string
	Message string
	Help    string
	Index   *int
}

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

type Validator

type Validator interface {
	Validate() error
}

Validator is an interface that structs can implement enabling the use of wrappers.

Jump to

Keyboard shortcuts

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