diff --git a/index.js b/index.js index c8a8bce..22502fc 100644 --- a/index.js +++ b/index.js @@ -23,8 +23,8 @@ module.exports = { schema.mergeAndValidate(cb); }, - getOptions: function() { - return SchemaPack.getOptions(); + getOptions: function(mode, criteria) { + return SchemaPack.getOptions(mode, criteria); }, // new API diff --git a/lib/options.js b/lib/options.js index 407b9e7..15f99c9 100644 --- a/lib/options.js +++ b/lib/options.js @@ -15,18 +15,30 @@ module.exports = { * * @param {string} [mode='document'] Describes use-case. 'document' will return an array * with all options being described. 'use' will return the default values of all options + * @param {Object} criteria Decribes required criteria for options to be returned. can have properties + * external: + * usage: (Array of supported usage type - CONVERSION, VALIDATION) * @returns {mixed} An array or object (depending on mode) that describes available options */ - getOptions: function(mode = 'document') { + getOptions: function(mode = 'document', criteria = {}) { + // Override mode & criteria if first arg is criteria (objects) + if (typeof mode === 'object') { + criteria = mode; + mode = 'document'; + } + let optsArray = [ { - name: 'Set request name source', + name: 'Naming requests', id: 'requestNameSource', type: 'enum', - default: 'fallback', - availableOptions: ['url', 'fallback'], - description: 'Option for setting source for a request name', - external: true + default: 'Fallback', + availableOptions: ['Url', 'Fallback'], + description: 'Determines how the requests inside the generated collection will be named.' + + ' If “Fallback” is selected, the request will be named after one of the following schema' + + ' values: description, operationid, url.', + external: true, + usage: ['CONVERSION'] }, { name: 'Set indent character', @@ -35,55 +47,50 @@ module.exports = { default: 'Space', availableOptions: ['Space', 'Tab'], description: 'Option for setting indentation character', - external: true + external: true, + usage: ['CONVERSION'] }, { - name: 'Toggle for collapsing folder for long routes', + name: 'Collapse redundant folders', id: 'collapseFolders', type: 'boolean', default: true, - description: 'Determines whether the importer should attempt to collapse redundant folders into one.' + - ' Folders are redundant if they have only one child element, and don\'t' + - ' have any folder-level data to persist.', - external: true + description: 'Importing will collapse all folders that have only one child element and lack ' + + 'persistent folder-level data.', + external: true, + usage: ['CONVERSION'] }, { - name: 'Set root request parameters type', + name: 'Request parameter generation', id: 'requestParametersResolution', type: 'enum', - default: 'schema', - availableOptions: ['example', 'schema'], - description: 'Determines how request parameters (query parameters, path parameters, headers, or the request' + - ' body) should be generated. Setting this to schema will cause the importer to use parameter\'s' + - ' [schema](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject)' + - ' as an indicator; example will cause the' + - ' [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject)' + - ' (if provided) to be picked up.', - external: true + default: 'Schema', + availableOptions: ['Example', 'Schema'], + description: 'Select whether to generate the request parameters based on the schema or the' + + ' examples in the schema.', + external: true, + usage: ['CONVERSION'] }, { - name: 'Set example request and response parameters type', + name: 'Response parameter generation', id: 'exampleParametersResolution', type: 'enum', - default: 'example', - availableOptions: ['example', 'schema'], - description: 'Determines how response parameters (query parameters, path parameters, headers, or the response' + - ' body) should be generated. Setting this to schema will cause the importer to use parameter\'s' + - ' [schema](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject)' + - ' as an indicator; example will cause the' + - ' [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject)' + - ' (if provided) to be picked up.', - external: true + default: 'Example', + availableOptions: ['Example', 'Schema'], + description: 'Select whether to generate the response parameters based on the schema or the' + + ' examples in the schema.', + external: true, + usage: ['CONVERSION'] }, { - name: 'Set folder strategy', + name: 'Folder organization', id: 'folderStrategy', type: 'enum', - default: 'paths', - availableOptions: ['paths', 'tags'], - description: 'Determines whether the importer should attempt to create the folders according' + - ' to paths or tags which are given in the spec.', - external: true + default: 'Paths', + availableOptions: ['Paths', 'Tags'], + description: 'Select whether to create folders according to the spec’s paths or tags.', + external: true, + usage: ['CONVERSION'] }, { name: 'Enable Schema Faking', @@ -91,15 +98,17 @@ module.exports = { type: 'boolean', default: true, description: 'Whether or not schemas should be faked.', - external: false + external: false, + usage: ['CONVERSION'] }, { name: 'Short error messages during request <> schema validation', id: 'shortValidationErrors', type: 'boolean', default: false, - description: 'Whether detailed error messages are required for request <> schema validation operations', - external: false + description: 'Whether detailed error messages are required for request <> schema validation operations.', + external: true, + usage: ['VALIDATION'] }, { name: 'Properties to ignore during validation', @@ -109,7 +118,8 @@ module.exports = { description: 'Specific properties (parts of a request/response pair) to ignore during validation.' + ' Must be sent as an array of strings. Valid inputs in the array: PATHVARIABLE, QUERYPARAM,' + ' HEADER, BODY, RESPONSE_HEADER, RESPONSE_BODY', - external: false + external: true, + usage: ['VALIDATION'] }, { name: 'Whether MISSING_IN_SCHEMA mismatches should be returned', @@ -118,7 +128,8 @@ module.exports = { default: false, description: 'MISSING_IN_SCHEMA indicates that an extra parameter was included in the request. For most ' + 'use cases, this need not be considered an error.', - external: false + external: true, + usage: ['VALIDATION'] }, { name: 'Show detailed body validation messages', @@ -126,11 +137,24 @@ module.exports = { type: 'boolean', default: false, description: 'Determines whether to show detailed mismatch information for application/json content ' + - 'in the request/response body', - external: true + 'in the request/response body.', + external: true, + usage: ['VALIDATION'] } ]; + // Filter options based on criteria + if (_.isObject(criteria)) { + typeof criteria.external === 'boolean' && (optsArray = _.filter(optsArray, { external: criteria.external })); + if (_.isArray(criteria.usage)) { + _.forEach(criteria.usage, (usageCriteria) => { + optsArray = _.filter(optsArray, (option) => { + return _.includes(option.usage, usageCriteria); + }); + }); + } + } + if (mode === 'use') { // options to be used as default kv-pairs let defOptions = {}; @@ -138,7 +162,7 @@ module.exports = { // special handling for indent character as in documentation it states `Tab` and `Space` // but for the generation mode, we need actual values if (opt.id === 'indentCharacter') { - defOptions[opt.id] = opt.default === 'Tab' ? '\t' : ' '; + defOptions[opt.id] = opt.default === 'tab' ? '\t' : ' '; } else { defOptions[opt.id] = opt.default; diff --git a/lib/schemapack.js b/lib/schemapack.js index 624f52c..2243e9a 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -82,7 +82,7 @@ class SchemaPack { // hardcoding this option - not exposed to users yet this.computedOptions.schemaFaker = true; let indentCharacter = this.computedOptions.indentCharacter; - this.computedOptions.indentCharacter = indentCharacter === 'Tab' ? '\t' : ' '; + this.computedOptions.indentCharacter = indentCharacter === 'tab' ? '\t' : ' '; this.validate(); } @@ -476,8 +476,8 @@ class SchemaPack { }, 0); } - static getOptions() { - return getOptions(); + static getOptions(mode, criteria) { + return getOptions(mode, criteria); } } diff --git a/lib/utils.js b/lib/utils.js index f4c03ff..b44065d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,5 @@ +const _ = require('lodash'); + // this will have non-OAS-related utils module.exports = { @@ -10,6 +12,11 @@ module.exports = { // set the default value to that option if the user has not defined if (userOptions[id] === undefined) { retVal[id] = defaultOptions[id].default; + + // ignore case-sensitivity for enum option with type string + if (defaultOptions[id].type === 'enum' && _.isString(retVal[id])) { + retVal[id] = _.toLower(defaultOptions[id].default); + } continue; } @@ -30,6 +37,10 @@ module.exports = { else { retVal[id] = defaultOptions[id].default; } + + // ignore case-sensitivity for string options + _.isString(retVal[id]) && (retVal[id] = _.toLower(retVal[id])); + break; case 'array': // user input needs to be parsed diff --git a/test/system/structure.test.js b/test/system/structure.test.js index f828190..958b0f1 100644 --- a/test/system/structure.test.js +++ b/test/system/structure.test.js @@ -8,72 +8,71 @@ const optionIds = [ 'folderStrategy', 'indentCharacter', 'requestNameSource', + 'shortValidationErrors', 'validationPropertiesToIgnore', 'showMissingInSchemaErrors', 'detailedBlobValidation' ], expectedOptions = { collapseFolders: { - name: 'Toggle for collapsing folder for long routes', + name: 'Collapse redundant folders', type: 'boolean', default: true, - description: 'Determines whether the importer should attempt to collapse redundant folders into one.' + - ' Folders are redundant if they have only one child element, and don\'t' + - ' have any folder-level data to persist.' + description: 'Importing will collapse all folders that have only one child element and lack ' + + 'persistent folder-level data.' }, requestParametersResolution: { - name: 'Set root request parameters type', + name: 'Request parameter generation', type: 'enum', - default: 'schema', - availableOptions: ['example', 'schema'], - description: 'Determines how request parameters (query parameters, path parameters, headers, or the request' + - ' body) should be generated. Setting this to schema will cause the importer to use parameter\'s' + - ' [schema](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject)' + - ' as an indicator; example will cause the' + - ' [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject)' + - ' (if provided) to be picked up.' + default: 'Schema', + availableOptions: ['Example', 'Schema'], + description: 'Select whether to generate the request parameters based on the schema or the' + + ' examples in the schema.' }, exampleParametersResolution: { - name: 'Set example request and response parameters type', + name: 'Response parameter generation', type: 'enum', - default: 'example', - availableOptions: ['example', 'schema'], - description: 'Determines how response parameters (query parameters, path parameters, headers, or the response' + - ' body) should be generated. Setting this to schema will cause the importer to use parameter\'s' + - ' [schema](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject)' + - ' as an indicator; example will cause the' + - ' [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject)' + - ' (if provided) to be picked up.' + default: 'Example', + availableOptions: ['Example', 'Schema'], + description: 'Select whether to generate the response parameters based on the schema or the' + + ' examples in the schema.' }, folderStrategy: { - name: 'Set folder strategy', + name: 'Folder organization', type: 'enum', - default: 'paths', - availableOptions: ['paths', 'tags'], - description: 'Determines whether the importer should attempt to create the folders according' + - ' to paths or tags which are given in the spec.' + default: 'Paths', + availableOptions: ['Paths', 'Tags'], + description: 'Select whether to create folders according to the spec’s paths or tags.' }, indentCharacter: { name: 'Set indent character', type: 'enum', - default: ' ', - availableOptions: [' ', '\t'], + default: 'Space', + availableOptions: ['Space', 'Tab'], description: 'Option for setting indentation character' }, requestNameSource: { - name: 'Set request name source', + name: 'Naming requests', type: 'enum', - default: 'fallback', - availableOptions: ['url', 'uKnown', 'fallback'], - description: 'Option for setting source for a request name' + default: 'Fallback', + availableOptions: ['Url', 'Fallback'], + description: 'Determines how the requests inside the generated collection will be named.' + + ' If “Fallback” is selected, the request will be named after one of the following schema' + + ' values: description, operationid, url.' + }, + shortValidationErrors: { + name: 'Short error messages during request <> schema validation', + type: 'boolean', + default: false, + description: 'Whether detailed error messages are required for request <> schema validation operations.' }, validationPropertiesToIgnore: { name: 'Properties to ignore during validation', type: 'array', default: [], description: 'Specific properties (parts of a request/response pair) to ignore during validation.' + - ' Must be sent as an array of strings. Valid inputs in the array: PATHVARIABLE, QUERYPARAM,' + - ' HEADER, BODY, RESPONSE_HEADER, RESPONSE_BODY' + ' Must be sent as an array of strings. Valid inputs in the array: PATHVARIABLE, QUERYPARAM,' + + ' HEADER, BODY, RESPONSE_HEADER, RESPONSE_BODY' }, showMissingInSchemaErrors: { name: 'Whether MISSING_IN_SCHEMA mismatches should be returned', @@ -88,7 +87,7 @@ const optionIds = [ type: 'boolean', default: false, description: 'Determines whether to show detailed mismatch information for application/json content ' + - 'in the request/response body' + 'in the request/response body.' } };