Skip to content

Commit

Permalink
fix: Correctly validate enum values in eq, neq and in methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek authored and soedirgo committed Jan 6, 2025
1 parent c511128 commit e556d3f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ export default class PostgrestFilterBuilder<
RelationName = unknown,
Relationships = unknown
> extends PostgrestTransformBuilder<Schema, Row, Result, RelationName, Relationships> {
eq<ColumnName extends string & keyof Row>(
column: ColumnName,
value: NonNullable<Row[ColumnName]>
): this
eq<Value extends unknown>(column: string, value: NonNullable<Value>): this
/**
* Match only rows where `column` is equal to `value`.
*
Expand All @@ -45,20 +40,24 @@ export default class PostgrestFilterBuilder<
* @param column - The column to filter on
* @param value - The value to filter with
*/
eq(column: string, value: unknown): this {
eq<ColumnName extends string>(
column: ColumnName,
value: ColumnName extends keyof Row ? NonNullable<Row[ColumnName]> : NonNullable<unknown>
): this {
this.url.searchParams.append(column, `eq.${value}`)
return this
}

neq<ColumnName extends string & keyof Row>(column: ColumnName, value: Row[ColumnName]): this
neq(column: string, value: unknown): this
/**
* Match only rows where `column` is not equal to `value`.
*
* @param column - The column to filter on
* @param value - The value to filter with
*/
neq(column: string, value: unknown): this {
neq<ColumnName extends string>(
column: ColumnName,
value: ColumnName extends keyof Row ? Row[ColumnName] : unknown
): this {
this.url.searchParams.append(column, `neq.${value}`)
return this
}
Expand Down Expand Up @@ -227,18 +226,16 @@ export default class PostgrestFilterBuilder<
return this
}

in<ColumnName extends string & keyof Row>(
column: ColumnName,
values: ReadonlyArray<Row[ColumnName]>
): this
in(column: string, values: readonly unknown[]): this
/**
* Match only rows where `column` is included in the `values` array.
*
* @param column - The column to filter on
* @param values - The values array to filter with
*/
in(column: string, values: readonly unknown[]): this {
in<ColumnName extends string>(
column: ColumnName,
values: ColumnName extends keyof Row ? ReadonlyArray<Row[ColumnName]> : unknown[]
): this {
const cleanedValues = Array.from(new Set(values))
.map((s) => {
// handle postgrest reserved characters
Expand Down
34 changes: 34 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
expectError(postgrest.from('users').select().eq('username', nullableVar))
}

// `.eq()`, '.neq()' and `.in()` validate value when column is an enum
{
expectError(postgrest.from('users').select().eq('status', 'invalid'))
expectError(postgrest.from('users').select().neq('status', 'invalid'))
expectError(postgrest.from('users').select().in('status', ['invalid']))

{
const { data, error } = await postgrest.from('users').select('status').eq('status', 'ONLINE')
if (error) {
throw new Error(error.message)
}
expectType<{ status: Database['public']['Enums']['user_status'] | null }[]>(data)
}

{
const { data, error } = await postgrest.from('users').select('status').neq('status', 'ONLINE')
if (error) {
throw new Error(error.message)
}
expectType<{ status: Database['public']['Enums']['user_status'] | null }[]>(data)
}

{
const { data, error } = await postgrest
.from('users')
.select('status')
.in('status', ['ONLINE', 'OFFLINE'])
if (error) {
throw new Error(error.message)
}
expectType<{ status: Database['public']['Enums']['user_status'] | null }[]>(data)
}
}

// can override result type
{
const { data, error } = await postgrest
Expand Down

0 comments on commit e556d3f

Please sign in to comment.