mirror of
https://github.com/CorentinTh/it-tools
synced 2024-07-05 08:58:50 +00:00
Compare commits
2 Commits
b717d28f7d
...
fcf11f1100
Author | SHA1 | Date | |
---|---|---|---|
|
fcf11f1100 | ||
|
3e3225a01d |
|
@ -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,
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
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));
|
||||
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>
|
Loading…
Reference in New Issue
Block a user