Skip to content

Commit

Permalink
fix(port-data): attempt to parse limit on listDocuments and queryDocu…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
TillaTheHun0 committed May 24, 2023
1 parent 740c2c3 commit 3f0923f
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 26 deletions.
182 changes: 158 additions & 24 deletions packages/port-data/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Deno.test('data', async (t) => {
})

await t.step('listDocuments', async (t) => {
await t.step('should validate the inputs', async () => {
await t.step('should validate the inputs', async (t) => {
assert(
await adapter.listDocuments({
db: '123',
Expand All @@ -264,17 +264,79 @@ Deno.test('data', async (t) => {
})
)

await assertRejects(() =>
adapter.listDocuments({
db: 'foo',
// @ts-ignore
limit: 'bar',
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
await t.step('limit', async (t) => {
await t.step('should parse to a number, if provided', async () => {
assert(
await adapter.listDocuments({
db: '123',
limit: 1000,
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
}),
)

assert(
await adapter.listDocuments({
db: '123',
limit: '456',
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
}),
)

assert(
await adapter.listDocuments({
db: '123',
limit: ' 456 ',
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
}),
)

assert(
await adapter.listDocuments({
db: '123',
limit: undefined,
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
}),
)
})
)

await t.step('should reject the unparseable value', async () => {
await assertRejects(() =>
adapter.listDocuments({
db: 'foo',
// @ts-ignore
limit: 'not parseable',
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
})
)

await assertRejects(() =>
adapter.listDocuments({
db: 'foo',
// @ts-ignore
limit: [],
startkey: '123',
endkey: '456',
keys: '123,456',
descending: false,
})
)
})
})

await assertRejects(() =>
adapter.listDocuments({
Expand Down Expand Up @@ -339,7 +401,7 @@ Deno.test('data', async (t) => {
})

await t.step('queryDocuments', async (t) => {
await t.step('should validate the inputs', async () => {
await t.step('should validate the inputs', async (t) => {
assert(
await adapter.queryDocuments({
db: 'foo',
Expand Down Expand Up @@ -418,19 +480,91 @@ Deno.test('data', async (t) => {
})
)

await assertRejects(() =>
adapter.queryDocuments({
db: '123',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
// @ts-ignore
limit: 'wut',
use_index: 'idx-name',
},
await t.step('limit', async (t) => {
await t.step('should parse to a number, if provided', async () => {
assert(
await adapter.queryDocuments({
db: 'foo',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
limit: 1000,
use_index: 'idx-name',
},
}),
)

assert(
await adapter.queryDocuments({
db: 'foo',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
limit: '1000',
use_index: 'idx-name',
},
}),
)

assert(
await adapter.queryDocuments({
db: 'foo',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
limit: ' 1000 ',
use_index: 'idx-name',
},
}),
)

assert(
await adapter.queryDocuments({
db: 'foo',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
limit: undefined,
use_index: 'idx-name',
},
}),
)
})
)

await t.step('should reject the unparseable value', async () => {
await assertRejects(() =>
adapter.queryDocuments({
db: '123',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
// @ts-ignore
limit: 'wut',
use_index: 'idx-name',
},
})
)

await assertRejects(() =>
adapter.queryDocuments({
db: '123',
query: {
selector: { name: { $gt: 'mike' } },
fields: ['name'],
sort: ['ASC' as const],
// @ts-ignore
limit: [],
use_index: 'idx-name',
},
})
)
})
})

await assertRejects(() =>
adapter.queryDocuments({
Expand Down
23 changes: 21 additions & 2 deletions packages/port-data/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ const SortEnum = z.enum(['ASC', 'DESC'])

const doc = z.record(z.any())

const maybeNumber = z.preprocess(
(val) => {
if (typeof val === 'number') return val
/**
* Let zod do its job and reject the value
*/
if (typeof val !== 'string') return val

const parsed = parseFloat(val)
/**
* The string could not be parsed into a number
* so let zod do its job and reject the value
*/
if (isNaN(parsed)) return val
return parsed
},
z.number(),
)

export const port = z.object({
createDatabase: z
.function()
Expand Down Expand Up @@ -73,7 +92,7 @@ export const port = z.object({
.args(
z.object({
db: z.string(),
limit: z.number().optional(),
limit: maybeNumber.optional(),
startkey: z.string().optional(),
endkey: z.string().optional(),
// TODO: should we just make this an array?
Expand All @@ -92,7 +111,7 @@ export const port = z.object({
selector: z.record(z.any()).optional(),
fields: z.array(z.string()).optional(),
sort: z.array(z.union([SortEnum, z.record(SortEnum)])).optional(),
limit: z.number().optional(),
limit: maybeNumber.optional(),
use_index: z.string().optional(),
}),
}),
Expand Down

0 comments on commit 3f0923f

Please sign in to comment.