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

Form data validation seems incorrect. #184

Open
JaekwanLee opened this issue Sep 24, 2018 · 3 comments
Open

Form data validation seems incorrect. #184

JaekwanLee opened this issue Sep 24, 2018 · 3 comments

Comments

@JaekwanLee
Copy link

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
Copy link
Member

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
Copy link

DanStory commented Jan 4, 2019

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
Copy link
Member

This will be sorted soon, sorry for the delay.

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

No branches or pull requests

3 participants