package module
Version: v0.4.0 Latest Latest

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

Go to latest
Published: Jun 18, 2018 License: MIT Imports: 14 Imported by: 4


Value Parser Library by aah framework

Build Status Code Coverage Go Report Card Release Version Godoc

Value Parser houses Parse & bind parameters for Controller Action and Validator (internally aah uses library developed by Dean Karn). Also It sanitizes the string values to prevent XSS attacks.


  • v0.4.0 released and tagged on Jun 17, 2018.


go get -u

Visit official website to learn more about aah framework.



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.



This section is empty.


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

Version valpar library version no.


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.


  • 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 for detailed documentation.


type Error added in v0.4.0

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 added in v0.4.0

func (e Error) String() string

String is Stringer interface.

type Errors added in v0.4.0

type Errors []*Error

Errors type represents list errors.

func ValidateValues added in v0.4.0

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 added in v0.4.0

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