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
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const crypto = require("crypto");
const cryptoOrigCreateHash = crypto.createHash;
crypto.createHash = algorithm =>
cryptoOrigCreateHash(algorithm === "md4" ? "sha256" : algorithm);
const webpack = require("webpack");

const config = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build:analyze": "ANALYZE=true yarn build"
},
"engines": {
"node": "16.x"
"node": "20.x"
},
"dependencies": {
"@babel/plugin-transform-flow-strip-types": "^7.16.0",
Expand Down
64 changes: 64 additions & 0 deletions pages/js-object-to-zod.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import ConversionPanel from "@components/ConversionPanel";
import { EditorPanelProps } from "@components/EditorPanel";
import Form, { InputType } from "@components/Form";
import { useSettings } from "@hooks/useSettings";
import * as React from "react";
import { useCallback } from "react";

interface Settings {
rootName: string;
}

const formFields = [
{
type: InputType.TEXT_INPUT,
key: "rootName",
label: "Root Schema Name"
}
];

export default function JsObjectToZod() {
const name = "JS Object to Zod Schema";

const [settings, setSettings] = useSettings(name, {
rootName: "schema"
});

const transformer = useCallback(
async ({ value }) => {
const { jsonToZod } = await import("json-to-zod");
const objectValue = eval("(" + value + ")");
return jsonToZod(objectValue, settings.rootName, true);
},
[settings]
);

const getSettingsElement = useCallback<EditorPanelProps["settingElement"]>(
({ open, toggle }) => {
return (
<Form<Settings>
title={name}
onSubmit={setSettings}
open={open}
toggle={toggle}
formsFields={formFields}
initialValues={settings}
/>
);
},
[]
);

return (
<ConversionPanel
transformer={transformer}
editorTitle="JS Object"
editorLanguage="javascript"
editorDefaultValue="jsObject"
resultTitle="Zod Schema"
resultLanguage={"typescript"}
editorSettingsElement={getSettingsElement}
settings={settings}
/>
);
}
6 changes: 6 additions & 0 deletions utils/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ export const categorizedRoutes = [
path: "/js-object-to-typescript",
desc: "An online REPL for converting JS Object to Typescript."
},
{
label: "to Zod Schema",
path: "/js-object-to-zod",
packageUrl: "https://www.npmjs.com/package/json-to-zod",
packageName: "json-to-zod"
}
]
},
{
Expand Down