Files
directus/api/tests/utils/calculate-field-depth.test.ts
ian 443d3f6734 Add depth limit to filtering (#11845)
* Add depth limit to filtering

* Add depth limit to GraphQL

* Add docs

* Rename environment variable

* Add simple deep filter depth calculation

* Update error message

* Shift fields depth check to base function

* Remove unused var

* Implement GraphQL filter depth

* Add check for _and & _or filters in GraphQL

* Add check for _and & _or filters in REST

* Remove commented code

* Add check for REST filter query

* Add REST tests

* Setup m2m using directus fields

* Add GraphQL tests

* Fix linter error

* Cleanup calculateDepth + add docs/tests

* Remove validator in GraphQL

* Add depth checking for nested sort

* Enable source map to display correct error lines

* Set max relational depth to be at least 2

* Update tests

* Add unit test for deep _sort

* Add minimum value in docs

* Refactor depth validation to be in validateQuery

* Add boolean parameter for calculation of _sort in deep query

* Use array of keys to parse dot notation

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
2022-06-15 11:52:54 -04:00

90 lines
1.3 KiB
TypeScript

import { calculateFieldDepth } from '../../src/utils/calculate-field-depth';
test('Calculates basic depth', () => {
const filter = {
name: {
_eq: 'test',
},
};
const result = calculateFieldDepth(filter);
expect(result).toBe(1);
});
test('Calculates relational depth', () => {
const filter = {
author: {
name: {
_eq: 'test',
},
},
};
const result = calculateFieldDepth(filter);
expect(result).toBe(2);
});
test('Ignores _and/_or', () => {
const filter = {
_and: [
{
_or: [
{
author: {
name: {
_eq: 'Directus',
},
},
},
{
status: {
_eq: 'published',
},
},
],
},
{
category: {
_eq: 'recipes',
},
},
],
};
const result = calculateFieldDepth(filter);
expect(result).toBe(2);
});
test('Skips underscore prefix in tree', () => {
const deep = {
translations: {
_filter: {
language_id: {
code: {
_eq: 'nl-NL',
},
},
},
},
};
const result = calculateFieldDepth(deep);
expect(result).toBe(3);
});
test('Calculates _sort in deep correctly', () => {
const deep = {
articles: {
_sort: ['sort', 'category.type.sort'],
},
};
const result = calculateFieldDepth(deep, ['_sort']);
expect(result).toBe(4);
});