Skip to content

fix(enum): fix enum always return first item #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
16 changes: 15 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('index.test.js', () => {
id: '@integer(60, 100)',
name: '@string'
}],
status: 'available'
status: '@pick("available","pending","sold")'
}

const orderSchema = {
Expand All @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions test/specs/v1.2/store.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@
"description": "Order Status",
"enum": [
"placed",
" approved",
" delivered"
"approved",
"delivered"
]
},
"shipDate": {
Expand Down