Skip to content

Commit

Permalink
Error for sidebar entries that match multiple entry schemas (#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Jul 5, 2024
1 parent fe3e4a1 commit 2bab648
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .changeset/great-plums-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@astrojs/starlight': minor
---

Makes sidebar entry parsing stricter in Starlight config

**鈿狅笍 Potentially breaking change:** Previously Starlight would accept a sidebar entry that matched one of its expected shapes, even if it included additional properties. For example, including both `link` and `items` was considered valid, with `items` being ignored. Now, it is an error to include more than one of `link`, `items`, or `autogenerate` in a sidebar entry.

If you see errors after updating, look for sidebar entries in the Starlight configuration in `astro.config.mjs` that include too many keys and remove the one that was previously ignored.
54 changes: 52 additions & 2 deletions packages/starlight/__tests__/basics/config-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ test('errors with bad sidebar config', () => {
Invalid config passed to starlight integration
Hint:
**sidebar.0**: Did not match union.
> Expected type \`{ link: string } | { items: array } | { autogenerate: object }\`
> Expected type \`{ link: string; } | { items: array; } | { autogenerate: object; }\`
> Received \`{ "label": "Example", "href": "/" }\`"
`
);
Expand All @@ -190,7 +190,57 @@ test('errors with bad nested sidebar config', () => {
Invalid config passed to starlight integration
Hint:
**sidebar.0.items.1**: Did not match union.
> Expected type \`{ link: string } | { items: array } | { autogenerate: object }\`
> Expected type \`{ link: string } | { items: array; } | { autogenerate: object; }\`
> Received \`{ "label": "Example", "items": [ { "label": "Nested Example 1", "link": "/" }, { "label": "Nested Example 2", "link": true } ] }\`"
`);
});

test('errors with sidebar entry that includes `link` and `items`', () => {
expect(() =>
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [
{ label: 'Parent', link: '/parent', items: [{ label: 'Child', link: '/parent/child' }] },
],
})
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
Invalid config passed to starlight integration
Hint:
**sidebar.0**: Unrecognized key(s) in object: 'items'"
`);
});

test('errors with sidebar entry that includes `link` and `autogenerate`', () => {
expect(() =>
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [{ label: 'Parent', link: '/parent', autogenerate: { directory: 'test' } }],
})
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
Invalid config passed to starlight integration
Hint:
**sidebar.0**: Unrecognized key(s) in object: 'autogenerate'"
`);
});

test('errors with sidebar entry that includes `items` and `autogenerate`', () => {
expect(() =>
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [
{
label: 'Parent',
items: [{ label: 'Child', link: '/parent/child' }],
autogenerate: { directory: 'test' },
},
],
})
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
Invalid config passed to starlight integration
Hint:
**sidebar.0**: Unrecognized key(s) in object: 'autogenerate'"
`);
});
6 changes: 3 additions & 3 deletions packages/starlight/schemas/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const SidebarLinkItemSchema = SidebarBaseSchema.extend({
link: z.string(),
/** HTML attributes to add to the link item. */
attrs: SidebarLinkItemHTMLAttributesSchema(),
});
}).strict();
export type SidebarLinkItem = z.infer<typeof SidebarLinkItemSchema>;

const AutoSidebarGroupSchema = SidebarGroupSchema.extend({
Expand All @@ -50,7 +50,7 @@ const AutoSidebarGroupSchema = SidebarGroupSchema.extend({
/** How many directories deep to include from this directory in the sidebar. Default: `Infinity`. */
// depth: z.number().optional(),
}),
});
}).strict();
export type AutoSidebarGroup = z.infer<typeof AutoSidebarGroupSchema>;

type ManualSidebarGroupInput = z.input<typeof SidebarGroupSchema> & {
Expand Down Expand Up @@ -80,7 +80,7 @@ const ManualSidebarGroupSchema: z.ZodType<
items: z.lazy(() =>
z.union([SidebarLinkItemSchema, ManualSidebarGroupSchema, AutoSidebarGroupSchema]).array()
),
});
}).strict();

export const SidebarItemSchema = z.union([
SidebarLinkItemSchema,
Expand Down

0 comments on commit 2bab648

Please sign in to comment.