Skip to content

Commit

Permalink
asyncapi#506 Update ajv
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenRover committed Jul 9, 2022
1 parent 933058d commit 8e58835
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 110 deletions.
11 changes: 7 additions & 4 deletions lib/asyncapiSchemaFormatParser.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const ParserError = require('./errors/parser-error');
const asyncapi = require('@asyncapi/specs');
const { improveAjvErrors } = require('./utils');
const cloneDeep = require('lodash.clonedeep');

const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: 'auto',
strictNumbers: true,
strictSchema: false,
validateSchema: true,
logger: false,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

addFormats(ajv);

module.exports = {
parse,
Expand Down Expand Up @@ -89,7 +92,7 @@ function preparePayloadSchema(asyncapiSchema, version) {
const definitions = asyncapiSchema.definitions;
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
delete definitions['http://json-schema.org/draft-07/schema'];
delete definitions['http://json-schema.org/draft-04/schema'];

return {
$ref: payloadSchema,
definitions
Expand Down
16 changes: 10 additions & 6 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const fetch = require('node-fetch');
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const asyncapi = require('@asyncapi/specs');
const $RefParser = require('@apidevtools/json-schema-ref-parser');
const mergePatch = require('tiny-merge-patch').apply;
Expand All @@ -17,13 +18,14 @@ const xParserCircle = 'x-parser-circular';
const xParserMessageParsed = 'x-parser-message-parsed';

const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: 'auto',
logger: false,
strictNumbers: true,
strictSchema: false,
validateSchema: true,
logger: false,
});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

addFormats(ajv);

/**
* @module @asyncapi/parser
Expand Down Expand Up @@ -190,8 +192,10 @@ function getValidator(version) {
const asyncapiSchema = asyncapi[String(version)];
// Remove the meta schemas because it is already present within Ajv, and it's not possible to add duplicate schemas.
delete asyncapiSchema.definitions['http://json-schema.org/draft-07/schema'];
delete asyncapiSchema.definitions['http://json-schema.org/draft-04/schema'];
ajv.addSchema(asyncapiSchema, version);

// validate=false because AsyncApi schema are not fulfilling: https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-02#section-9.3.1.2
// else we get: '"additionalItems" is ignored when "items" is not an array of schemas'
ajv.addSchema(asyncapi[String(version)], version, false, true);
validate = ajv.getSchema(version);
}
return validate;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"@apidevtools/json-schema-ref-parser": "^9.0.6",
"@asyncapi/specs": "^3.0.0",
"@fmvilas/pseudo-yaml-ast": "^0.3.1",
"ajv": "^6.10.1",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"js-yaml": "^3.13.1",
"json-to-ast": "^2.1.0",
"lodash.clonedeep": "^4.5.0",
Expand Down
40 changes: 8 additions & 32 deletions test/asyncapiSchemaFormatParser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,27 @@ describe('asyncapiSchemaFormatParser', function() {
title: 'This is not a valid AsyncAPI Schema Object.',
parsedJSON,
validationErrors: [{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object,boolean',
title: '/channels/mychannel/publish/message/payloadundefined must be object,boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
jsonPointer: "/channels/mychannel/publish/message/payloadundefined"
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be object,boolean',
title: '/channels/mychannel/publish/message/payloadundefined must be object,boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
jsonPointer: "/channels/mychannel/publish/message/payloadundefined"
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should be boolean',
title: '/channels/mychannel/publish/message/payloadundefined must be boolean',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
jsonPointer: '/channels/mychannel/publish/message/payloadundefined'
}
},
{
title: '/channels/mychannel/publish/message/payload/additionalProperties should match some schema in anyOf',
title: '/channels/mychannel/publish/message/payloadundefined must match a schema in anyOf',
location: {
jsonPointer: '/channels/mychannel/publish/message/payload/additionalProperties',
startLine: 13,
startColumn: 38,
startOffset: offset(252, 13),
endLine: 15,
endColumn: 15,
endOffset: offset(297, 15)
jsonPointer: '/channels/mychannel/publish/message/payloadundefined'
}
}]
};
Expand Down
104 changes: 37 additions & 67 deletions test/parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,19 @@ describe('parse()', function() {
title: 'There were errors validating the AsyncAPI document.',
parsedJSON: JSON.parse(invalidAsyncAPI),
validationErrors: [{
title: '/info should have required property \'title\'',
title: '/ must have required property \'channels\'',
location: {
endColumn: 31,
endLine: 1,
endOffset: 29,
jsonPointer: '/info',
startColumn: 29,
startLine: 1,
startOffset: 27
jsonPointer: '/'
}
},
{
title: '/info should have required property \'version\'',
title: '/ must have required property \'version\'',
location: {
endColumn: 31,
endLine: 1,
endOffset: 29,
jsonPointer: '/info',
startColumn: 29,
startLine: 1,
startOffset: 27
jsonPointer: '/'
}
},
{
title: '/ should have required property \'channels\'',
title: '/ must have required property \'title\'',
location: { jsonPointer: '/' }
}]
};
Expand All @@ -135,39 +123,33 @@ describe('parse()', function() {
parsedJSON: JSON.parse(invalidYamlOutput),
validationErrors: [
{
title: '/ should NOT have additional properties',
title: '/ must NOT have additional properties',
location: {
endColumn: 11,
endLine: 15,
endOffset: 315,
jsonPointer: '/test',
startLine: 15,
startColumn: 1,
startOffset: offset(305,15),
endLine: 15,
endColumn: 11,
endOffset: offset(315,15)
startLine: 15,
startOffset: 305
}
},
{
title: '/info should NOT have additional properties',
title: '/ must have required property \'title\'',
location: {
jsonPointer: '/info/test',
startLine: 3,
startColumn: 3,
startOffset: offset(24,3),
endLine: 3,
endColumn: 13,
endOffset: offset(34,3)
jsonPointer: '/'
}
},
{
title: '/info should have required property \'title\'',
title: '/ must NOT have additional properties',
location: {
jsonPointer: '/info',
startLine: 2,
endColumn: 11,
endLine: 15,
endOffset: 315,
jsonPointer: '/test',
startColumn: 1,
startOffset: offset(16,2),
endLine: 4,
endColumn: 19,
endOffset: offset(53,4)
startLine: 15,
startOffset: 305
}
}
]
Expand All @@ -185,15 +167,9 @@ describe('parse()', function() {
title: 'There were errors validating the AsyncAPI document.',
validationErrors: [
{
title: '/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1off/parameters/streetlightId/$ref should match format \"uri-reference\"',
title: '/ must match format \"uri-reference\"',
location: {
jsonPointer: '/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1off/parameters/streetlightId/$ref',
startLine: 67,
startColumn: 9,
startOffset: offset(1970, 67),
endLine: 68,
endColumn: 46,
endOffset: offset(2024, 68),
jsonPointer: '/'
}
}
]
Expand All @@ -213,39 +189,33 @@ describe('parse()', function() {
parsedJSON: JSON.parse(invalidJsonOutput),
validationErrors: [
{
title: '/ should NOT have additional properties',
title: '/ must NOT have additional properties',
location: {
endColumn: 15,
endLine: 23,
endOffset: 303,
jsonPointer: '/test',
startLine: 23,
startColumn: 11,
startOffset: offset(299,23),
endLine: 23,
endColumn: 15,
endOffset: offset(303,23)
startLine: 23,
startOffset: 299
}
},
{
title: '/info should NOT have additional properties',
title: '/ must have required property \'title\'',
location: {
jsonPointer: '/info/test',
startLine: 4,
startColumn: 12,
startOffset: offset(45,4),
endLine: 4,
endColumn: 16,
endOffset: offset(49,4)
jsonPointer: '/'
}
},
{
title: '/info should have required property \'title\'',
title: '/ must NOT have additional properties',
location: {
jsonPointer: '/info',
startLine: 3,
endColumn: 15,
endLine: 23,
endOffset: 303,
jsonPointer: '/test',
startColumn: 11,
startOffset: offset(33,3),
endLine: 6,
endColumn: 4,
endOffset: offset(74,6)
startLine: 23,
startOffset: 299
}
}
]
Expand Down

0 comments on commit 8e58835

Please sign in to comment.