How to perform validation on an image's dimensions in an upload collection?

default discord avatar
sibergenome
4 months ago
4

I can't find any options in the documentation for uploads on how to add a validation error for an image's dimensions. Has anyone implemented this before? Any info in any demos, or hopefully some docs that I've missed?



Thanks!

  • default discord avatar
    imcorfitz
    4 months ago

    I believe you should use the hooks.beforeValidate to do this.


    Edit: I have now tested it 😄



     hooks: {
        beforeValidate: [
          (req): void => {
            const image = req.data;
            // Make sure uploaded image is big enough
            if (image && image.width < 640) {
              throw new Error('Image must be at least 640px wide');
            }
          },
        ],
      },


    Screenshot_2023-05-27_at_12.37.33.png
  • default discord avatar
    sibergenome
    4 months ago

    Thanks heaps! Works smoothly and perfectly. I love a simple solution.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.