Skip to content

Commit 87512fc

Browse files
committed
Merge branch 'feat/json-to-schema' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents 4f7356d + f76aaa2 commit 87512fc

File tree

6 files changed

+126
-1
lines changed

6 files changed

+126
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"flatten-anything": "^4.0.1",
110110
"fuse.js": "^6.6.2",
111111
"hash-wasm": "^4.11.0",
112+
"generate-schema": "^2.6.0",
112113
"highlight.js": "^11.7.0",
113114
"iarna-toml-esm": "^3.0.5",
114115
"ibantools": "^4.3.3",

pnpm-lock.yaml

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { tool as icalParser } from './ical-parser';
5656
import { tool as icalGenerator } from './ical-generator';
5757
import { tool as imageToCss } from './image-to-css';
5858
import { tool as jsonToGo } from './json-to-go';
59+
import { tool as jsonToSchema } from './json-to-schema';
5960
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
6061
import { tool as numeronymGenerator } from './numeronym-generator';
6162
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -259,6 +260,7 @@ export const toolsByCategory: ToolCategory[] = [
259260
jqTester,
260261
jqMemo,
261262
jsonpathMemo,
263+
jsonToSchema,
262264
],
263265
},
264266
{
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)