openapi-sampler
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -266,68 +266,28 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.OpenAPISampler = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
exports.allOfSample = allOfSample; | ||
var _traverse2 = require("./traverse"); | ||
var _traverse = require("./traverse"); | ||
var _utils = require("./utils"); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function allOfSample(into, children, options, spec, context) { | ||
var res = (0, _traverse2.traverse)(into, options, spec); | ||
var subSamples = []; | ||
var _iterator = _createForOfIteratorHelper(children), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var subSchema = _step.value; | ||
var _traverse = (0, _traverse2.traverse)(_objectSpread({ | ||
type: res.type | ||
}, subSchema), options, spec, context), | ||
type = _traverse.type, | ||
readOnly = _traverse.readOnly, | ||
writeOnly = _traverse.writeOnly, | ||
value = _traverse.value; | ||
if (res.type && type && type !== res.type) { | ||
console.warn('allOf: schemas with different types can\'t be merged'); | ||
res.type = type; | ||
} | ||
res.type = res.type || type; | ||
res.readOnly = res.readOnly || readOnly; | ||
res.writeOnly = res.writeOnly || writeOnly; | ||
if (value != null) subSamples.push(value); | ||
let res = (0, _traverse.traverse)(into, options, spec); | ||
const subSamples = []; | ||
for (let subSchema of children) { | ||
const { | ||
type, | ||
readOnly, | ||
writeOnly, | ||
value | ||
} = (0, _traverse.traverse)({ | ||
type: res.type, | ||
...subSchema | ||
}, options, spec, context); | ||
if (res.type && type && type !== res.type) { | ||
console.warn('allOf: schemas with different types can\'t be merged'); | ||
res.type = type; | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
res.type = res.type || type; | ||
res.readOnly = res.readOnly || readOnly; | ||
res.writeOnly = res.writeOnly || writeOnly; | ||
if (value != null) subSamples.push(value); | ||
} | ||
if (res.type === 'object') { | ||
res.value = _utils.mergeDeep.apply(void 0, [res.value || {}].concat(_toConsumableArray(subSamples.filter(function (sample) { | ||
return _typeof(sample) === 'object'; | ||
})))); | ||
res.value = (0, _utils.mergeDeep)(res.value || {}, ...subSamples.filter(sample => typeof sample === 'object')); | ||
return res; | ||
@@ -339,4 +299,3 @@ } else { | ||
} | ||
var lastSample = subSamples[subSamples.length - 1]; | ||
const lastSample = subSamples[subSamples.length - 1]; | ||
res.value = lastSample != null ? lastSample : res.value; | ||
@@ -354,3 +313,3 @@ return res; | ||
exports.inferType = inferType; | ||
var schemaKeywordTypes = { | ||
const schemaKeywordTypes = { | ||
multipleOf: 'number', | ||
@@ -377,3 +336,2 @@ maximum: 'number', | ||
}; | ||
function inferType(schema) { | ||
@@ -383,9 +341,6 @@ if (schema.type !== undefined) { | ||
} | ||
var keywords = Object.keys(schemaKeywordTypes); | ||
const keywords = Object.keys(schemaKeywordTypes); | ||
for (var i = 0; i < keywords.length; i++) { | ||
var keyword = keywords[i]; | ||
var type = schemaKeywordTypes[keyword]; | ||
let keyword = keywords[i]; | ||
let type = schemaKeywordTypes[keyword]; | ||
if (schema[keyword] !== undefined) { | ||
@@ -395,3 +350,2 @@ return type; | ||
} | ||
return null; | ||
@@ -410,3 +364,3 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _infer.inferType; | ||
@@ -416,40 +370,26 @@ } | ||
exports._samplers = void 0; | ||
var _traverse = require("./traverse"); | ||
var _index = require("./samplers/index"); | ||
var _infer = require("./infer"); | ||
var _samplers = {}; | ||
exports._samplers = _samplers; | ||
var defaults = { | ||
const defaults = { | ||
skipReadOnly: false, | ||
maxSampleDepth: 15 | ||
}; | ||
function sample(schema, options, spec) { | ||
var opts = Object.assign({}, defaults, options); | ||
let opts = Object.assign({}, defaults, options); | ||
(0, _traverse.clearCache)(); | ||
return (0, _traverse.traverse)(schema, opts, spec).value; | ||
} | ||
; | ||
function _registerSampler(type, sampler) { | ||
_samplers[type] = sampler; | ||
} | ||
; | ||
_registerSampler('array', _index.sampleArray); | ||
_registerSampler('boolean', _index.sampleBoolean); | ||
_registerSampler('integer', _index.sampleNumber); | ||
_registerSampler('number', _index.sampleNumber); | ||
_registerSampler('object', _index.sampleObject); | ||
_registerSampler('string', _index.sampleString); | ||
@@ -464,40 +404,28 @@ | ||
exports.sampleArray = sampleArray; | ||
var _traverse2 = require("../traverse"); | ||
function sampleArray(schema) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var spec = arguments.length > 2 ? arguments[2] : undefined; | ||
var context = arguments.length > 3 ? arguments[3] : undefined; | ||
var depth = context && context.depth || 1; | ||
var arrayLength = Math.min(schema.maxItems != undefined ? schema.maxItems : Infinity, schema.minItems || 1); // for the sake of simplicity, we're treating `contains` in a similar way to `items` | ||
var items = schema.prefixItems || schema.items || schema.contains; | ||
var _traverse = require("../traverse"); | ||
function sampleArray(schema, options = {}, spec, context) { | ||
const depth = context && context.depth || 1; | ||
let arrayLength = Math.min(schema.maxItems != undefined ? schema.maxItems : Infinity, schema.minItems || 1); | ||
// for the sake of simplicity, we're treating `contains` in a similar way to `items` | ||
const items = schema.prefixItems || schema.items || schema.contains; | ||
if (Array.isArray(items)) { | ||
arrayLength = Math.max(arrayLength, items.length); | ||
} | ||
var itemSchemaGetter = function itemSchemaGetter(itemNumber) { | ||
let itemSchemaGetter = itemNumber => { | ||
if (Array.isArray(items)) { | ||
return items[itemNumber] || {}; | ||
} | ||
return items || {}; | ||
}; | ||
var res = []; | ||
let res = []; | ||
if (!items) return res; | ||
for (var i = 0; i < arrayLength; i++) { | ||
var itemSchema = itemSchemaGetter(i); | ||
var _traverse = (0, _traverse2.traverse)(itemSchema, options, spec, { | ||
for (let i = 0; i < arrayLength; i++) { | ||
let itemSchema = itemSchemaGetter(i); | ||
let { | ||
value: sample | ||
} = (0, _traverse.traverse)(itemSchema, options, spec, { | ||
depth: depth + 1 | ||
}), | ||
sample = _traverse.value; | ||
}); | ||
res.push(sample); | ||
} | ||
return res; | ||
@@ -513,3 +441,2 @@ } | ||
exports.sampleBoolean = sampleBoolean; | ||
function sampleBoolean(schema) { | ||
@@ -527,3 +454,3 @@ return true; // let be optimistic :) | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _array.sampleArray; | ||
@@ -534,3 +461,3 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _boolean.sampleBoolean; | ||
@@ -541,3 +468,3 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _number.sampleNumber; | ||
@@ -548,3 +475,3 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _object.sampleObject; | ||
@@ -555,15 +482,10 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _string.sampleString; | ||
} | ||
}); | ||
var _array = require("./array"); | ||
var _boolean = require("./boolean"); | ||
var _number = require("./number"); | ||
var _object = require("./object"); | ||
var _string = require("./string"); | ||
@@ -578,18 +500,16 @@ | ||
exports.sampleNumber = sampleNumber; | ||
function sampleNumber(schema) { | ||
var res = 0; | ||
let res = 0; | ||
if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double')) { | ||
res = 0.1; | ||
} | ||
if (typeof schema.exclusiveMinimum === 'boolean' || typeof schema.exclusiveMaximum === 'boolean') { | ||
//legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans | ||
//legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans | ||
if (schema.maximum && schema.minimum) { | ||
res = schema.exclusiveMinimum ? Math.floor(schema.minimum) + 1 : schema.minimum; | ||
if (schema.exclusiveMaximum && res >= schema.maximum || !schema.exclusiveMaximum && res > schema.maximum) { | ||
res = (schema.maximum + schema.minimum) / 2; | ||
} | ||
return res; | ||
} | ||
if (schema.minimum) { | ||
@@ -602,3 +522,2 @@ if (schema.exclusiveMinimum) { | ||
} | ||
if (schema.maximum) { | ||
@@ -615,6 +534,4 @@ if (schema.exclusiveMaximum) { | ||
} | ||
if (schema.exclusiveMinimum) { | ||
res = Math.floor(schema.exclusiveMinimum) + 1; | ||
if (res === schema.exclusiveMaximum) { | ||
@@ -629,3 +546,2 @@ res = (res + Math.floor(schema.exclusiveMaximum) - 1) / 2; | ||
} | ||
return res; | ||
@@ -641,21 +557,13 @@ } | ||
exports.sampleObject = sampleObject; | ||
var _traverse = require("../traverse"); | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function sampleObject(schema) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var spec = arguments.length > 2 ? arguments[2] : undefined; | ||
var context = arguments.length > 3 ? arguments[3] : undefined; | ||
var res = {}; | ||
var depth = context && context.depth || 1; | ||
if (schema && _typeof(schema.properties) === 'object') { | ||
var requiredKeys = Array.isArray(schema.required) ? schema.required : []; | ||
var requiredKeyDict = requiredKeys.reduce(function (dict, key) { | ||
function sampleObject(schema, options = {}, spec, context) { | ||
let res = {}; | ||
const depth = context && context.depth || 1; | ||
if (schema && typeof schema.properties === 'object') { | ||
let requiredKeys = Array.isArray(schema.required) ? schema.required : []; | ||
let requiredKeyDict = requiredKeys.reduce((dict, key) => { | ||
dict[key] = true; | ||
return dict; | ||
}, {}); | ||
Object.keys(schema.properties).forEach(function (propertyName) { | ||
Object.keys(schema.properties).forEach(propertyName => { | ||
// skip before traverse that could be costly | ||
@@ -665,30 +573,24 @@ if (options.skipNonRequired && !requiredKeyDict.hasOwnProperty(propertyName)) { | ||
} | ||
var sample = (0, _traverse.traverse)(schema.properties[propertyName], options, spec, { | ||
propertyName: propertyName, | ||
const sample = (0, _traverse.traverse)(schema.properties[propertyName], options, spec, { | ||
propertyName, | ||
depth: depth + 1 | ||
}); | ||
if (options.skipReadOnly && sample.readOnly) { | ||
return; | ||
} | ||
if (options.skipWriteOnly && sample.writeOnly) { | ||
return; | ||
} | ||
res[propertyName] = sample.value; | ||
}); | ||
} | ||
if (schema && _typeof(schema.additionalProperties) === 'object') { | ||
var propertyName = schema.additionalProperties['x-additionalPropertiesName'] || 'property'; | ||
res["".concat(String(propertyName), "1")] = (0, _traverse.traverse)(schema.additionalProperties, options, spec, { | ||
if (schema && typeof schema.additionalProperties === 'object') { | ||
const propertyName = schema.additionalProperties['x-additionalPropertiesName'] || 'property'; | ||
res[`${String(propertyName)}1`] = (0, _traverse.traverse)(schema.additionalProperties, options, spec, { | ||
depth: depth + 1 | ||
}).value; | ||
res["".concat(String(propertyName), "2")] = (0, _traverse.traverse)(schema.additionalProperties, options, spec, { | ||
res[`${String(propertyName)}2`] = (0, _traverse.traverse)(schema.additionalProperties, options, spec, { | ||
depth: depth + 1 | ||
}).value; | ||
} | ||
return res; | ||
@@ -704,18 +606,12 @@ } | ||
exports.sampleString = sampleString; | ||
var _utils = require("../utils"); | ||
var passwordSymbols = 'qwerty!@#$%^123456'; | ||
const passwordSymbols = 'qwerty!@#$%^123456'; | ||
function emailSample() { | ||
return 'user@example.com'; | ||
} | ||
function idnEmailSample() { | ||
return 'пошта@укр.нет'; | ||
} | ||
function passwordSample(min, max) { | ||
var res = 'pa$$word'; | ||
let res = 'pa$$word'; | ||
if (min > res.length) { | ||
@@ -725,28 +621,23 @@ res += '_'; | ||
} | ||
return res; | ||
} | ||
function commonDateTimeSample(_ref) { | ||
var min = _ref.min, | ||
max = _ref.max, | ||
omitTime = _ref.omitTime, | ||
omitDate = _ref.omitDate; | ||
var res = (0, _utils.toRFCDateTime)(new Date('2019-08-24T14:15:22.123Z'), omitTime, omitDate, false); | ||
function commonDateTimeSample({ | ||
min, | ||
max, | ||
omitTime, | ||
omitDate | ||
}) { | ||
let res = (0, _utils.toRFCDateTime)(new Date('2019-08-24T14:15:22.123Z'), omitTime, omitDate, false); | ||
if (res.length < min) { | ||
console.warn("Using minLength = ".concat(min, " is incorrect with format \"date-time\"")); | ||
console.warn(`Using minLength = ${min} is incorrect with format "date-time"`); | ||
} | ||
if (max && res.length > max) { | ||
console.warn("Using maxLength = ".concat(max, " is incorrect with format \"date-time\"")); | ||
console.warn(`Using maxLength = ${max} is incorrect with format "date-time"`); | ||
} | ||
return res; | ||
} | ||
function dateTimeSample(min, max) { | ||
return commonDateTimeSample({ | ||
min: min, | ||
max: max, | ||
min, | ||
max, | ||
omitTime: false, | ||
@@ -756,7 +647,6 @@ omitDate: false | ||
} | ||
function dateSample(min, max) { | ||
return commonDateTimeSample({ | ||
min: min, | ||
max: max, | ||
min, | ||
max, | ||
omitTime: true, | ||
@@ -766,7 +656,6 @@ omitDate: false | ||
} | ||
function timeSample(min, max) { | ||
return commonDateTimeSample({ | ||
min: min, | ||
max: max, | ||
min, | ||
max, | ||
omitTime: false, | ||
@@ -776,66 +665,49 @@ omitDate: true | ||
} | ||
function defaultSample(min, max) { | ||
var res = (0, _utils.ensureMinLength)('string', min); | ||
let res = (0, _utils.ensureMinLength)('string', min); | ||
if (max && res.length > max) { | ||
res = res.substring(0, max); | ||
} | ||
return res; | ||
} | ||
function ipv4Sample() { | ||
return '192.168.0.1'; | ||
} | ||
function ipv6Sample() { | ||
return '2001:0db8:85a3:0000:0000:8a2e:0370:7334'; | ||
} | ||
function hostnameSample() { | ||
return 'example.com'; | ||
} | ||
function idnHostnameSample() { | ||
return 'приклад.укр'; | ||
} | ||
function uriSample() { | ||
return 'http://example.com'; | ||
} | ||
function uriReferenceSample() { | ||
return '../dictionary'; | ||
} | ||
function uriTemplateSample() { | ||
return 'http://example.com/{endpoint}'; | ||
} | ||
function iriSample() { | ||
return 'http://example.com/entity/1'; | ||
} | ||
function iriReferenceSample() { | ||
return '/entity/1'; | ||
} | ||
function uuidSample(_min, _max, propertyName) { | ||
return (0, _utils.uuid)(propertyName || 'id'); | ||
} | ||
function jsonPointerSample() { | ||
return '/json/pointer'; | ||
} | ||
function relativeJsonPointerSample() { | ||
return '1/relative/json/pointer'; | ||
} | ||
function regexSample() { | ||
return '/regex/'; | ||
} | ||
var stringFormats = { | ||
const stringFormats = { | ||
'email': emailSample, | ||
@@ -868,7 +740,6 @@ 'idn-email': idnEmailSample, | ||
}; | ||
function sampleString(schema, options, spec, context) { | ||
var format = schema.format || 'default'; | ||
var sampler = stringFormats[format] || defaultSample; | ||
var propertyName = context && context.propertyName; | ||
let format = schema.format || 'default'; | ||
let sampler = stringFormats[format] || defaultSample; | ||
let propertyName = context && context.propertyName; | ||
return sampler(schema.minLength | 0, schema.maxLength, propertyName); | ||
@@ -885,31 +756,11 @@ } | ||
exports.traverse = traverse; | ||
var _openapiSampler = require("./openapi-sampler"); | ||
var _allOf = require("./allOf"); | ||
var _infer = require("./infer"); | ||
var _utils = require("./utils"); | ||
var _jsonPointer = _interopRequireDefault(require("json-pointer")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var $refCache = {}; // for circular JS references we use additional array and not object as we need to compare entire schemas and not strings | ||
var seenSchemasStack = []; | ||
let $refCache = {}; | ||
// for circular JS references we use additional array and not object as we need to compare entire schemas and not strings | ||
let seenSchemasStack = []; | ||
function clearCache() { | ||
@@ -919,6 +770,4 @@ $refCache = {}; | ||
} | ||
function inferExample(schema) { | ||
var example; | ||
let example; | ||
if (schema.const !== undefined) { | ||
@@ -933,9 +782,7 @@ example = schema.const; | ||
} | ||
return example; | ||
} | ||
function tryInferExample(schema) { | ||
var example = inferExample(schema); // case when we don't infer example from schema but take from `const`, `examples`, `default` or `enum` keywords | ||
const example = inferExample(schema); | ||
// case when we don't infer example from schema but take from `const`, `examples`, `default` or `enum` keywords | ||
if (example !== undefined) { | ||
@@ -949,6 +796,4 @@ return { | ||
} | ||
return; | ||
} | ||
function traverse(schema, options, spec, context) { | ||
@@ -961,3 +806,2 @@ // checking circular JS references by checking context | ||
} | ||
if (context && context.depth > options.maxSampleDepth) { | ||
@@ -967,3 +811,2 @@ (0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
} | ||
if (schema.$ref) { | ||
@@ -973,13 +816,8 @@ if (!spec) { | ||
} | ||
var ref = decodeURIComponent(schema.$ref); | ||
let ref = decodeURIComponent(schema.$ref); | ||
if (ref.startsWith('#')) { | ||
ref = ref.substring(1); | ||
} | ||
var referenced = _jsonPointer.default.get(spec, ref); | ||
var result; | ||
const referenced = _jsonPointer.default.get(spec, ref); | ||
let result; | ||
if ($refCache[ref] !== true) { | ||
@@ -990,10 +828,8 @@ $refCache[ref] = true; | ||
} else { | ||
var referencedType = (0, _infer.inferType)(referenced); | ||
const referencedType = (0, _infer.inferType)(referenced); | ||
result = (0, _utils.getResultForCircular)(referencedType); | ||
} | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return result; | ||
} | ||
if (schema.example !== undefined) { | ||
@@ -1008,10 +844,9 @@ (0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
} | ||
if (schema.allOf !== undefined) { | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
return tryInferExample(schema) || (0, _allOf.allOfSample)(_objectSpread(_objectSpread({}, schema), {}, { | ||
return tryInferExample(schema) || (0, _allOf.allOfSample)({ | ||
...schema, | ||
allOf: undefined | ||
}), schema.allOf, options, spec, context); | ||
}, schema.allOf, options, spec, context); | ||
} | ||
if (schema.oneOf && schema.oneOf.length) { | ||
@@ -1021,6 +856,6 @@ if (schema.anyOf) { | ||
} | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); // Make sure to pass down readOnly and writeOnly annotations from the parent | ||
var firstOneOf = Object.assign({ | ||
// Make sure to pass down readOnly and writeOnly annotations from the parent | ||
const firstOneOf = Object.assign({ | ||
readOnly: schema.readOnly, | ||
@@ -1031,7 +866,7 @@ writeOnly: schema.writeOnly | ||
} | ||
if (schema.anyOf && schema.anyOf.length) { | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); // Make sure to pass down readOnly and writeOnly annotations from the parent | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
var firstAnyOf = Object.assign({ | ||
// Make sure to pass down readOnly and writeOnly annotations from the parent | ||
const firstAnyOf = Object.assign({ | ||
readOnly: schema.readOnly, | ||
@@ -1042,30 +877,23 @@ writeOnly: schema.writeOnly | ||
} | ||
if (schema.if && schema.then) { | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
var ifSchema = schema.if, | ||
then = schema.then, | ||
rest = _objectWithoutProperties(schema, ["if", "then"]); | ||
const { | ||
if: ifSchema, | ||
then, | ||
...rest | ||
} = schema; | ||
return traverse((0, _utils.mergeDeep)(rest, ifSchema, then), options, spec, context); | ||
} | ||
var example = inferExample(schema); | ||
var type = null; | ||
let example = inferExample(schema); | ||
let type = null; | ||
if (example === undefined) { | ||
example = null; | ||
type = schema.type; | ||
if (Array.isArray(type) && schema.type.length > 0) { | ||
type = schema.type[0]; | ||
} | ||
if (!type) { | ||
type = (0, _infer.inferType)(schema); | ||
} | ||
var sampler = _openapiSampler._samplers[type]; | ||
let sampler = _openapiSampler._samplers[type]; | ||
if (sampler) { | ||
@@ -1075,3 +903,2 @@ example = sampler(schema, options, spec, context); | ||
} | ||
(0, _utils.popSchemaStack)(seenSchemasStack, context); | ||
@@ -1084,23 +911,20 @@ return { | ||
}; | ||
function traverseOneOrAnyOf(schema, selectedSubSchema) { | ||
var inferred = tryInferExample(schema); | ||
const inferred = tryInferExample(schema); | ||
if (inferred !== undefined) { | ||
return inferred; | ||
} | ||
var localExample = traverse(_objectSpread(_objectSpread({}, schema), {}, { | ||
const localExample = traverse({ | ||
...schema, | ||
oneOf: undefined, | ||
anyOf: undefined | ||
}), options, spec, context); | ||
var subSchemaExample = traverse(selectedSubSchema, options, spec, context); | ||
if (_typeof(localExample.value) === 'object' && _typeof(subSchemaExample.value) === 'object') { | ||
var mergedExample = (0, _utils.mergeDeep)(localExample.value, subSchemaExample.value); | ||
return _objectSpread(_objectSpread({}, subSchemaExample), {}, { | ||
}, options, spec, context); | ||
const subSchemaExample = traverse(selectedSubSchema, options, spec, context); | ||
if (typeof localExample.value === 'object' && typeof subSchemaExample.value === 'object') { | ||
const mergedExample = (0, _utils.mergeDeep)(localExample.value, subSchemaExample.value); | ||
return { | ||
...subSchemaExample, | ||
value: mergedExample | ||
}); | ||
}; | ||
} | ||
return subSchemaExample; | ||
@@ -1122,5 +946,2 @@ } | ||
exports.popSchemaStack = popSchemaStack; | ||
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function pad(number) { | ||
@@ -1130,18 +951,12 @@ if (number < 10) { | ||
} | ||
return number; | ||
} | ||
function toRFCDateTime(date, omitTime, omitDate, milliseconds) { | ||
var res = omitDate ? '' : date.getUTCFullYear() + '-' + pad(date.getUTCMonth() + 1) + '-' + pad(date.getUTCDate()); | ||
if (!omitTime) { | ||
res += 'T' + pad(date.getUTCHours()) + ':' + pad(date.getUTCMinutes()) + ':' + pad(date.getUTCSeconds()) + (milliseconds ? '.' + (date.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) : '') + 'Z'; | ||
} | ||
return res; | ||
} | ||
; | ||
function ensureMinLength(sample, min) { | ||
@@ -1151,20 +966,10 @@ if (min > sample.length) { | ||
} | ||
return sample; | ||
} | ||
function mergeDeep() { | ||
var isObject = function isObject(obj) { | ||
return obj && _typeof(obj) === 'object'; | ||
}; | ||
for (var _len = arguments.length, objects = new Array(_len), _key = 0; _key < _len; _key++) { | ||
objects[_key] = arguments[_key]; | ||
} | ||
return objects.reduce(function (prev, obj) { | ||
Object.keys(obj || {}).forEach(function (key) { | ||
var pVal = prev[key]; | ||
var oVal = obj[key]; | ||
function mergeDeep(...objects) { | ||
const isObject = obj => obj && typeof obj === 'object'; | ||
return objects.reduce((prev, obj) => { | ||
Object.keys(obj || {}).forEach(key => { | ||
const pVal = prev[key]; | ||
const oVal = obj[key]; | ||
if (isObject(pVal) && isObject(oVal)) { | ||
@@ -1178,4 +983,5 @@ prev[key] = mergeDeep(pVal, oVal); | ||
}, Array.isArray(objects[objects.length - 1]) ? [] : {}); | ||
} // deterministic UUID sampler | ||
} | ||
// deterministic UUID sampler | ||
@@ -1185,3 +991,3 @@ function uuid(str) { | ||
var random = jsf32(hash, hash, hash, hash); | ||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { | ||
var r = random() * 16 % 16 | 0; | ||
@@ -1192,3 +998,2 @@ return (c == 'x' ? r : r & 0x3 | 0x8).toString(16); | ||
} | ||
function getResultForCircular(type) { | ||
@@ -1199,11 +1004,8 @@ return { | ||
} | ||
function popSchemaStack(seenSchemasStack, context) { | ||
if (context) seenSchemasStack.pop(); | ||
} | ||
function hashCode(str) { | ||
var hash = 0; | ||
if (str.length == 0) return hash; | ||
for (var i = 0; i < str.length; i++) { | ||
@@ -1214,6 +1016,4 @@ var char = str.charCodeAt(i); | ||
} | ||
return hash; | ||
} | ||
function jsf32(a, b, c, d) { | ||
@@ -1220,0 +1020,0 @@ return function () { |
{ | ||
"name": "openapi-sampler", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Tool for generation samples based on OpenAPI payload/response schema", | ||
@@ -5,0 +5,0 @@ "main": "dist/openapi-sampler.js", |
export function sampleNumber(schema) { | ||
let res = 0; | ||
if (typeof schema.exclusiveMinimum === 'boolean' || typeof schema.exclusiveMaximum === 'boolean') { //legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans | ||
if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double')) { | ||
res = 0.1; | ||
} | ||
if (typeof schema.exclusiveMinimum === 'boolean' || typeof schema.exclusiveMaximum === 'boolean') { //legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans | ||
if (schema.maximum && schema.minimum) { | ||
@@ -5,0 +8,0 @@ res = schema.exclusiveMinimum ? Math.floor(schema.minimum) + 1 : schema.minimum; |
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
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
54142
1482