fix: invalid option error when modifying enums in docsSchema's extend#3936
fix: invalid option error when modifying enums in docsSchema's extend#3936miichom wants to merge 15 commits into
Conversation
🦋 Changeset detectedLatest commit: d58283d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for astro-starlight ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Hello! Thank you for opening your first PR to Starlight! ✨ Here’s what will happen next:
|
There was a problem hiding this comment.
Thanks for the PR @miichom! And apologies for the slow response — I knew this would be a tricky one and needed time to play around with it to be sure the fix would work.
I’ve pushed up some tests for schema extension — previously we only touched that logic very minimally in an unrelated test. I included one for your case of extending a z.enum() property, which now passes on this branch (running that test against main fails, as you reported in #3935). However, the second part of this is type testing: this helps us ensure that the types inferred from an extended docs schema are correct.
Your changes seem to introduce an issue with the types when extending an object, e.g.
docsSchema({
extend: z.object({ sidebar: z.object({ custom: z.number() }) }),
})The default properties of the sidebar field are missing, with only the custom extension present. AFAICT this issue only impacts the types — you can see that in the failing test in CI. At runtime, all properties are parsed by Zod as expected.
Would you be able to take a look?
Just to make sure, is only the type check an issue? Is this snapshot correct here and the result with |
Oh damn, you’re 100% correct. I thought I remembered checking this and seeing that everything expected was there, but nope, they’re dropped. So, yes, the issue is present for both runtime values AND types. I updated that snapshot to reflect the runtime breakage too. |
|
Played around with a potential fix in #4040. I did that in a different PR to not mess around with the current PR where I cherry-picked all the commits from this PR in a single commit and added what I worked on top of it (also added 1 test and tweaked an existing one). This is a draft as I'm not yet sure if this is the right approach or there are better ones, and I did not yet review it properly as I was so head down in the code that I did not yet take a step back. It's also overly commented as I was sometime getting lost when implementing it and I did not clean anything up yet. If we ever decide to move forward with this, we should cherry pick only the relevant commits from #4040 in this PR so author credit is preserved and the history is cleaner. |
I personally think for now keeping it commented is a good idea, I will have a proper look when I get home. |
|
Okay, it took sometime but I preferred @HiDeoo schema merging over my one - I did remove the comments, but I am happy to re-add them if it helps others in the future. I added another test as the |
delucis
left a comment
There was a problem hiding this comment.
Thanks for your patience on this one @miichom!
I think we’re happy to accept this change with HiDeoo’s implementation. I wonder if it would be good to move most of this new code into a file under packages/starlight/utils/ to help keep the main schema.ts file focused on its primary purpose of declaring the schema. What do you think?
|
I think that is a great idea! |

Description
In the starlight docs, it mentions
docsSchema()can modify fields, however it is unable to modifyz.enumfields The likely cause is from howZodIntersectionare treated.Why this happens
The Zod documentation explicitly notes that intersections (a logical "AND") produce a
ZodIntersectioninstance rather than aZodObject.From the Zod v4 docs:
This limitation also affects any tooling that expects to operate on a
ZodObject, including schema‑amending utilities likedocsSchema().Closes **hero.actions.1.variant**: Invalid option: expected one of "primary"|"secondary"|"minimal" #3935
What does this PR change? Give us a brief description.
This PR replace the
andintersection indocsSchemawithextendto support the modifying ofZodEnum.Did you change something visual? A before/after screenshot can be helpful.
Nothing was changed.