Files
directus/docs/reference/filter-rules.md
2020-10-15 09:23:43 -04:00

2.0 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.

Syntax

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

Examples

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

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