Categorygithub.com/Nivl/go-params
modulepackage
0.0.0-20190421073528-b7dc2fed3e61
Repository: https://github.com/nivl/go-params.git
Documentation: pkg.go.dev

# 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"`
}

# Packages

No description provided by the author
No description provided by the author

# Functions

New creates a new Params object from a struct.
NewOptions returns a ParamOptions from a StructTag.

# Constants

ErrMsgArrayTooBig represents the error message corresponding to an array being too big.
ErrMsgArrayTooSmall represents the error message corresponding to an array being too small.
ErrMsgCorruptedFile represents the error message corresponding to a corrupted file.
ErrMsgEmptyFile represents the error message corresponding to an empty file being sent.
ErrMsgEmptyItem represents the error message corresponding to an array containing an empty item.
ErrMsgEmptyParameter represents the error message corresponding to a missing param.
ErrMsgEnum represents the error message corresponding to a field that doesn't contain a value set in an enum.
ErrMsgIntegerTooBig represents the error message corresponding to an integer being too big.
ErrMsgIntegerTooSmall represents the error message corresponding to an integer being too small.
ErrMsgInvalidBoolean represents the error message corresponding to an invalid boolean.
ErrMsgInvalidEmail represents the error message corresponding to an invalid Email address.
ErrMsgInvalidImage represents the error message corresponding to an invalid image.
ErrMsgInvalidInteger represents the error message corresponding to an invalid integer.
ErrMsgInvalidSlug represents the error message corresponding to an invalid slug.
ErrMsgInvalidSlugOrUUID represents the error message corresponding to a field that is neither a slug or a UUIDv4.
ErrMsgInvalidURL represents the error message corresponding to an invalid URL.
ErrMsgInvalidUUID represents the error message corresponding to an invalid UUID.
ErrMsgMaxLen represents the error message corresponding to a field that exceed the maximum number of char.
ErrMsgMissingParameter represents the error message corresponding to a missing param.

# Structs

Options represent all the options for a field.
Param represents a struct param.
Params is a struct used to parse and extract params from an other struct.

# Interfaces

CustomValidation is an interface used to implements custom validation on a structure.
Scanner is an interface used to allow injecting data into a custom type.