From 56e6b78d9d60c7da2027ad1358df72eeba4e82a1 Mon Sep 17 00:00:00 2001 From: xchunzhao Date: Thu, 25 Jul 2019 16:49:56 +0800 Subject: [PATCH 1/2] fix(enum): fix enum always return first item --- lib/index.js | 3 ++- lib/utils.js | 16 +++++++++++++++- test/index.test.js | 4 ++-- test/specs/v1.2/store.json | 4 ++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index 489c427..be28c18 100644 --- a/lib/index.js +++ b/lib/index.js @@ -64,7 +64,8 @@ function sampleFromSchema (schema) { if (schema['enum']) { if (schema['default']) return schema['default'] - return utils.normalizeArray(schema['enum'])[0] + const enumSchema = utils.normalizeArray(schema['enum']) + return utils.pickEnum(enumSchema) } if (type === 'file') { diff --git a/lib/utils.js b/lib/utils.js index 215eac2..630ce3f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -28,10 +28,24 @@ function inferSchema (thing) { return thing } +function pickEnum(thing) { + let result = "@pick(["; + const resultEnum = thing.map(item => { + if(typeof item === 'string'){ + return '"' + item + '"' + } + return item + }) + result += resultEnum.join(',') + result += "])" + return result +} + module.exports = { isObject: isObject, objectify: objectify, isFunc: isFunc, inferSchema: inferSchema, - normalizeArray: normalizeArray + normalizeArray: normalizeArray, + pickEnum: pickEnum } diff --git a/test/index.test.js b/test/index.test.js index 3dffadf..8b9d756 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -24,7 +24,7 @@ describe('index.test.js', () => { id: '@integer(60, 100)', name: '@string' }], - status: 'available' + status: '@pick(["available","pending","sold"])' } const orderSchema = { @@ -33,7 +33,7 @@ describe('index.test.js', () => { petId: '@integer(60, 100)', quantity: '@integer(60, 100)', shipDate: '@datetime', - status: 'placed' + status: '@pick(["placed","approved","delivered"])' } const userSchema = { diff --git a/test/specs/v1.2/store.json b/test/specs/v1.2/store.json index 6744082..04c40cd 100644 --- a/test/specs/v1.2/store.json +++ b/test/specs/v1.2/store.json @@ -131,8 +131,8 @@ "description": "Order Status", "enum": [ "placed", - " approved", - " delivered" + "approved", + "delivered" ] }, "shipDate": { From 362aabc6fce47876b2c5a6d317c560385089d74c Mon Sep 17 00:00:00 2001 From: xchunzhao Date: Thu, 25 Jul 2019 17:08:17 +0800 Subject: [PATCH 2/2] fix: fix enum always return first item --- lib/utils.js | 4 ++-- test/index.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 630ce3f..eab25d2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -29,7 +29,7 @@ function inferSchema (thing) { } function pickEnum(thing) { - let result = "@pick(["; + let result = "@pick("; const resultEnum = thing.map(item => { if(typeof item === 'string'){ return '"' + item + '"' @@ -37,7 +37,7 @@ function pickEnum(thing) { return item }) result += resultEnum.join(',') - result += "])" + result += ")" return result } diff --git a/test/index.test.js b/test/index.test.js index 8b9d756..e10a2a4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -24,7 +24,7 @@ describe('index.test.js', () => { id: '@integer(60, 100)', name: '@string' }], - status: '@pick(["available","pending","sold"])' + status: '@pick("available","pending","sold")' } const orderSchema = { @@ -33,7 +33,7 @@ describe('index.test.js', () => { petId: '@integer(60, 100)', quantity: '@integer(60, 100)', shipDate: '@datetime', - status: '@pick(["placed","approved","delivered"])' + status: '@pick("placed","approved","delivered")' } const userSchema = {