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.

validate all JSON.parse/stringify with zod

Oleg Isonen1dd5b333edf0
diff --git a/apps/designer/app/routes/canvas/$projectId.tsx b/apps/designer/app/routes/canvas/$projectId.tsx
index 66354931fb..af902c6c55 100644
--- a/apps/designer/app/routes/canvas/$projectId.tsx
+++ b/apps/designer/app/routes/canvas/$projectId.tsx
@@ -20,7 +20,7 @@ export const loader: LoaderFunction = async ({ params }): Promise<Data> => {
} catch (error) {
if (error instanceof Error) {
return {
- errors: error.message,
+ errors: `Loading canvas data error: \n ${error.message}`,
env,
};
}
diff --git a/apps/designer/app/shared/db/breakpoints.server.ts b/apps/designer/app/shared/db/breakpoints.server.ts
index 21107c1807..f96d87f5cb 100644
--- a/apps/designer/app/shared/db/breakpoints.server.ts
+++ b/apps/designer/app/shared/db/breakpoints.server.ts
@@ -3,7 +3,8 @@ import {
type Project,
type Tree,
type Breakpoint,
- schema,
+ BreakpointSchema,
+ BreakpointsSchema,
} from "@webstudio-is/sdk";
import ObjectId from "bson-objectid";
import { applyPatches, type Patch } from "immer";
@@ -21,8 +22,8 @@ export const load = async (treeId?: Tree["id"]) => {
if (breakpoint === null) {
throw new Error("Breakpoint not found");
}
- const values = JSON.parse(breakpoint.values);
- schema.Breakpoints.parse(values);
+ const values: Array<Breakpoint> = JSON.parse(breakpoint.values);
+ BreakpointsSchema.parse(values);
return {
...breakpoint,
values,
@@ -39,7 +40,7 @@ export const create = async (
treeId: Project["id"],
values: Array<Breakpoint>
) => {
- schema.Breakpoints.parse(values);
+ BreakpointsSchema.parse(values);
const data = {
treeId,
@@ -74,7 +75,7 @@ export const patch = async (
if (breakpoints === null) return;
const nextValues = applyPatches(breakpoints.values, patches);
- schema.Breakpoints.parse(nextValues);
+ BreakpointsSchema.parse(nextValues);
await prisma.breakpoints.update({
where: { treeId },
diff --git a/apps/designer/app/shared/db/project.server.ts b/apps/designer/app/shared/db/project.server.ts
index cc70e3a504..da6baf4a51 100644
--- a/apps/designer/app/shared/db/project.server.ts
+++ b/apps/designer/app/shared/db/project.server.ts
@@ -1,20 +1,25 @@
import slugify from "slugify";
import { nanoid } from "nanoid";
import type { Project as BaseProject, User } from "@webstudio-is/sdk";
+import { z } from "zod";
import { prisma, Prisma } from "./prisma.server";
import * as db from ".";
+const TreeHistorySchema = z.array(z.string());
+
export type Project = Omit<BaseProject, "prodTreeIdHistory"> & {
- prodTreeIdHistory: string[];
+ prodTreeIdHistory: z.infer<typeof TreeHistorySchema>;
};
-const parseProject = (project: BaseProject | null): Project | null =>
- project
- ? {
- ...project,
- prodTreeIdHistory: JSON.parse(project.prodTreeIdHistory),
- }
- : null;
+const parseProject = (project: BaseProject | null): Project | null => {
+ if (project === null) return null;
+ const prodTreeIdHistory = JSON.parse(project.prodTreeIdHistory);
+ TreeHistorySchema.parse(prodTreeIdHistory);
+ return {
+ ...project,
+ prodTreeIdHistory,
+ };
+};
export const loadById = async (
projectId?: Project["id"]
@@ -149,6 +154,8 @@ export const update = async ({
}
}
+ TreeHistorySchema.parse(data.prodTreeIdHistory);
+
try {
const project = await prisma.project.update({
data: {
diff --git a/apps/designer/app/shared/db/props.server.ts b/apps/designer/app/shared/db/props.server.ts
index 6814449bac..0d4ef73a71 100644
--- a/apps/designer/app/shared/db/props.server.ts
+++ b/apps/designer/app/shared/db/props.server.ts
@@ -1,4 +1,8 @@
-import { type Tree, type AllUserProps } from "@webstudio-is/sdk";
+import {
+ type Tree,
+ type AllUserProps,
+ UserPropsSchema,
+} from "@webstudio-is/sdk";
import { applyPatches, type Patch } from "immer";
import { prisma } from "./prisma.server";
import { Project } from "./project.server";
@@ -25,10 +29,14 @@ export const loadByTreeId = async (treeId: Tree["id"]) => {
where: { treeId },
});
- return instancePropsEntries.map((tree) => ({
- ...tree,
- props: JSON.parse(tree.props),
- }));
+ return instancePropsEntries.map((instanceProps) => {
+ const props = JSON.parse(instanceProps.props);
+ UserPropsSchema.parse(props);
+ return {
+ ...instanceProps,
+ props,
+ };
+ });
};
export const clone = async ({
@@ -75,14 +83,16 @@ export const patch = async (
);
await Promise.all(
- Object.values(nextProps).map(({ id, instanceId, treeId, props }) =>
- prisma.instanceProps.upsert({
+ Object.values(nextProps).map(({ id, instanceId, treeId, props }) => {
+ UserPropsSchema.parse(props);
+ const propsString = JSON.stringify(props);
+ return prisma.instanceProps.upsert({
where: { id: id },
- create: { id: id, instanceId, treeId, props: JSON.stringify(props) },
+ create: { id: id, instanceId, treeId, props: propsString },
update: {
- props: JSON.stringify(props),
+ props: propsString,
},
- })
- )
+ });
+ })
);
};
diff --git a/apps/designer/app/shared/db/tree.server.ts b/apps/designer/app/shared/db/tree.server.ts
index 26784ddcef..095bce7512 100644
--- a/apps/designer/app/shared/db/tree.server.ts
+++ b/apps/designer/app/shared/db/tree.server.ts
@@ -1,4 +1,9 @@
-import { type Instance, type Breakpoint, type Tree } from "@webstudio-is/sdk";
+import {
+ type Instance,
+ type Breakpoint,
+ type Tree,
+ InstanceSchema,
+} from "@webstudio-is/sdk";
import { applyPatches, type Patch } from "immer";
import { prisma } from "./prisma.server";
import { createInstance } from "~/shared/tree-utils";
@@ -58,9 +63,10 @@ export const createRootInstance = (breakpoints: Array<Breakpoint>) => {
};
export const create = async (root: Instance): Promise<DbTree> => {
- const newRoot = JSON.stringify(root);
+ InstanceSchema.parse(root);
+ const rootString = JSON.stringify(root);
return await prisma.tree.create({
- data: { root: newRoot },
+ data: { root: rootString },
});
};
@@ -71,9 +77,11 @@ export const loadById = async (treeId: string): Promise<Tree | null> => {
if (tree === null) return null;
+ const root = JSON.parse(tree.root);
+ InstanceSchema.parse(root);
return {
...tree,
- root: JSON.parse(tree.root),
+ root,
};
};