Skip to content

Commit 6e970c9

Browse files
dominiciampogo
authored andcommitted
sdk/node: prevent v1.x params for queries
Closes #3333 Author: Dominic Dagradi <ddagradi@gmail.com> Date: Mon Apr 16 10:21:10 2018 -0700 upstream:d7557e1015446c554ea453311b7fc9dd2367ef82
1 parent ececae6 commit 6e970c9

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/query.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (typeof (Symbol as any).asyncIterator === 'undefined') {
66
import { Client } from './client'
77
import { Page } from './page'
88
import { PageParams } from './types'
9+
import { validate } from './validate'
910

1011
/**
1112
* A query for Sequence items.
@@ -20,7 +21,11 @@ export class Query<QueryParamType> {
2021
private itemName: string,
2122
private method: string,
2223
private queryParams?: QueryParamType
23-
) {}
24+
) {
25+
if (queryParams) {
26+
validate(queryParams, 'QueryParamsSchema')
27+
}
28+
}
2429

2530
/**
2631
* Retrieve a page of results.

src/validate.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,24 @@ ajv.addSchema(
4646
},
4747
'UpdateTagsSchema'
4848
)
49+
50+
ajv.addSchema(
51+
{
52+
type: 'object',
53+
properties: {
54+
filter: { type: 'string' },
55+
filterParams: {
56+
type: 'array',
57+
items: {
58+
anyOf: [{ type: 'string' }, { type: 'number' }],
59+
},
60+
},
61+
pageSize: { type: 'number' },
62+
cursor: { type: 'string' },
63+
summary: { type: 'boolean' },
64+
groupBy: { type: 'array', items: { type: 'string' } },
65+
},
66+
additionalProperties: false,
67+
},
68+
'QueryParamsSchema'
69+
)

test/validateSchema.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,36 @@ describe('Schemas', () => {
7373
})
7474
})
7575
})
76+
77+
describe('Query', () => {
78+
it('rejects list with after', () => {
79+
try {
80+
client.accounts.list({ after: 'foo' }).all()
81+
} catch (err) {
82+
expect(err.message).to.contain("should NOT have additional properties '.after'")
83+
return
84+
}
85+
assert(false, 'should not accept `after` field')
86+
})
87+
88+
it('rejects sum with sum by', () => {
89+
try {
90+
client.actions.sum({ sumBy: ['foo'] }).all()
91+
} catch (err) {
92+
expect(err.message).to.contain("should NOT have additional properties '.sumBy'")
93+
return
94+
}
95+
assert(false, 'should not accept `sumBy` field')
96+
})
97+
98+
it('rejects list with start time', () => {
99+
try {
100+
client.transactions.list({ startTime: 0 }).all()
101+
} catch (err) {
102+
expect(err.message).to.contain("should NOT have additional properties '.startTime'")
103+
return
104+
}
105+
assert(false, 'should not accept `startTime` field')
106+
})
107+
})
76108
})

0 commit comments

Comments
 (0)