Swag: Support declare a parameter in struct as required

Created on 15 May 2018  路  4Comments  路  Source: swaggo/swag

  1. I define a struct model with go-playground/validator.v8 for validation:
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"`
}
  1. I want to make required fields declare in docs.go
"forms.SigninForm": {
            "type": "object",
            "required": [ .   // expected
                "category", 
                "username",
                "password"
            ],
            "properties": {
                "category": {
                    "type": "integer"
                },
                "password": {
                    "type": "string"
                },
                "username": {
                    "type": "string"
                }
            }
        },
feature

Most helpful comment

Usage:

  1. If you use gin framework default model binding and validation, the validation tag name is 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"`
}
  1. If you use go-playground/validator.v8 , the validation tag name must be init to 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

All 4 comments

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:

  1. If you use gin framework default model binding and validation, the validation tag name is 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"`
}
  1. If you use go-playground/validator.v8 , the validation tag name must be init to 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wingsofovnia picture wingsofovnia  路  3Comments

henrahmagix picture henrahmagix  路  7Comments

lfaoro picture lfaoro  路  4Comments

touyu picture touyu  路  3Comments

dz0ny picture dz0ny  路  5Comments