dmitriy-nz / nestjs-form-data

NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
https://www.npmjs.com/package/nestjs-form-data
MIT License
126 stars 24 forks source link

Support for optional file validation #50

Closed mondyfy closed 1 year ago

mondyfy commented 1 year ago

Current Behavior: When using the nestjs-form-data package, it seems that there is no built-in support for handling optional file validation. The library provides excellent support for handling file uploads, but it lacks a feature to easily specify that a file upload is optional and should not trigger validation errors if absent. image

Validations used: image

Expected Behavior: I would expect the nestjs-form-data library to have a mechanism to define certain file fields as optional, meaning that if a file is not provided during the upload, it should not trigger validation errors.

Steps to Reproduce:

  1. Attempt to create an endpoint using nestjs-form-data where a file upload is optional.
  2. Upload a request without the optional file field.
  3. Observe that validation errors are still triggered even though the file is optional.

Environment:

Additional Context: I believe that adding support for optional file validation would enhance the usability and flexibility of the nestjs-form-data, especially in scenarios where certain files are not required for every upload.

dmitriy-nz commented 1 year ago

@mondyfy
Hi! Thanks for your feedback! For mark field as optional use @IsOptional() decorator then class-validator will know that the field is optional. Here's an example

dmitriy-nz commented 1 year ago

Please close your issue if it is solved

mondyfy commented 1 year ago

Sorry I missed that part, the issue was with the null value passed in the key:

curl --request POST \
  --url http://localhost:3000/api/v1/candidate-story \
  --header 'Content-Type: multipart/form-data' \
  --form file=

As it has been resolved, closing as completed. Thank you @dmitriy-nz