params

package module
v0.0.0-...-b7dc2fe Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2019 License: MIT Imports: 14 Imported by: 4

README

Params options

Actions Status Go Report Card codecov GoDoc

Source types (from:"")

  • url: The param is part of the URL as in /item/your-param.
  • query: The param is part of the query string as in item?id=your-param.
  • form: The param is part of the body of the request. It can be from a JSON payload or a basic form-urlencoded payload.
  • file: The param is a file sent using multipart/form-data. The param type MUST be a *formfile.FormFile.

Params type (params:"")

global options (works on most of the types)
  • required: The field is required and an error will be returned if the field is empty (or contains less than 1 element for an array).
String specific params
  • trim: The value will be trimmed of its trailing spaces.
  • uuid: The value is required to be a valid UUIDv4.
  • url: The value is required to be a valid http(s) url.
  • email: The value is required to be a valid email.
  • slug: The value is required to be a valid slug.
  • slugOrUuid: The value is required to be either a valid slug or a valid UUIDv4.

If used on an array, those params will be applied on each values of the array

Pointers specific params
  • noempty: The value cannot be empty. The pointer can be nil, but if a value is provided it cannot be an empty string. the difference with required is that required does not accept nil pointer (works on array too).
Files specific params
  • image: The provided file is required to be an image.
Array specific params
  • no_empty_items: If the array contains an empty item, an error will be thrown.

Ignoring and naming json:""

  • Use json:"_" to prevent a field to be altered or checked.
  • Use json:"field_name" to name a field.

Default value

Use default:"my_value" to set a default value. The default value will be used if nothing is found in the payload or if the provided value is an empty string.

For an array, you can use a comma (,) to separate a default value of multiple data: default:"1,2,3" on a []string type will output []string{1,2,3}.

Enum values

You can set a comma separated list of valid values using enum:"value1,value2,value3". Empty and nil values are accepted.

Min/Max values for integers

You can set a min value or a max value for an integer using min_int:"0" max_int:"10".

If used on an array, those params will be applied on each values of the array

Maxlen of a string

Use maxlen:"255" to make sure the len of a string is not bigger than 255 char. Any invalid values (including 0) will be ignored.

If used on an array, those params will be applied on each values of the array

Custom Validator

You can add a custom validator by implementing params.CustomValidation.

Examples

type UpdateParams struct {
  ID        string  `from:"url" json:"id" params:"required,uuid"`
  Name      *string `from:"form" json:"name" params:"trim,noempty"`
  ShortName *string `from:"form" json:"short_name" params:"trim"`
  Website   *string `from:"form" json:"website" params:"url,trim"`
  InTrash   *bool   `from:"form" json:"in_trash"`
  Items     []int   `from:"form" json:"items" max_items="3" params:"trim,empty"`
}

Documentation

Index

Constants

