Key Exists
Checks if a specific field exists in the record, regardless of its value.
Overview
The key_exists condition evaluates whether a record contains a specified field. It returns true if the field is present (even if the value is null, 0, false, or an empty string) and false if the field is absent.
Use Cases
- Data Validation: Ensure required fields are present before processing
- Optional Field Routing: Route records with optional fields to specialized handlers
- Schema Detection: Identify records with specific structures
- Feature Flags: Route based on the presence of feature flag fields
Configuration
| Setting | Type | Required | Description |
|---|---|---|---|
key | string | Yes | The field path to check for existence. Supports dot notation for nested fields (e.g., user.email). |
not | boolean | No | If true, inverts the condition (checks that the key does NOT exist). |
Examples
Basic Field Check
Check if a record contains an email field:
{
"type_id": "key_exists",
"config": {
"key": "email"
}
}
Matches:
{"email": "user@example.com", "name": "John"}
{"email": null, "name": "Jane"}
{"email": "", "name": "Bob"}
Does not match:
{"name": "Alice", "phone": "555-1234"}
{"user": {"name": "Charlie"}}
Nested Field Check
Check if a record contains a nested field:
{
"type_id": "key_exists",
"config": {
"key": "user.preferences.notifications"
}
}
Matches:
{
"user": {
"preferences": {
"notifications": true
}
}
}
Does not match:
{
"user": {
"preferences": {
"theme": "dark"
}
}
}
Array Element Check
Check if an array element exists:
{
"type_id": "key_exists",
"config": {
"key": "tags.0"
}
}
Matches:
{"tags": ["production", "critical"]}
{"tags": [null]}
Does not match:
{"tags": []}
{"labels": ["production"]}
Check Field Does NOT Exist
Use the not parameter to match records missing a field:
{
"type_id": "key_exists",
"config": {
"key": "deleted_at",
"not": true
}
}
Matches:
{"id": "123", "status": "active"}
{"id": "456", "archived": false}
Does not match:
{"id": "789", "deleted_at": "2024-01-15T10:00:00Z"}
{"id": "012", "deleted_at": null}
Common Patterns
Required Field Validation
Ensure all required fields exist before processing:
{
"operator": "and",
"conditions": [
{"type_id": "key_exists", "config": {"key": "id"}},
{"type_id": "key_exists", "config": {"key": "timestamp"}},
{"type_id": "key_exists", "config": {"key": "event_type"}}
]
}
Optional Enhancement Detection
Route records with optional enhancement data:
{
"operator": "or",
"conditions": [
{"type_id": "key_exists", "config": {"key": "metadata.enriched"}},
{"type_id": "key_exists", "config": {"key": "processing.enhanced"}}
]
}
Schema Version Detection
Route based on schema indicators:
{
"operator": "and",
"conditions": [
{"type_id": "key_exists", "config": {"key": "schema_version"}},
{"type_id": "key_exists", "config": {"key": "v2_fields.new_attribute"}}
]
}
Exclude Records Missing Required Data
Filter out records that don't have essential fields:
{
"operator": "and",
"conditions": [
{"type_id": "key_exists", "config": {"key": "user_id"}},
{"type_id": "key_exists", "config": {"key": "session_id", "not": true}}
]
}
This matches records that have user_id but do NOT have session_id.
Best Practices
-
Use for presence, not value: This condition only checks existence. To check values, use
equalsorequals_anyinstead. -
Handle nested gracefully: The condition safely handles missing parent objects without errors.
-
Consider null values: Remember that
nullvalues count as "exists" - the field is present even if its value is null. -
Combine with other conditions: Often used with
equalsto first check existence, then validate the value. -
Use
notfor absence checks: Instead of usingnoroperator, you can usenot: trueto check for missing fields.
Limitations
- Does not validate the type or value of the field
- Case-sensitive field matching
- Does not support wildcard patterns in the key path
Troubleshooting
Condition not matching when expected:
- Verify the exact field name and casing
- Check for typos in the key path
- Use dot notation correctly for nested fields
- Remember that array indices start at 0
False positives with null values:
- The condition returns
truefor null values - If you need to exclude nulls, combine with an
equalscondition
Nested field issues:
- Ensure all parent objects exist in the path
- The condition returns
falseif any parent is missing