mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
feat: add JSON validation utility with detailed error messages
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { tool as jsonPrettify } from './prettify/meta';
|
||||
import { tool as jsonMinify } from './minify/meta';
|
||||
import { tool as jsonStringify } from './stringify/meta';
|
||||
import { tool as validateJson } from './validateJson/meta';
|
||||
|
||||
export const jsonTools = [jsonPrettify, jsonMinify, jsonStringify];
|
||||
export const jsonTools = [
|
||||
validateJson,
|
||||
jsonPrettify,
|
||||
jsonMinify,
|
||||
jsonStringify
|
||||
];
|
||||
|
98
src/pages/tools/json/validateJson/index.tsx
Normal file
98
src/pages/tools/json/validateJson/index.tsx
Normal file
@@ -0,0 +1,98 @@
|
||||
import { Box } from '@mui/material';
|
||||
import React, { useRef, useState, useEffect } from 'react';
|
||||
import ToolTextInput from '@components/input/ToolTextInput';
|
||||
import ToolTextResult from '@components/result/ToolTextResult';
|
||||
import ToolInputAndResult from '@components/ToolInputAndResult';
|
||||
import ToolInfo from '@components/ToolInfo';
|
||||
import Separator from '@components/Separator';
|
||||
import ToolExamples, {
|
||||
CardExampleType
|
||||
} from '@components/examples/ToolExamples';
|
||||
import { FormikProps } from 'formik';
|
||||
import { validateJson } from './servcie';
|
||||
import { ToolComponentProps } from '@tools/defineTool';
|
||||
|
||||
const exampleCards: CardExampleType<{}>[] = [
|
||||
{
|
||||
title: 'Valid JSON Object',
|
||||
description: 'This example shows a correctly formatted JSON object.',
|
||||
sampleText: `{
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
"city": "New York"
|
||||
}`,
|
||||
sampleResult: '✅ Valid JSON',
|
||||
sampleOptions: {}
|
||||
},
|
||||
{
|
||||
title: 'Invalid JSON Missing Quotes',
|
||||
description:
|
||||
'This example shows an invalid JSON structure where property names are missing quotes.',
|
||||
sampleText: `{
|
||||
name: "John",
|
||||
age: 30,
|
||||
city: "New York"
|
||||
}`,
|
||||
sampleResult: "❌ Error: Expected property name or '}' in JSON ",
|
||||
sampleOptions: {}
|
||||
},
|
||||
{
|
||||
title: 'Invalid JSON with Trailing Comma',
|
||||
description:
|
||||
'This example shows an invalid JSON with a trailing comma at the end of the object.',
|
||||
sampleText: `{
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
"city": "New York",
|
||||
}`,
|
||||
sampleResult: '❌ Error: Expected double-quoted property name',
|
||||
sampleOptions: {}
|
||||
}
|
||||
];
|
||||
|
||||
export default function ValidateJson({ title }: ToolComponentProps) {
|
||||
const [input, setInput] = useState<string>('');
|
||||
const [result, setResult] = useState<string>('');
|
||||
const formRef = useRef<FormikProps<{}>>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (input) {
|
||||
compute(input);
|
||||
}
|
||||
}, [input]);
|
||||
|
||||
const compute = (input: string) => {
|
||||
const { valid, error } = validateJson(input);
|
||||
if (valid) {
|
||||
setResult('✅ Valid JSON');
|
||||
} else {
|
||||
setResult(`❌ ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<ToolInputAndResult
|
||||
input={
|
||||
<ToolTextInput title="Input JSON" value={input} onChange={setInput} />
|
||||
}
|
||||
result={<ToolTextResult title="Validation Result" value={result} />}
|
||||
/>
|
||||
|
||||
<ToolInfo
|
||||
title="What is JSON Validation?"
|
||||
description="JSON validation checks the syntax and structure of a JSON file to ensure it follows proper JSON format."
|
||||
/>
|
||||
|
||||
<Separator backgroundColor="#5581b5" margin="50px" />
|
||||
|
||||
<ToolExamples
|
||||
title={title}
|
||||
exampleCards={exampleCards}
|
||||
getGroups={() => []}
|
||||
formRef={formRef}
|
||||
setInput={setInput}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
13
src/pages/tools/json/validateJson/meta.ts
Normal file
13
src/pages/tools/json/validateJson/meta.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('json', {
|
||||
name: 'Validate JSON',
|
||||
path: 'validateJson',
|
||||
icon: 'lets-icons:json-light',
|
||||
description:
|
||||
'Validate JSON data and identify formatting issues such as missing quotes, trailing commas, and incorrect brackets.',
|
||||
shortDescription: 'Quickly validate a JSON data structure.',
|
||||
keywords: ['validate', 'json', 'syntax'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
14
src/pages/tools/json/validateJson/servcie.ts
Normal file
14
src/pages/tools/json/validateJson/servcie.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export const validateJson = (
|
||||
input: string
|
||||
): { valid: boolean; error?: string } => {
|
||||
try {
|
||||
JSON.parse(input);
|
||||
return { valid: true };
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
const message = error.message;
|
||||
return { valid: false, error: error.message };
|
||||
}
|
||||
return { valid: false, error: 'Unknown error occurred' };
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user