Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds YAML to Zod Schema, YAML category #376

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions pages/yaml-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";
import yaml from "yaml";

interface Settings {
rootName: string;
}

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

export default function YamlToZod() {
const name = "YAML to Zod Schema";

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

const transformer = useCallback(
async ({ value }) => {
const yamlToJSON = JSON.stringify(yaml.parse(value));
const { jsonToZod } = await import("json-to-zod");
return jsonToZod(JSON.parse(yamlToJSON), 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="YAML"
editorLanguage="yaml"
resultTitle="Zod Schema"
resultLanguage={"typescript"}
editorSettingsElement={getSettingsElement}
settings={settings}
/>
);
}
32 changes: 19 additions & 13 deletions utils/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,25 +355,35 @@ export const categorizedRoutes = [
]
},
{
category: "Others",
category: "YAML",
iconName: "",
content: [
{
label: "XML to JSON",
path: "/xml-to-json",
packageName: "xml-js",
packageUrl: "https://github.com/nashwaan/xml-js"
},
{
label: "YAML to JSON",
label: "to JSON",
path: "/yaml-to-json",
packageName: "yaml",
packageUrl: "https://github.com/tj/js-yaml"
},
{
label: "YAML to TOML",
label: "to TOML",
path: "/yaml-to-toml"
},
{
label: "to Zod Schema",
path: "/yaml-to-zod"
}
]
},
{
category: "Others",
iconName: "",
content: [
{
label: "XML to JSON",
path: "/xml-to-json",
packageName: "xml-js",
packageUrl: "https://github.com/nashwaan/xml-js"
},
{
label: "Markdown to HTML",
path: "/markdown-to-html",
Expand All @@ -386,10 +396,6 @@ export const categorizedRoutes = [
packageUrl: "https://www.npmjs.com/package/@iarna/toml",
packageName: "@iarna/toml"
},
{
label: "TOML to YAML",
path: "/toml-to-yaml"
Comment on lines -390 to -391
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been removed ?

},
{
label: "Cadence to Go",
path: "/cadence-to-go"
Expand Down