Accepting file array
A product may have more than a single image.
Now, validation is working and we also made it more elegant. However, we should allow a product to have many images of it on the store, and not just one. So, we should make some simple adjustments in the route handler and its decorators:
Pluralize the
FileInterceptortoFilesInterceptorPluralize the
@UploadedFile()decorator to@UploadedFiles()Change the type of
fileto an array of files
And, for correctness, let's also pluralize:
The route path, from
imagetoimagesThe extracted field and the variable, from
filetofilesThe method, from
uploadImage()touploadImages()
And lastly, also define a constant for the maximum amount of files, we'll do that in files/util/file.constants. The max amount of a product's images will be defined here.
export const MaxFileCount = {
PRODUCT_IMAGES: 5,
} as const satisfies Record<string, number>;Then, use this value in the FilesInterceptor as the second argument. Now, no more than 5 files may be sent at once.
Commit - Accepting file array in route handler
Last updated