@asyncapi/parser
Advanced tools
Comparing version 1.11.0 to 1.11.1
@@ -7,2 +7,10 @@ const Ajv = require('ajv'); | ||
const ajv = new Ajv({ | ||
jsonPointers: true, | ||
allErrors: true, | ||
schemaId: 'id', | ||
logger: false, | ||
}); | ||
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); | ||
module.exports = { | ||
@@ -23,11 +31,5 @@ parse, | ||
const ajv = new Ajv({ | ||
jsonPointers: true, | ||
allErrors: true, | ||
schemaId: 'id', | ||
logger: false, | ||
}); | ||
const payloadSchema = preparePayloadSchema(asyncapi[parsedAsyncAPIDocument.asyncapi]); | ||
const validate = ajv.compile(payloadSchema); | ||
const validate = getValidator(parsedAsyncAPIDocument.asyncapi); | ||
const valid = validate(payload); | ||
const errors = validate.errors && [...validate.errors]; | ||
@@ -38,3 +40,3 @@ if (!valid) throw new ParserError({ | ||
parsedJSON: parsedAsyncAPIDocument, | ||
validationErrors: improveAjvErrors(addFullPathToDataPath(validate.errors, pathToPayload), originalAsyncAPIDocument, fileFormat), | ||
validationErrors: improveAjvErrors(addFullPathToDataPath(errors, pathToPayload), originalAsyncAPIDocument, fileFormat), | ||
}); | ||
@@ -64,2 +66,18 @@ } | ||
/** | ||
* Creates (or reuses) a function that validates an AsyncAPI Schema Object based on the passed AsyncAPI version. | ||
* | ||
* @private | ||
* @param {Object} version AsyncAPI version. | ||
* @returns {Function} Function that validates an AsyncAPI Schema Object based on the passed AsyncAPI version. | ||
*/ | ||
function getValidator(version) { | ||
let validate = ajv.getSchema(version); | ||
if (!validate) { | ||
ajv.addSchema(preparePayloadSchema(asyncapi[String(version)]), version); | ||
validate = ajv.getSchema(version); | ||
} | ||
return validate; | ||
} | ||
/** | ||
* To validate schema of the payload we just need a small portion of official AsyncAPI spec JSON Schema, the definition of the schema must be | ||
@@ -69,3 +87,3 @@ * a main part of the JSON Schema | ||
* @private | ||
* @param {Object} asyncapiSchema AsyncAPI specification JSON Schema | ||
* @param {Object} asyncapiSchema AsyncAPI specification JSON Schema | ||
* @returns {Object} valid JSON Schema document describing format of AsyncAPI-valid schema for message payload | ||
@@ -72,0 +90,0 @@ */ |
const path = require('path'); | ||
const fetch = require('node-fetch'); | ||
const Ajv = require('ajv'); | ||
const fetch = require('node-fetch'); | ||
const asyncapi = require('@asyncapi/specs'); | ||
@@ -19,2 +19,10 @@ const $RefParser = require('@apidevtools/json-schema-ref-parser'); | ||
const ajv = new Ajv({ | ||
jsonPointers: true, | ||
allErrors: true, | ||
schemaId: 'id', | ||
logger: false, | ||
}); | ||
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); | ||
/** | ||
@@ -90,13 +98,6 @@ * @module @asyncapi/parser | ||
const ajv = new Ajv({ | ||
jsonPointers: true, | ||
allErrors: true, | ||
schemaId: 'id', | ||
logger: false, | ||
}); | ||
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); | ||
const validate = ajv.compile(asyncapi[parsedJSON.asyncapi]); | ||
const validate = getValidator(parsedJSON.asyncapi); | ||
const valid = validate(parsedJSON); | ||
const errors = validate.errors && [...validate.errors]; | ||
if (!valid) throw new ParserError({ | ||
@@ -106,3 +107,3 @@ type: 'validation-errors', | ||
parsedJSON, | ||
validationErrors: improveAjvErrors(validate.errors, asyncapiYAMLorJSON, initialFormat), | ||
validationErrors: improveAjvErrors(errors, asyncapiYAMLorJSON, initialFormat), | ||
}); | ||
@@ -181,2 +182,18 @@ | ||
/** | ||
* Creates (or reuses) a function that validates an AsyncAPI document based on the passed AsyncAPI version. | ||
* | ||
* @private | ||
* @param {Object} version AsyncAPI version. | ||
* @returns {Function} Function that validates an AsyncAPI document based on the passed AsyncAPI version. | ||
*/ | ||
function getValidator(version) { | ||
let validate = ajv.getSchema(version); | ||
if (!validate) { | ||
ajv.addSchema(asyncapi[String(version)], version); | ||
validate = ajv.getSchema(version); | ||
} | ||
return validate; | ||
} | ||
async function customDocumentOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options) { | ||
@@ -183,0 +200,0 @@ validateServerVariables(parsedJSON, asyncapiYAMLorJSON, initialFormat); |
{ | ||
"name": "@asyncapi/parser", | ||
"version": "1.11.0", | ||
"version": "1.11.1", | ||
"description": "JavaScript AsyncAPI parser.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1214868
7667