Skip to content

Commit

Permalink
Fix regression
Browse files Browse the repository at this point in the history
While trying to be type correct, coercing the value to string has the unintended side effect of allowing regexes (or any other type) as $regex arg. So instead now throwing an explciit error for that case.

Also reverted the type where it returns an Array. Apparently there are cases where it is not an array.
  • Loading branch information
pvdz committed Jun 17, 2020
1 parent 4298e55 commit 8f6dc39
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/gatsby/src/redux/run-fast-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function createFilterCacheKey(

function prepareQueryArgs(
filterFields: Array<IInputQuery> | IInputQuery = {}
): Array<IPreparedQueryArg> {
): IPreparedQueryArg {
const filters = {}
Object.keys(filterFields).forEach(key => {
const value = filterFields[key]
Expand All @@ -96,7 +96,12 @@ function prepareQueryArgs(
} else {
switch (key) {
case `regex`:
filters[`$regex`] = prepareRegex(String(value))
if (typeof value !== `string`) {
throw new Error(
`The $regex comparator is expecting the regex as a string, not an actual regex or anything else`
)
}
filters[`$regex`] = prepareRegex(value)
break
case `glob`:
filters[`$regex`] = makeRe(value)
Expand Down

0 comments on commit 8f6dc39

Please sign in to comment.