Any way to get access to the local API within validation functions?

default discord avatar
2 years ago
1 2

Pretty much the title. I'd like to check the file extension of an uploaded media. The upload isn't directly on the collection but rather stored in a central library of image assets and the value that gets passed into validate() is a string.

However, I've tried a few different ways of importing the payload local API and I can't seem to get an instance of it. As per the docs I'm importing it as late as possible, not sure why it's not returning anything

      validate: async (value: string): Promise<string | boolean> => {
        const payload = (await import('payload')).default

          collection: 'media-image',
          id: value

        // :( -- payload.findByID is not a function

        console.log(value, payload)
        return true
  • discord user avatar
    Payload Team
    2 years ago

    Hey Richard,

    So here's the complicated part about this: validation functions are run in both the Admin panel and the server, by design. That means there's no such thing as the Local API within a validator function. That said, could you use fetch to access your REST or GraphQL API?

    Payload uses isomorphic-fetch in the background so a simple fetch call will be 100% supported in both browser and server.

    Looking forward to hearing your thoughts!

    3 replies
  • discord user avatar
    Payload Team
    2 years ago

    Ah - as a followup, and I just thought about this now, is that through a future feature we could potentially add an additional parameter to validate functions that denotes whether it's being run on the server or browser, and if on the server, we could pass the req that includes Payload and its local APIs.

    Would this be worthwhile to you? It breaks away from the universal aspect of validation functions but hey—could be valuable.

  • discord user avatar
    Payload Team
    2 years ago

    Alternative to passing server or browser in a param, would it be better to add a serverValidate function to the collection definition that can be used in addition to validate?
    Then server side, Payload can call validate and serverValidate when they are both defined.

  • default discord avatar
    2 years ago

    I think @DanRibbens is correct that would be the preferable solution. I think it's more explicit.

    Another alternative might be to somehow add a depth parameter to fetch the related data for you? Not sure if that could work on the admin though.

    I'll use fetch for now!

  • default discord avatar
    9 months ago

    validation functions are run in both the Admin panel and the server, by design

    ...which means that you can run local API queries - you just have to take care that it's NOT happening in the browser:

    validate: async (value, {data, id}) => {
       if (payload.findByID) {
       // server validation
          const doc = await payload.findByID({
             collection: collectionSlug,
             id: id
          return doc.title // example
       } else {
       // browser validation
Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!


Connect with the Payload Community on Discord



Can't find what you're looking for?

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