mirror of
https://github.com/directus/directus.git
synced 2026-01-22 16:07:58 -05:00
* Add Quickstart Guide * Update installation * Remove unused files * Update support/backing * Tweaks in concepts * Setup file structure for API reference 2.0 * Setup page layout for reference * Add clean-urls plugin * getting started updates * Finish authentication rest * getting started updates * Render stylus in 2 spaces * Various * Various * Finish activity docs * Add collections reference * Add extension reference * concepts updates * Fields/tweaks * Add files doc * Add revisions * concepts docs * More api reference * Finish rest api reference (finally) * initial concepts * More things * Add assets api ref * Move sections from file to assets * Add environment variables * contributing docs * Add field transforms page * Left align table headers * concept links * Add API config * Fix mobile nav * Add migrating a project * doc link fixes Co-authored-by: Ben Haynes <ben@rngr.org>
3.0 KiB
3.0 KiB
Filter Rules
Permissions, validation, and the API's
filterparameter all rely on a specific JSON structure to define their rules. This page describes the syntax for creating flat, relational, or complex filter rules.
Syntax
- Field — Any valid root field, relational field, or logical operator
- Operator — Any valid API operator prefaced with an underscore
- Value — Any valid static value, or dynamic variable
{
<field>: {
<operator>: <value>
}
}
Examples
{
"title": {
"_contains": "Directus"
}
}
{
"owner": {
"_eq": "$CURRENT_USER"
}
}
{
"datetime": {
"_lte": "$NOW"
}
}
Filter Operators
| Operator | Description |
|---|---|
_eq |
Equal to |
_neq |
Not equal to |
_lt |
Less than |
_lte |
Less than or equal to |
_gt |
Greater than |
_gte |
Greater than or equal to |
_in |
Exists in one of the values |
_nin |
Not in one of the values |
_null |
It is null |
_nnull |
It is not null |
_contains |
Contains the substring |
_ncontains |
Doesn't contain the substring |
_between |
The value is between two values |
_nbetween |
The value is not between two values |
_empty |
The value is empty (null or falsy) |
_nempty |
The value is not empty (null or falsy) |
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": [
{
"owner": {
"_eq": "$CURRENT_USER"
}
},
{
"status": {
"_in": ["published", "draft"]
}
}
]
},
{
"_and": [
{
"owner": {
"_neq": "$CURRENT_USER"
}
},
{
"status": {
"_in": ["published"]
}
}
]
}
]
}
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