Parameter Rules

Overview

In go-zero, we declared HTTP service via api language, and then generated HTTP service code via goctl, after our systematic introduction to API norm.

The rules for checking parameters are already embedded in go-zero. Next, look at the rules for receiving/checking the parameters in go-zero.

Arguments Receive Rules

In the api description language, we can declare the parameter receiving rules in the tag. Currently, the parameter receiving rules supported by go-zero are as follows:

Parameter Rules - 图1Receive rulesNoteParameter Rules - 图2Scope of entrySample
jsonjson serializationRequest Body&Responsejson:"foo"
pathRoute parametersRequest Bodypath:"id"
formpost request form (supported for content-type -data and x-www-urrencoded) parameter request receiving identifier, get requested query parameterRequest Bodyform:"name"
headerHTTP Request Receipt IdentifierRequest Bodyheader:"Content-Length"
Parameter Rules - 图3Warm reminder

go-zero does not support multiple tags to receive parameters, that is, a field can only have one tag, and the following writing may cause the parameters to not be received:

  1. type Foo {
  2. Name string `json:"name" form:"name"`
  3. }

Parameter verification rules

In the api description language, we can state the acceptance rules for parameters in a tag in a tag. In addition to this we also support the verification of parameters, which are valid only for request , the parameter validation rule is written in tag value, currently supported by go-zero the following parameters:

Parameter Rules - 图4Receive rulesNoteSample
optionalThe current field is an optional parameter, allowing zero value (zero value)json:"foo,optional"
optionsCurrent parameter can only receive an enumeration valuejson:"gender,options=[foo,bar]"
defaultCurrent Argument Defaultjson:"gender,default=male"
rangeThe valid range of the current parameter value, only valid for the value. Details of the writing rule are given belowjson:"age,range=[0:120]"

::note range expressed value rule

  1. Left close interval:(min:max], meaning that min is less than or equal to max, when min is default, min represents value 0, max is unlimited when max is default, min and max are not allowed to default at the same time
  2. Left right interval:[min:max), which indicates that it is less than min max, when max is default, max represents a value of 0, min is large when min is missing
  3. Shutdown interval:[min:max], denotes less than min less than equals max, when min is default, min represents value 0, max is infinite when max is default, min and max are not allowed to coalesce
  4. Open interval:(min:max), indicates that min is less than max, when min is default, min represents a value of 0, max is large when max is default,min and max cannot be used simultaneous :::