Custom Policy for Category Validation Not Triggering in Strapi 4

  • Strapi Version: 4
  • Operating System: Mac
  • Database: mysql
  • Node Version: 18.15.0
  • NPM Version: 9.5.0

I’m trying to validate the category data using a custom policy, but it seems the execution is not reaching the policy since I’m unable to see the log In validation policy. being printed.

Here’s the code I’m using:

// src/api/category/policies/validation.ts
export default (policyContext, config, { strapi }) => {
  strapi.log.info('In validation policy.');

  const canDoSomething = true;

  if (canDoSomething) {
    return true;
  }

  return false;
};

I’ve added the policy to the update route in the router configuration like this:

//src/api/category/routes/category.ts
import { factories } from "@strapi/strapi";

export default factories.createCoreRouter("api::category.category", {
  config: {
    update: { policies: ["validation"] },
  },
});

However, the policy doesn’t seem to be invoked, and I’m not seeing the log output. Any ideas on why this might be happening or how to ensure the policy gets executed?

You are not using the correct naming convention to call your policy. Here is an example of what it should look like. strapi-policy-examples/src/api/item/routes/item.js at 5319d930bbfb51681b5cb26fc0a22d432dd8cd01 · PaulBratslavsky/strapi-policy-examples · GitHub

As mentioned in the documentation, both validation and api::category.validation can be used in policies. I’ve tried both approaches but still do not see the log output. Any ideas on what could be going wrong?