소스 검색

feat: json 格式校验

gaozhan 5 년 전
부모
커밋
4e6d2ca50a
2개의 변경된 파일23개의 추가작업 그리고 6개의 파일을 삭제
  1. 6 1
      src/pages/fengkong/features/group/edit/$name.js
  2. 17 5
      src/utils/utils.js

+ 6 - 1
src/pages/fengkong/features/group/edit/$name.js

@@ -2,6 +2,7 @@ import React from 'react'
 import { Form, Input, Row, Col, Button, Modal, message } from 'antd'
 import { connect } from 'dva'
 import AceEditor from 'react-ace'
+import { isJSON } from '@/utils/utils'
 import { detail, update, test } from '../service'
 
 import 'ace-builds/src-noconflict/mode-java'
@@ -58,9 +59,13 @@ class Add extends React.PureComponent {
       message.warn('请先提交策略内容!')
       return false
     }
+    if (!isJSON(this.state.testContent)) {
+      message.warn('输入数据格式错误!')
+      return false
+    }
     test({
       group: this.state.name,
-      input: JSON.parse(this.state.testContent)
+      input: this.state.testContent
     }).then(res => {
       if (res.code === 0) {
         this.setState({ testResult: JSON.stringify(res) })

+ 17 - 5
src/utils/utils.js

@@ -1,6 +1,18 @@
-export function arrayToObj(arr) {
+export function arrayToObj (arr) {
   return arr.reduce((t, c) => {
-    t[c.value] = c.label;
-    return t;
-  }, {});
-}
+    t[c.value] = c.label
+    return t
+  }, {})
+}
+
+export function isJSON (str) {
+  if (typeof str === 'string') {
+    try {
+      JSON.parse(str)
+      return true
+    } catch (e) {
+      console.log(e)
+      return false
+    }
+  }
+}