File Uploader validators
Custom validators allow you to add the necessary checks for uploaded files and
collections. These validators are pure functions, meaning they have no side
effects and do not depend on external state. In the current version, only
synchronous validators are implemented. Validators are invoked at various stages
during file handling: before, during, and after upload. If a validator returns
an error, that error will be available in the errors
field of all related
events.
There are two ways to use custom validators:
- File validator.
- Collection validator.
File validator
Validators are needed to check the suitability of a file.
You can create your own validator to check that the file is uploaded
with a specified parameter: size, type, content, resolution, etc.
For example, use a function that checks for the file type imagesOnly
:
Where arguments:
outputEntry
- see OutputFileEntry for more details.api
- context that provides access to the necessary fields and methods, such as attribute values from theuc-config
element and thel10n
method for localizing messages.
⚠️ Note: Access to the fileInfo
object is only possible after a successful
file upload. Therefore, you need to check the upload status and ensure it is
success
before attempting to access fileInfo
.
To attach the validator:
Collection validator
Custom validator for checking collections:
Where arguments:
collection
- Object describing the file collection, see OutputCollectionState for more details.api
- Uploader API instance that provides access to the localization and configuration options needed for validation, see Uploader API for more details.
To attach the validator:
Errors
Each error should be returned in the following format:
Where:
message
(required) — the error message that will be displayed to the user.payload
(optional) — additional data that may be useful for error handling.
You can capture any error through event subscriptions. Each custom validator has
an error of the type CUSTOM_ERROR
.
l10n
for custom validation
You need to extend your locale definitions to implement error handling with
localization l10n
. This allows you to provide localized error messages. Below
is an example of adding a custom error message for a file type validation:
- Import Locale Files:
First, import your existing locale files. These files contain the default localized strings for your application.
- Extend Locales:
UseUC.defineLocale
to extend the existing locales by adding a new key for the error message. This ensures that your application can handle the new error message in different languages.
- Define a Custom Validator:
Create a custom validator function that uses thel10n
method from the Uploader APIapi
to retrieve the localized error message. This ensures that the message is appropriately localized based on the current language setting.
You can efficiently create and connect your own validators to validate uploaded files and collections and handle any errors that arise, providing a high level of user experience and flexibility when validating data.