valpar

package module
v0.4.0 Latest Latest
Warning

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

README

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 github.com/go-playground/validator.v9 library developed by Dean Karn). Also It sanitizes the string values to prevent XSS attacks.

News

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

Installation

go get -u aahframework.org/valpar.v0

Visit official website https://aahframework.org to learn more about aah framework.

Documentation

Overview

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

aah internally uses library `https://github.com/go-playground/validator` for enabling validations feature across.

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

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

Version valpar library version no.

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