Structs: What is the difference between a "regular field" and a "required field constraint" #4287
Replies: 2 comments 7 replies
-
|
One difference that arises is with default values. You can supply a default value for a required field, but it won't be used at export time, whereas a default value for a regular value will be used. I have a hard time understanding why it's valid to specify a default value for a required field. It may have something to do with unifying several mentions of the same struct field, some of them required, some not. |
Beta Was this translation helpful? Give feedback.
-
|
@JensRantil one mental model that works is to remember to stick to required and optional fields for schemas, and only use regular fields for data and templates. And yes, a schema could use incomplete values in place of required field errors, which is partly why the CUE language only had regular and optional fields for a while. However, required fields are still very useful for good error messages and to truly distinguish missing fields from invalid/incomplete values. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am reading https://cuelang.org/docs/tour/types/structs/ and it states
and
I have been rereading these sentences over and over and I am struggling why I would use one over the other.
For example, have a look at this example. Is the only reason for using a required field contraint to get a slightly more friendly error message (that it's missing)?
Beta Was this translation helpful? Give feedback.
All reactions