Skip to content

Commit

Permalink
fix: added validation for empty attachments (#7318)
Browse files Browse the repository at this point in the history
* fix: added validation for empty attachments

* fix: added storybookbasedir
  • Loading branch information
sebastianwzq committed May 8, 2024
1 parent 6e11d0a commit aa051b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
autoAcceptChanges: develop
exitOnceUploaded: true
onlyChanged: true
storybookBaseDir: frontend/.storybook
# Skip running Chromatic on dependabot PRs
skip: dependabot/**
# Only run when the frontend directory has changes
Expand Down
20 changes: 12 additions & 8 deletions frontend/src/components/Field/Attachment/Attachment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ export const Attachment = forwardRef<AttachmentProps, 'div'>(

const fileValidator = useCallback<NonNullable<DropzoneProps['validator']>>(
(file) => {
if (
!IMAGE_UPLOAD_TYPES_TO_COMPRESS.includes(file.type) &&
maxSize &&
file.size > maxSize
) {
return {
code: 'file-too-large',
message: `You have exceeded the limit, please upload a file below ${readableMaxSize}`,
if (!IMAGE_UPLOAD_TYPES_TO_COMPRESS.includes(file.type)) {
if (maxSize && file.size > maxSize) {
return {
code: 'file-too-large',
message: `You have exceeded the limit, please upload a file below ${readableMaxSize}`,
}
}
if (file.size === 0) {
return {
code: 'file-empty',
message: `You have uploaded an empty file, please upload a valid attachment`,
}
}
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const makeAttachmentSizeValidator: AttachmentValidatorConstructor =
(attachmentField) => (response) => {
const { attachmentSize } = attachmentField
const byteSizeLimit = parseInt(attachmentSize) * MB

// Check if the attachment content is empty
if (response.content.byteLength === 0) {
return left(`AttachmentValidator:\t File is empty.`)
}

return response.content.byteLength <= byteSizeLimit
? right(response)
: left(`AttachmentValidator:\t File size more than limit`)
Expand Down

0 comments on commit aa051b9

Please sign in to comment.