rules

package
v0.8.8 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLengthTooLong is the error that returns in case of too long length.
	ErrLengthTooLong = NewError("validation_length_too_long", "the length must be no more than {{.max}}")
	// ErrLengthTooShort is the error that returns in case of too short length.
	ErrLengthTooShort = NewError("validation_length_too_short", "the length must be no less than {{.min}}")
	// ErrLengthInvalid is the error that returns in case of an invalid length.
	ErrLengthInvalid = NewError("validation_length_invalid", "the length must be exactly {{.min}}")
	// ErrLengthOutOfRange is the error that returns in case of out of range length.
	ErrLengthOutOfRange = NewError("validation_length_out_of_range", "the length must be between {{.min}} and {{.max}}")
	// ErrLengthEmptyRequired is the error that returns in case of non-empty value.
	ErrLengthEmptyRequired = NewError("validation_length_empty_required", "the value must be empty")
)
View Source
var (
	// ErrRequired is the error that returns when a value is required.
	ErrRequired = NewError("validation_required", "cannot be blank")
	// ErrNilOrNotEmpty is the error that returns when a value is not nil and is empty.
	ErrNilOrNotEmpty = NewError("validation_nil_or_not_empty_required", "cannot be blank")
)
View Source
var ErrMatchInvalid = NewError("validation_match_invalid", "must be in a valid format")

ErrMatchInvalid is the error that returns in case of invalid format.

View Source
var NilOrNotEmpty = RequiredRule{/* contains filtered or unexported fields */}

NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty. NilOrNotEmpty differs from Required in that it treats a nil pointer as valid.

View Source
var Required = RequiredRule{/* contains filtered or unexported fields */}

Required is a validation rule that checks if a value is not empty. A value is considered not empty if - integer, float: not zero - bool: true - string, array, slice, map: len() > 0 - interface, pointer: not nil and the referenced value is not empty - any other types

Functions

func By

func By(f RuleFunc) validate.Rule

func EnsureString

func EnsureString(value interface{}) (string, error)

EnsureString ensures the given value is a string. If the value is a byte slice, it will be typecast into a string. An error is returned otherwise.

func Indirect

func Indirect(value interface{}) (interface{}, bool)

Indirect returns the value that the given interface or pointer references to. If the value implements driver.Valuer, it will deal with the value returned by the Value() method instead. A boolean value is also returned to indicate if the value is nil or not (only applicable to interface, pointer, map, and slice). If the value is neither an interface nor a pointer, it will be returned back.

func IsEmpty

func IsEmpty(value interface{}) bool

IsEmpty checks if a value is empty or not. A value is considered empty if - integer, float: zero - bool: false - string, array: len() == 0 - slice, map: nil or len() == 0 - interface, pointer: nil or the referenced value is empty

func LengthOfValue

func LengthOfValue(value interface{}) (int, error)

LengthOfValue returns the length of a value that is a string, slice, map, or array. An error is returned for all other types.

func StringOrBytes

func StringOrBytes(value interface{}) (isString bool, str string, isBytes bool, bs []byte)

StringOrBytes typecasts a value into a string or byte slice. Boolean flags are returned to indicate if the typecasting succeeds or not.

func ToFloat

func ToFloat(value interface{}) (float64, error)

ToFloat converts the given value to a float64. An error is returned for all incompatible types.

func ToInt

func ToInt(value interface{}) (int64, error)

ToInt converts the given value to an int64. An error is returned for all incompatible types.

func ToUint

func ToUint(value interface{}) (uint64, error)

ToUint converts the given value to an uint64. An error is returned for all incompatible types.

Types

type Error

type Error interface {
	Error() string
	Code() string
	Message() string
	SetMessage(string) Error
	Params() map[string]interface{}
	SetParams(map[string]interface{}) Error
}

Error interface represents an validation error

func NewError

func NewError(code, message string) Error

NewError create new validation error.

type ErrorObject

type ErrorObject struct {
	// contains filtered or unexported fields
}

ErrorObject is the default validation error that implements the Error interface.

func (ErrorObject) AddParam

func (e ErrorObject) AddParam(name string, value interface{}) Error

AddParam add parameter to the error's parameters.

func (ErrorObject) Code

func (e ErrorObject) Code() string

Code get the error's translation code.

func (ErrorObject) Error

func (e ErrorObject) Error() string

Error returns the error message.

func (ErrorObject) Message

func (e ErrorObject) Message() string

Message return the error's message.

func (ErrorObject) Params

func (e ErrorObject) Params() map[string]interface{}

Params returns the error's params.

func (ErrorObject) SetCode

func (e ErrorObject) SetCode(code string) Error

SetCode set the error's translation code.

func (ErrorObject) SetMessage

func (e ErrorObject) SetMessage(message string) Error

SetMessage set the error's message.

func (ErrorObject) SetParams

func (e ErrorObject) SetParams(params map[string]interface{}) Error

SetParams set the error's params.

type LengthRule

type LengthRule struct {
	// contains filtered or unexported fields
}

LengthRule is a validation rule that checks if a value's length is within the specified range.

func Length

func Length(min, max int) LengthRule

Length returns a validation rule that checks if a value's length is within the specified range. If max is 0, it means there is no upper bound for the length. This rule should only be used for validating strings, slices, maps, and arrays. An empty value is considered valid. Use the Required rule to make sure a value is not empty.

func RuneLength

func RuneLength(min, max int) LengthRule

RuneLength returns a validation rule that checks if a string's rune length is within the specified range. If max is 0, it means there is no upper bound for the length. This rule should only be used for validating strings, slices, maps, and arrays. An empty value is considered valid. Use the Required rule to make sure a value is not empty. If the value being validated is not a string, the rule works the same as Length.

func (LengthRule) Error

func (r LengthRule) Error(message string) LengthRule

Error sets the error message for the rule.

func (LengthRule) ErrorObject

func (r LengthRule) ErrorObject(err Error) LengthRule

ErrorObject sets the error struct for the rule.

func (LengthRule) Validate

func (r LengthRule) Validate(value interface{}) error

Validate checks if the given value is valid or not.

type MatchRule

type MatchRule struct {
	// contains filtered or unexported fields
}

MatchRule is a validation rule that checks if a value matches the specified regular expression.

func Match

func Match(re *regexp.Regexp) MatchRule

Match returns a validation rule that checks if a value matches the specified regular expression. This rule should only be used for validating strings and byte slices, or a validation error will be reported. An empty value is considered valid. Use the Required rule to make sure a value is not empty.

func (MatchRule) Error

func (r MatchRule) Error(message string) MatchRule

Error sets the error message for the rule.

func (MatchRule) ErrorObject

func (r MatchRule) ErrorObject(err Error) MatchRule

ErrorObject sets the error struct for the rule.

func (MatchRule) Validate

func (r MatchRule) Validate(value interface{}) error

Validate checks if the given value is valid or not.

type RequiredRule

type RequiredRule struct {
	// contains filtered or unexported fields
}

RequiredRule is a rule that checks if a value is not empty.

func (RequiredRule) Error

func (r RequiredRule) Error(message string) RequiredRule

Error sets the error message for the rule.

func (RequiredRule) ErrorObject

func (r RequiredRule) ErrorObject(err Error) RequiredRule

ErrorObject sets the error struct for the rule.

func (RequiredRule) Validate

func (r RequiredRule) Validate(value interface{}) error

Validate checks if the given value is valid or not.

func (RequiredRule) When

func (r RequiredRule) When(condition bool) RequiredRule

When sets the condition that determines if the validation should be performed.

type RuleFunc

type RuleFunc func(value interface{}) error

Jump to

Keyboard shortcuts

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