Gatsby-source-sanity plugin causing ValidationError on build
Failed to validate error Error [ValidationError]: child "error" fails because ["error" must be an object]If I remove the plugin code from gatsby-config. It builds fine. The plugin objectlooks like this (I removed the projectId from this message):
{ resolve: 'gatsby-source-sanity',options: {
projectId: 'my-id',
dataset: 'production',
// a token with read permissions is required// if you have a private dataset
token: <http://process.env.MY|process.env.MY>_SANITY_TOKEN,},
},
Any ideas what I'm missing?
I can see the issue! The problem is in your token configuration. You have:
token: <http://process.env.MY|process.env.MY>_SANITY_TOKEN,This syntax is incorrect. The token option expects a string value, but what you have there looks like it got mangled (possibly by a Slack auto-formatter turning it into a link). It should be:
token: process.env.MY_SANITY_TOKEN,Notice there are no angle brackets, no http://, and no pipe character - just the plain environment variable reference.
Your corrected plugin configuration should look like:
{
resolve: 'gatsby-source-sanity',
options: {
projectId: 'my-id',
dataset: 'production',
token: process.env.MY_SANITY_TOKEN,
},
}The validation error you're seeing ("child "error" fails because ["error" must be an object]") is Gatsby's way of saying the plugin options didn't validate correctly - in this case because the malformed token value caused the plugin initialization to fail.
A few additional tips for getting started with gatsby-source-sanity:
- If your dataset is public, you can actually omit the
tokenoption entirely - For draft content preview during development, add
watchMode: trueandoverlayDrafts: trueto your options (this requires a token with read permissions). The overlayDrafts feature lets you see draft content in development while keeping only published content in production. - Important: You need to deploy your GraphQL API from your Sanity Studio first by running
sanity graphql deployin your studio folder. The gatsby-source-sanity plugin relies on this deployed GraphQL schema to work properly.
Once you fix that token syntax, your build should work fine!
Show original thread3 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.