The ability to require that a struct field contains a certain value #3712
Replies: 10 comments 1 reply
-
I think this is already well supported by cue Try;
Probably, unifying a definition - #contains2 will close your field struct, so instead you may want to use an embedding
I think there is a users group to get support for questions you may have. Im looking for it now myself... |
Beta Was this translation helpful? Give feedback.
-
For your reference - support/usage questions can go in the github discussions. That will be best as it will not clutter the issue log. |
Beta Was this translation helpful? Give feedback.
-
Indeed, try Discord or Slack for questions, which are linked from the website. You can also use discussions here on GitHub. Closing for now as you got an answer in any case. |
Beta Was this translation helpful? Give feedback.
-
https://cuelang.org/play/?id=BdZubTaNYLG#w=function&i=cue&f=eval&o=cue |
Beta Was this translation helpful? Give feedback.
-
Keep the struct open: https://cuelang.org/play/?id=O1dTQ-fi74D#w=function&i=cue&f=eval&o=cue |
Beta Was this translation helpful? Give feedback.
-
@mvdan apologies for continuing this discussion on a closed issue, but since you commented, I think I am misunderstanding something fundamental here. When I change this to something that the struct does not contain as a value, cue doesn't seem to mind? I would like for there to be an error if the value is not in the struct, using some expression following the selector for the struct field in question, i.e. https://cuelang.org/play/?id=wA7Jk_i6q99#w=function&i=cue&f=eval&o=cue |
Beta Was this translation helpful? Give feedback.
-
Use a required field: https://cuelang.org/play/?id=gL1wxi-ArDx#w=function&i=cue&f=export&o=cue |
Beta Was this translation helpful? Give feedback.
-
That would be a constraint on the key, not the value. |
Beta Was this translation helpful? Give feedback.
-
My apologies, I misunderstood what you meant by "value" earlier. You want to require that at least one field in the struct contain the value "c". I'm not sure that that's easy to do currently in CUE. I will reopen for the time being because I might need @myitcv to chime in here with his experience. My immediate attempt is a comprehension (https://cuelang.org/play/?id=zzVA4tm5wWQ#w=function&i=cue&f=export&o=cue) but it doesn't work as the definition by itself already results in an error. It might be that #575 is required for such an approach to work. |
Beta Was this translation helpful? Give feedback.
-
#575 would be a general solution that would solve this case and many others. Alternatively, a |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
Yes. We model lists as structs like
field: { "0": "a", "1": "b" }
which means we can notuse
list.Contains` to validate that the field contains a certain value.Describe the solution you'd like
We are looking for a way to practically do
struct.Contains
in a way that allows us to unify the field. In this case,field: struct.Contains("a")
would unify butfield: struct.Contains("c")
would not. Alternatively, a way to use cue currently to achieve this.Describe alternatives you've considered
So far nothing has come up as a viable solution.
Additional context
Add any other context or screenshots about the feature request here.
Beta Was this translation helpful? Give feedback.
All reactions