@@ -112,33 +112,33 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
112
112
return this . _selectFn ( obj ) ;
113
113
}
114
114
115
- private hasPathRecursive (
116
- obj : unknown ,
117
- basePath : string ,
118
- objectPath ?: string ,
119
- ) : boolean {
120
- const v = get ( obj , basePath ) ;
115
+ private hasPathRecursive ( obj : unknown , path : string ) : boolean {
116
+ // normalise json operators "->" and "->>" to "."
117
+ const pathElements = path . replace ( / - > > | - > / g, '.' ) . split ( '.' ) ;
121
118
122
- // Return early if we are not searching for a nested value and the path is valid
123
- if ( ! objectPath && typeof v !== 'undefined' ) return true ;
119
+ // we are at the deepest level
120
+ 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 ;
124
123
125
- // If we are looking for a nested value and we found an array, validate that all array elements have a value for the required path
126
- if ( objectPath && Array . isArray ( v ) ) {
127
- return v . every ( ( i ) => typeof get ( i , objectPath ) !== 'undefined' ) ;
124
+ // else check if the path exists
125
+ return typeof get ( obj , pathElements [ 0 ] ) !== 'undefined' ;
128
126
}
129
127
130
- const pathElements = basePath . replace ( / - > > | - > / g, '.' ) . split ( '.' ) ;
131
- const currentPathElement = pathElements . pop ( ) ;
132
-
133
- // Return if arrived at root level
134
- // obj is valid if v is null, because the foreign key relation can be null
135
- if ( pathElements . length === 0 ) return v === null ;
136
- // If there are levels to go up to, add current path element to object path and go up
137
- return this . hasPathRecursive (
138
- obj ,
139
- pathElements . join ( '.' ) ,
140
- [ currentPathElement , objectPath ] . filter ( Boolean ) . join ( '.' ) ,
141
- ) ;
128
+ // go deeper
129
+ const currentPathElement = pathElements . shift ( ) ;
130
+ const v = get ( obj , currentPathElement ! ) ;
131
+
132
+ // undefined means the path does not exist
133
+ if ( typeof v === 'undefined' ) return false ;
134
+
135
+ // if we have an array, check if all elements have the path
136
+ if ( Array . isArray ( v ) ) {
137
+ return v . every ( ( i ) => this . hasPathRecursive ( i , pathElements . join ( '.' ) ) ) ;
138
+ }
139
+
140
+ // if we dont have an array, continue recursively
141
+ return this . hasPathRecursive ( v , pathElements . join ( '.' ) ) ;
142
142
}
143
143
144
144
private applyFilterFn (
0 commit comments