Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation hook runs after beforeChange hook, causing validation issues #11169

Open
ozzythecoder opened this issue Feb 13, 2025 · 0 comments
Open
Assignees

Comments

@ozzythecoder
Copy link

Describe the Bug

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:

export const Things: 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'
        }
        return true as const
      },
      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

Admin UI when submitting:

Image

Link to the code that reproduces this issue

https://github.com/ozzythecoder/validation-error-repro

Reproduction Steps

  1. Create new Payload project - pnpm dlx create-payload-app@latest
  2. Create a new collection with one text field
  3. 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).
  4. 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)
  5. Run app in dev mode
  6. Navigate to the admin panel in a browser
  7. 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
@ozzythecoder ozzythecoder added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Feb 13, 2025
@AlessioGr AlessioGr self-assigned this Feb 13, 2025
@github-actions github-actions bot removed status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants