url
: The param is part of the URL as in/item/your-param
.query
: The param is part of the query string as initem?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 usingmultipart/form-data
. The param type MUST be a*formfile.FormFile
.
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).
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
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 withrequired
is thatrequired
does not accept nil pointer (works on array too).
image
: The provided file is required to be an image.
no_empty_items
: If the array contains an empty item, an error will be thrown.
- Use
json:"_"
to prevent a field to be altered or checked. - Use
json:"field_name"
to name a field.
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}
.
You can set a comma separated list of valid values using
enum:"value1,value2,value3"
. Empty and nil values are accepted.
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
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
You can add a custom validator by implementing params.CustomValidation
.
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"`
}