# 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: ```json { "type_id": "key_exists", "config": { "key": "email" } } ``` **Matches:** ```json {"email": "user@example.com", "name": "John"} {"email": null, "name": "Jane"} {"email": "", "name": "Bob"} ``` **Does not match:** ```json {"name": "Alice", "phone": "555-1234"} {"user": {"name": "Charlie"}} ``` ### Nested Field Check Check if a record contains a nested field: ```json { "type_id": "key_exists", "config": { "key": "user.preferences.notifications" } } ``` **Matches:** ```json { "user": { "preferences": { "notifications": true } } } ``` **Does not match:** ```json { "user": { "preferences": { "theme": "dark" } } } ``` ### Array Element Check Check if an array element exists: ```json { "type_id": "key_exists", "config": { "key": "tags.0" } } ``` **Matches:** ```json {"tags": ["production", "critical"]} {"tags": [null]} ``` **Does not match:** ```json {"tags": []} {"labels": ["production"]} ``` ### Check Field Does NOT Exist Use the `not` parameter to match records missing a field: ```json { "type_id": "key_exists", "config": { "key": "deleted_at", "not": true } } ``` **Matches:** ```json {"id": "123", "status": "active"} {"id": "456", "archived": false} ``` **Does not match:** ```json {"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: ```json { "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: ```json { "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: ```json { "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: ```json { "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 1. **Use for presence, not value**: This condition only checks existence. To check values, use `equals` or `equals_any` instead. 2. **Handle nested gracefully**: The condition safely handles missing parent objects without errors. 3. **Consider null values**: Remember that `null` values count as "exists" - the field is present even if its value is null. 4. **Combine with other conditions**: Often used with `equals` to first check existence, then validate the value. 5. **Use `not` for absence checks**: Instead of using `nor` operator, you can use `not: true` to 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 `true` for null values - If you need to exclude nulls, combine with an `equals` condition **Nested field issues:** - Ensure all parent objects exist in the path - The condition returns `false` if any parent is missing