Model validations in a lifecycle hook like beforeUpdate

I think I got how to manually check for validations in v4 using this way:

const { createCoreService } = require('@strapi/strapi').factories;

module.exports = createCoreService('api::something.something', ({ strapi }) => ({
    async create(params) {
        try {
            const model = strapi.contentTypes[("api::something.something")];

            // const isDraft = isDraft(params.data, model); 
            // dont know how to check for draft, 
            // but it will set to default if 3rd parameter is 
            // not provided in below method call

            const validData = await strapi.entityValidator.validateEntityCreation(model, params.data, {});

            // if validation fails, it will throw an exception which you can handle below

            const result = await super.create(params);

        catch (e) {
            // do something with validation
            throw e;
        }

        return result;
    }
}));

1 Like