validate

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ContainsDigit = RuleSet{
	Name: "containsDigit",
	ValidateFunc: func(rule RuleSet) bool {
		str, ok := rule.FieldValue.(string)
		if !ok {
			return false
		}
		return hasDigit(str)
	},
	MessageFunc: func(set RuleSet) string {
		return "must contain at least 1 numeric character"
	},
}
View Source
var ContainsSpecial = RuleSet{
	Name: "containsSpecial",
	ValidateFunc: func(rule RuleSet) bool {
		str, ok := rule.FieldValue.(string)
		if !ok {
			return false
		}
		return hasSpecialChar(str)
	},
	MessageFunc: func(set RuleSet) string {
		return "must contain at least 1 special character"
	},
}
View Source
var ContainsUpper = RuleSet{
	Name: "containsUpper",
	ValidateFunc: func(rule RuleSet) bool {
		str, ok := rule.FieldValue.(string)
		if !ok {
			return false
		}
		for _, ch := range str {
			if unicode.IsUpper(rune(ch)) {
				return true
			}
		}
		return false
	},
	MessageFunc: func(set RuleSet) string {
		return "must contain at least 1 uppercase character"
	},
}
View Source
var Email = RuleSet{
	Name: "email",
	MessageFunc: func(set RuleSet) string {
		return "is not a valid email address"
	},
	ValidateFunc: func(set RuleSet) bool {
		email, ok := set.FieldValue.(string)
		if !ok {
			return false
		}
		return emailRegex.MatchString(email)
	},
}
View Source
var Required = RuleSet{
	Name: "required",
	MessageFunc: func(set RuleSet) string {
		return "is a required field"
	},
	ValidateFunc: func(rule RuleSet) bool {
		str, ok := rule.FieldValue.(string)
		if !ok {
			return false
		}
		return len(str) > 0
	},
}
View Source
var Time = RuleSet{
	Name: "time",
	ValidateFunc: func(set RuleSet) bool {
		t, ok := set.FieldValue.(time.Time)
		if !ok {
			return false
		}
		return t.After(time.Time{})
	},
	MessageFunc: func(set RuleSet) string {
		return "is not a valid time"
	},
}
View Source
var URL = RuleSet{
	Name: "url",
	MessageFunc: func(set RuleSet) string {
		return "is not a valid url"
	},
	ValidateFunc: func(set RuleSet) bool {
		u, ok := set.FieldValue.(string)
		if !ok {
			return false
		}
		return urlRegex.MatchString(u)
	},
}
View Source
var Username = RuleSet{
	Name: "username",
	MessageFunc: func(set RuleSet) string {
		return "is not a valid username"
	},
	ValidateFunc: func(set RuleSet) bool {
		username, ok := set.FieldValue.(string)
		if !ok {
			return false
		}
		return usernRegex.MatchString(username)
	},
}

Functions

This section is empty.

Types

type Errors

type Errors map[string][]string

Errors is a map holding all the possible errors that may occur during validation.

func Request

func Request(r *http.Request, data any, schema Schema) (Errors, bool)

Request parses an http.Request into data and validates it based on the given schema.

func Validate

func Validate(data any, fields Schema) (Errors, bool)

Validate validates data based on the given Schema.

func (Errors) Add

func (e Errors) Add(field string, msg string)

Add adds an error for a specific field

func (Errors) Any

func (e Errors) Any() bool

Any return true if there is any error.

func (Errors) Get

func (e Errors) Get(field string) []string

Get returns all the errors for the given field.

func (Errors) Has

func (e Errors) Has(field string) bool

Has returns true whether the given field has any errors.

type Numeric

type Numeric interface {
	int | float64
}

type RuleSet

type RuleSet struct {
	Name         string
	RuleValue    any
	FieldValue   any
	FieldName    any
	ErrorMessage string
	MessageFunc  func(RuleSet) string
	ValidateFunc func(RuleSet) bool
}

RuleSet holds the state of a single rule.

func EQ

func EQ[T comparable](v T) RuleSet

func GT

func GT[T Numeric](n T) RuleSet

func GTE

func GTE[T Numeric](n T) RuleSet

func In

func In[T any](values []T) RuleSet

func LT

func LT[T Numeric](n T) RuleSet

func LTE

func LTE[T Numeric](n T) RuleSet

func Max

func Max(n int) RuleSet

func Min

func Min(n int) RuleSet

func Rules

func Rules(rules ...RuleSet) []RuleSet

Rules is a function that takes any amount of RuleSets

func TimeAfter

func TimeAfter(t time.Time) RuleSet

func TimeBefore

func TimeBefore(t time.Time) RuleSet

func (RuleSet) Message

func (set RuleSet) Message(msg string) RuleSet

Message overrides the default message of a RuleSet

type Schema

type Schema map[string][]RuleSet

Schema represents a validation schema.

func Merge

func Merge(schema, other Schema) Schema

Merge merges the two given schemas, returning a new Schema.

Jump to

Keyboard shortcuts

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