type SigninForm struct {
Category int `form:"category" json:"category" binding:"required"`
Username string `form:"username" json:"username" binding:"required"`
Password string `form:"password" json:"password" binding:"required"`
}
"forms.SigninForm": {
"type": "object",
"required": [ . // expected
"category",
"username",
"password"
],
"properties": {
"category": {
"type": "integer"
},
"password": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
Why is this required?
Is this supported by swagger?
Is this supported by swagger?
Yes, this is supported by swagger. For example in Swagger Editor Line 650
Pet:
type: "object"
required:
- "name"
- "photoUrls"
properties:
The required field "name" and "photoUrls" will show a red mark in document of Pet model definition.
Usage:
binding, the required attribute must be set in the binding tag, example in testdata/simple/handle.go Pet definition:type Pet struct {
ID int `json:"id" example:"1" format:"int64"`
Category struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"category_name"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg" format:"url"`
SmallCategory struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"detail_category_name" binding:"required"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg"`
} `json:"small_category"`
} `json:"category"`
Name string `json:"name" example:"poti"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg" binding:"required"`
Tags []Tag `json:"tags"`
Pets *[]Pet2 `json:"pets"`
Pets2 []*Pet2 `json:"pets2"`
Status string `json:"status"`
Price float32 `json:"price" example:"3.25"`
IsAlive bool `json:"is_alive" example:"true"`
Data interface{} `json:"data"`
Hidden string `json:"-"`
UUID uuid.UUID `json:"uuid"`
Decimal decimal.Decimal `json:"decimal"`
}
validate, example of init validator config:func main() {
config := &validator.Config{TagName: "validate"}
validate = validator.New(config)
validateStruct()
validateField()
}
example in testdata/simple2/handle.go Pet definition:
type Pet struct {
ID int `example:"1" format:"int64"`
Category struct {
ID int `example:"1"`
Name string `example:"category_name"`
PhotoUrls []string `example:"http://test/image/1.jpg,http://test/image/2.jpg" format:"url"`
SmallCategory struct {
ID int `example:"1"`
Name string `example:"detail_category_name" validate:"required"`
PhotoUrls []string `example:"http://test/image/1.jpg,http://test/image/2.jpg"`
}
}
Name string `example:"poti"`
PhotoUrls []string `example:"http://test/image/1.jpg,http://test/image/2.jpg"`
Tags []Tag
Pets *[]Pet2
Pets2 []*Pet2
Status string
Price float32 `example:"3.25" validate:"required,gte=0,lte=130"`
IsAlive bool `example:"true"`
Data interface{}
Hidden string `json:"-"`
UUID uuid.UUID
Decimal decimal.Decimal
}
@knight07
This feature also solve your problem in gin-swagger
Most helpful comment
Usage:
binding, therequiredattribute must be set in thebindingtag, example in testdata/simple/handle.go Pet definition:validate, example of init validator config:example in testdata/simple2/handle.go Pet definition:
@knight07
This feature also solve your problem in gin-swagger