@@ -13,19 +13,20 @@ import {
1313 Tabs ,
1414 Switch ,
1515 Row ,
16- Col
16+ Col ,
17+ Alert
1718} from 'antd' ;
1819import constants from '../../constants/variable.js' ;
1920import AceEditor from 'client/components/AceEditor/AceEditor' ;
2021import _ from 'underscore' ;
21- import { isJson , deepCopyJson } from '../../common.js' ;
22+ import { isJson , deepCopyJson , json5_parse } from '../../common.js' ;
2223import axios from 'axios' ;
2324import ModalPostman from '../ModalPostman/index.js' ;
2425import CheckCrossInstall , { initCrossRequest } from './CheckCrossInstall.js' ;
2526import './Postman.scss' ;
2627import ProjectEnv from '../../containers/Project/Setting/ProjectEnv/index.js' ;
2728import json5 from 'json5' ;
28- const { handleParamsValue, ArrayToObject } = require ( 'common/utils.js' ) ;
29+ const { handleParamsValue, ArrayToObject, schemaValidator } = require ( 'common/utils.js' ) ;
2930const {
3031 handleParams,
3132 checkRequestBodyIsRaw,
@@ -116,6 +117,7 @@ export default class Run extends Component {
116117 this . state = {
117118 loading : false ,
118119 resStatusCode : null ,
120+ test_valid_msg : null ,
119121 resStatusText : null ,
120122 case_env : '' ,
121123 mock_verify : false ,
@@ -208,6 +210,7 @@ export default class Run extends Component {
208210 ...data ,
209211 req_body_other : body ,
210212 resStatusCode : null ,
213+ test_valid_msg : null ,
211214 resStatusText : null
212215 } ,
213216 ( ) => this . props . type === 'inter' && this . initEnvState ( data . case_env , data . env )
@@ -260,11 +263,9 @@ export default class Run extends Component {
260263 }
261264
262265 handleValue ( val , global ) {
263- // console.log('val',val);
264- // console.log('global',global);
265266 let globalValue = ArrayToObject ( global ) ;
266267 return handleParamsValue ( val , {
267- global :globalValue
268+ global : globalValue
268269 } ) ;
269270 }
270271
@@ -295,7 +296,6 @@ export default class Run extends Component {
295296 loading : true
296297 } ) ;
297298
298-
299299 let options = handleParams ( this . state , this . handleValue ) ,
300300 result ;
301301
@@ -335,6 +335,15 @@ export default class Run extends Component {
335335 res_body_type : 'json'
336336 } ) ;
337337 }
338+
339+ // 对 返回值数据结构 和定义的 返回数据结构 进行 格式校验
340+ let validResult = this . resBodyValidator ( this . props . data , result . body ) ;
341+ if ( ! validResult . valid ) {
342+ this . setState ( { test_valid_msg : `返回参数 ${ validResult . message } ` } ) ;
343+ } else {
344+ this . setState ( { test_valid_msg : '' } ) ;
345+ }
346+
338347 this . setState ( {
339348 resStatusCode : result . status ,
340349 resStatusText : result . statusText ,
@@ -343,6 +352,20 @@ export default class Run extends Component {
343352 } ) ;
344353 } ;
345354
355+ // 返回数据与定义数据的比较判断
356+ resBodyValidator = ( interfaceData , test_res_body ) => {
357+ const { res_body_type, res_body_is_json_schema, res_body } = interfaceData ;
358+ let validResult = { valid : true } ;
359+
360+ if ( res_body_type === 'json' && res_body_is_json_schema ) {
361+ const schema = json5_parse ( res_body ) ;
362+ const params = json5_parse ( test_res_body ) ;
363+ validResult = schemaValidator ( schema , params ) ;
364+ }
365+
366+ return validResult ;
367+ } ;
368+
346369 changeParam = ( name , v , index , key ) => {
347370 key = key || 'value' ;
348371 const pathParam = deepCopyJson ( this . state [ name ] ) ;
@@ -848,6 +871,21 @@ export default class Run extends Component {
848871 >
849872 { this . state . resStatusCode + ' ' + this . state . resStatusText }
850873 </ h2 >
874+ { this . state . test_valid_msg && (
875+ < Alert
876+ message = {
877+ < span >
878+ Warning
879+ < Tooltip title = "针对定义为 json schema 的返回数据进行格式校验" >
880+ < Icon type = "question-circle-o" />
881+ </ Tooltip >
882+ </ span >
883+ }
884+ type = "warning"
885+ showIcon
886+ description = { this . state . test_valid_msg }
887+ />
888+ ) }
851889
852890 < div className = "container-header-body" >
853891 < div className = "header" >
@@ -861,7 +899,7 @@ export default class Run extends Component {
861899 readOnly = { true }
862900 className = "pretty-editor-header"
863901 data = { this . state . test_res_header }
864- mode = "json"
902+ mode = "json"
865903 />
866904 </ div >
867905 < div className = "resizer" >
0 commit comments