Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagmock

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagmock - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

.travis.yml

60

lib/generators/index.js
'use strict';
var Chance = require('chance').Chance();
var Format = require('./format');
var Util = require('../util');

@@ -19,3 +20,3 @@ var Generators = module.exports = {

if (schema) {
var type = schema.type;
var type = schema.type || findType(schema);
var example = schema.examples || schema.example;

@@ -27,3 +28,3 @@ /**

mock = example;
} else if (type) {
} else {
/**

@@ -92,10 +93,21 @@ * Get the mock generator from the `type` of the schema

var opts = {};
if (schema.minimum) {
var intmock;
if (Util.isInteger(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 1;
}
if (schema.maximum) {
if (Util.isInteger(schema.maximum)) {
opts.max = (schema.exclusiveMaximum) ? schema.maximum : schema.maximum - 1;
}
return Chance.integer(opts);
//Generate a number that is multiple of schema.multipleOf
if (Util.isInteger(schema.multipleOf) && schema.multipleOf > 0) {
//Use the muplilier as the min number
opts.min = schema.multipleOf;
//Use the max/muplilier as the new max value
opts.max = (Util.isInteger(opts.max)) ? (Math.floor(opts.max/schema.multipleOf)) : opts.max;
intmock = Chance.integer(opts);
intmock = intmock * schema.multipleOf;
} else {
intmock = Chance.integer(opts);
}
return intmock;
}

@@ -110,2 +122,3 @@

var opts = {};
var nummock;
if (schema.minimum) {

@@ -117,3 +130,14 @@ opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 0.1;

}
return Chance.floating(opts);
//Generate a number that is multiple of schema.multipleOf
if (schema.multipleOf > 0) {
//Use the muplilier as the min number
opts.min = schema.multipleOf;
//Use the max/muplilier as the new max value
opts.max = (opts.max) ? opts.max/schema.multipleOf : opts.max;
nummock = Chance.floating(opts);
nummock = nummock * schema.multipleOf;
} else {
nummock = Chance.floating(opts);
}
return nummock;
}

@@ -142,2 +166,9 @@

mockStr = enumMock(schema);
} else if (schema.pattern) {
/**
* If `pattern` is defined for the property
*/
//Load only if pattern exist.
var randexp = require('randexp').randexp;
mockStr = randexp(schema.pattern);
} else if(Format[schema.format]) {

@@ -150,3 +181,4 @@ /**

mockStr = Chance.string({
length: Chance.integer(opts)
length: Chance.integer(opts),
alpha: true //Use only alpha characters
});

@@ -170,1 +202,13 @@ }

}
//Find out the type based on schema props
//(This is not a complete list or full proof solution)
function findType(schema) {
var type = 'object'// Use 'object' as the default type
if (schema.enum || schema.pattern) {
type = 'string';
} else if (schema.items) {
type = 'array';
}
return type;
}

20

lib/index.js

@@ -72,3 +72,3 @@ 'use strict';

//all the operations described under this path
//var commParams = pathObj.parameters;
var commParams = pathObj.parameters;
if (opsObj) {

@@ -78,3 +78,4 @@ //Found the operation

path: pathStr,
operation: options.operation
operation: options.operation,
commonParams: commParams
}, opsObj, mock, options);

@@ -88,3 +89,4 @@ } else {

path: pathStr,
operation: operation
operation: operation,
commonParams: commParams
}, pathObj[operation], opsMock, options);

@@ -106,3 +108,3 @@ mock[operation] = opsMock;

if (options.mockParams) {
mock.parameters = mockParameters(opsObj, options);
mock.parameters = mockParameters(resolved, opsObj, options);
}

@@ -112,3 +114,3 @@ //Mock Requests

resolved.consumes = opsObj.consumes;
mock.request = mockRequest(resolved, mock.parameters || mockParameters(opsObj, options));
mock.request = mockRequest(resolved, mock.parameters || mockParameters(resolved, opsObj, options));
}

@@ -155,6 +157,8 @@ }

*/
function mockParameters(opsObj) {
function mockParameters(resolved, opsObj) {
var mockParam = {};
var parameters = opsObj.parameters;
if (parameters && parameters.length > 0) {
var parameters = resolved.commonParams || [];
//Combine common parameters
parameters = parameters.concat(opsObj.parameters || []);
if (parameters.length > 0) {
//Iterate over each parameter

@@ -161,0 +165,0 @@ parameters.forEach(function(param) {

@@ -6,1 +6,8 @@ 'use strict';

};
module.exports.isInteger = function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
{
"name": "swagmock",
"version": "0.0.2",
"version": "0.0.3",
"description": "Mock data generator for swagger api",

@@ -8,3 +8,4 @@ "main": "lib/index.js",

"lint": "eslint lib",
"test": "mocha tests/*.js"
"test": "mocha tests/*.js",
"cover": "istanbul cover _mocha -- tests/*.js"
},

@@ -32,2 +33,3 @@ "repository": {

"moment": "^2.13.0",
"randexp": "^0.4.2",
"swagger-parser": "^3.4.1"

@@ -37,4 +39,5 @@ },

"eslint": "^2.13.0",
"istanbul": "^0.4.4",
"mocha": "^2.5.3"
}
}

@@ -187,2 +187,10 @@ {

"format": "int64"
},
{
"name": "petName",
"in": "query",
"description": "Name of pet to return",
"required": true,
"type": "string",
"pattern": "awesome+ (pet|cat|bird)"
}],

@@ -385,2 +393,3 @@ "responses": {

"minimum": 1.0,
"multipleOf": 2,
"format": "int64"

@@ -561,3 +570,12 @@ }],

}
}
},
"parameters": [
{
"name": "common",
"in": "query",
"description": "A common parameter",
"required": true,
"type": "string"
}
]
},

@@ -564,0 +582,0 @@ "/user/{username}": {

@@ -59,2 +59,6 @@ var Assert = require('assert');

Assert.ok(Number.isInteger(params.path[0].value), 'OK value for petId');
Assert.ok(params.query, 'Generated query parameter');
Assert.ok(params.query[0].name === 'petName', 'generated mock parameter for petName');
Assert.ok(/awesome+ (pet|cat|bird)/.test(params.query[0].value), 'OK value for petName');
done();

@@ -128,3 +132,2 @@ });

var params = mock.parameters;
console.log("===>", params);
Assert.ok(params, 'Generated parameters');

@@ -145,2 +148,17 @@

});
it('should generate parameter mock for path /user/logout', function(done) {
swagmock.parameters({
path: '/user/logout',
operation: 'get'
}, function(err, mock) {
Assert.ok(!err, 'No error');
Assert.ok(mock, 'Generated mock');
var params = mock.parameters;
Assert.ok(params, 'Generated parameters');
Assert.ok(params.query, 'Generated path parameter');
Assert.ok(params.query[0].name === 'common', 'generated mock parameter for common parameter');
done();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc