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:

  1. File validator.
  2. Collection validator.

File validator

You can create your own validator to check the file size. For example, use a function that checks for the file type imagesOnly:

const imagesOnly = (outputEntry, api) => {
  if (!outputEntry.isImage) {
    return {
      message: api.l10n('file-is-not-an-image-type')
    }
  }
}

Where arguments:

  • outputEntry - Object describing the file, see OutputFileEntry 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.

⚠️ 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:

const config = document.querySelector('lr-config')

config.fileValidators = [imagesOnly]

Collection validator

Custom validator for checking collections:

const hasAtLeastOneImage = (collection, api) => {
  const hasImage = collection.allEntries.some(file => file.isImage)
  if (!hasImage) {
    return {
      message: 'You need to select at least one image'
    }
  }
}

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:

const config = document.querySelector('lr-config')

config.collectionValidators = [hasAtLeastOneImage]

Errors

Each error should be returned in the following format:

type CustomValidatorError = {
  message: string
  payload?: Record<string, unknown>
}

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.

const ctx = document.querySelector('lr-upload-ctx-provider')

ctx.addEventListener('change', event => {
  const errors = event.detail.errors
  // Here you can see all errors from the validator
})

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:

  1. Import Locale Files:
    First, import your existing locale files. These files contain the default localized strings for your application.
import { default as en } from './locales/file-uploader/en.js'
import { default as fr } from './locales/file-uploader/fr.js'
  1. Extend Locales:
    Use LR.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.
LR.defineLocale('en', {
  ...en,
  ...{
    'file-is-not-an-image-type': 'File is not an image type'
  }
})
LR.defineLocale('fr', {
  ...fr,
  ...{
    'file-is-not-an-image-type': "Le fichier n'est pas une image"
  }
})

LR.registerBlocks(LR)
  1. Define a Custom Validator:
    Create a custom validator function that uses the l10n method from the Uploader API api to retrieve the localized error message. This ensures that the message is appropriately localized based on the current language setting.
const someFuncValidator = (outputEntry, api) => {
  return {
    message: api.l10n('file-is-not-an-image-type'),
    payload: { entry: outputEntry }
  }
}

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.