Skip to content

Commit e556d3f

Browse files
kamilogoreksoedirgo
authored andcommitted
fix: Correctly validate enum values in eq, neq and in methods
1 parent c511128 commit e556d3f

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/PostgrestFilterBuilder.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ export default class PostgrestFilterBuilder<
3232
RelationName = unknown,
3333
Relationships = unknown
3434
> extends PostgrestTransformBuilder<Schema, Row, Result, RelationName, Relationships> {
35-
eq<ColumnName extends string & keyof Row>(
36-
column: ColumnName,
37-
value: NonNullable<Row[ColumnName]>
38-
): this
39-
eq<Value extends unknown>(column: string, value: NonNullable<Value>): this
4035
/**
4136
* Match only rows where `column` is equal to `value`.
4237
*
@@ -45,20 +40,24 @@ export default class PostgrestFilterBuilder<
4540
* @param column - The column to filter on
4641
* @param value - The value to filter with
4742
*/
48-
eq(column: string, value: unknown): this {
43+
eq<ColumnName extends string>(
44+
column: ColumnName,
45+
value: ColumnName extends keyof Row ? NonNullable<Row[ColumnName]> : NonNullable<unknown>
46+
): this {
4947
this.url.searchParams.append(column, `eq.${value}`)
5048
return this
5149
}
5250

53-
neq<ColumnName extends string & keyof Row>(column: ColumnName, value: Row[ColumnName]): this
54-
neq(column: string, value: unknown): this
5551
/**
5652
* Match only rows where `column` is not equal to `value`.
5753
*
5854
* @param column - The column to filter on
5955
* @param value - The value to filter with
6056
*/
61-
neq(column: string, value: unknown): this {
57+
neq<ColumnName extends string>(
58+
column: ColumnName,
59+
value: ColumnName extends keyof Row ? Row[ColumnName] : unknown
60+
): this {
6261
this.url.searchParams.append(column, `neq.${value}`)
6362
return this
6463
}
@@ -227,18 +226,16 @@ export default class PostgrestFilterBuilder<
227226
return this
228227
}
229228

230-
in<ColumnName extends string & keyof Row>(
231-
column: ColumnName,
232-
values: ReadonlyArray<Row[ColumnName]>
233-
): this
234-
in(column: string, values: readonly unknown[]): this
235229
/**
236230
* Match only rows where `column` is included in the `values` array.
237231
*
238232
* @param column - The column to filter on
239233
* @param values - The values array to filter with
240234
*/
241-
in(column: string, values: readonly unknown[]): this {
235+
in<ColumnName extends string>(
236+
column: ColumnName,
237+
values: ColumnName extends keyof Row ? ReadonlyArray<Row[ColumnName]> : unknown[]
238+
): this {
242239
const cleanedValues = Array.from(new Set(values))
243240
.map((s) => {
244241
// handle postgrest reserved characters

test/index.test-d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,40 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
2121
expectError(postgrest.from('users').select().eq('username', nullableVar))
2222
}
2323

24+
// `.eq()`, '.neq()' and `.in()` validate value when column is an enum
25+
{
26+
expectError(postgrest.from('users').select().eq('status', 'invalid'))
27+
expectError(postgrest.from('users').select().neq('status', 'invalid'))
28+
expectError(postgrest.from('users').select().in('status', ['invalid']))
29+
30+
{
31+
const { data, error } = await postgrest.from('users').select('status').eq('status', 'ONLINE')
32+
if (error) {
33+
throw new Error(error.message)
34+
}
35+
expectType<{ status: Database['public']['Enums']['user_status'] | null }[]>(data)
36+
}
37+
38+
{
39+
const { data, error } = await postgrest.from('users').select('status').neq('status', 'ONLINE')
40+
if (error) {
41+
throw new Error(error.message)
42+
}
43+
expectType<{ status: Database['public']['Enums']['user_status'] | null }[]>(data)
44+
}
45+
46+
{
47+
const { data, error } = await postgrest
48+
.from('users')
49+
.select('status')
50+
.in('status', ['ONLINE', 'OFFLINE'])
51+
if (error) {
52+
throw new Error(error.message)
53+
}
54+
expectType<{ status: Database['public']['Enums']['user_status'] | null }[]>(data)
55+
}
56+
}
57+
2458
// can override result type
2559
{
2660
const { data, error } = await postgrest

0 commit comments

Comments
 (0)