Inconsistent fields in beforeUpdate

I’m doing a work-around for the year+ old RBAC bug that keeps reseting required text fields configured as “not updatable” (RBAC permissions resets on required fields when restarting Strapi · Issue #16890 · strapi/strapi · GitHub) in a lifecycle. Basically, I’m adding a check in beforeUpdate to ensure the content in the field that should not be editable hasn’t been changed. I tested multiple times on my local instance and didn’t run into any issues when I did various tests (only updating field that shouldn’t be updated, updating only other fields, updating combinations of the two, etc).

Of course, I’ve deployed to prod and my first edit of another field caused reported an error as if I had edited the field that shouldn’t be modified. I had to go at it several times before I could recreate it locally and it seems the issue is the field in question is sometimes not included in event.params.data so my check fails cause I end up comparing null against the value looked up via entityService. Why would this happen? I though all root-fields (not relations or components) always were included in event.params.data?

Unfortunately, I can’t recreate it 100% of the time as it’s completely random. I’ll work around it by adding a check if the field is there (easy but come on).

Expected params.data :

boutique: beforeUpdate
 event.params.data: {
   id: 1,
   description: 'Ampoulesfds',
   ident: '1',
   categories: { disconnect: [], connect: [] },
   updatedBy: 3,
   updatedAt: 2024-08-14T16:13:01.045Z
 }

Randomly (no ‘ident’ field):

boutique: beforeUpdate
 event.params.data: {
   id: 1,
   description: 'Ampoulesdd',
   categories: { disconnect: [], connect: [] },
   updatedBy: 3,
   updatedAt: 2024-08-14T16:11:31.637Z
 }

This topic has been created from a Discord post (1273328781884395662) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

You are likely hitting publish event

I’m updating a published entry

There are events that are not included the whole object

The publish/unpublished event is one of thouse, that I’m aware of

I’m basically checking if the even.params include field I’m after and only then run check

Wait no, sorry, the above example was in a collection with no draft & publish. Regardless, I’ve seen the issue in both cases, just by updating an unrelated field. In the above output, the description field is just something for internal use

The question is why the field wasn’t there at first. I updated beforeUpdate to do a console log and, because starlight restarted, it then suddenly showed the missing field next time I tried to save

There are some optimisations, it’s possible that if you PATCH you have partial object in event

You can check in network tab what you send when you update in content manager

But in any case publish unpublish would not include whole object

The test is quite simple e.g. undefined means that attribute was not pussed, null or anything else means it passed

But as I clarified, the above extract was from a collection without draft & publish

I will add the check. It just seems crazy that this is so arbitrary.