Skip to content

feat: allow multiple filter values, fixes #7328 #7329

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

paulroub
Copy link

@paulroub paulroub commented Nov 14, 2024

Summary

Allows a collection filter to match one of a number of values; still allows the existing single-value syntax, as well as an array of values.

Need to manage a collection that's a combination of existing categories (rather than re-categorize all those posts).

Test plan

  • Added a unit test for the array variation, existing (non-array) tests still path.
  • Tested against my collections, with single-value, single-element-array and multiple-element-array variations all working as expected.

Checklist

Please add a x inside each checkbox:

A picture of a cute animal (not mandatory but encouraged)

rp

@paulroub paulroub marked this pull request as ready for review November 14, 2024 18:36
@paulroub paulroub requested a review from a team as a code owner November 14, 2024 18:36
@martinjagodic martinjagodic linked an issue Nov 15, 2024 that may be closed by this pull request
@martinjagodic
Copy link
Member

@paulroub, thanks for your contribution. Before we review, can you please ensure the tests are passing?

@paulroub paulroub force-pushed the add-multiple-filter-values branch from e507ff3 to ed53741 Compare November 15, 2024 17:20
@paulroub
Copy link
Author

@martinjagodic Fixed the linting issues, along the way had to tell stylelint to ignore backend.ts as it was trying to parse a cast as CSS and was not happy with the result. No actual styles in this file, of course, so that should be harmless.

@paulroub paulroub force-pushed the add-multiple-filter-values branch from 74e69f2 to 7300752 Compare January 23, 2025 20:11
@paulroub
Copy link
Author

Rebased on current main, confirmed npx run test:all still succeeds.

@paulroub paulroub force-pushed the add-multiple-filter-values branch from 7300752 to e8d3ef2 Compare January 30, 2025 22:24
@martinjagodic martinjagodic requested a review from Copilot July 10, 2025 11:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds the ability for collection filters to match against multiple values by updating the value type, normalizing inputs, and enhancing the filter logic.

  • Expand FilterRule.value to accept arrays, Immutable List, or typed records
  • Normalize raw filter and field values via a new valuesAsArray helper
  • Update filterEntries to use normalized arrays and add a corresponding unit test
  • Ignore backend.ts in Stylelint to prevent lint failures on new code

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/decap-cms-core/src/types/redux.ts Broaden FilterRule.value type to support multi-value inputs
packages/decap-cms-core/src/backend.ts Implement valuesAsArray and update filterEntries for arrays
packages/decap-cms-core/src/tests/backend.spec.js Add test covering array-based filter values
.stylelintrc Include src/backend.ts in lint ignore list
Comments suppressed due to low confidence (1)

packages/decap-cms-core/src/tests/backend.spec.js:98

  • This test covers plain arrays but doesn't verify behavior when value or entry.data[field] are Immutable List or StaticallyTypedRecord<List<string>>. Add tests for those types to ensure full coverage of the new normalization logic.
    it('filters multiple values', () => {

valuesAsArray(rawValue: string | List<string> | StaticallyTypedRecord<List<string>>): string[] {
let values: string[] = [];

if ((<StaticallyTypedRecord<List<string>>>rawValue).toJS) {
Copy link
Preview

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

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

The check for toJS comes before distinguishing Immutable List instances, so Lists (which also have toJS) will be treated as records. This leads to invoking .toJS().toArray() on a plain JS array, causing a runtime error. Consider using List.isList(rawValue) first, then detect records by checking for toJS.

Suggested change
if ((<StaticallyTypedRecord<List<string>>>rawValue).toJS) {
if (List.isList(rawValue)) {
values = rawValue.toArray();
} else if ((<StaticallyTypedRecord<List<string>>>rawValue).toJS) {

Copilot uses AI. Check for mistakes.

@martinjagodic
Copy link
Member

@paulroub it's been a while, I hope this is still relevant to you.

When I try this with my test config, the collection fails to load entries - even before I tried the list of filters. See screenshot below.

I also have an idea of extending this further by adding an operator like with the condition field.

Condition example:

condition: {
  field: layout,
  value: list-filters,
  operator: '!='
}

Maybe we should combine these functions, as it's potentially the same feature on a different level.

Screenshot 2025-07-10 at 13 48 40

@paulroub
Copy link
Author

When I try this with my test config, the collection fails to load entries - even before I tried the list of filters. See screenshot below.

I'm so far unable to reproduce this -- any chance you could share your test config?

@paulroub
Copy link
Author

When I try this with my test config, the collection fails to load entries - even before I tried the list of filters. See screenshot below.

Reproduced, fixed, added a test case. We were assuming the presence of the filtered-on field on all
entries in the collection. Now just failing the match if (e.g.) we're filtering on "category" but a post doesn't have one.

I also have an idea of extending this further by adding an operator like with the condition field.

I had considered that as well, and poked at it a bit. I hadn't considered extending the syntax, that would make things much more doable.

Perhaps a follow-on? This version seems useful on its own.

@martinjagodic
Copy link
Member

Great, thanks! We can do that in a later PR, I agree. If all is ok now, we should merge this for the 3.9 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support multiple possible values in a collection filter
2 participants