mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 13:29:13 -04:00
feat(new tool): JSON To Schema
Convert JSON data to JSON Schema, MySQL DDL, Mongoose Schema, Google BigQuery schema or ClickHouse Table Schema
This commit is contained in:
parent
b430baef40
commit
2c2b302de1
8 changed files with 168 additions and 10 deletions
1
components.d.ts
vendored
1
components.d.ts
vendored
|
@ -109,6 +109,7 @@ declare module '@vue/runtime-core' {
|
|||
JsonDiff: typeof import('./src/tools/json-diff/json-diff.vue')['default']
|
||||
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
|
||||
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
|
||||
JsonToSchema: typeof import('./src/tools/json-to-schema/json-to-schema.vue')['default']
|
||||
JsonToToml: typeof import('./src/tools/json-to-toml/json-to-toml.vue')['default']
|
||||
JsonToYaml: typeof import('./src/tools/json-to-yaml-converter/json-to-yaml.vue')['default']
|
||||
JsonViewer: typeof import('./src/tools/json-viewer/json-viewer.vue')['default']
|
||||
|
|
|
@ -61,6 +61,7 @@
|
|||
"figlet": "^1.7.0",
|
||||
"figue": "^1.2.0",
|
||||
"fuse.js": "^6.6.2",
|
||||
"generate-schema": "^2.6.0",
|
||||
"highlight.js": "^11.7.0",
|
||||
"iarna-toml-esm": "^3.0.5",
|
||||
"ibantools": "^4.3.3",
|
||||
|
|
35
pnpm-lock.yaml
generated
35
pnpm-lock.yaml
generated
|
@ -83,6 +83,9 @@ dependencies:
|
|||
fuse.js:
|
||||
specifier: ^6.6.2
|
||||
version: 6.6.2
|
||||
generate-schema:
|
||||
specifier: ^2.6.0
|
||||
version: 2.6.0
|
||||
highlight.js:
|
||||
specifier: ^11.7.0
|
||||
version: 11.7.0
|
||||
|
@ -3354,7 +3357,7 @@ packages:
|
|||
dependencies:
|
||||
'@unhead/dom': 0.5.1
|
||||
'@unhead/schema': 0.5.1
|
||||
'@vueuse/shared': 10.8.0(vue@3.3.4)
|
||||
'@vueuse/shared': 10.11.0(vue@3.3.4)
|
||||
unhead: 0.5.1
|
||||
vue: 3.3.4
|
||||
transitivePeerDependencies:
|
||||
|
@ -3987,19 +3990,19 @@ packages:
|
|||
- vue
|
||||
dev: false
|
||||
|
||||
/@vueuse/shared@10.3.0(vue@3.3.4):
|
||||
resolution: {integrity: sha512-kGqCTEuFPMK4+fNWy6dUOiYmxGcUbtznMwBZLC1PubidF4VZY05B+Oht7Jh7/6x4VOWGpvu3R37WHi81cKpiqg==}
|
||||
/@vueuse/shared@10.11.0(vue@3.3.4):
|
||||
resolution: {integrity: sha512-fyNoIXEq3PfX1L3NkNhtVQUSRtqYwJtJg+Bp9rIzculIZWHTkKSysujrOk2J+NrRulLTQH9+3gGSfYLWSEWU1A==}
|
||||
dependencies:
|
||||
vue-demi: 0.14.5(vue@3.3.4)
|
||||
vue-demi: 0.14.8(vue@3.3.4)
|
||||
transitivePeerDependencies:
|
||||
- '@vue/composition-api'
|
||||
- vue
|
||||
dev: false
|
||||
|
||||
/@vueuse/shared@10.8.0(vue@3.3.4):
|
||||
resolution: {integrity: sha512-dUdy6zwHhULGxmr9YUg8e+EnB39gcM4Fe2oKBSrh3cOsV30JcMPtsyuspgFCUo5xxFNaeMf/W2yyKfST7Bg8oQ==}
|
||||
/@vueuse/shared@10.3.0(vue@3.3.4):
|
||||
resolution: {integrity: sha512-kGqCTEuFPMK4+fNWy6dUOiYmxGcUbtznMwBZLC1PubidF4VZY05B+Oht7Jh7/6x4VOWGpvu3R37WHi81cKpiqg==}
|
||||
dependencies:
|
||||
vue-demi: 0.14.7(vue@3.3.4)
|
||||
vue-demi: 0.14.5(vue@3.3.4)
|
||||
transitivePeerDependencies:
|
||||
- '@vue/composition-api'
|
||||
- vue
|
||||
|
@ -5765,6 +5768,14 @@ packages:
|
|||
engines: {node: '>=10'}
|
||||
dev: false
|
||||
|
||||
/generate-schema@2.6.0:
|
||||
resolution: {integrity: sha512-EUBKfJNzT8f91xUk5X5gKtnbdejZeE065UAJ3BCzE8VEbvwKI9Pm5jaWmqVeK1MYc1g5weAVFDTSJzN7ymtTqA==}
|
||||
hasBin: true
|
||||
dependencies:
|
||||
commander: 2.20.3
|
||||
type-of-is: 3.5.1
|
||||
dev: false
|
||||
|
||||
/gensync@1.0.0-beta.2:
|
||||
resolution: {integrity: sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
|
@ -8555,6 +8566,11 @@ packages:
|
|||
engines: {node: '>=12.20'}
|
||||
dev: false
|
||||
|
||||
/type-of-is@3.5.1:
|
||||
resolution: {integrity: sha512-SOnx8xygcAh8lvDU2exnK2bomASfNjzB3Qz71s2tw9QnX8fkAo7aC+D0H7FV0HjRKj94CKV2Hi71kVkkO6nOxg==}
|
||||
engines: {node: '>=0.10.5'}
|
||||
dev: false
|
||||
|
||||
/typed-array-buffer@1.0.0:
|
||||
resolution: {integrity: sha512-Y8KTSIglk9OZEr8zywiIHG/kmQ7KWyjseXs1CbSo8vC42w7hg2HgYTxSWwP0+is7bWDc1H+Fo026CpHFwm8tkw==}
|
||||
engines: {node: '>= 0.4'}
|
||||
|
@ -9158,8 +9174,8 @@ packages:
|
|||
vue: 3.3.4
|
||||
dev: false
|
||||
|
||||
/vue-demi@0.14.7(vue@3.3.4):
|
||||
resolution: {integrity: sha512-EOG8KXDQNwkJILkx/gPcoL/7vH+hORoBaKgGe+6W7VFMvCYJfmF2dGbvgDroVnI8LU7/kTu8mbjRZGBU1z9NTA==}
|
||||
/vue-demi@0.14.8(vue@3.3.4):
|
||||
resolution: {integrity: sha512-Uuqnk9YE9SsWeReYqK2alDI5YzciATE0r2SkA6iMAtuXvNTMNACJLJEXNXaEy94ECuBe4Sk6RzRU80kjdbIo1Q==}
|
||||
engines: {node: '>=12'}
|
||||
hasBin: true
|
||||
requiresBuild: true
|
||||
|
@ -9449,6 +9465,7 @@ packages:
|
|||
|
||||
/workbox-google-analytics@7.0.0:
|
||||
resolution: {integrity: sha512-MEYM1JTn/qiC3DbpvP2BVhyIH+dV/5BjHk756u9VbwuAhu0QHyKscTnisQuz21lfRpOwiS9z4XdqeVAKol0bzg==}
|
||||
deprecated: It is not compatible with newer versions of GA starting with v4, as long as you are using GAv3 it should be ok, but the package is not longer being maintained
|
||||
dependencies:
|
||||
workbox-background-sync: 7.0.0
|
||||
workbox-core: 7.0.0
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import { useRouteQuery } from '@vueuse/router';
|
||||
import { computed } from 'vue';
|
||||
import { useStorage } from '@vueuse/core';
|
||||
|
||||
export { useQueryParam };
|
||||
export { useQueryParam, useQueryParamOrStorage };
|
||||
|
||||
const transformers = {
|
||||
number: {
|
||||
|
@ -16,6 +17,12 @@ const transformers = {
|
|||
fromQuery: (value: string) => value.toLowerCase() === 'true',
|
||||
toQuery: (value: boolean) => (value ? 'true' : 'false'),
|
||||
},
|
||||
object: {
|
||||
fromQuery: (value: string) => {
|
||||
return JSON.parse(value);
|
||||
},
|
||||
toQuery: (value: object) => JSON.stringify(value),
|
||||
},
|
||||
};
|
||||
|
||||
function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue: T }) {
|
||||
|
@ -33,3 +40,27 @@ function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue:
|
|||
},
|
||||
});
|
||||
}
|
||||
|
||||
function useQueryParamOrStorage<T>({ name, storageName, defaultValue }: { name: string; storageName: string; defaultValue: T }) {
|
||||
const type = typeof defaultValue;
|
||||
const transformer = transformers[type as keyof typeof transformers] ?? transformers.string;
|
||||
|
||||
const storageRef = useStorage(storageName, defaultValue);
|
||||
const proxyDefaultValue = transformer.toQuery(defaultValue as never);
|
||||
const proxy = useRouteQuery(name, proxyDefaultValue);
|
||||
|
||||
const r = ref(defaultValue);
|
||||
|
||||
watch(r,
|
||||
(value) => {
|
||||
proxy.value = transformer.toQuery(value as never);
|
||||
storageRef.value = value as never;
|
||||
},
|
||||
{ deep: true });
|
||||
|
||||
r.value = (proxy.value && proxy.value !== proxyDefaultValue
|
||||
? transformer.fromQuery(proxy.value) as unknown as T
|
||||
: storageRef.value as T) as never;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
|
|||
|
||||
import { tool as textToUnicode } from './text-to-unicode';
|
||||
import { tool as safelinkDecoder } from './safelink-decoder';
|
||||
import { tool as jsonToSchema } from './json-to-schema';
|
||||
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
|
||||
import { tool as numeronymGenerator } from './numeronym-generator';
|
||||
import { tool as macAddressGenerator } from './mac-address-generator';
|
||||
|
@ -148,6 +149,7 @@ export const toolsByCategory: ToolCategory[] = [
|
|||
dockerRunToDockerComposeConverter,
|
||||
xmlFormatter,
|
||||
yamlViewer,
|
||||
jsonToSchema,
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
8
src/tools/json-to-schema/generate-schema.d.ts
vendored
Normal file
8
src/tools/json-to-schema/generate-schema.d.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
declare module 'generate-schema' {
|
||||
function generic(jsonObject: object);
|
||||
function mongoose(jsonObject: object);
|
||||
function bigquery(jsonObject: object);
|
||||
function json(title: string, jsonObject: object);
|
||||
function mysql(tableName: string, jsonObject: object);
|
||||
function clickhouse(tableName: string, jsonObject: object, dateField: string);
|
||||
}
|
12
src/tools/json-to-schema/index.ts
Normal file
12
src/tools/json-to-schema/index.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { Braces } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Json to Schema',
|
||||
path: '/json-to-schema',
|
||||
description: 'Convert JSON data to JSON Schema, MySQL DDL, Mongoose Schema, Google BigQuery schema or ClickHouse Table Schema',
|
||||
keywords: ['json', 'schema', 'mysql', 'sql', 'ddl', 'mongoose', 'bigquery', 'clickhouse', 'table'],
|
||||
component: () => import('./json-to-schema.vue'),
|
||||
icon: Braces,
|
||||
createdAt: new Date('2024-05-11'),
|
||||
});
|
86
src/tools/json-to-schema/json-to-schema.vue
Normal file
86
src/tools/json-to-schema/json-to-schema.vue
Normal file
|
@ -0,0 +1,86 @@
|
|||
<script setup lang="ts">
|
||||
import JSON5 from 'json5';
|
||||
import GenerateSchema from 'generate-schema';
|
||||
import { withDefaultOnError } from '../../utils/defaults';
|
||||
import type { UseValidationRule } from '@/composable/validation';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const formats = [
|
||||
{ value: 'generic', label: 'Generic' },
|
||||
{ value: 'json', label: 'JSON Schema' },
|
||||
{ value: 'mysql', label: 'MySQL Table Schema' },
|
||||
{ value: 'mongoose', label: 'Mongoose Schema' },
|
||||
{ value: 'bigquery', label: 'Google BigQuery schema' },
|
||||
{ value: 'clickhouse', label: 'ClickHouse Table Schema' },
|
||||
];
|
||||
|
||||
const tableName = ref('TableName');
|
||||
const format = useQueryParamOrStorage({ name: 'fmt', storageName: 'json-to-schema:fmt', defaultValue: 'json' });
|
||||
|
||||
function convertJsonToSchema(value: string) {
|
||||
const object = JSON5.parse(value);
|
||||
switch (format.value) {
|
||||
case 'json':
|
||||
return JSON.stringify(GenerateSchema.json(tableName.value, object), null, 2);
|
||||
case 'mysql':
|
||||
return GenerateSchema.mysql(tableName.value, object);
|
||||
case 'mongoose':
|
||||
return JSON.stringify(GenerateSchema.mongoose(object), null, 2);
|
||||
case 'bigquery':
|
||||
return JSON.stringify(GenerateSchema.bigquery(object), null, 2);
|
||||
case 'clickhouse':
|
||||
return JSON.stringify(GenerateSchema.clickhouse(tableName.value, object, 'theDateField'), null, 2);
|
||||
default:
|
||||
return JSON.stringify(GenerateSchema.generic(object), null, 2);
|
||||
}
|
||||
}
|
||||
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => convertJsonToSchema(value), '');
|
||||
const schemaLanguage = computed(() => {
|
||||
switch (format.value) {
|
||||
case 'mysql':
|
||||
return 'sql';
|
||||
case 'mongoose':
|
||||
return 'json';
|
||||
case 'bigquery':
|
||||
return 'json';
|
||||
case 'clickhouse':
|
||||
return 'json';
|
||||
default:
|
||||
return 'json';
|
||||
}
|
||||
});
|
||||
|
||||
const rules: UseValidationRule<string>[] = [
|
||||
{
|
||||
validator: (v: string) => v === '' || JSON5.parse(v),
|
||||
message: 'Provided JSON is not valid.',
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<c-select
|
||||
v-model:value="format"
|
||||
:options="formats"
|
||||
placeholder="Target Schema format"
|
||||
/>
|
||||
<c-input-text
|
||||
v-if="['clickhouse', 'json', 'mysql'].includes(format)"
|
||||
v-model:value="tableName"
|
||||
label="Table Name"
|
||||
placeholder="Table Name"
|
||||
mb-2
|
||||
/>
|
||||
<n-divider />
|
||||
|
||||
<format-transformer
|
||||
input-label="Your JSON"
|
||||
input-placeholder="Paste your JSON here..."
|
||||
output-label="Your schema"
|
||||
:output-language="schemaLanguage"
|
||||
:input-validation-rules="rules"
|
||||
:transformer="transformer"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
Loading…
Add table
Add a link
Reference in a new issue