Documentation
¶
Overview ¶
Package urlvalues implements strict decoding of url.Values to given struct.
WARNING: This package is experimental, API will change!
It is safe for concurrent use by multiple goroutines.
Strict validation rules
- (optional) error on unknown param
- including param matching real, but not qualified enough field name:
- struct without .field (TODO in case custom handler not registered)
- map without [key]
- error on array overflow
- array with out-of-bound [index]
- too many params for array field
- error on scalar overflow
- multiple values for non-slice/array field
- multiple values for same `array[index]` or `map[key]` (in case this array/map doesn't have values of slice/array type)
- error on no values for non-pointer/slice/array field tagged `form:"…,required"`
- panic on unknown `form:""` tag option
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Errs ¶
Errs contain Decode errors.
Errs key can be "-" or pattern for corresponding Decode param values key.
Key "-" will contain all keys from Decode param values which are not correspond to any of Decode param v field and thus can't be decoded. This key won't exists if IgnoreUnknown option is used.
Pattern is same as values key with map key names replaced with [key] and array/slice indices replaced with [idx]. Example: if Decode was called with this key in values
FieldA.MapField[something].SliceOfSliceField[42][1].FieldB
then related key in Errs will be
FieldA.MapField[key].SliceOfSliceField[idx][idx].FieldB
type StrictDecoder ¶
type StrictDecoder struct {
// contains filtered or unexported fields
}
StrictDecoder wraps https://godoc.org/github.com/go-playground/form#Decoder to add strict validation of url.Values and normalize returned errors.
Differences from wrapped form.Decoder:
- Decoding to field with interface type is not supported.
- To make field required (meaning url.Values must contain any value for this field, including empty string) tag field with: `form:"…,required"`
func NewStrictDecoder ¶
func NewStrictDecoder(opts ...StrictDecoderOption) *StrictDecoder
NewStrictDecoder returns new StrictDecoder.
It's recommended to create one instance (for each opts) and reuse it to enable caching.
func (*StrictDecoder) Decode ¶ added in v0.2.0
func (d *StrictDecoder) Decode(v interface{}, values url.Values) error
Decode will decode values to v (which must be a pointer to a struct).
It'll normalize form.Decoder panics and errors and return nil or Errs. It will panic if called with wrong v, but never panics on wrong values.
type StrictDecoderOption ¶ added in v0.2.0
type StrictDecoderOption func(*StrictDecoder)
StrictDecoderOption is for internal use only and exported just to make golint happy.
func IgnoreUnknown ¶ added in v0.2.0
func IgnoreUnknown() StrictDecoderOption
IgnoreUnknown return an option for NewStrictDecoder.
With this option Decode won't return errors related to unknown keys in url.Values.
func MaxArraySize ¶ added in v0.2.0
func MaxArraySize(size uint) StrictDecoderOption
MaxArraySize return an option for NewStrictDecoder.
See https://godoc.org/github.com/go-playground/form#Decoder.SetMaxArraySize
func Mode ¶ added in v0.2.0
func Mode(mode form.Mode) StrictDecoderOption
Mode return an option for NewStrictDecoder.
See https://godoc.org/github.com/go-playground/form#Decoder.SetMode
func TagName ¶ added in v0.2.0
func TagName(tagName string) StrictDecoderOption
TagName return an option for NewStrictDecoder.
See https://godoc.org/github.com/go-playground/form#Decoder.SetTagName