|
2 | 2 | * This file contains util functions that are common between versions
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -const parse = require('../parse.js'); |
| 5 | +const parse = require('../parse.js'), |
| 6 | + _ = require('lodash'), |
| 7 | + typesMap = { |
| 8 | + integer: { |
| 9 | + int32: '<integer>', |
| 10 | + int64: '<long>' |
| 11 | + }, |
| 12 | + number: { |
| 13 | + float: '<float>', |
| 14 | + double: '<double>' |
| 15 | + }, |
| 16 | + string: { |
| 17 | + byte: '<byte>', |
| 18 | + binary: '<binary>', |
| 19 | + date: '<date>', |
| 20 | + 'date-time': '<dateTime>', |
| 21 | + password: '<password>' |
| 22 | + }, |
| 23 | + boolean: '<boolean>', |
| 24 | + array: '<array>', |
| 25 | + object: '<object>' |
| 26 | + }, |
| 27 | + |
| 28 | + /* eslint-disable arrow-body-style */ |
| 29 | + schemaTypeToJsValidator = { |
| 30 | + 'string': (d) => typeof d === 'string', |
| 31 | + 'number': (d) => !isNaN(d), |
| 32 | + 'integer': (d) => !isNaN(d) && Number.isInteger(Number(d)), |
| 33 | + 'boolean': (d) => _.isBoolean(d) || d === 'true' || d === 'false', |
| 34 | + 'array': (d) => Array.isArray(d), |
| 35 | + 'object': (d) => typeof d === 'object' && !Array.isArray(d) |
| 36 | + }; |
| 37 | + |
| 38 | +/** |
| 39 | + * Remove the # character from the beginning of a schema path |
| 40 | + * @param {string} schemaPath - a defined schemaPath |
| 41 | + * @returns {string} - the schema path with # removed |
| 42 | + */ |
| 43 | +function removeSharpAndSlashFromFirstPosition(schemaPath) { |
| 44 | + return schemaPath[0] === '#' ? schemaPath.slice(2) : schemaPath; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Remove the defined word from the last position of a schema path |
| 49 | + * @param {string} schemaPath - a defined schemaPath |
| 50 | + * @param {string} word - word to remove |
| 51 | + * @returns {string} - the schema path with type removed |
| 52 | + */ |
| 53 | +function removeWordFromLastPosition(schemaPath, word) { |
| 54 | + let splittedDataPath = schemaPath.split('/'); |
| 55 | + if (splittedDataPath[splittedDataPath.length - 1] === word) { |
| 56 | + splittedDataPath.splice(-1); |
| 57 | + } |
| 58 | + return splittedDataPath.join('/'); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Checks if value is the representation of its type like: |
| 63 | + * "<integer>" |
| 64 | + * |
| 65 | + * @param {string} value - Value to check for |
| 66 | + * @param {string} type - The type in the schemna |
| 67 | + * @returns {Boolean} the value is the representation of its type |
| 68 | + */ |
| 69 | +function compareType(value, type) { |
| 70 | + return value === '<' + type + '>'; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Checks if value is the representation of its type like: |
| 75 | + * "<integer>" |
| 76 | + * Works in array types |
| 77 | + * @param {string} value - Value to check for |
| 78 | + * @param {*} types - The types in the schemna |
| 79 | + * @returns {Boolean} the value is the representation of its type |
| 80 | + */ |
| 81 | +function isTypeValueArrayCheck(value, types) { |
| 82 | + return types.find((type) => { |
| 83 | + return compareType(value, type); |
| 84 | + }) !== undefined; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Checks if value is the representation of its type like: |
| 89 | + * "<integer>" |
| 90 | + * Works in array types |
| 91 | + * @param {string} value - Value to check for |
| 92 | + * @param {*} types - The types in the schemna |
| 93 | + * @returns {Boolean} the value is the representation of its type |
| 94 | + */ |
| 95 | +function checkValueOnlyTypes(value, types) { |
| 96 | + return Array.isArray(types) ? isTypeValueArrayCheck(value, types) : compareType(value, types); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Checks if value is postman variable or not |
| 101 | + * |
| 102 | + * @param {string} type - type to look for |
| 103 | + * @param {string} format - format from schema |
| 104 | + * @returns {Boolean} postman variable or not |
| 105 | + */ |
| 106 | +function getDefaultFromTypeAndFormat(type, format) { |
| 107 | + return typesMap[type][format]; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +/** |
| 112 | + * Checks if value is the representation of its type like: |
| 113 | + * "<integer>" |
| 114 | + * Works in array types |
| 115 | + * @param {string} value - Value to check for |
| 116 | + * @param {*} types - The types in the schema |
| 117 | + * @param {*} format - format from the schema |
| 118 | + * @returns {Boolean} the value is the representation of its type |
| 119 | + */ |
| 120 | +function checkValueTypesAndFormat(value, types, format) { |
| 121 | + let typesNotInMapp = [], |
| 122 | + typesArray = Array.isArray(types) ? types : [types], |
| 123 | + found = typesArray.find((type) => { |
| 124 | + let defaultValue; |
| 125 | + if (typesMap.hasOwnProperty(type)) { |
| 126 | + defaultValue = getDefaultFromTypeAndFormat(type, format); |
| 127 | + |
| 128 | + // in case the format is a custom format (email, hostname etc.) |
| 129 | + // https://swagger.io/docs/specification/data-models/data-types/#string |
| 130 | + if (!defaultValue && format) { |
| 131 | + defaultValue = '<' + format + '>'; |
| 132 | + } |
| 133 | + } |
| 134 | + else { |
| 135 | + typesNotInMapp.push(type); |
| 136 | + } |
| 137 | + return defaultValue === value; |
| 138 | + }); |
| 139 | + |
| 140 | + if (found) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + found = typesNotInMapp.find((type) => { |
| 145 | + let defaultValue; |
| 146 | + defaultValue = '<' + type + (format ? ('-' + format) : '') + '>'; |
| 147 | + return defaultValue === value; |
| 148 | + }); |
| 149 | + |
| 150 | + return found !== undefined; |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Checks if value is equal to the defined default |
| 155 | + * Works in array types |
| 156 | + * @param {string} value - Value to check for |
| 157 | + * @param {*} definedDefault - The defined default value of the schema |
| 158 | + * @returns {Boolean} wheter value is equal to the defined or not |
| 159 | + */ |
| 160 | +function checkValueEqualsDefault(value, definedDefault) { |
| 161 | + return value === definedDefault; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Checks if value is the representation of its type like: |
| 166 | + * "<integer>" |
| 167 | + * Works in array types |
| 168 | + * @param {string} value - Value to check for |
| 169 | + * @param {*} schema - The schema portion used in validation |
| 170 | + * @returns {Boolean} the value is the representation of its type |
| 171 | + */ |
| 172 | +function isTypeValue(value, schema) { |
| 173 | + if (schema.hasOwnProperty('type') && schema.hasOwnProperty('default')) { |
| 174 | + const isDefault = checkValueEqualsDefault(value, schema.default); |
| 175 | + if (isDefault) { |
| 176 | + return true; |
| 177 | + } |
| 178 | + } |
| 179 | + if (schema.hasOwnProperty('type') && !schema.hasOwnProperty('format')) { |
| 180 | + return checkValueOnlyTypes(value, schema.type); |
| 181 | + } |
| 182 | + if (schema.hasOwnProperty('type') && schema.hasOwnProperty('format')) { |
| 183 | + return checkValueTypesAndFormat(value, schema.type, schema.format); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Checks if value is correcta according to schema |
| 189 | + * If the value should be numeric, it tries to convert and then validate |
| 190 | + * also validates if the value is a correct representation in the form of |
| 191 | + * <long> or <integer> etc for integers format 32 or format 64 |
| 192 | + * @param {string} value - Value to check for |
| 193 | + * @param {*} schema - The schema portion used in validation |
| 194 | + * @returns {Boolean} the value is the representation of its type |
| 195 | + */ |
| 196 | +function checkIsCorrectType(value, schema) { |
| 197 | + if (schema.hasOwnProperty('type') && |
| 198 | + typeof schemaTypeToJsValidator[schema.type] === 'function') { |
| 199 | + const isCorrectType = schemaTypeToJsValidator[schema.type](value); |
| 200 | + if (isCorrectType) { |
| 201 | + return true; |
| 202 | + } |
| 203 | + } |
| 204 | + return isTypeValue(value, schema); |
| 205 | +} |
6 | 206 |
|
7 | 207 | module.exports = {
|
8 | 208 |
|
@@ -54,15 +254,21 @@ module.exports = {
|
54 | 254 | isANumber = (value) => {
|
55 | 255 | return !isNaN(value);
|
56 | 256 | },
|
57 |
| - formattedElements = splittedDataPath.map((element) => { |
| 257 | + formattedElements = splittedDataPath.map((element, index) => { |
58 | 258 | if (element !== '' && isANumber(element)) {
|
59 | 259 | return `[${element}]`;
|
60 | 260 | }
|
61 |
| - return element; |
| 261 | + if (element === '' || element[0] === '.') { |
| 262 | + return element; |
| 263 | + } |
| 264 | + if (index === 0 && !initialDotIfExist) { |
| 265 | + return `${element}`; |
| 266 | + } |
| 267 | + return `.${element}`; |
62 | 268 | }),
|
63 | 269 | formattedDataPath = formattedElements.join('');
|
64 | 270 |
|
65 |
| - return `${initialDotIfExist}${formattedDataPath}`; |
| 271 | + return `${formattedDataPath}`; |
66 | 272 | },
|
67 | 273 |
|
68 | 274 | handleExclusiveMaximum: function(schema, max) {
|
@@ -101,5 +307,43 @@ module.exports = {
|
101 | 307 | }
|
102 | 308 | }
|
103 | 309 | return min;
|
| 310 | + }, |
| 311 | + |
| 312 | + /** |
| 313 | + * Removes initial "#/" from a schema path and the last "/type" segment |
| 314 | + * @param {string} schemaPath - The OAS 3.x specification specified in either YAML or JSON |
| 315 | + * @returns {string} - The schemaPath with initial #/ and last "/type" removed |
| 316 | + */ |
| 317 | + formatSchemaPathFromAJVErrorToConvertToDataPath: function (schemaPath) { |
| 318 | + return removeWordFromLastPosition(removeWordFromLastPosition(removeSharpAndSlashFromFirstPosition(schemaPath), |
| 319 | + 'type'), 'format'); |
| 320 | + }, |
| 321 | + |
| 322 | + typesMap, |
| 323 | + |
| 324 | + /** |
| 325 | + * Checks if value is the representation of its type like: |
| 326 | + * "<integer>" |
| 327 | + * Works in array types |
| 328 | + * @param {string} value - Value to check for |
| 329 | + * @param {*} schema - The schema portion used in validation |
| 330 | + * @returns {Boolean} the value is the representation of its type |
| 331 | + */ |
| 332 | + isTypeValue, |
| 333 | + |
| 334 | + /** |
| 335 | + * Checks if value is correcta according to schema |
| 336 | + * If the value should be numeric, it tries to convert and then validate |
| 337 | + * also validates if the value is a correct representation in the form of |
| 338 | + * <long> or <integer> etc for integers format 32 or format 64 |
| 339 | + * @param {string} value - Value to check for |
| 340 | + * @param {*} schema - The schema portion used in validation |
| 341 | + * @returns {Boolean} the value is the representation of its type |
| 342 | + */ |
| 343 | + checkIsCorrectType, |
| 344 | + |
| 345 | + |
| 346 | + isKnownType: function(schema) { |
| 347 | + return typeof schemaTypeToJsValidator[schema.type] === 'function'; |
104 | 348 | }
|
105 | 349 | };
|
0 commit comments