Files
directus/docs/configuration/filter-rules.md
Nitwel 3e1fce8968 Relational Interfaces Rework 🐰🕳️ (#12082)
* add basic util function for later use

* sketch up useRelationMultiple implementation

* update relation util and start reworking relational interfaces

* support filter in m2o

* start working on useRelationMultiple

* continue working on relational foundation

* finish basic use-relation-multiple composable

* get o2m pretty close to being finished

* finish up list-o2m

* replace old interfaces

* copy existing interface

* rewrite vue file

* highlight deleted items

* use Search instead of Get

* support selection on m2m

* finish up m2m

* replace old files

* update files interface

* fix permission checks

* fix styling

* add sketches to explain relations

* rewrite m2a interface

* add usage hint to use-relation-multiple

* update file interface to use new composable

* update file-image interface

* fix image not being shown

* revert names

* fix selection of existing items

* fix loading selected items

* in between commit

* update translations interface

* try using composable recursive

* try linear approach on value modeling

* finish rewriting list-o2m-tree-view interface

* revert api changes

* fix sorting for list-o2m-tree-view

* fix selected items in created array

* Add direct download option to files interface

* Fix linter warnings

* Weird that it's being difficult, but ok

* Cast existingItemCount to number at all times

* fix page gets set below 1

* align pagination to the right

* highlight deselecting row

* show min 1 skeleton loader

* only filter selected when item exists

* fix working on new items

* Fix linter warning

* fix deselecting selected items

* show different icon depending if icon is local

* add changes from #12611

* Add _some vs _none support to o2m

* finish filtering out selected items

* Use get instead of search request

* fix save and stay

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>

* fix adding items to m2o

* Fix linter warning

* Handle no-type better

* Clean up axios usage

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
2022-04-14 22:57:17 +02:00

6.3 KiB

Filter Rules

Permissions, validation, and the API's filter parameter all rely on a specific JSON structure to define their rules. This page describes the syntax for creating flat, relational, or complex filter rules.

toc

Syntax

{
	<field>: {
		<operator>: <value>
	}
}

Examples

{
	"title": {
		"_contains": "Directus"
	}
}
{
	"owner": {
		"_eq": "$CURRENT_USER"
	}
}
{
	"datetime": {
		"_lte": "$NOW"
	}
}

Filter Operators

Operator Title (in app) Operator Description
Equals _eq Equal to
Doesn't equal _neq Not equal to
Less than _lt Less than
Less than or equal to _lte Less than or equal to
Greater than _gt Greater than
Greater than or equal to _gte Greater than or equal to
Is one of _in Matches any of the values
Is not one of _nin Doesn't match any of the values
Is null _null Is null
Isn't null _nnull Is not null
Contains _contains Contains the substring
Doesn't contain _ncontains Doesn't contain the substring
Starts with _starts_with Starts with
Doesn't start with _nstarts_with Doesn't start with
Ends with _ends_with Ends with
Doesn't end with _nends_with Doesn't end with
Is between _between Is between two values (inclusive)
Isn't between _nbetween Is not between two values (inclusive)
Is empty _empty Is empty (null or falsy)
Isn't empty _nempty Is not empty (null or falsy)
Intersects _intersects [1] Value intersects a given point
Doesn't intersect _nintersects [1] Value does not intersect a given point
Intersects Bounding box _intersects_bbox [1] Value is in a bounding box
Doesn't intersect bounding box _nintersects_bbox [1] Value is not in a bounding box

The following operator has no Title on the Filter Interface as it is only available in validation permissions:

Operator Description
_regex [2] Field has to match regex

[1] Only available on Geometry types.
[2] JavaScript "flavor" regex. Make sure to escape backslashes.

Relational

You can target related values by nesting field names. For example, if you have a relational Many-to-One author field, you can set a rule for the author.name field using the following syntax.

{
	"author": {
		"name": {
			"_eq": "Rijk van Zanten"
		}
	}
}

Logical Operators

You can nest or group multiple rules using the _and or _or logical operators. Each operator holds an array of rules, allowing for more complex filtering.

{
	"_or": [
		{
			"_and": [
				{
					"user_created": {
						"_eq": "$CURRENT_USER"
					}
				},
				{
					"status": {
						"_in": ["published", "draft"]
					}
				}
			]
		},
		{
			"_and": [
				{
					"user_created": {
						"_neq": "$CURRENT_USER"
					}
				},
				{
					"status": {
						"_in": ["published"]
					}
				}
			]
		}
	]
}

Some vs None in One-to-Many

When applying filters to a one-to-many field, Directus will default to a "some" search, for example in:

{
	"categories": {
		"name": {
			"_eq": "Recipe"
		}
	}
}

the top level parent will be returned if one of the categories has the name Recipe. This behavior can be overridden by using the explicit _some and _none operators, for example:

{
	"categories": {
		"_none": {
			"name": {
				"_eq": "Recipe"
			}
		}
	}
}

will fetch all parent items that don't have the category "Recipe"

Dynamic Variables

In addition to static values, you can also filter against dynamic values using the following variables.

  • $CURRENT_USER — The primary key of the currently authenticated user
  • $CURRENT_ROLE — The primary key of the role for the currently authenticated user
  • $NOW — The current timestamp
  • $NOW(<adjustment>) - The current timestamp plus/minus a given distance, for example $NOW(-1 year), $NOW(+2 hours)

::: tip Nested User / Role variables in Permissions

When configuring permissions, $CURRENT_USER and $CURRENT_ROLE allow you to specify any (nested) field under the current user/role as well as the root ID. For example: $CURRENT_ROLE.name or $CURRENT_USER.avatar.filesize. This includes custom fields that were added to the directus_users/directus_roles tables.

Note: This feature is only available for permissions, validation, and presets. Regular filters and conditional fields currently only support the root ID.

:::