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..eab25d2 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..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: '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": {