Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Validate method for server parameter structs #2309

Open
mwieser opened this issue Jun 10, 2020 · 2 comments
Open

No Validate method for server parameter structs #2309

mwieser opened this issue Jun 10, 2020 · 2 comments

Comments

@mwieser
Copy link

mwieser commented Jun 10, 2020

Problem statement

When generating models with swagger generate model or swagger generate server the resulting models from the swagger definitions all implement a validation method like Validate(formats strfmt.Registry) error. However, the generated models for parameters do not include this functionality.

Validations for the parameter are currently generated but only used while binding the param struct. In order to use the validations standalone, without having to bind them with the generated method (for example when binding with a different framework), I propose to add a Validate(formats strfmt.Registry) error method to

to be able to use the parameter validation separately.

Is there any interest in adding this functionality?

Environment

swagger version: 0.23.0
go version: 1.14.2
OS: Debian

@fredbi
Copy link
Contributor

fredbi commented Jun 27, 2020

The runtime.Validatable interface was primarily defined for genuine for swagger schemas.
That is true that the validation of swagger simple types is a pain in the generated code.
This is mostly because the unmarshaling of these parameters is more complex than just reading from a json body.
Refactoring the parameter.gotmpl to have some reusable part (could be used, for example, to validate params on the client before sending a request) is a good idea. Feel free to post a PR along these lines.

@fredbi fredbi added enhancement model Related to swagger generate model command generator and removed model Related to swagger generate model command labels Jun 27, 2020
@mwieser
Copy link
Author

mwieser commented Aug 7, 2020

I am currently working on a PR for this and I was wondering if the creation of a new array/map is necessary in the validation methods. At the moment when the validation takes place a new array/map is build and then reassigned to the property. Can this be omitted?

// validateSimpleArrayWithValidationBody validates an inline body parameter
func (o *GetSimpleArrayWithValidationParams) validateSimpleArrayWithValidationBody(formats strfmt.Registry) error {
	simpleArrayWithValidationIC := o.SimpleArrayWithValidation

	var simpleArrayWithValidationIR []int64
	for i, simpleArrayWithValidationIV := range simpleArrayWithValidationIC {
		simpleArrayWithValidationI := simpleArrayWithValidationIV

		if err := validate.MaximumInt(fmt.Sprintf("%s.%v", "simpleArrayWithValidation", i), "body", int64(simpleArrayWithValidationI), 12, false); err != nil {
			return err
		}

		simpleArrayWithValidationIR = append(simpleArrayWithValidationIR, simpleArrayWithValidationI)
	}

	o.SimpleArrayWithValidation = simpleArrayWithValidationIR
	return nil
}

I would suggest to shorten it down to something like this:

// validateSimpleArrayWithValidationBody validates an inline body parameter
func (o *GetSimpleArrayWithValidationParams) validateSimpleArrayWithValidationBody(formats strfmt.Registry) error {
	simpleArrayWithValidationIC := o.SimpleArrayWithValidation

	for i, simpleArrayWithValidationIV := range simpleArrayWithValidationIC {
		simpleArrayWithValidationI := simpleArrayWithValidationIV

		if err := validate.MaximumInt(fmt.Sprintf("%s.%v", "simpleArrayWithValidation", i), "body", int64(simpleArrayWithValidationI), 12, false); err != nil {
			return err
		}

	}

	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants