Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Local API in validate

default discord avatar
jakob56112 years ago
3

Is it possible to use Local API in the field validate function?


If I manually import payload, it will be undefined.

  • discord user avatar
    dribbens
    2 years ago

    You should have access to

    payload

    in your validate function without having to import it.



    validate: async (value, { payload }) => {
      const getSomething = await payload.find({ /** whatever you need */});
    }


    Does this help?

  • default discord avatar
    jakob56112 years ago

    I have tried it this way, but the outcome is the same:


    "Cannot read properties of undefined (reading "find")"



    This is the code I tried just now:



    validate: async (value, {payload}) => { const getSomething = await payload.find({ collection: 'pages' }); console.log(getSomething); },
  • discord user avatar
    jarrod_not_jared
    2 years ago

    I don't think this is true, bc validate is used on the frontend so it would not have access to the payload class



    So no, you will not be able to use payload in the validate function.You could instead use a beforeValidate hook and access payload there and throw an error if you need.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.