Any way to get access to the local API within validation functions? #68
-
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 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
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
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 Payload uses Looking forward to hearing your thoughts! |
Beta Was this translation helpful? Give feedback.
-
...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
}
} |
Beta Was this translation helpful? Give feedback.
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 simplefetch
call will be 100% supported in both browser and server.Looking forward to hearing your thoughts!