sway icon indicating copy to clipboard operation
sway copied to clipboard

Form data validation seems incorrect.

Open JaekwanLee opened this issue 6 years ago • 3 comments

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?

JaekwanLee avatar Sep 24 '18 21:09 JaekwanLee

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.

whitlockjc avatar Sep 25 '18 07:09 whitlockjc

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.

DanStory avatar Jan 04 '19 16:01 DanStory

This will be sorted soon, sorry for the delay.

whitlockjc avatar Jan 04 '19 21:01 whitlockjc