Skip to content

Add custom upload validator to allow_upload/3 options#4189

Merged
josevalim merged 2 commits intophoenixframework:mainfrom
arthurnovak:custom_upload_validator
Apr 17, 2026
Merged

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

Conversation

@arthurnovak
Copy link
Copy Markdown
Contributor

@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
Contributor 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.

@SteffenDE SteffenDE requested a review from josevalim April 17, 2026 12:33
@josevalim josevalim merged commit 3179652 into phoenixframework:main Apr 17, 2026
6 of 8 checks passed
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

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.

5 participants