From 1efa45a8e423f0e869b0608cff69bff8460cf886 Mon Sep 17 00:00:00 2001 From: EfeDurmaz16 Date: Fri, 15 May 2026 13:57:24 +0300 Subject: [PATCH] fix(schema): make visible optional for validation --- packages/core/src/schema.test.ts | 17 ++++++++++++++++- packages/core/src/schema.ts | 10 +++++++++- packages/image/src/schema.ts | 2 +- packages/ink/src/schema.ts | 2 +- packages/next/src/schema.ts | 4 ++-- packages/react-email/src/schema.ts | 2 +- packages/react-native/src/schema.ts | 2 +- packages/react-pdf/src/schema.ts | 2 +- packages/react/src/schema.ts | 2 +- packages/solid/src/schema.ts | 2 +- packages/svelte/src/schema.ts | 2 +- packages/vue/src/schema.ts | 2 +- 12 files changed, 36 insertions(+), 13 deletions(-) diff --git a/packages/core/src/schema.test.ts b/packages/core/src/schema.test.ts index 94a57baf..e7bb2587 100644 --- a/packages/core/src/schema.test.ts +++ b/packages/core/src/schema.test.ts @@ -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() }, }), ), }), @@ -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); diff --git a/packages/core/src/schema.ts b/packages/core/src/schema.ts index 2dbb4d62..6397418f 100644 --- a/packages/core/src/schema.ts +++ b/packages/core/src/schema.ts @@ -327,8 +327,16 @@ export type InferSpec = TDef extends { ? InferSpecObject : unknown; +type OptionalSpecKeys = { + [K in keyof Shape]: Shape[K] extends { optional: true } ? K : never; +}[keyof Shape]; + +type RequiredSpecKeys = Exclude>; + type InferSpecObject = { - [K in keyof Shape]: InferSpecField; + [K in RequiredSpecKeys]: InferSpecField; +} & { + [K in OptionalSpecKeys]?: InferSpecField; }; type InferSpecField = diff --git a/packages/image/src/schema.ts b/packages/image/src/schema.ts index 3806bc5e..70c9b16d 100644 --- a/packages/image/src/schema.ts +++ b/packages/image/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/ink/src/schema.ts b/packages/ink/src/schema.ts index 7fe80189..90da41de 100644 --- a/packages/ink/src/schema.ts +++ b/packages/ink/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/next/src/schema.ts b/packages/next/src/schema.ts index c05492ff..dc33a566 100644 --- a/packages/next/src/schema.ts +++ b/packages/next/src/schema.ts @@ -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(), @@ -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(), diff --git a/packages/react-email/src/schema.ts b/packages/react-email/src/schema.ts index 38a38725..a874ca36 100644 --- a/packages/react-email/src/schema.ts +++ b/packages/react-email/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/react-native/src/schema.ts b/packages/react-native/src/schema.ts index 702d2f5a..1e3ade3d 100644 --- a/packages/react-native/src/schema.ts +++ b/packages/react-native/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/react-pdf/src/schema.ts b/packages/react-pdf/src/schema.ts index 0fe5bd8b..c3ecac13 100644 --- a/packages/react-pdf/src/schema.ts +++ b/packages/react-pdf/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/react/src/schema.ts b/packages/react/src/schema.ts index 48664e99..5dbc2773 100644 --- a/packages/react/src/schema.ts +++ b/packages/react/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/solid/src/schema.ts b/packages/solid/src/schema.ts index a261fb32..faf3ada6 100644 --- a/packages/solid/src/schema.ts +++ b/packages/solid/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/svelte/src/schema.ts b/packages/svelte/src/schema.ts index fd73e18a..554db25e 100644 --- a/packages/svelte/src/schema.ts +++ b/packages/svelte/src/schema.ts @@ -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() }, }), ), }), diff --git a/packages/vue/src/schema.ts b/packages/vue/src/schema.ts index 1e0586be..b0c7531b 100644 --- a/packages/vue/src/schema.ts +++ b/packages/vue/src/schema.ts @@ -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() }, }), ), }),