我正在尝试使用got-swag(npm包)测试一个swagger api
当我尝试使用json模式验证json响应时,当单独的yaml文件合并为一个yaml和json文件时,我得到一个parseError。原点是-validate()行。 validate()源自got-swag包,但它的底层 jsonschema.validate(数据,架构); 依赖
x-tests:
- description: Should return array of ferries
steps:
- get('/api/get/link/ferries')
- equal(res.statusCode, 200)
- ok(res.json.length > 0)
- validate(res.json, $ref: '#/definitions/ferry')
这是结果错误:
throw new exports.ParserError('while parsing a block mapping', this.marks.slice(-1)[0], "expected <block end>, but found " + token.id, token.start_mark);
^
while parsing a block mapping
on line 29, column 15
expected <block end>, but found <scalar>
on line 29, column 76
at ParserError.YAMLError [as constructor] (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\errors.js:70:46)
at ParserError.MarkedYAMLError [as constructor] (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\errors.js:90:45)
at new ParserError (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\parser.js:17:48)
at Loader.__dirname.Parser.Parser.parse_block_mapping_key (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\parser.js:433:15)
at Loader.__dirname.Parser.Parser.check_event (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\parser.js:61:48)
at Loader.__dirname.Composer.Composer.compose_mapping_node (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\composer.js:248:20)
at Loader.__dirname.Composer.Composer.compose_node (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\composer.js:160:21)
at Loader.__dirname.Composer.Composer.compose_sequence_node (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\composer.js:216:30)
at Loader.__dirname.Composer.Composer.compose_node (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\composer.js:158:21)
at Loader.__dirname.Composer.Composer.compose_mapping_node (C:\Users\dvbets\Documents\Workspace\Repos\node_modules\yaml-js\lib\composer.js:250:27)
答案 0 :(得分:1)
为了逃避内部:
字符,该行必须用引号括起来:
- "validate(res.json, $ref: '#/definitions/ferry')"
^
:
是YAML中的特殊字符,是key: value
对的分隔符。在没有转义的情况下,该行被解析为具有值validate(res.json, $ref
的键名'#/definitions/ferry')
,并且解析器在结束引号后的)
处扼流。
相关:How to escape indicator characters (i.e. : or - ) in YAML