From ca7d5180d3059133ed40c66101e53d68ac646704 Mon Sep 17 00:00:00 2001 From: suchothendav Date: Wed, 6 Jul 2016 11:09:08 -0700 Subject: [PATCH] Support for pattern #3 --- lib/generators/index.js | 7 +++++++ package.json | 1 + tests/fixture/petstore.json | 8 ++++++++ tests/param_mockgen.js | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/lib/generators/index.js b/lib/generators/index.js index f93b6a3..75ef2a7 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -158,6 +158,13 @@ function stringMock(schema) { * If `enum` is defined for the property */ 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]) { /** * If a `format` is defined for the property diff --git a/package.json b/package.json index 27c5405..1f92cd4 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "dependencies": { "chance": "^1.0.3", "moment": "^2.13.0", + "randexp": "^0.4.2", "swagger-parser": "^3.4.1" }, "devDependencies": { diff --git a/tests/fixture/petstore.json b/tests/fixture/petstore.json index df2bcb0..88a19a7 100644 --- a/tests/fixture/petstore.json +++ b/tests/fixture/petstore.json @@ -184,6 +184,14 @@ "required": true, "type": "integer", "format": "int64" + }, + { + "name": "petName", + "in": "query", + "description": "Name of pet to return", + "required": true, + "type": "string", + "pattern": "awesome+ (pet|cat|bird)" }], "responses": { "200": { diff --git a/tests/param_mockgen.js b/tests/param_mockgen.js index 4f43d64..4ff1d18 100644 --- a/tests/param_mockgen.js +++ b/tests/param_mockgen.js @@ -56,6 +56,10 @@ describe('Parameter Mock generator', function () { Assert.ok(params.path, 'Generated path parameter'); Assert.ok(params.path[0].name === 'petId', 'generated mock parameter for petId'); 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(); }); });