feat: allow nested fields in filter rules
This commit is contained in:
parent
19d8952c5f
commit
b931d3d153
@ -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,
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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))));
|
||||
|
Loading…
x
Reference in New Issue
Block a user