Skip to content

Using minItems not generating correct validation code #2597

@omrihq

Description

@omrihq

Problem statement

Using minItems generates validation for minimum number of items, but doesn't include the name of the path, which is required for verbose errors

Swagger specification

paths:
  /v1/blah:
    patch:
      produces:
        - application/json
      parameters:
        - name: newArray
          in: body
          schema:
            $ref: '#/definitions/BlahArray'

definitions:
  BlahArray:
    description: The array of blah numbers
    type: array
    items:
      type: integer
    minItems: 1

Output

// In file blah_array.go
type BlahArray []int64

// Validate validates this blah array
func (m Blah) Validate(formats strfmt.Registry) error {
	var res []error

	iBlahArraySize := int64(len(m))

	if err := validate.MinItems("", "body", iBlahArraySize, 1); err != nil {
		return err
	}

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

Notice: validate.MinItems("", "body", iBlahArraySize, 1);, which results in error copy such as " in body should have at least 1 items"

Expectation would be: validate.MinItems("BlahArray", "body", iBlahArraySize, 1);, which would return "BlahArray in body should have at least 1 items" or something

Environment

swagger version: 2.0
go version: 1.15
OS: macOS

Thanks for maintaining this library, it is a joy to use

Metadata

Metadata

Assignees

No one assigned

    Labels

    modelRelated to swagger generate model commandpending PRvalidatorRelated to codegen generation of validations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions