Custom validators

To achieve some validation on fields you can use lifecycles.

module.exports = {
  lifecycles: {
    // Called before an entry is created
    beforeCreate(data) {
      // calling a custom services which handles the validation process
      // and returns a message if something is wrong and true if everything is ok
      let message = await strapi.services.zzz.validation(data);
      if (message != true){
          throw new Error(message);
       }
    },
  },
};

The problem here is that the image gets uploaded when you select it inside the media field. So when you create a new entry in your collection type the image already exists.

To achieve custom validation during media upload you should use extensions.
The file you need is located here: ./node_modules/strapi-plugin-upload/services/Upload.js. Copy its content to ./extensions/upload/services/Upload.js and add your custom logic inside it.

4 Likes