Skip to content

fix: incorrect value inside beforeValidate field hooks - #11433

Merged
JarrodMFlesch merged 2 commits into
mainfrom
fix/missing-before-validate-value
Feb 27, 2025
Merged

JarrodMFlesch merged 2 commits into
mainfrom
fix/missing-before-validate-value

Conversation

@JarrodMFlesch

@JarrodMFlesch JarrodMFlesch commented Feb 27, 2025 •

Copy link
Copy Markdown
Contributor

What?

value within the beforeValidate field hook was not correctly falling back to the document value when no value was passed inside the request for the field.

Why?

The fallback logic was running after the beforeValidate field hooks are called.

How?

Run the fallback logic before running the beforeValidate field hooks.

Fixes #10923

@JarrodMFlesch
JarrodMFlesch merged commit b3e7a9d into main Feb 27, 2025
@JarrodMFlesch
JarrodMFlesch deleted the fix/missing-before-validate-value branch February 27, 2025 21:00
@github-actions

github-actions Bot commented Mar 4, 2025

Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.26.0

@SimonVreman

SimonVreman commented Mar 5, 2025 •

Copy link
Copy Markdown
Contributor

@JarrodMFlesch This appears to break required fields with access false, doesn't it? At least, in my app, example:

  • Tenant field, default value is the user tenant with access false for both update and create
  • Value used to be deleted by field access control (was between beforeValidate and the fallback logic)
  • Fallback logic then filled the field correctly (default on create, previous on update)

Now, the fallback happens before access control and the fallback value is thus deleted.

I don't think this PR should be rolled back, the new situation makes more sense to me, but maybe this was an unintended consequence.

Edit: worked around be removing field access control, and overwriting user value in beforeValidate

@SimonVreman

Copy link
Copy Markdown
Contributor

@JarrodMFlesch Actually, this is fundamentally broken now, for any field that the user is not allowed to update. Example:

{
  name: 'group',
  type: 'relationship',
  relationTo: 'groups',
  required: true,
  access: {
    update: () => false,
  }
}

If I previously updated another field, it would delete the group value if present in the update data, and then fallback to the old value. But now, access is executed after fallback, so the fallback is deleted and it will instead attempt to set the field to null. In this case, that fails, as the field is required. Still unexpected. But if not required, it will just wipe the field, without the user having touched it!

I'll open the issue

JarrodMFlesch added a commit that referenced this pull request Mar 5, 2025
### What?
Regression caused by #11433 

If a beforeChange hook was checking for a missing or undefined `value`
in order to change the value before inserting into the database, data
could be lost.

### Why?
In #11433 the logic for setting the fallback field value was moved above
the logic that cleared the value when access control returned false.

### How?
This change ensures that the fallback value is passed into the
beforeValidate function _and_ still available with the fallback value on
siblingData if access control returns false.

Fixes #11543
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data Loss] Value not set in bulk actions in the beforeValidate hook

2 participants