Skip to content

Commit 2c2b302

Browse files
committed
feat(new tool): JSON To Schema
Convert JSON data to JSON Schema, MySQL DDL, Mongoose Schema, Google BigQuery schema or ClickHouse Table Schema
1 parent b430bae commit 2c2b302

File tree

8 files changed

+168
-10
lines changed

8 files changed

+168
-10
lines changed

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ declare module '@vue/runtime-core' {
109109
JsonDiff: typeof import('./src/tools/json-diff/json-diff.vue')['default']
110110
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
111111
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
112+
JsonToSchema: typeof import('./src/tools/json-to-schema/json-to-schema.vue')['default']
112113
JsonToToml: typeof import('./src/tools/json-to-toml/json-to-toml.vue')['default']
113114
JsonToYaml: typeof import('./src/tools/json-to-yaml-converter/json-to-yaml.vue')['default']
114115
JsonViewer: typeof import('./src/tools/json-viewer/json-viewer.vue')['default']

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"figlet": "^1.7.0",
6262
"figue": "^1.2.0",
6363
"fuse.js": "^6.6.2",
64+
"generate-schema": "^2.6.0",
6465
"highlight.js": "^11.7.0",
6566
"iarna-toml-esm": "^3.0.5",
6667
"ibantools": "^4.3.3",

pnpm-lock.yaml

Lines changed: 26 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/composable/queryParams.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useRouteQuery } from '@vueuse/router';
22
import { computed } from 'vue';
3+
import { useStorage } from '@vueuse/core';
34

4-
export { useQueryParam };
5+
export { useQueryParam, useQueryParamOrStorage };
56

67
const transformers = {
78
number: {
@@ -16,6 +17,12 @@ const transformers = {
1617
fromQuery: (value: string) => value.toLowerCase() === 'true',
1718
toQuery: (value: boolean) => (value ? 'true' : 'false'),
1819
},
20+
object: {
21+
fromQuery: (value: string) => {
22+
return JSON.parse(value);
23+
},
24+
toQuery: (value: object) => JSON.stringify(value),
25+
},
1926
};
2027

2128
function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue: T }) {
@@ -33,3 +40,27 @@ function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue:
3340
},
3441
});
3542
}
43+
44+
function useQueryParamOrStorage<T>({ name, storageName, defaultValue }: { name: string; storageName: string; defaultValue: T }) {
45+
const type = typeof defaultValue;
46+
const transformer = transformers[type as keyof typeof transformers] ?? transformers.string;
47+
48+
const storageRef = useStorage(storageName, defaultValue);
49+
const proxyDefaultValue = transformer.toQuery(defaultValue as never);
50+
const proxy = useRouteQuery(name, proxyDefaultValue);
51+
52+
const r = ref(defaultValue);
53+
54+
watch(r,
55+
(value) => {
56+
proxy.value = transformer.toQuery(value as never);
57+
storageRef.value = value as never;
58+
},
59+
{ deep: true });
60+
61+
r.value = (proxy.value && proxy.value !== proxyDefaultValue
62+
? transformer.fromQuery(proxy.value) as unknown as T
63+
: storageRef.value as T) as never;
64+
65+
return r;
66+
}

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
66

77
import { tool as textToUnicode } from './text-to-unicode';
88
import { tool as safelinkDecoder } from './safelink-decoder';
9+
import { tool as jsonToSchema } from './json-to-schema';
910
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
1011
import { tool as numeronymGenerator } from './numeronym-generator';
1112
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -148,6 +149,7 @@ export const toolsByCategory: ToolCategory[] = [
148149
dockerRunToDockerComposeConverter,
149150
xmlFormatter,
150151
yamlViewer,
152+
jsonToSchema,
151153
],
152154
},
153155
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare module 'generate-schema' {
2+
function generic(jsonObject: object);
3+
function mongoose(jsonObject: object);
4+
function bigquery(jsonObject: object);
5+
function json(title: string, jsonObject: object);
6+
function mysql(tableName: string, jsonObject: object);
7+
function clickhouse(tableName: string, jsonObject: object, dateField: string);
8+
}

