validator

package
v0.0.0-...-0b42950 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Example (Basic)
package main

import (
	"fmt"
	"time"

	"github.com/pauluswi/alpine/pkg/validator"
)

func main() {
	// UserForm struct
	type UserForm struct {
		Name     string    `validate:"required,gte=7"`
		Email    string    `validate:"email"`
		Age      int       `validate:"required,numeric,min=1,max=99"`
		CreateAt int       `validate:"gte=1"`
		Safe     int       `validate:"-"`
		UpdateAt time.Time `validate:"required"`
		Code     string    `validate:"required"`
	}

	err := validator.Validate(UserForm{})
	fmt.Println(err.Error())

}
Output:

UserForm.Name must be required; UserForm.Email must be email; UserForm.Age must be required, actual value is 0; UserForm.CreateAt must be gte{1}, actual value is 0; UserForm.UpdateAt must be required, actual value is 0001-01-01 00:00:00 +0000 UTC; UserForm.Code must be required
Example (Change_validator)
package main

import (
	"fmt"
	"time"

	ovalidator "github.com/go-playground/validator/v10"

	"github.com/pauluswi/alpine/pkg/validator"
)

func main() {
	// UserForm struct
	type UserForm struct {
		Name     string    `validate:"required,gte=7"`
		Email    string    `validate:"email"`
		Age      int       `validate:"required,numeric,min=1,max=99"`
		CreateAt int       `validate:"gte=1"`
		Safe     int       `validate:"-"`
		UpdateAt time.Time `validate:"required"`
		Code     string    `validate:"required"`
	}

	custval := ovalidator.New()

	validator.SetGlobal(custval)
	err := validator.Validate(UserForm{})
	fmt.Println(err)

}
Output:

UserForm.Name must be required; UserForm.Email must be email; UserForm.Age must be required, actual value is 0; UserForm.CreateAt must be gte{1}, actual value is 0; UserForm.UpdateAt must be required, actual value is 0001-01-01 00:00:00 +0000 UTC; UserForm.Code must be required
Example (Optional_mode_compact)
package main

import (
	"fmt"
	"time"

	"github.com/pauluswi/alpine/pkg/validator"
)

func main() {
	// UserForm struct
	type UserForm struct {
		Name     string    `validate:"required,gte=7"`
		Email    string    `validate:"email"`
		Age      int       `validate:"required,numeric,min=1,max=99"`
		CreateAt int       `validate:"gte=1"`
		Safe     int       `validate:"-"`
		UpdateAt time.Time `validate:"required"`
		Code     string    `validate:"required"`
	}

	err := validator.ValidateWithOpts(UserForm{}, validator.Opts{Mode: validator.ModeCompact})
	fmt.Println(err.Error())

}
Output:

invalid fields: Name, Email, Age, CreateAt, UpdateAt, Code
Example (Optional_mode_verbose)
package main

import (
	"fmt"
	"time"

	"github.com/pauluswi/alpine/pkg/validator"
)

func main() {
	// UserForm struct
	type UserForm struct {
		Name     string    `validate:"required,gte=7"`
		Email    string    `validate:"email"`
		Age      int       `validate:"required,numeric,min=1,max=99"`
		CreateAt int       `validate:"gte=1"`
		Safe     int       `validate:"-"`
		UpdateAt time.Time `validate:"required"`
		Code     string    `validate:"required"`
	}

	err := validator.ValidateWithOpts(UserForm{}, validator.Opts{Mode: validator.ModeVerbose})
	fmt.Println(err.Error())

	// (SKIP) Output:
	// invalid fields at /Users/avreghlybarra/Developer/linkaja/neogodam/pkg/validator/example_test.go:61: UserForm.Name must be required; UserForm.Email must be email; UserForm.Age must be required, actual value is 0; UserForm.CreateAt must be gte{1}, actual value is 0; UserForm.UpdateAt must be required, actual value is 0001-01-01 00:00:00 +0000 UTC; UserForm.Code must be required
}
Output:

Example (Unwrap_to_original_error)
package main

import (
	"errors"
	"fmt"
	"time"

	ovalidator "github.com/go-playground/validator/v10"

	"github.com/pauluswi/alpine/pkg/validator"
)

func main() {
	// UserForm struct
	type UserForm struct {
		Name     string    `validate:"required,gte=7"`
		Email    string    `validate:"email"`
		Age      int       `validate:"required,numeric,min=1,max=99"`
		CreateAt int       `validate:"gte=1"`
		Safe     int       `validate:"-"`
		UpdateAt time.Time `validate:"required"`
		Code     string    `validate:"required"`
	}

	err := validator.ValidateWithOpts(UserForm{}, validator.Opts{Mode: validator.ModeVerbose})

	oerr := ovalidator.ValidationErrors{}
	_ = errors.As(err, &oerr)
	fmt.Println(oerr.Error())

}
Output:

Key: 'UserForm.Name' Error:Field validation for 'Name' failed on the 'required' tag
Key: 'UserForm.Email' Error:Field validation for 'Email' failed on the 'email' tag
Key: 'UserForm.Age' Error:Field validation for 'Age' failed on the 'required' tag
Key: 'UserForm.CreateAt' Error:Field validation for 'CreateAt' failed on the 'gte' tag
Key: 'UserForm.UpdateAt' Error:Field validation for 'UpdateAt' failed on the 'required' tag
Key: 'UserForm.Code' Error:Field validation for 'Code' failed on the 'required' tag

Index

Examples

Constants

View Source
const (
	ModeDefault = "default"
	ModeCompact = "compact"
	ModeVerbose = "verbose"
)

Variables

This section is empty.

Functions

func GetGlobal

func GetGlobal() *validator.Validate

func SetGlobal

func SetGlobal(v *validator.Validate)

func Validate

func Validate(s interface{}) (err error)

func ValidateWithOpts

func ValidateWithOpts(s interface{}, opt Opts) (err error)

Types

type Error

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

func (Error) Error

func (e Error) Error() (out string)

func (Error) Unwrap

func (e Error) Unwrap() error

type Opts

type Opts struct {
	Mode string
}

Jump to

Keyboard shortcuts

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