Skip to main content

required

required is a directive executor who tags a field as required. The "required" here doesn't mean the final value after decoding must be non-zero, it means the key/field must be present in the input data. If you want to ensure the value not to be zero/empty, use nonzero instead.

required always works as an accompany with some other directives. How it works is that it checks whether the field had been set by the former directives. Which means if you use required alone, or put it at the first place of the directives, it will always return an error "missing required field".

Signature

Name: "required"
Args: (no args)

Decoding

When the field were not set by the former directives, an error "missing required field" will occur.

Usage

type TokenInput struct {
// Make sure the access_token is present in the query string, we don't care about the value.
// The value can be empty, e.g. `?access_token=`.
Token string `in:"query=access_token;required"`
}
RequestOutput
GET /users?access_token=abc&page=1
&TokenInput{
Token: "abc",
}
GET /users?page=1
// error occurred
&InvalidFieldError{
Directive: "required",
Field: "access_token",
Key: "",
Value: nil,
ErrorMessage: "missing required field",
}

Encoding

Useless.