Compare commits

...

2 Commits

Author SHA1 Message Date
sharevb
fcf11f1100
Merge 3e3225a01d into b430baef40 2024-06-16 11:35:21 +00:00
sharevb
3e3225a01d feat(new tool): JSON To Schema
Convert JSON data to JSON Schema, MySQL DDL, Mongoose Schema, Google BigQuery schema or ClickHouse Table Schema
2024-06-16 12:55:53 +02:00
3 changed files with 100 additions and 0 deletions

View File

@ -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,
],
},
{

View 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'),
});

View 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));
case 'mysql':
return GenerateSchema.mysql(tableName.value, object);
case 'mongoose':
return JSON.stringify(GenerateSchema.mongoose(object));
case 'bigquery':
return JSON.stringify(GenerateSchema.bigquery(object));
case 'clickhouse':
return JSON.stringify(GenerateSchema.clickhouse(tableName.value, object, 'theDateField'));
default:
return JSON.stringify(GenerateSchema.generic(object));
}
}
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>