Skip to content

Commit 001a02f

Browse files
committed
Ignore unresolved postman collection/environment variables using an option
1 parent 981018e commit 001a02f

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

lib/options.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ module.exports = {
163163
description: 'Whether to show mismatches for incorrect name and description of request',
164164
external: true,
165165
usage: ['VALIDATION']
166+
},
167+
{
168+
name: 'Ignore mismatch for unresolved postman variables',
169+
id: 'ignoreUnresolvedVariables',
170+
type: 'boolean',
171+
default: false,
172+
description: 'Whether to ignore mismatch thrown upon validation of unresolved collection/environment variable',
173+
external: true,
174+
usage: ['VALIDATION']
166175
}
167176
];
168177

lib/schemaUtils.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,9 @@ module.exports = {
22362236
invalidJson = false,
22372237
ajv, validate,
22382238
valueToUse = value,
2239+
isPmVariable = (value) => {
2240+
return _.isString(value) && _.startsWith(value, '{{') && _.endsWith(value, '}}');
2241+
},
22392242

22402243
// This is dereferenced schema (converted to JSON schema for validation)
22412244
schema = deref.resolveRefs(openApiSchemaObj, parameterSourceOption, components,
@@ -2257,7 +2260,14 @@ module.exports = {
22572260
// When processing a reference, schema.type could also be undefined
22582261
if (schema && schema.type) {
22592262
if (typeof schemaTypeToJsValidator[schema.type] === 'function') {
2260-
if (!schemaTypeToJsValidator[schema.type](valueToUse)) {
2263+
let isCorrectType = schemaTypeToJsValidator[schema.type](valueToUse);
2264+
2265+
// Treat unresolved postman collection/environment variable as correct type
2266+
if (options.ignoreUnresolvedVariables && !isCorrectType && isPmVariable(valueToUse)) {
2267+
isCorrectType = !isCorrectType;
2268+
}
2269+
2270+
if (!isCorrectType) {
22612271
// if type didn't match, no point checking for AJV
22622272
let reason = '',
22632273
mismatchObj;
@@ -2337,16 +2347,26 @@ module.exports = {
23372347
// input was invalid. Don't throw mismatch
23382348
}
23392349

2340-
// As OAS only supports some of Json Schema keywords, and Ajv is supporting all keywords from Draft 7
2341-
// Remove keyword currently not supported in OAS to make both compatible with each other
2350+
// Filter validation errors for following cases
23422351
filteredValidationError = _.filter(validate.errors, (validationError) => {
23432352
// for invalid `propertyNames` two error are thrown by Ajv, which include error with `pattern` keyword
23442353
if (validationError.keyword === 'pattern') {
23452354
return !_.has(validationError, 'propertyName');
23462355
}
23472356

2348-
// Following are non-supported keywords in OpenAPI spec but supported in Json schema (Ajv)
2349-
return !_.includes(['propertyNames', 'const', 'additionalItems', 'dependencies'], validationError.keyword);
2357+
// As OAS only supports some of Json Schema keywords, and Ajv is supporting all keywords from Draft 7
2358+
// Remove keyword currently not supported in OAS to make both compatible with each other
2359+
else if (_.includes(['propertyNames', 'const', 'additionalItems', 'dependencies'],
2360+
validationError.keyword)) {
2361+
return false;
2362+
}
2363+
2364+
// Ignore unresolved variables from mismatch if option is set
2365+
else if (options.ignoreUnresolvedVariables &&
2366+
isPmVariable(_.get(valueToUse, validationError.dataPath.slice(1)))) {
2367+
return false;
2368+
}
2369+
return true;
23502370
});
23512371

23522372
// sort errors based on dataPath, as this will ensure no overriding later

test/system/structure.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const optionIds = [
1313
'showMissingInSchemaErrors',
1414
'detailedBlobValidation',
1515
'suggestAvailableFixes',
16-
'validateMetadata'
16+
'validateMetadata',
17+
'ignoreUnresolvedVariables'
1718
],
1819
expectedOptions = {
1920
collapseFolders: {
@@ -106,6 +107,12 @@ const optionIds = [
106107
type: 'boolean',
107108
default: false,
108109
description: 'Whether to show mismatches for incorrect name and description of request'
110+
},
111+
ignoreUnresolvedVariables: {
112+
name: 'Ignore mismatch for postman variables',
113+
type: 'boolean',
114+
default: false,
115+
description: 'Whether to ignore mismatch thrown upon validation of collection/environment variable'
109116
}
110117
};
111118

0 commit comments

Comments
 (0)