@asyncapi/parser
Advanced tools
Comparing version 1.7.0 to 1.8.0-2021-09-release.1
@@ -47,6 +47,9 @@ const Ajv = require('ajv'); | ||
'application/vnd.aai.asyncapi;version=2.1.0', | ||
'application/vnd.aai.asyncapi;version=2.2.0', | ||
'application/vnd.aai.asyncapi+json;version=2.0.0', | ||
'application/vnd.aai.asyncapi+json;version=2.1.0', | ||
'application/vnd.aai.asyncapi+json;version=2.2.0', | ||
'application/vnd.aai.asyncapi+yaml;version=2.0.0', | ||
'application/vnd.aai.asyncapi+yaml;version=2.1.0', | ||
'application/vnd.aai.asyncapi+yaml;version=2.2.0', | ||
'application/schema;version=draft-07', | ||
@@ -53,0 +56,0 @@ 'application/schema+json;version=draft-07', |
@@ -8,3 +8,4 @@ const ParserError = require('./errors/parser-error'); | ||
parseUrlQueryParameters, | ||
setNotProvidedParams | ||
setNotProvidedParams, | ||
getUnknownServers | ||
} = require('./utils'); | ||
@@ -302,3 +303,4 @@ const validationError = 'validation-errors'; | ||
/** | ||
* Validates if parameters specified in the channel have corresponding parameters object defined and if name does not contain url parameters | ||
* Validates if parameters specified in the channel have corresponding parameters object defined and if name does not contain url parameters. | ||
* Also validates that all servers listed for this channel are declared in the top-level servers object. | ||
* | ||
@@ -318,2 +320,3 @@ * @private | ||
const invalidChannelName = new Map(); //return object for invalid channel names with query parameters | ||
const unknownServers = new Map(); //return object for server names not declared in top-level servers object | ||
@@ -324,2 +327,3 @@ chnlsMap.forEach((val, key) => { | ||
const queryParameters = parseUrlQueryParameters(key); | ||
const unknownServerNames = getUnknownServers(parsedJSON, val); | ||
@@ -341,2 +345,7 @@ //channel variable validation: fill return obeject with missing parameters | ||
} | ||
//server validatoin: fill return object with unknown server names | ||
if (unknownServerNames.length > 0) { | ||
unknownServers.set(tilde(key), unknownServerNames); | ||
} | ||
}); | ||
@@ -359,8 +368,13 @@ | ||
); | ||
const allValidationErrors = parameterValidationErrors.concat( | ||
nameValidationErrors | ||
const serverValidationErrors = groupValidationErrors( | ||
'channels', | ||
'channel is defined for servers that are not defined in the document', | ||
unknownServers, | ||
asyncapiYAMLorJSON, | ||
initialFormat | ||
); | ||
const allValidationErrors = parameterValidationErrors.concat(nameValidationErrors).concat(serverValidationErrors); | ||
//channel variable validation: throw exception if channel validation failes | ||
if (notProvidedParams.size || invalidChannelName.size) { | ||
if (notProvidedParams.size || invalidChannelName.size || unknownServers.size) { | ||
throw new ParserError({ | ||
@@ -367,0 +381,0 @@ type: validationError, |
@@ -46,2 +46,28 @@ const { createMapOfType, getMapValueOfType, mix } = require('./utils'); | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
hasServers() { | ||
return !!this._json.servers; | ||
} | ||
/** | ||
* @returns {String[]} | ||
*/ | ||
servers() { | ||
if (!this._json.servers) return []; | ||
return this._json.servers; | ||
} | ||
/** | ||
* @param {number} index - Index of the server. | ||
* @returns {String} | ||
*/ | ||
server(index) { | ||
if (!this._json.servers) return null; | ||
if (typeof index !== 'number') return null; | ||
if (index > this._json.servers.length - 1) return null; | ||
return this._json.servers[+index]; | ||
} | ||
/** | ||
* @returns {PublishOperation} | ||
@@ -48,0 +74,0 @@ */ |
@@ -0,1 +1,2 @@ | ||
const MessageTrait = require('./message-trait'); | ||
const MessageTraitable = require('./message-traitable'); | ||
@@ -28,2 +29,18 @@ const Schema = require('./schema'); | ||
/** | ||
* @returns {MessageTrait[]} | ||
*/ | ||
traits() { | ||
const traits = this._json['x-parser-original-traits'] || this._json.traits; | ||
if (!traits) return []; | ||
return traits.map(t => new MessageTrait(t)); | ||
} | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
hasTraits() { | ||
return !!this._json['x-parser-original-traits'] || !!this._json.traits; | ||
} | ||
/** | ||
* @returns {any} | ||
@@ -30,0 +47,0 @@ */ |
const OperationTraitable = require('./operation-traitable'); | ||
const Message = require('./message'); | ||
const OperationTrait = require('./operation-trait'); | ||
@@ -22,2 +23,18 @@ /** | ||
/** | ||
* @returns {OperationTrait[]} | ||
*/ | ||
traits() { | ||
const traits = this._json['x-parser-original-traits'] || this._json.traits; | ||
if (!traits) return []; | ||
return traits.map(t => new OperationTrait(t)); | ||
} | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
hasTraits() { | ||
return !!this._json['x-parser-original-traits'] || !!this._json.traits; | ||
} | ||
/** | ||
* @returns {Message[]} | ||
@@ -24,0 +41,0 @@ */ |
@@ -31,10 +31,15 @@ const path = require('path'); | ||
/** | ||
* The complete list of parse configuration options used to parse the given data. | ||
* @typedef {Object} ParserOptions | ||
* @property {String=} path - Path to the AsyncAPI document. It will be used to resolve relative references. Defaults to current working dir. | ||
* @property {Object=} parse - Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @property {Object=} resolve - Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @property {Boolean=} applyTraits - Whether to resolve and apply traits or not. Defaults to true. | ||
*/ | ||
/** | ||
* Parses and validate an AsyncAPI document from YAML or JSON. | ||
* | ||
* @param {(String | Object)} asyncapiYAMLorJSON An AsyncAPI document in JSON or YAML format. | ||
* @param {Object} [options] Configuration options. | ||
* @param {String} [options.path] Path to the AsyncAPI document. It will be used to resolve relative references. Defaults to current working dir. | ||
* @param {Object} [options.parse] Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @param {Object} [options.resolve] Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @param {Object} [options.applyTraits=true] Whether to resolve and apply traits or not. | ||
* @param {ParserOptions=} options Configuration options object {@link ParserOptions} | ||
* @returns {Promise<AsyncAPIDocument>} The parsed AsyncAPI document. | ||
@@ -121,4 +126,4 @@ */ | ||
* @param {String} url URL where the AsyncAPI document is located. | ||
* @param {Object} [fetchOptions] Configuration to pass to the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request|fetch} call. | ||
* @param {Object} [options] Configuration to pass to the {@link module:Parser#parse} method. | ||
* @param {Object=} [fetchOptions] Configuration to pass to the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request|fetch} call. | ||
* @param {ParserOptions=} [options] Configuration to pass to the {@link ParserOptions} method. | ||
* @returns {Promise<AsyncAPIDocument>} The parsed AsyncAPI document. | ||
@@ -248,3 +253,3 @@ */ | ||
* @param {String} initialFormat information of the document was originally JSON or YAML | ||
* @param {Object} options Configuration options. | ||
* @param {ParserOptions} options Configuration options. {@link ParserOptions} | ||
*/ | ||
@@ -280,3 +285,3 @@ async function customChannelsOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options) { | ||
* @param {String} initialFormat information of the document was originally JSON or YAML | ||
* @param {Object} options Configuration options. | ||
* @param {ParserOptions} options Configuration options. {@link ParserOptions} | ||
*/ | ||
@@ -283,0 +288,0 @@ async function customComponentsMsgOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options) { |
@@ -32,3 +32,3 @@ const YAML = require('js-yaml'); | ||
const child = obj.children.find(c => { | ||
const child = obj.children.find(c => { | ||
if (!c) return; | ||
@@ -48,3 +48,3 @@ | ||
if (initialFormat === 'js') return { jsonPointer: `/${keys.join('/')}` }; | ||
let node; | ||
@@ -116,3 +116,3 @@ if (initialFormat === 'yaml') { | ||
} | ||
if (typeof asyncapiYAMLorJSON !== 'string') { | ||
@@ -211,3 +211,3 @@ throw new ParserError({ | ||
return str.match(/{(.+?)}/g); | ||
return str.match(/{(.+?)}/g); | ||
}; | ||
@@ -222,4 +222,4 @@ | ||
if (typeof str !== 'string') return; | ||
return str.match(/\?((.*=.*)(&?))/g); | ||
return str.match(/\?((.*=.*)(&?))/g); | ||
}; | ||
@@ -236,3 +236,3 @@ | ||
if (!obj) return arr; | ||
return arr.filter(val => { | ||
@@ -295,2 +295,26 @@ return !obj.hasOwnProperty(val); | ||
/** | ||
* Returns an array of server names listed in a channel's servers list that are not declared in the top-level servers object. | ||
* | ||
* @param {Map} parsedJSON the parsed AsyncAPI document, with potentially a top-level map of servers (keys are server names) | ||
* @param {Object} channel the channel object for which to validate the servers list (array elements are server names) | ||
* @private | ||
*/ | ||
utils.getUnknownServers = (parsedJSON, channel) => { | ||
// servers list on channel | ||
if (!channel) return []; // no channel: no unknown servers | ||
const channelServers = channel.servers; | ||
if (!channelServers || channelServers.length === 0) return []; // no servers listed on channel: no unknown servers | ||
// top-level servers map | ||
const servers = parsedJSON.servers; | ||
if (!servers) return channelServers; // servers list on channel but no top-level servers: all servers are unknown | ||
const serversMap = new Map(Object.entries(servers)); | ||
// retain only servers listed on channel that are not defined in the top-level servers map | ||
return channelServers.filter(serverName => { | ||
return !serversMap.has(serverName); | ||
}); | ||
}; | ||
/** | ||
* returns default schema format for a given asyncapi version | ||
@@ -297,0 +321,0 @@ * |
{ | ||
"name": "@asyncapi/parser", | ||
"version": "1.7.0", | ||
"version": "1.8.0-2021-09-release.1", | ||
"description": "JavaScript AsyncAPI parser.", | ||
@@ -17,4 +17,2 @@ "main": "lib/index.js", | ||
"release": "semantic-release", | ||
"get:version": "echo $npm_package_version", | ||
"get:name": "echo $npm_package_name", | ||
"lint": "eslint --max-warnings 0 --config \".eslintrc\" \".\"", | ||
@@ -72,3 +70,3 @@ "test:lib": "nyc --silent --no-clean mocha --exclude \"test/browser_test.js\" --exclude \"test/parseFromUrl_test.js\" --recursive", | ||
"@apidevtools/json-schema-ref-parser": "^9.0.6", | ||
"@asyncapi/specs": "2.8.0", | ||
"@asyncapi/specs": "2.9.0-2021-09-release.1", | ||
"@fmvilas/pseudo-yaml-ast": "^0.3.1", | ||
@@ -94,3 +92,3 @@ "ajv": "^6.10.1", | ||
{ | ||
"name": "2021-06-release", | ||
"name": "2021-09-release", | ||
"prerelease": true | ||
@@ -97,0 +95,0 @@ } |
@@ -243,3 +243,3 @@ <h5 align="center"> | ||
<td align="center"><a href="https://github.com/aeworxet"><img src="https://avatars.githubusercontent.com/u/16149591?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Viacheslav Turovskyi</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=aeworxet" title="Tests">⚠️</a></td> | ||
<td align="center"><a href="https://github.com/KhudaDad414"><img src="https://avatars.githubusercontent.com/u/32505158?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Khuda Dad Nomani</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=KhudaDad414" title="Code">💻</a> <a href="https://github.com/asyncapi/parser-js/issues?q=author%3AKhudaDad414" title="Bug reports">🐛</a></td> | ||
<td align="center"><a href="https://github.com/KhudaDad414"><img src="https://avatars.githubusercontent.com/u/32505158?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Khuda Dad Nomani</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=KhudaDad414" title="Code">💻</a> <a href="https://github.com/asyncapi/parser-js/issues?q=author%3AKhudaDad414" title="Bug reports">🐛</a> <a href="https://github.com/asyncapi/parser-js/commits?author=KhudaDad414" title="Tests">⚠️</a></td> | ||
<td align="center"><a href="https://github.com/aayushmau5"><img src="https://avatars.githubusercontent.com/u/54525741?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aayush Kumar Sahu</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=aayushmau5" title="Tests">⚠️</a></td> | ||
@@ -250,2 +250,3 @@ </tr> | ||
<td align="center"><a href="https://github.com/vishesh13byte"><img src="https://avatars.githubusercontent.com/u/66796715?v=4?s=100" width="100px;" alt=""/><br /><sub><b>vishesh13byte</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=vishesh13byte" title="Tests">⚠️</a></td> | ||
<td align="center"><a href="https://iamdevelopergirl.github.io/Website-With-Animations/"><img src="https://avatars.githubusercontent.com/u/16351809?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elakya</b></sub></a><br /><a href="https://github.com/asyncapi/parser-js/commits?author=iamdevelopergirl" title="Code">💻</a></td> | ||
</tr> | ||
@@ -252,0 +253,0 @@ </table> |
@@ -310,2 +310,8 @@ /** | ||
hasParameters(): boolean; | ||
hasServers(): boolean; | ||
servers(): String[]; | ||
/** | ||
* @param index - Index of the server. | ||
*/ | ||
server(index: number): string; | ||
publish(): PublishOperation; | ||
@@ -677,2 +683,4 @@ subscribe(): SubscribeOperation; | ||
payload(): Schema; | ||
traits(): MessageTrait[]; | ||
hasTraits(): boolean; | ||
originalPayload(): any; | ||
@@ -783,2 +791,4 @@ originalSchemaFormat(): string; | ||
hasMultipleMessages(): boolean; | ||
traits(): OperationTrait[]; | ||
hasTraits(): boolean; | ||
messages(): Message[]; | ||
@@ -1071,25 +1081,29 @@ message(): Message; | ||
/** | ||
* Parses and validate an AsyncAPI document from YAML or JSON. | ||
* @param asyncapiYAMLorJSON - An AsyncAPI document in JSON or YAML format. | ||
* @param [options] - Configuration options. | ||
* @param [options.path] - Path to the AsyncAPI document. It will be used to resolve relative references. Defaults to current working dir. | ||
* @param [options.parse] - Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @param [options.resolve] - Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @param [options.applyTraits = true] - Whether to resolve and apply traits or not. | ||
* @returns The parsed AsyncAPI document. | ||
* The complete list of parse configuration options used to parse the given data. | ||
* @property [path] - Path to the AsyncAPI document. It will be used to resolve relative references. Defaults to current working dir. | ||
* @property [parse] - Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @property [resolve] - Options object to pass to {@link https://apidevtools.org/json-schema-ref-parser/docs/options.html|json-schema-ref-parser}. | ||
* @property [applyTraits] - Whether to resolve and apply traits or not. Defaults to true. | ||
*/ | ||
function parse(asyncapiYAMLorJSON: string | any, options?: { | ||
type ParserOptions = { | ||
path?: string; | ||
parse?: any; | ||
resolve?: any; | ||
applyTraits?: any; | ||
}): Promise<AsyncAPIDocument>; | ||
applyTraits?: boolean; | ||
}; | ||
/** | ||
* Parses and validate an AsyncAPI document from YAML or JSON. | ||
* @param asyncapiYAMLorJSON - An AsyncAPI document in JSON or YAML format. | ||
* @param [options] - Configuration options object {@link ParserOptions} | ||
* @returns The parsed AsyncAPI document. | ||
*/ | ||
function parse(asyncapiYAMLorJSON: string | any, options?: ParserOptions): Promise<AsyncAPIDocument>; | ||
/** | ||
* Fetches an AsyncAPI document from the given URL and passes its content to the `parse` method. | ||
* @param url - URL where the AsyncAPI document is located. | ||
* @param [fetchOptions] - Configuration to pass to the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request|fetch} call. | ||
* @param [options] - Configuration to pass to the {@link module:Parser#parse} method. | ||
* @param [options] - Configuration to pass to the {@link ParserOptions} method. | ||
* @returns The parsed AsyncAPI document. | ||
*/ | ||
function parseFromUrl(url: string, fetchOptions?: any, options?: any): Promise<AsyncAPIDocument>; | ||
function parseFromUrl(url: string, fetchOptions?: any, options?: ParserOptions): Promise<AsyncAPIDocument>; | ||
/** | ||
@@ -1096,0 +1110,0 @@ * Registers a new schema parser. Schema parsers are in charge of parsing and transforming payloads to AsyncAPI Schema format. |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
1199816
7418
259
355016
2
+ Added@asyncapi/specs@2.9.0-2021-09-release.1(transitive)
- Removed@asyncapi/specs@2.8.0(transitive)