From cec882a2ec2690894c7957b9390600751cef3ac0 Mon Sep 17 00:00:00 2001 From: fmvilas Date: Thu, 15 Aug 2019 12:28:01 +0200 Subject: [PATCH] Fix parse errors --- lib/errors.js | 2 +- lib/parser.js | 2 +- test/parse_test.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/errors.js b/lib/errors.js index 48325a256..16d40123d 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -25,4 +25,4 @@ class ParserError extends Error { } } -module.exports.ParserError = ParserError; \ No newline at end of file +module.exports.ParserError = ParserError; diff --git a/lib/parser.js b/lib/parser.js index 87550ae9d..2399c2c57 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -78,7 +78,7 @@ async function parse(asyncapiYAMLorJSON, options = {}) { await iterateDocument(js, options); } catch (e) { - throw new ParserError(e); + throw new ParserError(e, e.errors); } return new AsyncAPIDocument(js); diff --git a/test/parse_test.js b/test/parse_test.js index 090d1f556..470fd2ae5 100644 --- a/test/parse_test.js +++ b/test/parse_test.js @@ -21,6 +21,36 @@ describe('parse()', function () { await expect(JSON.stringify(result.json())).to.equal(outputJSON); }); + it('should forward ajv errors', async function () { + try { + await parser.parse({"asyncapi": "unstable", "info": {}}); + } catch(e) { + const errors = [{ + keyword: 'required', + dataPath: '.info', + schemaPath: '#/required', + params: { missingProperty: 'title' }, + message: 'should have required property \'title\'' + }, + { + keyword: 'required', + dataPath: '.info', + schemaPath: '#/required', + params: { missingProperty: 'version' }, + message: 'should have required property \'version\'' + }, + { + keyword: 'required', + dataPath: '', + schemaPath: '#/required', + params: { missingProperty: 'channels' }, + message: 'should have required property \'channels\'' + }]; + + await expect(e.errors).to.deep.equal(errors); + } + }); + it('should not apply traits', async function () { const result = await parser.parse(inputYAML, { path: __filename, applyTraits: false }); await expect(JSON.stringify(result.json())).to.equal(outputJsonNoApplyTraits);