Skip to content

Commit e61b5fd

Browse files
document object assertions in the language reference
1 parent e4981ff commit e61b5fd

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

doc/ref/language.html.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,23 @@ Object locals end with a comma, instead of semicolon (like fields). They are not
575575

576576
Object locals can access `self` and `super` – they are "inside the object". As a consequence of this, **object locals are not available in Field Name Expressions**, because (in general) they depend on the object being already created, which requires the field names to be already known.
577577

578+
#### Object Assertions
579+
580+
Objects may contain assertions, which are boolean expressions that are checked when any of the object's fields are evaluated.
581+
582+
```
583+
local base = {
584+
assert self.a > 0 : "a must be positive",
585+
a: self.x * self.y,
586+
};
587+
588+
base + { x: 5, y: -5 }
589+
```
590+
591+
When this expression is manifested, the assertion will be checked, and in this case fail (since `a` evaluates to `-25`), producing an error with the provided message.
592+
593+
Note that object assertions are only checked if one or more of an object's fields are evaluated. Since Jsonnet is a lazy language (see [Rationale for Lazy Semantics](/articles/design.html#lazy)), an object might be defined but never evaluated if none of its fields are needed to produce the final output, and in this case the object's assertions are not checked. Listing an object's field names (for example, using the `std.objectFields` or `std.objectFieldsAll` standard library functions) does not evaluate the field values.
594+
578595
#### Field Name Expressions
579596

580597
Field names of Jsonnet objects can be arbitrary strings and can be calculated dynamically *when the object is created*.

0 commit comments

Comments
 (0)