Skip to content

Commit

Permalink
Merge pull request #417 from vultr/issue-416
Browse files Browse the repository at this point in the history
fix type checking of user parameters when passing in numbers and strings
  • Loading branch information
ddymko authored Dec 3, 2020
2 parents bb0bb33 + 92fbe0e commit 682b7f5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 23 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,33 @@ exports.initialize = (config) => {
// Parameters for the request are required, but none were passed in
throw new Error(`Missing parameter: ${parameter}`)
} else if (userParameter) {
if (endpointParameter.type !== 'array') {
if (endpointParameter.type === 'number') {
if (isNaN(Number(userParameter))) {
// Request requires a number but special character or alpha character was passed in
throw new Error(`Invalid parameter type: ${parameter}`)
}
} else if (
typeof userParameter !==
// eslint-disable-next-line valid-typeof
endpointParameter.type
) {
// Request parameter type does not match the parameter type that was passed in
throw new Error(`Invalid parameter type: ${parameter}`)
}
} else if (
if (
endpointParameter.type === 'array' &&
!Array.isArray(userParameter)
) {
// Request requires array but array was not passed in
throw new Error(`Invalid parameter type: ${parameter}`)
throw new Error(
`Invalid parameter type for ${parameter}, expected ${endpointParameter.type}`
)
} else if (
endpointParameter.type === 'number' &&
isNaN(Number(userParameter))
) {
// Request requires a number but special character or alpha character was passed in
throw new Error(
`Invalid parameter type for ${parameter}, expected ${endpointParameter.type}`
)
} else if (
endpointParameter.type !== 'array' &&
endpointParameter.type !== 'number' &&
typeof userParameter !==
// eslint-disable-next-line valid-typeof
endpointParameter.type
) {
// Request parameter type does not match the parameter type that was passed in
throw new Error(
`Invalid parameter type for ${parameter}, expected ${endpointParameter.type}`
)
} else {
// Parameters successfully validated
requestParameters[parameter] = userParameter
Expand Down
24 changes: 23 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,29 @@ describe('Vultr Instance', () => {
}).to.throw(Error)
})

it('requires required non-array parameters to match the specified parameter type', () => {
it('allows number parameters to be passed in as a number or string', () => {
const vultrInstance = vultr.initialize({ apiKey: config.apiKey })
expect(
typeof vultrInstance.server.create({
DCID: '1',
VPSPLANID: '202',
OSID: '127'
})
).to.equal('object')
})

it('requires number parameters to be contain only number characters', () => {
const vultrInstance = vultr.initialize({ apiKey: config.apiKey })
expect(() => {
vultrInstance.server.create({
DCID: '%',
VPSPLANID: '!@#$',
OSID: '^&*('
})
}).to.throw(Error)
})

it('requires required non-array and non-number parameters to match the specified parameter type', () => {
const vultrInstance = vultr.initialize({ apiKey: config.apiKey })
expect(() => {
vultrInstance.iso.create({ url: 1 })
Expand Down

0 comments on commit 682b7f5

Please sign in to comment.