-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add server variables and channel parameters validation (#91)
- Loading branch information
Showing
7 changed files
with
972 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
.vscode | ||
.nyc_output | ||
coverage | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const ParserError = require('./errors/parser-error'); | ||
const { parseUrlVariables, getMissingProps, groupValidationErrors } = require('./utils'); | ||
|
||
function validateServerVariables(parsedJSON, asyncapiYAMLorJSON, initialFormat) { | ||
const srvs = parsedJSON.servers; | ||
if (!srvs) return true; | ||
|
||
const srvsMap = new Map(Object.entries(srvs)); | ||
const notProvidedVariables = new Map(); | ||
|
||
srvsMap.forEach((val, key) => { | ||
const variables = parseUrlVariables(val.url); | ||
const notProvidedServerVars = notProvidedVariables.get(key); | ||
if (!variables) return; | ||
|
||
const missingServerVariables = getMissingProps(variables, val.variables); | ||
if (!missingServerVariables.length) return; | ||
|
||
notProvidedVariables.set(key, | ||
notProvidedServerVars | ||
? notProvidedServerVars.concat(missingServerVariables) | ||
: missingServerVariables); | ||
}); | ||
|
||
if (notProvidedVariables.size > 0) { | ||
throw new ParserError({ | ||
type: 'validation-errors', | ||
title: 'Not all server variables are described with variable object', | ||
parsedJSON, | ||
validationErrors: groupValidationErrors('/servers/', 'server does not have a corresponding variable object for', notProvidedVariables, asyncapiYAMLorJSON, initialFormat) | ||
}); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function validateChannelParams(parsedJSON, asyncapiYAMLorJSON, initialFormat) { | ||
const chnls = parsedJSON.channels; | ||
if (!chnls) return true; | ||
|
||
const chnlsMap = new Map(Object.entries(chnls)); | ||
const notProvidedParams = new Map(); | ||
|
||
chnlsMap.forEach((val, key) => { | ||
const variables = parseUrlVariables(key); | ||
const notProvidedChannelParams = notProvidedParams.get(key); | ||
if (!variables) return; | ||
|
||
const missingChannelParams = getMissingProps(variables, val.parameters); | ||
|
||
if (!missingChannelParams.length) return; | ||
|
||
notProvidedParams.set(key, | ||
notProvidedChannelParams | ||
? notProvidedChannelParams.concat(missingChannelParams) | ||
: missingChannelParams); | ||
}); | ||
|
||
if (notProvidedParams.size > 0) { | ||
throw new ParserError({ | ||
type: 'validation-errors', | ||
title: 'Not all channel parameters are described with parameter object', | ||
parsedJSON, | ||
validationErrors: groupValidationErrors('/channels/', 'channel does not have a corresponding parameter object for', notProvidedParams, asyncapiYAMLorJSON, initialFormat) | ||
}); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
validateChannelParams, | ||
validateServerVariables | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.