Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse <StarlightPage /> frontmatter asynchronously #2279

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-suns-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes an issue with frontmatter schemas containing collection references used with the `<StarlightPage />` component and an Astro version greater than `4.14.0`.
28 changes: 24 additions & 4 deletions packages/starlight/utils/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,31 @@ export function parseWithFriendlyErrors<T extends z.Schema>(
input: z.input<T>,
message: string
): z.output<T> {
const parsedConfig = schema.safeParse(input, { errorMap });
if (!parsedConfig.success) {
throw new AstroError(message, parsedConfig.error.issues.map((i) => i.message).join('\n'));
return processParsedData(schema.safeParse(input, { errorMap }), message);
}

/**
* Asynchronously parse data with a Zod schema that contains asynchronous refinements or transforms
* and throw a nicely formatted error if it is invalid.
*
* @param schema The Zod schema to use to parse the input.
* @param input Input data that should match the schema.
* @param message Error message preamble to use if the input fails to parse.
* @returns Validated data parsed by Zod.
*/
export async function parseAsyncWithFriendlyErrors<T extends z.Schema>(
schema: T,
input: z.input<T>,
message: string
): Promise<z.output<T>> {
return processParsedData(await schema.safeParseAsync(input, { errorMap }), message);
}

function processParsedData(parsedData: z.SafeParseReturnType<any, any>, message: string) {
if (!parsedData.success) {
throw new AstroError(message, parsedData.error.issues.map((i) => i.message).join('\n'));
}
return parsedConfig.data;
return parsedData.data;
}

const errorMap: z.ZodErrorMap = (baseError, ctx) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/starlight/utils/starlight-page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'astro/zod';
import { type ContentConfig, type SchemaContext } from 'astro:content';
import config from 'virtual:starlight/user-config';
import { parseWithFriendlyErrors } from './error-map';
import { parseWithFriendlyErrors, parseAsyncWithFriendlyErrors } from './error-map';
import { stripLeadingAndTrailingSlashes } from './path';
import {
getSiteTitle,
Expand Down Expand Up @@ -198,7 +198,9 @@ async function getStarlightPageFrontmatter(frontmatter: StarlightPageFrontmatter
}),
});

return parseWithFriendlyErrors(
// Starting with Astro 4.14.0, a frontmatter schema that contains collection references will
// contain an async transform.
return parseAsyncWithFriendlyErrors(
schema,
frontmatter,
'Invalid frontmatter props passed to the `<StarlightPage/>` component.'
Expand Down
Loading