validator

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 6 Imported by: 0

README

Validator

How to use

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"}
	emailRX := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)

	v := validator.New()
	v.Check("name", validator.Case{Cond: validator.NotEmpty(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, emailRX), Msg: "must be valid email"})

	if ok := v.Valid(); !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.NotEmpty(d.Name), Msg: "must not be empty"},
		validator.Case{Cond: validator.MinLength(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.RangeLength(d.Phone, 4, 10), Msg: "must be between 4 to 10 digits"},
	)

	if ok := v.Valid(); !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 Empty

func Empty(value string) bool

Empty checks if a string is empty.

func EndsWith

func EndsWith(value, suffix string) bool

EndsWith check whether a string ends with a particular suffix.

func In

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

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

func IsNumber

func IsNumber(value string) bool

IsNumber checks if the given string is all numbers.

func Matches

func Matches(value string, rx *regexp.Regexp) bool

Matches will match a string with a regular expression.

func Max

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

Max checks if a value is equal or lesser than a maximum. For length, use MaxLength instead.

func MaxLength

func MaxLength(value string, max int) bool

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

func Min

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

Min checks if a value is equal or bigger than a minimum. For length, use MinLength instead.

func MinLength

func MinLength(value string, min int) bool

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

func NotEmpty

func NotEmpty(value string) bool

NotEmpty checks if the given value is not empty.

func Range

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

Range checks if a value is inside a number range, inclusive. For length, use RangeLength instead.

func RangeLength

func RangeLength(value string, min, max int) bool

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

func StartsWith

func StartsWith(value, prefix string) bool

StartsWith check whether a string starts with a particular prefix.

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 implements 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 Valid 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) Valid

func (v *Validator) Valid() bool

Valid 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