question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validating file input

See original GitHub issue

Coming from Yup. I’m trying to validate a required file input with no success:

file: z.any().refine(val => val.length > 0, "File is required")

Any tips?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

42reactions
zanzlendercommented, Jul 21, 2022

Putting this here if anyone still has this problem. The solution that worked for me was this one:

const MAX_FILE_SIZE = 500000;
const ACCEPTED_IMAGE_TYPES = ["image/jpeg", "image/jpg", "image/png", "image/webp"];

const RegistrationSchema = z.object({
  profileImage: z
    .any()
    .refine((files) => files?.length == 1, "Image is required.")
    .refine((files) => files?.[0]?.size <= MAX_FILE_SIZE, `Max file size is 5MB.`)
    .refine(
      (files) => ACCEPTED_IMAGE_TYPES.includes(files?.[0]?.type),
      ".jpg, .jpeg, .png and .webp files are accepted."
    ),
});

No image provided image

Too big image

Wrong type image

All good ✔ image

Then finally, in the data object we get image

11reactions
niklasgrewecommented, May 3, 2021

@colinhacks thanks for your answer. I am also looking for a solution. Can i also check if the File MIME-Type is for e.g. an image/*. How can i do that with zod?

and when i try to use:

file: z.instanceof(File)

i get the error:

ReferenceError: File is not defined
Read more comments on GitHub >

github_iconTop Results From Across the Web

File Type Validation while Uploading it using JavaScript
In this article, we will learn how to implement file type validation by checking file extension before uploading it using Javascript.
Read more >
How to validate a file upload field using Javascript/jquery
Check it's value property: In jQuery (since your tag mentions it): $('#fileInput').val(). Or in vanilla JavaScript: document.
Read more >
File Upload Validation Techniques - Triaxiom Security
We summarize some of the common file upload validation techniques that can and should be used to thwart many of the common file...
Read more >
How to validate file input on a File Upload Widget in a ...
Answer · Check if the end user did not select a file to upload : Add an If with the Condition set to...
Read more >
<input type="file"> - HTML: HyperText Markup Language | MDN
The accept attribute doesn't validate the types of the selected files; it provides hints for browsers to guide users towards selecting the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found