validator

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 6 Imported by: 0

README

Validator

Example

package main

import (
	"fmt"
	"regexp"

	"github.com/hossein1376/grape/validator"
)

type data struct {
	Name  string
	Age   int
	Email string
	Phone string
}

func main() {
	simpleValidation()
	multiValidation()
}

func simpleValidation() {
	d := data{Name: "", Age: -2, Email: "asdf"}
	emailRE := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)

	v := validator.New()
	v.Check("name", validator.Case{Cond: validator.Not(validator.Empty(d.Name)), Msg: "must not be empty"})
	v.Check("age", validator.Case{Cond: validator.Min(d.Age, 0), Msg: "must not be positive"})
	v.Check("email", validator.Case{Cond: validator.Matches(d.Email, emailRE), Msg: "must be valid email"})

	if ok := v.Validate(); !ok {
		fmt.Println("simpleValidation:", v.Errors)
		// name: must not be empty, age: must not be positive, email: must be valid email
		return
	}
	fmt.Println("Valid input!")
}

func multiValidation() {
	d := data{Name: "", Phone: "123a"}

	v := validator.New()
	v.Check("name",
		validator.Case{Cond: validator.Not(validator.Empty(d.Name)), Msg: "must not be empty"},
		validator.Case{Cond: validator.LengthMin(d.Name, 2), Msg: "must not be less than 2 characters"},
	)
	v.Check("phone",
		validator.Case{Cond: validator.IsNumber(d.Phone), Msg: "must be only numbers"},
		validator.Case{Cond: validator.LengthRange(d.Phone, 4, 10), Msg: "must be between 4 to 10 digits"},
	)

	if ok := v.Validate(); !ok {
		fmt.Println("multiValidation:", v.Errors)
		// phone: must be only numbers, name: must not be empty, name: must not be less than 2 characters
		return
	}
	fmt.Println("Valid input!")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v0.4.0

func Contains[T ~string](input T, values ...T) bool

Contains check if the input contains any of the given values.

func Empty

func Empty[T ~string](input T) bool

Empty checks if the given input is empty (of len zero).

func EndsWith

func EndsWith[T ~string](input T, suffixes ...T) bool

EndsWith check whether the input ends with one of the given suffixes.

func In

func In[T comparable](input T, list ...T) bool

In checks if the input is present in the given list of arguments.

func IsNumber

func IsNumber[T ~string](input T) bool

IsNumber checks if the given string is all numbers.

func Len added in v0.4.0

func Len[S any, T ~[]S](input T, l int) bool

Len checks if the given input is of given length. For strings, use [Length*] functions.

func LenRange added in v0.4.0

func LenRange[S any, T ~[]S](input T, low, high int) bool

LenRange checks if the given input is in the given range, inclusive. For strings, use [Length*] functions.

func LengthMax added in v0.4.0

func LengthMax[T ~string](input T, max int) bool

LengthMax checks if a string's utf8 length is equal or lesser the given maximum.

func LengthMin added in v0.4.0

func LengthMin[T ~string](input T, min int) bool

LengthMin checks if a string's utf8 length is equal or greater the given minimum.

func LengthRange added in v0.4.0

func LengthRange[T ~string](input T, min, max int) bool

LengthRange checks if a string's utf8 length is inside the given range, inclusive.

func Matches

func Matches(input string, re *regexp.Regexp) bool

Matches will match a string with a regular expression.

func Max

func Max[T cmp.Ordered](input T, max T) bool

Max checks if the input is equal or lesser than a maximum. For length, use LengthMax instead.

func Min

func Min[T cmp.Ordered](input T, min T) bool

Min checks if the input is equal or bigger than a minimum. For length, use LengthMin instead.

func Not added in v0.4.0

func Not(predicate bool) bool

Not returns the negation of the given predicate.

func Range

func Range[T cmp.Ordered](input T, min, max T) bool

Range checks if the input is inside a number range, inclusive. For length, use LengthRange instead.

func StartsWith

func StartsWith[T ~string](input T, prefixes ...T) bool

StartsWith check whether the input starts with one of the given prefixes.

func Unique

func Unique[T cmp.Ordered](values []T) bool

Unique checks if all elements of a given slice are unique.

Types

type Case

type Case struct {
	Cond bool
	Msg  string
}

Case is a test-case, consisting of two parts. Cond will call a function that returns a boolean. If it returns false, Msg will be added to the [Validator.Errors].

type ValidationError added in v0.2.0

type ValidationError map[string][]string

ValidationError holds all validation error messages. It also implements the error interface.

func (ValidationError) Error added in v0.2.0

func (v ValidationError) Error() string

Error returns validation errors in the following format:

"field_1: validation error, field_2: first error"

type Validator

type Validator struct {
	Errors ValidationError `json:"errors"`
}

Validator will check for cases by [Check] method and will return a boolean with the [Validate] method. If a validation error happens, the Msg will be stored inside the [Errors] map.

func New

func New() *Validator

New will return an instance of Validator.

func (*Validator) Check

func (v *Validator) Check(key string, cases ...Case)

Check accepts name of the field as the first argument, following by an arbitrary number of validation Case.

func (Validator) Validate added in v0.4.0

func (v Validator) Validate() bool

Validate returns a boolean indicating whether validation was successful or not.

Jump to

Keyboard shortcuts

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