Skip to content

Add custom upload validator to allow_upload/3 options#4189

Open
arthurnovak wants to merge 2 commits intophoenixframework:mainfrom
arthurnovak:custom_upload_validator
Open

Add custom upload validator to allow_upload/3 options#4189
arthurnovak wants to merge 2 commits intophoenixframework:mainfrom
arthurnovak:custom_upload_validator

Conversation

@arthurnovak
Copy link
Copy Markdown

@arthurnovak arthurnovak commented Mar 30, 2026

Currently, allow_upload/3 supports validation only for entry size and accepted file types via the :max_file_size and :accept options. It would be useful to allow custom validation logic to run against each upload entry and return tailored errors when validation fails.

This change introduces support for custom upload validation by adding a :validator option to allow_upload/3.

The validator is a user-defined function that is executed for each upload entry and can return custom error reasons when validation does not pass.

:validator - An optional 1-arity function for performing custom validation on each upload entry. The function receives the upload entry and must return either :ok or {:error, reason}.

When an error tuple is returned, the entry is marked as failed and the error is exposed as reason via upload_errors/2.

@preciz @SteffenDE

@andrewtimberlake
Copy link
Copy Markdown
Contributor

This would be very useful. I’ve recently jumped through some hoops to validate that uploaded images are square cropped for example.

@arthurnovak
Copy link
Copy Markdown
Author

hey @preciz @SteffenDE, any chance you can have a look at it?

@preciz
Copy link
Copy Markdown
Contributor

preciz commented Apr 9, 2026

I can but I'm not a maintainer, so let's wait for them as usual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants