Skip to main content

Default values and optional fields


This documentation is for an older version of Dagger, which is no longer actively maintained.

We encourage you to upgrade and refer to the documentation for the most current version.

If you cannot upgrade to the latest version, please contact us in the help forum on Discord. When contacting us, please let us know why you cannot move to the latest version. From there, our team will work with you on your use case.

When writing a Cue config, you will sometimes want to set default values in your package.

The most common way you'll encounter in our codebase is: key: type | *value:

defaultValue: string | *"foo"
defaultValue: bool | *false

You'll also encounter the *null default value, which is self explanatory:

// here, defaultValue either accepts a #PersonalDefinition, or stays null by default
defaultValue: #PersonalDefinition | *null

To test the type of defaultValue, you can directly do such assertion:

if defaultValue != "foo" | if defaultValue != false | if defaultValue != null {

if defaultValue == "foo" | if defaultValue == false | if defaultValue == null {

However, don't get confused with the optional fields. Optional fields check whether a key is concrete at the given scope in the DAG. You declare them with ? at the end of their name: foo?.

foo?: string // 1. declare foo. It remains undefined for now

foo: "bar" // 2. Now, `foo` gets concrete. The field isn't undefined anymore

To check on a field's concreteness, use the bottom value _|_:

if foo != _|_ { // if foo is not `undefined`

if foo == _|_ { // if foo is `undefined`