Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly validate enum values in eq, neq and in methods #588

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: Correctly validate enum values in eq, neq and in methods
  • Loading branch information
kamilogorek committed Dec 31, 2024
commit ae2e24406268ee50c30fa45fa6fbd8316f49d977
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I'm curios about is if we ever allowed for .eq<'number'>('id', 42) for example 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not part of the API, even though we've never explicitly disallowed it

/**
* 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