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

default discord avatar
sibergenomelast year
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
    imcorfitzlast year

    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
    sibergenomelast year

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

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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