Throwing Validation Error from Custom Hook

Dear community,

I’m currently trying to throw a validation error from a custom hook in Directus, and I came across this GitHub thread that provides an example of how it was done in previous versions:

module.exports = function registerHooks({ filter }, { exceptions }) {
	filter('test.items.create', async (input) => {
		if (!input.name) {
			throw new exceptions.FailedValidationException({
				message: 'Missing name',
				path: ['name'],
				type: 'custom.name.invalid',
				context: {
					invalids: [''],
				},
			});

However, since Directus v10 changed how errors are handled in extensions, this approach no longer seems to work.

Does anyone know if it’s still possible to throw a validation error using the current @directus/errors package? If so, could you share an example or point me in the right direction?

Thanks in advance!
Deborah

Hello, here is an example.

import { defineHook } from '@directus/extensions-sdk';
import { InvalidPayloadError } from '@directus/errors';

export default defineHook(({ filter }) => {
  filter('items.create', (input, { collection }) => {
    if (collection === 'my_collection') {
      if (!input.some_important_field) {
        throw new InvalidPayloadError('The field "some_important_field" is required.');
      }
    }

    return input;
  });
});