View Source
const (
	// ErrMsgMissingParameter represents the error message corresponding to
	// a missing param
	ErrMsgMissingParameter = "parameter missing"

	// ErrMsgEmptyParameter represents the error message corresponding to
	// a missing param
	ErrMsgEmptyParameter = "parameter can be omitted but not empty"

	// ErrMsgInvalidUUID represents the error message corresponding to
	// an invalid UUID
	ErrMsgInvalidUUID = "not a valid uuid"

	// ErrMsgInvalidSlug represents the error message corresponding to
	// an invalid slug
	ErrMsgInvalidSlug = "not a valid slug"

	// ErrMsgInvalidSlugOrUUID represents the error message corresponding to
	// a field that is neither a slug or a UUIDv4
	ErrMsgInvalidSlugOrUUID = "not a valid slug"

	// ErrMsgInvalidURL represents the error message corresponding to
	// an invalid URL
	ErrMsgInvalidURL = "not a valid url"

	// ErrMsgInvalidEmail represents the error message corresponding to
	// an invalid Email address
	ErrMsgInvalidEmail = "not a valid email"

	// ErrMsgInvalidImage represents the error message corresponding to
	// an invalid image
	ErrMsgInvalidImage = "not a valid image"

	// ErrMsgMaxLen represents the error message corresponding to
	// a field that exceed the maximum number of char
	ErrMsgMaxLen = "too many chars"

	// ErrMsgEnum represents the error message corresponding to
	// a field that doesn't contain a value set in an enum
	ErrMsgEnum = "not a valid value"

	// ErrMsgInvalidBoolean represents the error message corresponding to
	// an invalid boolean
	ErrMsgInvalidBoolean = "invalid boolean"

	// ErrMsgInvalidInteger represents the error message corresponding to
	// an invalid integer
	ErrMsgInvalidInteger = "invalid integer"

	// ErrMsgIntegerTooBig represents the error message corresponding to
	// an integer being too big
	ErrMsgIntegerTooBig = "value too high"

	// ErrMsgIntegerTooSmall represents the error message corresponding to
	// an integer being too small
	ErrMsgIntegerTooSmall = "value too small"

	// ErrMsgEmptyFile represents the error message corresponding to
	// an empty file being sent
	ErrMsgEmptyFile = "file empty"

	// ErrMsgCorruptedFile represents the error message corresponding to
	// a corrupted file
	ErrMsgCorruptedFile = "file seems corrupted"

	// ErrMsgArrayTooBig represents the error message corresponding to
	// an array being too big
	ErrMsgArrayTooBig = "too many elements"

	// ErrMsgArrayTooSmall represents the error message corresponding to
	// an array being too small
	ErrMsgArrayTooSmall = "too few elements"

	// ErrMsgEmptyItem represents the error message corresponding to
	// an array containing an empty item
	ErrMsgEmptyItem = "array cannot contain empty items"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomValidation

type CustomValidation interface {
	// IsValid checks if the provided params are valid.
	// returns a boolean, the name of the field that failed (if any),
	// and the error (if any)
	IsValid() (isValid bool, fieldFailing string, err error)
}

CustomValidation is an interface used to implements custom validation on a structure

type Options

type Options struct {
	// AuthorizedValues represents the list of authorized value for this param
	// enum:"and,or"
	AuthorizedValues []string

	// MinInt represents the minimum value accepted for an integer
	// min_int:"1"
	MinInt *int

	// MaxInt represents the maximum value accepted for an integer
	// max_int:"255"
	MaxInt *int

	// MaxItems represents the maximum number of values accepted by an array
	// max_items:"10"
	MaxItems *int

	// MinItems represents the minimum number of values accepted by an array
	// min_items:"10"
	MinItems *int

	// MaxLen represents the maximum length a param can have (under its string
	// form). Any invalid values (including 0) will be ignored
	// maxlen:"255"
	MaxLen int

	// Name contains the name of the field in the payload
	// json:"my_field"
	Name string

	// Ignore means the field should not been parsed
	// json:"-"
	Ignore bool

	// Required means the request should fail with a Bad Request if the field is missing.
	// params:"required"
	Required bool

	// Trim means the field needs to be trimmed before being retrieved and checked
	// params:"trim"
	Trim bool

	// ValidateUUID means the field should contain a valid UUIDv4
	// params:"uuid"
	ValidateUUID bool

	// ValidateSlug means the field should contain a valid slug
	// params:"slug"
	ValidateSlug bool

	// ValidateSlugOrUUID means the field should contain a valid slug or a valid
	// UUIDv4
	// params:"slugOrUuid"
	ValidateSlugOrUUID bool

	// ValidateEmail means the field should contain a valid email
	// params:"email"
	ValidateEmail bool

	// ValidateURL means the field should contain a valid url
	// params:"url"
	ValidateURL bool

	// ValidateImage means the field should contain a valid image
	// params:"image"
	ValidateImage bool

	// NoEmpty means the field should not contain an empty value
	// The difference between `required` and `noempty` is that `require` does`
	// not accept nil pointer. `noempty` accepts nil pointer, but if a value is
	// provided it cannot be an empty string.
	// params:"noempty"
	NoEmpty bool

	// NoEmptyItems means all items of an array needs to have a value
	//params:"no_empty_items"
	NoEmptyItems bool
}

Options represent all the options for a field

func NewOptions

func NewOptions(tags *reflect.StructTag) (*Options, error)

NewOptions returns a ParamOptions from a StructTag

func (*Options) ApplyTransformations

func (opts *Options) ApplyTransformations(value string) string

ApplyTransformations applies all the wanted transformations to the given value

func (*Options) Validate

func (opts *Options) Validate(value string, wasProvided, isArrayItem bool) error

Validate checks the given value passes the options set

func (*Options) ValidateFileContent

func (opts *Options) ValidateFileContent(file io.ReadSeeker) (mimeType string, err error)

ValidateFileContent checks the given file passes the options set

func (*Options) ValidateSlice

func (opts *Options) ValidateSlice(values []string, wasProvided bool) error

ValidateSlice checks the given slice passes the options set

type Param

type Param struct {
	Value *reflect.Value
	Info  *reflect.StructField
	Tags  *reflect.StructTag
}

Param represents a struct param

func (*Param) SetFile

func (p *Param) SetFile(source formfile.FileHolder) error

SetFile sets the value of the param using the provided source to find the file

func (*Param) SetValue

func (p *Param) SetValue(source url.Values) error

SetValue sets the value of the param using the provided source

type Params

type Params struct {
	// contains filtered or unexported fields
}

Params is a struct used to parse and extract params from an other struct

func New

func New(data interface{}) *Params

New creates a new Params object from a struct

func (*Params) Extract

func (p *Params) Extract() (sources map[string]url.Values, files map[string]*formfile.FormFile)

Extract extracts the data from the paramsStruct and returns them as a map of url.Values

func (*Params) Parse

func (p *Params) Parse(sources map[string]url.Values, fileHolder formfile.FileHolder) error

Parse fills the paramsStruct using the provided sources

type Scanner

type Scanner interface {
	ScanString(date string) error
}

Scanner is an interface used to allow injecting data into a custom type

Directories

Path Synopsis
mockformfile
Package mockformfile is a generated GoMock package.
Package mockformfile is a generated GoMock package.

Jump to

Keyboard shortcuts

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