DEV Community

Pocket PDF
Pocket PDF

Posted on AI-assisted

File Validation in JavaScript: Why File Extensions Aren't Enough

A filename is a surprisingly unreliable source of information.

Someone can rename photo.jpg to document.pdf without changing a single byte inside the file.

The extension changes. The contents don't.

This creates an interesting problem when building browser-based file converters. The interface needs to accept the right files, reject unsupported inputs and explain failures without getting in the user's way.

  1. Start with the File API

The browser's File API provides useful information about selected files.

const file = input.files[0];

if (!file) {
throw new Error("Please select a file.");
}

console.log(file.name);
console.log(file.size);
console.log(file.type);

These properties are useful for initial checks.

You can check whether a file exists, whether it is empty and whether its reported MIME type matches the expected format.

However, file.type is not guaranteed to be reliable.

It may be empty, incorrect or based on information supplied by the operating system.

Treat it as a useful hint, not proof.

  1. Check the actual file contents

For formats with recognizable signatures, inspecting the initial bytes can provide another validation layer.

For example, a standard PDF normally begins with a PDF header.

async function hasPdfHeader(file) {
const buffer = await file.slice(0, 5).arrayBuffer();

const header = new TextDecoder().decode(buffer);

return header === "%PDF-";
}

This is a simple preliminary check.

It doesn't establish that the entire document is valid. A file could contain the expected header and still be corrupted later.

Likewise, some valid files have structures that require more sophisticated parsing.

The important distinction is between identifying a likely file format and validating a document that a particular library can actually process.

  1. Let the parser do its job

For formats such as PDF, parsing is a more meaningful validation step.

A library can discover whether the document structure is readable and whether the requested operation is supported.

But even successful parsing doesn't guarantee that every feature will survive conversion.

Consider a few examples:

A PDF might contain encrypted objects.

A document might reference unusual fonts.

An image might use an unsupported color mode.

A video might use a codec that the browser cannot decode.

Validation should therefore happen in stages.

First check basic input requirements. Then inspect the format where practical. Finally, handle errors from the actual processing library.

Don't assume that passing the first check means the operation will succeed.

  1. Return useful errors

A generic message such as "Something went wrong" isn't particularly helpful.

Try distinguishing between common failure categories:

function getFileError(error) {
if (error?.name === "AbortError") {
return "Processing was cancelled.";
}

if (error?.name === "NotReadableError") {
return "The selected file could not be read.";
}

return "The file could not be processed.";
}

This is only a starting point. Real applications should map known library errors to accurate explanations rather than guessing from error messages.

Also avoid showing raw stack traces to ordinary users.

They are useful during development, not especially useful beside a download button.

The important distinction

File validation isn't one check.

It's a sequence of increasingly meaningful checks, each answering a different question.

Does a file exist? Does its reported type look reasonable? Do its contents resemble the expected format? Can the processing library parse it? Can the requested operation complete?

I've been applying this approach while building Pocket PDF, where the tools handle PDFs, images, audio, video and text directly in the browser.

You can find the project at https://www.pocketpdf.site.

A file extension is a good place to start. It just shouldn't be the final judge.

How much validation do you normally perform before passing a file to a processing library?

Top comments (1)

Collapse
 
pocketpdf profile image
Pocket PDF •

I would add MIME-type validation to the discussion, but I wouldn't trust it as the only check. Browser-reported file types can be empty or misleading, so inspecting the actual contents and letting the parser validate the structure is a better approach. That's something I've been working through at pocketpdf.site.