valpar

package
v0.12.5 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package valpar provides feature of request value parsing, handling, binding and validating.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTypeOrParserIsNil returned when supplied `reflect.Type` or parser is nil to
	// the method `AddValueParser`.
	ErrTypeOrParserIsNil = errors.New("valpar: type or value parser is nil")

	// ErrValueParserIsAlreadyExists returned when given `reflect.Type` is already exists
	// in type parser list.
	ErrValueParserIsAlreadyExists = errors.New("valpar: value parser is already exists")

	// TimeFormats is configured values from aah.conf under `format { ... }`
	TimeFormats []string

	// StructTagName is used while binding struct fields.
	StructTagName string
)

Functions

func AddValueParser

func AddValueParser(typ reflect.Type, parser Parser) error

AddValueParser method adds given custom value parser for the `reflect.Type`

func Body

func Body(contentType string, body io.Reader, typ reflect.Type) (reflect.Value, error)

Body method parse the body based on Content-Type.

func Struct

func Struct(key string, typ reflect.Type, params url.Values) (reflect.Value, error)

Struct method parses the value based on Content-Type. It handles JSON and XML.

func Validate

func Validate(s interface{}) (validator.ValidationErrors, error)

Validate method is to validate struct via underneath validator.

Returns:

  • For validation errors: returns `validator.ValidationErrors` and nil

  • For invalid input: returns nil, error (invalid input such as nil, non-struct, etc.)

  • For no validation errors: nil, nil

func ValidateValue

func ValidateValue(v interface{}, constraint string) bool

ValidateValue method is to validate individual value. Returns true if validation is passed otherwise false.

For example:

i := 15
result := valpar.ValidateValue(i, "gt=1,lt=10")

emailAddress := "sample@sample"
result := valpar.ValidateValue(emailAddress, "email")

numbers := []int{23, 67, 87, 23, 90}
result := valpar.ValidateValue(numbers, "unique")

func Validator

func Validator() *validator.Validate

Validator method return the default validator of aah framework.

Refer to https://godoc.org/gopkg.in/go-playground/validator.v9 for detailed documentation.

Types

type Error

type Error struct {
	Field      string
	Value      string
	Key        string // i18n key
	Msg        string // i18n message
	Constraint string
}

Error represents single validation error details.

func (Error) String

func (e Error) String() string

String is Stringer interface.

type Errors

type Errors []*Error

Errors type represents list errors.

func ValidateValues

func ValidateValues(values map[string]string, constraints map[string]string) Errors

ValidateValues method validates the values with respective constraints. Returns nil if no errors otherwise slice of error.

func (Errors) String

func (e Errors) String() string

String is Stringer interface.

type Parser

type Parser func(key string, typ reflect.Type, params url.Values) (reflect.Value, error)

Parser interface is used to implement string -> type value parsing. This is similar to standard `strconv` package. It deals with reflect value.

func ValueParser

func ValueParser(typ reflect.Type) (Parser, bool)

ValueParser method returns the parser based on `reflect.Type` and `reflect.Kind`. It returns most of the value parser except Pointer and Struct kind. Since Pointer and Struct handled separately.

Jump to

Keyboard shortcuts

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