Browse Source

fix json validator

AykutSarac 2 years ago
parent
commit
67b5d6a1f8
1 changed files with 9 additions and 10 deletions
  1. 9 10
      src/components/MonacoEditor/index.tsx

+ 9 - 10
src/components/MonacoEditor/index.tsx

@@ -58,18 +58,17 @@ export const MonacoEditor = ({
 
 
   React.useEffect(() => {
   React.useEffect(() => {
     const formatTimer = setTimeout(() => {
     const formatTimer = setTimeout(() => {
-      try {
-        if (!value) {
-          setHasError(false);
-          return setJson("{}");
-        }
-
-        const parsedJSON = JSON.stringify(parse(value), null, 2);
-        setJson(parsedJSON);
+      if (!value) {
         setHasError(false);
         setHasError(false);
-      } catch (jsonError: any) {
-        setHasError(true);
+        return setJson("{}");
       }
       }
+
+      const errors = [];
+      const parsedJSON = JSON.stringify(parse(value, errors), null, 2);
+      if (errors.length) return setHasError(true);
+
+      setJson(parsedJSON);
+      setHasError(false);
     }, 1200);
     }, 1200);
 
 
     return () => clearTimeout(formatTimer);
     return () => clearTimeout(formatTimer);