Skip to content
Open
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
17 changes: 16 additions & 1 deletion packages/core/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const testSchema = defineSchema((s) => ({
type: s.ref("catalog.components"),
props: s.propsOf("catalog.components"),
children: s.array(s.string()),
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down Expand Up @@ -583,6 +583,21 @@ describe("catalog.validate", () => {
expect(result.data).toEqual(spec);
});

it("does not require optional visible fields", () => {
const result = catalog.validate({
root: "card-1",
elements: {
"card-1": {
type: "Card",
props: { title: "Hello" },
children: [],
},
},
});

expect(result.success).toBe(true);
});

it("rejects spec with wrong root type", () => {
const result = catalog.validate({ root: 123, elements: {} });
expect(result.success).toBe(false);
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,16 @@ export type InferSpec<TDef extends SchemaDefinition, TCatalog> = TDef extends {
? InferSpecObject<Shape, TCatalog>
: unknown;

type OptionalSpecKeys<Shape> = {
[K in keyof Shape]: Shape[K] extends { optional: true } ? K : never;
}[keyof Shape];

type RequiredSpecKeys<Shape> = Exclude<keyof Shape, OptionalSpecKeys<Shape>>;

type InferSpecObject<Shape, TCatalog> = {
[K in keyof Shape]: InferSpecField<Shape[K], TCatalog>;
[K in RequiredSpecKeys<Shape>]: InferSpecField<Shape[K], TCatalog>;
} & {
[K in OptionalSpecKeys<Shape>]?: InferSpecField<Shape[K], TCatalog>;
};

type InferSpecField<T, TCatalog> =
Expand Down
2 changes: 1 addition & 1 deletion packages/image/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const schema = defineSchema(
type: s.ref("catalog.components"),
props: s.propsOf("catalog.components"),
children: s.array(s.string()),
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/ink/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const schema = defineSchema(
/** Child element keys (flat reference) */
children: s.array(s.string()),
/** Visibility condition */
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export const schema = defineSchema(
type: s.ref("catalog.components"),
props: s.propsOf("catalog.components"),
children: s.array(s.string()),
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
state: s.any(),
Expand Down Expand Up @@ -264,7 +264,7 @@ export const schema = defineSchema(
type: s.ref("catalog.components"),
props: s.propsOf("catalog.components"),
children: s.array(s.string()),
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
state: s.any(),
Expand Down
2 changes: 1 addition & 1 deletion packages/react-email/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const schema = defineSchema(
type: s.ref("catalog.components"),
props: s.propsOf("catalog.components"),
children: s.array(s.string()),
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const schema = defineSchema(
/** Child element keys (flat reference) */
children: s.array(s.string()),
/** Visibility condition */
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/react-pdf/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const schema = defineSchema(
type: s.ref("catalog.components"),
props: s.propsOf("catalog.components"),
children: s.array(s.string()),
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const schema = defineSchema(
/** Child element keys (flat reference) */
children: s.array(s.string()),
/** Visibility condition */
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/solid/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const schema = defineSchema(
/** Child element keys (flat reference) */
children: s.array(s.string()),
/** Visibility condition */
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const schema = defineSchema(
/** Child element keys (flat reference) */
children: s.array(s.string()),
/** Visibility condition */
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const schema = defineSchema(
/** Child element keys (flat reference) */
children: s.array(s.string()),
/** Visibility condition */
visible: s.any(),
visible: { ...s.any(), ...s.optional() },
}),
),
}),
Expand Down