Collection validators
What is a collection validator?
A collection validator is a rule that checks the whole selection of files at once. Use it when your logic depends on relationships between files rather than a single file.
- If the selection breaks your rule, return an error object with a message users can see.
- If everything is okay, return nothing and the selection is accepted.
Unlike file validators, collection validators are synchronous and there is no validation queue.
How it can be used
- Prevent duplicates across the selection (by name, size, hash, or another stable key you compute).
- Ensure uniform type across all files (for example, all entries share the same top-level MIME like
image/*
orapplication/*
). - Cross-file dependencies (for example, if a CSV is present, a README must also be present).
What users will see
When the selection doesn’t meet your rule, the uploader shows a clear message explaining what to fix.
Validation model and signature
Collection validators are synchronous functions:
See also: OutputCollectionState, UploaderPublicApi
Error type returned on failure:
Here, payload
is an optional object where you can include extra details about the error (for example, actual vs expected dimensions) and then catch them in your app via events or the API.
When it runs
Collection validators re-run on specific selection-related changes:
- Triggers:
- When files are added to or removed from the list
- When any file’s
errors
list changes (for example, a file becomes valid/invalid after file-level validation, or uploading fails)
- Not triggered: general file state changes (upload progress, post-upload edits, uploading success) unless they cause an
errors
change
Implications:
- Do not rely on properties that appear only after upload (for example,
fileInfo
,cdnUrl
,cdnUrlModifiers
). - It’s fine to base collection rules on whether files passed/failed file validators by inspecting
entry.errors
. - Prefer rules that can be decided from information available at selection time (filenames, sizes, MIME types, or flags already present on entries).
- If you need to validate post-upload information directly, use a file validator with an appropriate
runOn
and check the whole collection for custom errors there.
Examples
Specified mix of file types
No duplicates in the selection
Uniform top-level MIME type
CSV requires a README in the selection
Error localization
Show validation messages in the user’s language:
- Single-language projects: return plain strings from your validators.
- Multi-language projects: define localization keys, extend your locales, and reference those keys from validators.
Learn how to set up keys and extend locales: Localization
Best practices
- Keep rules fast and deterministic; collection validators are synchronous.
- Prefer clear, actionable messages so users know how to fix the selection.
- Combine with file validators for per-file checks (dimensions, metadata) and use collection validators for cross-file logic (duplicates, uniform types, cross-file dependencies).