apigee-127 / sway

A library that simplifies OpenAPI (fka Swagger) integrations/tooling.
MIT License
190 stars 92 forks source link

Form data validation seems incorrect. #184

Open JaekwanLee opened 6 years ago

JaekwanLee commented 6 years ago

It seems weird to see file type of formData is always expected under req.files. I see multer(a popular library for multi-part form data) sometimes use req.file for a single file.

In lib/types/parameter.js, it always throws an error due to validation failure.

  switch (this.in) {
  case 'body':
    value = req.body;
    break;
  case 'formData':
    // For formData, either the value is a file or a property of req.body.  req.body as a whole can never be the
    // value since the JSON Schema for formData parameters does not allow a type of 'object'.
    if (type === 'file') {
      if (_.isUndefined(req.files)) {
        throw new Error('req.files must be provided for \'formData\' parameters of type \'file\'');
      }

      value = req.files[this.name];
    } else {
      if (_.isUndefined(req.body)) {
        throw new Error('req.body must be provided for \'formData\' parameters');
      }
      value = req.body[this.name];
    }
    break;

Should it be more generous in the case of formData?

whitlockjc commented 6 years ago

I don't disagree. I do think that sway aims to try to give a convention and it wouldn't take much to work around this but at the same time, I can make sway smarter to treat req.file as an array with req.file in it. I'll get working on it.

DanStory commented 5 years ago

With a formData parameter named for example as file, the parameter validation fails when it is required. This is do to req.files[this.name] returning undefined, as it is an array not an object.

Both req.body and req.fields use the parameter name, and may be better suited to get the parameter value.

whitlockjc commented 5 years ago

This will be sorted soon, sorry for the delay.