assert()
Checks for a condition. If the condition is false, outputs error messages and fails the query.
Syntax
assert(
condition,
message)
Parameters
Name | Type | Required | Description |
---|---|---|---|
condition | bool | ✓ | The conditional expression to evaluate. The condition must be evaluated to constant during the query analysis phase. |
message | string | ✓ | The message used if assertion is evaluated to false . |
Note
condition
must be evaluated to constant during the query analysis phase. In other words, it can be constructed from other expressions referencing constants, and can't be bound to row-context.
Returns
Returns true
if the condition is true
.
Raises a semantic error if the condition is evaluated to false
.
Examples
The following query defines a function checkLength()
that checks input string length, and uses assert
to validate input length parameter (checks that it's greater than zero).
let checkLength = (len:long, s:string)
{
assert(len > 0, "Length must be greater than zero") and
strlen(s) > len
};
datatable(input:string)
[
'123',
'4567'
]
| where checkLength(len=long(-1), input)
Running this query yields an error:
assert() has failed with message: 'Length must be greater than zero'
Example of running with valid len
input:
let checkLength = (len:long, s:string)
{
assert(len > 0, "Length must be greater than zero") and strlen(s) > len
};
datatable(input:string)
[
'123',
'4567'
]
| where checkLength(len=3, input)
Output
input |
---|
4567 |