Skip to content

Commit 8dc1bbe

Browse files
authored
Merge pull request #577 from psteinroe/fix/nested-objs
fix: has paths on nested objects
2 parents 521bb5a + 7ea6fd1 commit 8dc1bbe

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

.changeset/eighty-turtles-remain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@supabase-cache-helpers/postgrest-core": patch
3+
---
4+
5+
fix: hasPaths for nested objects

packages/postgrest-core/src/postgrest-filter.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
113113
}
114114

115115
private hasPathRecursive(obj: unknown, path: string): boolean {
116+
// obj is valid if v is null, because the foreign key relation can be null
117+
if (obj === null) return true;
118+
116119
// normalise json operators "->" and "->>" to "."
117120
const pathElements = path.replace(/->>|->/g, '.').split('.');
118121

119122
// we are at the deepest level
120123
if (pathElements.length === 1) {
121-
// obj is valid if v is null, because the foreign key relation can be null
122-
if (obj === null) return true;
123-
124124
// else check if the path exists
125125
return typeof get(obj, pathElements[0]) !== 'undefined';
126126
}

packages/postgrest-core/tests/postgrest-filter.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,20 @@ describe('PostgrestFilter', () => {
520520
).toEqual(true);
521521
});
522522

523+
it('with null value for nested object', () => {
524+
expect(
525+
new PostgrestFilter({
526+
filters: [],
527+
paths: [
528+
{
529+
path: 'null_value.value.nested',
530+
declaration: 'null_value.value.nested',
531+
},
532+
],
533+
}).hasPaths(MOCK),
534+
).toEqual(true);
535+
});
536+
523537
it('with empty array', () => {
524538
expect(
525539
new PostgrestFilter({

0 commit comments

Comments
 (0)