Skip to content

Commit

Permalink
Parse <StarlightPage /> frontmatter asynchronously (#2279)
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Sep 4, 2024
1 parent 5062d30 commit 62d59e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
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

0 comments on commit 62d59e2

Please sign in to comment.