2

I have a error model that looked like this

Error:
    type: object
    properties:
      code:
        type: string
        example: missing_field
        description: This field contains a string succinctly identifying the problem.
      message:
        type: string
        example: The `first_name` field is required.
        description: This field contain a plainly-written, developer-oriented explanation of the solution to the problem in complete, well-formed sentences.
      more_info:
        type: string
        format: url
        example: https://docs.api.example.com/v2/users/create_user#first_name
        description: This field SHOULD contain a publicly-accessible URL where information about the error can be read in a web browser.
      target:
        $ref: '#/definitions/Error_target'
    description: error model

I used go-swagger to generate server stub. In models/error.go file

import (
    "context"

    "github.com/go-openapi/errors"
    "github.com/go-openapi/strfmt"
    "github.com/go-openapi/swag"
)

// swagger:model Error
type Error struct {

    // This field contains a string succinctly identifying the problem.
    // Example: missing_field
    Code string `json:"code,omitempty"`

    // This field contain a plainly-written, developer-oriented explanation of the solution to the problem in complete, well-formed sentences.
    // Example: The `first_name` field is required.
    Message string `json:"message,omitempty"`

    // This field SHOULD contain a publicly-accessible URL where information about the error can be read in a web browser.
    // Example: https://docs.api.example.com/v2/users/create_user#first_name
    MoreInfo string `json:"more_info,omitempty"`

    // target
    Target *ErrorTarget `json:"target,omitempty"`
}

// Validate validates this error
func (m *Error) Validate(formats strfmt.Registry) error {
    var res []error

    if err := m.validateTarget(formats); err != nil {
        res = append(res, err)
    }

    if len(res) > 0 {
        return errors.CompositeValidationError(res...)
    }
    return nil
}

func (m *Error) validateTarget(formats strfmt.Registry) error {
...
}

The function Validate(formats strfmt.Registry) takes in a parameter called formats of type strfmt.Registry. Not sure, how and where to use this validate function and what to pass a argument for formats. Any example is greatly appreciated.

BhanuKiran
  • 2,631
  • 3
  • 20
  • 36

0 Answers0