@astrojs/starlight 0.41.4 → 0.41.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @astrojs/starlight
2
2
 
3
+ ## 0.41.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4095](https://github.com/withastro/starlight/pull/4095) [`bb06434`](https://github.com/withastro/starlight/commit/bb06434a1bc199b59f9d7953d0e41c6c287b1aa2) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes a regression when using a union to [extend](https://starlight.astro.build/reference/frontmatter/#extend) Starlight’s `docsSchema()`.
8
+
3
9
  ## 0.41.4
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/starlight",
3
- "version": "0.41.4",
3
+ "version": "0.41.5",
4
4
  "description": "Build beautiful, high-performance documentation websites with Astro",
5
5
  "keywords": [
6
6
  "docs",
package/schema.ts CHANGED
@@ -115,7 +115,8 @@ const StarlightFrontmatterSchema = (context: SchemaContext) =>
115
115
  type DefaultSchema = ReturnType<typeof StarlightFrontmatterSchema>;
116
116
 
117
117
  /** Base subset of Zod types that we support passing to the `extend` option. */
118
- type BaseSchema<T extends z.ZodRawShape = z.ZodRawShape> = z.ZodObject<T>;
118
+ type BaseObjectSchema = z.ZodObject<z.ZodRawShape, z.core.$ZodObjectConfig>;
119
+ type BaseSchema = BaseObjectSchema | z.ZodUnion<readonly BaseObjectSchema[]>;
119
120
 
120
121
  /** Type that extends Starlight’s default schema with an optional, user-defined schema. */
121
122
  type ExtendedSchema<T extends BaseSchema = never> = [T] extends [never]
@@ -10,17 +10,19 @@ export type DeepMergedSchema<Default extends z.core.$ZodType, User extends z.cor
10
10
  ? z.ZodDefault<DeepMergedSchema<UnwrappedSchema<Default>, WrappedSchema>>
11
11
  : User extends z.ZodPrefault<infer WrappedSchema extends z.core.$ZodType>
12
12
  ? z.ZodPrefault<DeepMergedSchema<UnwrappedSchema<Default>, WrappedSchema>>
13
- : UnwrappedSchema<Default> extends z.ZodObject<infer DefaultShape>
14
- ? User extends z.ZodObject<infer UserShape, infer UserConfig>
15
- ? z.ZodObject<DeepMergedShape<DefaultShape, UserShape>, UserConfig>
16
- : User
17
- : UnwrappedSchema<Default> extends z.ZodArray<
18
- infer DefaultItemSchema extends z.core.$ZodType
19
- >
20
- ? User extends z.ZodArray<infer UserItemSchema extends z.core.$ZodType>
21
- ? z.ZodArray<DeepMergedSchema<DefaultItemSchema, UserItemSchema>>
13
+ : User extends z.ZodUnion<infer Users extends readonly z.core.$ZodType[]>
14
+ ? z.ZodUnion<DeepMergedUnionMembers<UnwrappedSchema<Default>, Users>>
15
+ : UnwrappedSchema<Default> extends z.ZodObject<infer DefaultShape>
16
+ ? User extends z.ZodObject<infer UserShape, infer UserConfig>
17
+ ? z.ZodObject<DeepMergedShape<DefaultShape, UserShape>, UserConfig>
22
18
  : User
23
- : User;
19
+ : UnwrappedSchema<Default> extends z.ZodArray<
20
+ infer DefaultItemSchema extends z.core.$ZodType
21
+ >
22
+ ? User extends z.ZodArray<infer UserItemSchema extends z.core.$ZodType>
23
+ ? z.ZodArray<DeepMergedSchema<DefaultItemSchema, UserItemSchema>>
24
+ : User
25
+ : User;
24
26
 
25
27
  /** Type-level equivalent of {@link unwrapSchema}. */
26
28
  type UnwrappedSchema<T extends z.core.$ZodType> =
@@ -48,6 +50,16 @@ type DeepMergedShape<Default extends z.ZodRawShape, User extends z.ZodRawShape>
48
50
  : never;
49
51
  };
50
52
 
53
+ /** Merged union schemas where each union member is deep-merged with the default schema. */
54
+ type DeepMergedUnionMembers<
55
+ Default extends z.core.$ZodType,
56
+ Users extends readonly z.core.$ZodType[],
57
+ > = {
58
+ [Key in keyof Users]: Users[Key] extends z.core.$ZodType
59
+ ? DeepMergedSchema<Default, Users[Key]>
60
+ : Users[Key];
61
+ };
62
+
51
63
  export function deepMergeSchemas(defaultSchema: z.ZodType, userSchema: z.ZodType): z.ZodType {
52
64
  const unwrappedDefaultSchema = unwrapSchema(defaultSchema);
53
65
 
@@ -58,6 +70,13 @@ export function deepMergeSchemas(defaultSchema: z.ZodType, userSchema: z.ZodType
58
70
  } as Parameters<typeof userSchema.clone>[0]);
59
71
  }
60
72
 
73
+ if (isZodUnion(userSchema)) {
74
+ return userSchema.clone({
75
+ ...userSchema._zod.def,
76
+ options: userSchema.options.map((schema) => deepMergeSchemas(unwrappedDefaultSchema, schema)),
77
+ });
78
+ }
79
+
61
80
  if (isZodObject(unwrappedDefaultSchema) && isZodObject(userSchema)) {
62
81
  const shape: z.core.$ZodLooseShape = { ...unwrappedDefaultSchema.shape };
63
82
 
@@ -119,3 +138,7 @@ function isZodObject(schema: z.ZodType): schema is z.ZodObject<z.ZodRawShape> {
119
138
  function isZodArray(schema: z.ZodType): schema is z.ZodArray<z.ZodType> {
120
139
  return schema._zod.def.type === 'array';
121
140
  }
141
+
142
+ function isZodUnion(schema: z.ZodType): schema is z.ZodUnion<readonly z.ZodType[]> {
143
+ return schema._zod.def.type === 'union';
144
+ }