Skip to main content

@check

The @check attribute adds validations without raising exceptions if they fail. Results can be inspected at runtime.

Field Check​

class Foo {
bar int @check(less_than_zero, {{ this < 0 }})
}

Block Check​

class Bar {
baz int
quux string
@@check(quux_limit, {{ this.quux|length < this.baz }})
}

Syntax​

@check(name, {{ jinja_expression }})
ComponentDescription
nameIdentifier for the check
jinja_expressionExpression that returns true/false

Benefits​

  • Non-Intrusive: Validation without interrupting data flow
  • Runtime Inspection: Check results accessible in code
  • Multiple Checks: Stack multiple checks on a field

Example with Multiple Checks​

class Student {
first_name string @assert({{ this|length > 0 }})
last_name string @assert({{ this|length > 0 }})
age int @check(old_enough, {{ this > 5 }}) @check(u8, {{ this|abs < 255 }})
concentration string
@@check(age_threshold, {{ this.concentration != "calculus" or this.age > 12 }})
}

The this Keyword​

  • In field attributes: this refers to the field value
  • In block attributes: this refers to the entire object

See Also​