valpar.v0: Index | Files

package valpar

import ""

Package valpar implements methods to parse string value into Go typed reflect value.

aah internally uses library `` for enabling validations feature across.

It is typically used to do parse value of Controller action parameters for auto binding and validation.


Package Files

validator.go valpar.go version.go


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
var Version = "0.4.0"

Version valpar library version no.

func AddValueParser Uses

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

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

func Body Uses

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

Body method parse the body based on Content-Type.

func Struct Uses

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 Uses

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

Validate method is to validate struct via underneath validator.


- 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 Uses

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 Uses

func Validator() *validator.Validate

Validator method return the default validator of aah framework.

Refer to for detailed documentation.

type Error Uses

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 Uses

func (e Error) String() string

String is Stringer interface.

type Errors Uses

type Errors []*Error

Errors type represents list errors.

func ValidateValues Uses

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 Uses

func (e Errors) String() string

String is Stringer interface.

type Parser Uses

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 Uses

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.

Package valpar imports 14 packages (graph) and is imported by 3 packages. Updated 2018-06-18. Refresh now. Tools for package owners.