validator

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 3 Imported by: 3

README

validator

Validator is not validator, just a string error list helper :P

Why

f := flash()

if name == "" {
    f.AddError("name required")
}
if email == "" {
    f.AddError("email required")
}
if !govalidator.IsEmail(email) {
    f.AddError("invalid email")
}
if err := validatePassword(pass); err != nil {
    f.AddError("invalid password")
}
// ...
if f.HasError() {
    // ...
}

// no error

Then

v := validator.New() // this is not the validator :P

v.Must(name != "", "name required")
v.Must(email != "", "email required")
v.Must(govalidator.IsEmail(email), "invalid email")
v.Must(validatePassword(pass), "invalid password")
// ...
if !v.Valid() {
    // has error, ...
}

// no error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsError added in v1.1.0

func IsError(err error) bool

IsError returns true if given error is validate error

Types

type Error

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

Error is the validate error

func (*Error) Error

func (err *Error) Error() string

Error implements error interface

func (*Error) Errors

func (err *Error) Errors() []error

Errors returns errors

func (*Error) Strings

func (err *Error) Strings() []string

Strings returns errors in strings

type Validator

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

Validator type

func New

func New() *Validator

New creates new validator

func (*Validator) Add

func (v *Validator) Add(err ...error)

Add adds errors

func (*Validator) Error

func (v *Validator) Error() error

Error returns error if has error

func (*Validator) Must

func (v *Validator) Must(x interface{}, msg interface{}) bool

Must checks x must not an error or true if bool and return true if valid

msg must be error or string msg can be nil if x is error

func (*Validator) Mustf added in v1.2.0

func (v *Validator) Mustf(x interface{}, format string, a ...interface{}) bool

Mustf calls fmt.Sprintf(format, a...) and pass to v.Must

func (*Validator) String

func (v *Validator) String() string

String implements fmt.Stringer

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid returns true if no error

Jump to

Keyboard shortcuts

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