Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

add zod schema for components

Roman Pominov9ba4cf2c0f80
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index ad6249ee29..f0daa9b802 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -62,8 +62,8 @@ export const findInsertLocation = (
const path = getInstancePathWithPositions(rootInstance, selectedInstanceId);
path.reverse();
- const parentIndex = path.findIndex(({ item }) =>
- components[item.component].canAcceptChild()
+ const parentIndex = path.findIndex(
+ ({ item }) => components[item.component].canAcceptChildren
);
// Just in case selected Instance is not in the tree for some reason.
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 9f4575e818..346966cfdc 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -108,8 +108,8 @@ export const useDragAndDrop = () => {
path.splice(0, dragItemIndex + 1);
}
- const data = path.find((instance) =>
- components[instance.component].canAcceptChild()
+ const data = path.find(
+ (instance) => components[instance.component].canAcceptChildren
);
if (data === undefined) {
diff --git a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
index 2084674370..007c8004d3 100644
--- a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
@@ -22,7 +22,7 @@ const tmp = {
return components[item.component].isInlineOnly !== true;
},
canAcceptChild(item: Instance) {
- return components[item.component].canAcceptChild();
+ return components[item.component].canAcceptChildren;
},
getItemChildren(item: Instance) {
return item.children.filter(
diff --git a/packages/jest-config/index.js b/packages/jest-config/index.js
index fe63f6d2ec..716a7294c9 100644
--- a/packages/jest-config/index.js
+++ b/packages/jest-config/index.js
@@ -2,7 +2,7 @@ module.exports = {
testEnvironment: "node",
testMatch: ["<rootDir>/src/**/*.test.ts"],
transform: {
- "^.+\\.ts?$": "esbuild-jest",
+ "^.+\\.tsx?$": "esbuild-jest",
},
moduleNameMapper: {
"^~/(.*)$": "<rootDir>/src/$1",
diff --git a/packages/react-sdk/src/components/body.ws.tsx b/packages/react-sdk/src/components/body.ws.tsx
index 934bb6932f..78df09c85b 100644
--- a/packages/react-sdk/src/components/body.ws.tsx
+++ b/packages/react-sdk/src/components/body.ws.tsx
@@ -50,15 +50,17 @@ const defaultStyle = {
type: "keyword",
value: "#232323",
},
-};
+} as const;
-export default {
+const meta: WsComponentMeta<typeof Body> = {
Icon: BodyIcon,
Component: Body,
- canAcceptChild: () => true,
+ canAcceptChildren: true,
isContentEditable: false,
label: "Body",
isInlineOnly: false,
isListed: false,
defaultStyle,
-} as WsComponentMeta<typeof Body>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/bold.ws.tsx b/packages/react-sdk/src/components/bold.ws.tsx
index cd0eacce68..3d3c70f984 100644
--- a/packages/react-sdk/src/components/bold.ws.tsx
+++ b/packages/react-sdk/src/components/bold.ws.tsx
@@ -2,12 +2,14 @@ import { FontBoldIcon } from "@webstudio-is/icons";
import type { WsComponentMeta } from "./component-type";
import { Bold } from "./bold";
-export default {
+const meta: WsComponentMeta<typeof Bold> = {
Icon: FontBoldIcon,
Component: Bold,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: false,
label: "Bold Text",
isInlineOnly: true,
isListed: false,
-} as WsComponentMeta<typeof Bold>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/box.ws.ts b/packages/react-sdk/src/components/box.ws.ts
index 5d1243b0ab..2a03ed1e2c 100644
--- a/packages/react-sdk/src/components/box.ws.ts
+++ b/packages/react-sdk/src/components/box.ws.ts
@@ -7,15 +7,17 @@ const defaultStyle = {
type: "keyword",
value: "border-box",
},
-};
+} as const;
-export default {
+const meta: WsComponentMeta<typeof Box> = {
Icon: SquareIcon,
Component: Box,
defaultStyle,
- canAcceptChild: () => true,
+ canAcceptChildren: true,
isContentEditable: false,
isInlineOnly: false,
isListed: true,
label: "Box",
-} as WsComponentMeta<typeof Box>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/button.ws.tsx b/packages/react-sdk/src/components/button.ws.tsx
index 39e4d59c91..4e0ad94c0d 100644
--- a/packages/react-sdk/src/components/button.ws.tsx
+++ b/packages/react-sdk/src/components/button.ws.tsx
@@ -2,13 +2,15 @@ import { ButtonIcon } from "@webstudio-is/icons";
import type { WsComponentMeta } from "./component-type";
import { Button } from "./button";
-export default {
+const meta: WsComponentMeta<typeof Button> = {
Icon: ButtonIcon,
Component: Button,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: true,
isInlineOnly: false,
isListed: true,
label: "Button",
children: ["Button text you can edit"],
-} as WsComponentMeta<typeof Button>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/component-type.ts b/packages/react-sdk/src/components/component-type.ts
index be9ff1bb6c..5180e2955e 100644
--- a/packages/react-sdk/src/components/component-type.ts
+++ b/packages/react-sdk/src/components/component-type.ts
@@ -1,10 +1,15 @@
import type { Style } from "../css";
+import { z } from "zod";
export type WsComponentMeta<ComponentType> = {
Component: ComponentType;
- Icon: ComponentType;
- defaultStyle: Style;
- canAcceptChild: () => boolean;
+
+ // @todo: can we do better than `any` here?
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ Icon: any;
+
+ defaultStyle?: Style;
+ canAcceptChildren: boolean;
// Should children of the component be editable?
// Should only be possible for components like paragraph, heading etc.
isContentEditable: boolean;
@@ -13,5 +18,42 @@ export type WsComponentMeta<ComponentType> = {
// Should be listed in the components list.
isListed: boolean;
label: string;
- children: Array<string>;
+ children?: Array<string>;
};
+
+export const WsComponentMetaSchema = z.lazy(() =>
+ z
+ .object({
+ Component: z.any(),
+ Icon: z.any(),
+ defaultStyle: z.optional(z.any()),
+ canAcceptChildren: z.boolean(),
+ isContentEditable: z.boolean(),
+ isInlineOnly: z.boolean(),
+ isListed: z.boolean(),
+ label: z.string(),
+ children: z.optional(z.array(z.string())),
+ })
+
+ // We need these restrictions because of limitation of current drag&drop implementation.
+ // We need drop target components to never have `string` in their children, only Instances.
+ // Otherwise placement index detection will be confused.
+ .refine(
+ (val) =>
+ val.canAcceptChildren === false || val.isContentEditable === false,
+ {
+ message:
+ "Content editable componetns are not allowed to accept children via drag&drop",
+ path: ["canAcceptChildren"],
+ }
+ )
+ .refine(
+ (val) => val.children === undefined || val.isContentEditable === true,
+ {
+ // @todo: consider adding the same check in the InstanceSchema
+ message:
+ "Non-content editable componetns are not allowed to have `string` children",
+ path: ["children"],
+ }
+ )
+) as z.ZodType<WsComponentMeta<unknown>>;
diff --git a/packages/react-sdk/src/components/form.ws.tsx b/packages/react-sdk/src/components/form.ws.tsx
index 313e97d0da..8dad7ef2a2 100644
--- a/packages/react-sdk/src/components/form.ws.tsx
+++ b/packages/react-sdk/src/components/form.ws.tsx
@@ -12,15 +12,17 @@ const defaultStyle = {
type: "keyword",
value: "border-box",
},
-};
+} as const;
-export default {
+const meta: WsComponentMeta<typeof Form> = {
Icon: FormIcon,
Component: Form,
defaultStyle,
- canAcceptChild: () => true,
+ canAcceptChildren: true,
isContentEditable: false,
isInlineOnly: false,
isListed: true,
label: "Form",
-} as WsComponentMeta<typeof Form>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/heading.ws.tsx b/packages/react-sdk/src/components/heading.ws.tsx
index e94f02110c..1eb5286357 100644
--- a/packages/react-sdk/src/components/heading.ws.tsx
+++ b/packages/react-sdk/src/components/heading.ws.tsx
@@ -2,13 +2,15 @@ import { HeadingIcon } from "@webstudio-is/icons";
import type { WsComponentMeta } from "./component-type";
import { Heading } from "./heading";
-export default {
+const meta: WsComponentMeta<typeof Heading> = {
Icon: HeadingIcon,
Component: Heading,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: true,
isInlineOnly: false,
isListed: true,
label: "Heading",
children: ["Heading you can edit"],
-} as WsComponentMeta<typeof Heading>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/index.test.ts b/packages/react-sdk/src/components/index.test.ts
new file mode 100644
index 0000000000..6c9024af84
--- /dev/null
+++ b/packages/react-sdk/src/components/index.test.ts
@@ -0,0 +1,9 @@
+import * as components from "./index";
+import { WsComponentMetaSchema } from "./component-type";
+
+test.each(Object.entries(components))("validating: %s", (name, component) => {
+ if (name === "default") {
+ return;
+ }
+ WsComponentMetaSchema.parse(component);
+});
diff --git a/packages/react-sdk/src/components/input.ws.tsx b/packages/react-sdk/src/components/input.ws.tsx
index 1d4351b983..cad322ca8f 100644
--- a/packages/react-sdk/src/components/input.ws.tsx
+++ b/packages/react-sdk/src/components/input.ws.tsx
@@ -2,12 +2,14 @@ import { InputIcon } from "@webstudio-is/icons";
import type { WsComponentMeta } from "./component-type";
import { Input } from "./input";
-export default {
+const meta: WsComponentMeta<typeof Input> = {
Icon: InputIcon,
Component: Input,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: false,
isInlineOnly: false,
isListed: true,
label: "Input",
-} as WsComponentMeta<typeof Input>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/italic.ws.tsx b/packages/react-sdk/src/components/italic.ws.tsx
index 78ec0b730b..a00ce7adba 100644
--- a/packages/react-sdk/src/components/italic.ws.tsx
+++ b/packages/react-sdk/src/components/italic.ws.tsx
@@ -2,12 +2,14 @@ import { FontItalicIcon } from "@webstudio-is/icons";
import type { WsComponentMeta } from "./component-type";
import { Italic } from "./italic";
-export default {
+const meta: WsComponentMeta<typeof Italic> = {
Icon: FontItalicIcon,
Component: Italic,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: false,
isInlineOnly: true,
label: "Italic Text",
isListed: false,
-} as WsComponentMeta<typeof Italic>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/link.ws.tsx b/packages/react-sdk/src/components/link.ws.tsx
index c925e053a0..caed8e571c 100644
--- a/packages/react-sdk/src/components/link.ws.tsx
+++ b/packages/react-sdk/src/components/link.ws.tsx
@@ -12,16 +12,18 @@ const defaultStyle = {
type: "keyword",
value: "inline-block",
},
-};
+} as const;
-export default {
+const meta: WsComponentMeta<typeof Link> = {
Icon: Link2Icon,
Component: Link,
defaultStyle,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: true,
isInlineOnly: false,
isListed: true,
label: "Link",
children: ["Link text you can edit"],
-} as WsComponentMeta<typeof Link>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/paragraph.ws.tsx b/packages/react-sdk/src/components/paragraph.ws.tsx
index feeb98aaf8..f950724a1b 100644
--- a/packages/react-sdk/src/components/paragraph.ws.tsx
+++ b/packages/react-sdk/src/components/paragraph.ws.tsx
@@ -2,13 +2,15 @@ import { TextAlignLeftIcon } from "@webstudio-is/icons";
import type { WsComponentMeta } from "./component-type";
import { Paragraph } from "./paragraph";
-export default {
+const meta: WsComponentMeta<typeof Paragraph> = {
Icon: TextAlignLeftIcon,
Component: Paragraph,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: true,
isInlineOnly: false,
isListed: true,
label: "Paragraph",
children: ["Pragraph you can edit"],
-} as WsComponentMeta<typeof Paragraph>;
+};
+
+export default meta;
diff --git a/packages/react-sdk/src/components/text-block.ws.tsx b/packages/react-sdk/src/components/text-block.ws.tsx
index a37beee322..1b620930c7 100644
--- a/packages/react-sdk/src/components/text-block.ws.tsx
+++ b/packages/react-sdk/src/components/text-block.ws.tsx
@@ -8,16 +8,18 @@ const defaultStyle = {
unit: "em",
value: 1,
},
-};
+} as const;
-export default {
+const meta: WsComponentMeta<typeof TextBlock> = {
Icon: TextIcon,
Component: TextBlock,
defaultStyle,
- canAcceptChild: () => false,
+ canAcceptChildren: false,
isContentEditable: true,
isInlineOnly: false,
isListed: true,
label: "Text Block",
children: ["Block of text you can edit"],
-} as WsComponentMeta<typeof TextBlock>;
+};
+
+export default meta;