You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On the field level, the function passed to validation is running before the beforeChange hook. This is causing problems with data transformations, for example during encryption.
In the attached repo, I've included a simple, contrived example: a collection with a single field with a validation function to check for a minimum length, and a beforeChange hook that returns a string under that minimum length. This causes the operation to fail, even though beforeChange received a valid input.
Collection config:
exportconstThings: CollectionConfig={slug: 'things',fields: [{name: 'name',type: 'text',required: true,validate: (data)=>{console.log('----------')console.log('validation hook',Date.now())console.log('data:',data)console.log('----------')if(data&&data.length<8){return'Name is too short'}returntrueasconst},hooks: {beforeChange: [({ value })=>{console.log('----------')console.log('beforeChange',Date.now())console.log('value:',value)return'short'// this should never make it back to `validate`},],},},],}
Logs:
POST /admin/collections/things/create 200 in 7ms
----------
beforeChange 1739480768752
value: Very very long string
----------
validation hook 1739480769754
data: short
----------
[15:06:09] INFO: The following field is invalid: Name
POST /api/things?depth=0&fallback-locale=null 400 in 1012ms
Create new Payload project - pnpm dlx create-payload-app@latest
Create a new collection with one text field
On this text field, create a validation function that logs a timestamp, and then validates the string (my example returns an error message if length < 8).
On the same field, create a beforeChange hook (that also logs a timestamp) and returns a string that would fail validation. (short fails length < 8, in my example)
Run app in dev mode
Navigate to the admin panel in a browser
Attempt to create a new document on this collection, while observing the logs
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
Binaries:
Node: 23.7.0
npm: 10.9.2
Yarn: N/A
pnpm: 10.3.0
Relevant Packages:
payload: 3.23.0
next: 15.1.5
@payloadcms/db-postgres: 3.23.0
@payloadcms/email-nodemailer: 3.23.0
@payloadcms/graphql: 3.23.0
@payloadcms/next/utilities: 3.23.0
@payloadcms/payload-cloud: 3.23.0
@payloadcms/richtext-lexical: 3.23.0
@payloadcms/translations: 3.23.0
@payloadcms/ui/shared: 3.23.0
react: 19.0.0
react-dom: 19.0.0
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:24 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6030
Available memory (MB): 18432
Available CPU cores: 12
The text was updated successfully, but these errors were encountered:
Describe the Bug
On the field level, the function passed to
validation
is running before thebeforeChange
hook. This is causing problems with data transformations, for example during encryption.In the attached repo, I've included a simple, contrived example: a collection with a single field with a validation function to check for a minimum length, and a beforeChange hook that returns a string under that minimum length. This causes the operation to fail, even though
beforeChange
received a valid input.Collection config:
Logs:
Admin UI when submitting:
Link to the code that reproduces this issue
https://github.com/ozzythecoder/validation-error-repro
Reproduction Steps
pnpm dlx create-payload-app@latest
length < 8
).beforeChange
hook (that also logs a timestamp) and returns a string that would fail validation. (short
failslength < 8
, in my example)Which area(s) are affected? (Select all that apply)
area: core
Environment Info
The text was updated successfully, but these errors were encountered: