diff --git a/packages/core/src/lib/util/__tests__/filter.util.spec.ts b/packages/core/src/lib/util/__tests__/filter.util.spec.ts index 88329caa..a04bd5e2 100644 --- a/packages/core/src/lib/util/__tests__/filter.util.spec.ts +++ b/packages/core/src/lib/util/__tests__/filter.util.spec.ts @@ -64,6 +64,17 @@ describe('filterEntries', () => { }, }); + const mockNestedEntry = createMockEntry({ + path: 'path/to/nested-object-field.md', + data: { + nested: { + object: { + field: 'yes', + }, + }, + }, + }); + const entries: Entry[] = [ mockEnglishEntry, mockFrenchEntry, @@ -71,6 +82,7 @@ describe('filterEntries', () => { mockUnderscoreIndexEntry, mockRandomFileNameEntry, mockTags1and4Entry, + mockNestedEntry, ]; describe('field rules', () => { @@ -219,4 +231,12 @@ describe('filterEntries', () => { ).toEqual([mockUnderscoreIndexEntry]); }); }); + + describe('nested fields', () => { + it('should filter based on multiple rules (must match all rules)', () => { + expect(filterEntries(entries, [{ field: 'nested.object.field', value: 'yes' }])).toEqual([ + mockNestedEntry, + ]); + }); + }); }); diff --git a/packages/core/src/lib/util/filter.util.ts b/packages/core/src/lib/util/filter.util.ts index dd5a60ed..9ce65e74 100644 --- a/packages/core/src/lib/util/filter.util.ts +++ b/packages/core/src/lib/util/filter.util.ts @@ -1,9 +1,10 @@ import { parse } from 'path'; +import get from 'lodash/get'; import type { Entry, FieldFilterRule, FilterRule } from '@staticcms/core/interface'; export function entryMatchesFieldRule(entry: Entry, filterRule: FieldFilterRule): boolean { - const fieldValue = entry.data?.[filterRule.field]; + const fieldValue = get(entry.data, filterRule.field); if ('pattern' in filterRule) { if (Array.isArray(fieldValue)) { return Boolean(fieldValue.find(v => new RegExp(filterRule.pattern).test(String(v))));