domains / / Azure/Azure-Sentinel
Null Safety Patterns
When fields or parameters are optional (may be missing, null, or empty-string), code must be defensive so queries/playbooks/parsers don’t fail and so “unknown” stays unknown.
When fields or parameters are optional (may be missing, null, or empty-string), code must be defensive so queries/playbooks/parsers don’t fail and so “unknown” stays unknown.
Do this:
- Use
isempty()/isnotempty()instead ofisnull()when source can be""(empty string) as well as null.- Example:
TimeGenerated=iff(isempty(EventDate), now(), todatetime(EventDate))
- Example:
- Before casting/using optional values, guard with
isnotemptyso empty-string doesn’t coerce to a valid value (e.g.,0).- Example:
SourcePort = iff(isnotempty(SourcePort), toint(SourcePort), int(null))
- Example:
- For optional arrays/fields used in string building or JSON/table output, wrap with
coalesce(and a safe empty value).- Example:
labelsSafe = coalesce(Indicator.labels, createArray())
- Example:
- For KQL that might run against missing columns, prefer
column_ifexists(<col>, <default>)(or ensure the parser guarantees the schema used by the workbook). - For Sentinel parser/function definitions, never remove/omit default
functionParametersvalues unless the query logic can handle parameters being unspecified.
If you apply these patterns consistently, you prevent runtime failures from missing fields/columns and avoid silent data-quality issues caused by incorrect null/empty handling.