src/tools/json-to-schema/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Braces } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Json to Schema',
6+
path: '/json-to-schema',
7+
description: 'Convert JSON data to JSON Schema, MySQL DDL, Mongoose Schema, Google BigQuery schema or ClickHouse Table Schema',
8+
keywords: ['json', 'schema', 'mysql', 'sql', 'ddl', 'mongoose', 'bigquery', 'clickhouse', 'table'],
9+
component: () => import('./json-to-schema.vue'),
10+
icon: Braces,
11+
createdAt: new Date('2024-05-11'),
12+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<script setup lang="ts">
2+
import JSON5 from 'json5';
3+
import GenerateSchema from 'generate-schema';
4+
import { withDefaultOnError } from '../../utils/defaults';
5+
import type { UseValidationRule } from '@/composable/validation';
6+
import { useQueryParamOrStorage } from '@/composable/queryParams';
7+
8+
const formats = [
9+
{ value: 'generic', label: 'Generic' },
10+
{ value: 'json', label: 'JSON Schema' },
11+
{ value: 'mysql', label: 'MySQL Table Schema' },
12+
{ value: 'mongoose', label: 'Mongoose Schema' },
13+
{ value: 'bigquery', label: 'Google BigQuery schema' },
14+
{ value: 'clickhouse', label: 'ClickHouse Table Schema' },
15+
];
16+
17+
const tableName = ref('TableName');
18+
const format = useQueryParamOrStorage({ name: 'fmt', storageName: 'json-to-schema:fmt', defaultValue: 'json' });
19+
20+
function convertJsonToSchema(value: string) {
21+
const object = JSON5.parse(value);
22+
switch (format.value) {
23+
case 'json':
24+
return JSON.stringify(GenerateSchema.json(tableName.value, object), null, 2);
25+
case 'mysql':
26+
return GenerateSchema.mysql(tableName.value, object);
27+
case 'mongoose':
28+
return JSON.stringify(GenerateSchema.mongoose(object), null, 2);
29+
case 'bigquery':
30+
return JSON.stringify(GenerateSchema.bigquery(object), null, 2);
31+
case 'clickhouse':
32+
return JSON.stringify(GenerateSchema.clickhouse(tableName.value, object, 'theDateField'), null, 2);
33+
default:
34+
return JSON.stringify(GenerateSchema.generic(object), null, 2);
35+
}
36+
}
37+
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => convertJsonToSchema(value), '');
38+
const schemaLanguage = computed(() => {
39+
switch (format.value) {
40+
case 'mysql':
41+
return 'sql';
42+
case 'mongoose':
43+
return 'json';
44+
case 'bigquery':
45+
return 'json';
46+
case 'clickhouse':
47+
return 'json';
48+
default:
49+
return 'json';
50+
}
51+
});
52+
53+
const rules: UseValidationRule<string>[] = [
54+
{
55+
validator: (v: string) => v === '' || JSON5.parse(v),
56+
message: 'Provided JSON is not valid.',
57+
},
58+
];
59+
</script>
60+
61+
<template>
62+
<div>
63+
<c-select
64+
v-model:value="format"
65+
:options="formats"
66+
placeholder="Target Schema format"
67+
/>
68+
<c-input-text
69+
v-if="['clickhouse', 'json', 'mysql'].includes(format)"
70+
v-model:value="tableName"
71+
label="Table Name"
72+
placeholder="Table Name"
73+
mb-2
74+
/>
75+
<n-divider />
76+
77+
<format-transformer
78+
input-label="Your JSON"
79+
input-placeholder="Paste your JSON here..."
80+
output-label="Your schema"
81+
:output-language="schemaLanguage"
82+
:input-validation-rules="rules"
83+
:transformer="transformer"
84+
/>
85+
</div>
86+
</template>

0 commit comments

Comments
 (0)