Replies: 2 comments 3 replies
-
Hmmm, I noticed just now that there is an explicit construction to create a closed struct:
It fails to validate cd $(mktemp -d)
(
echo "close({ a: string }) | close({ b: int })" > struct-disjunction.cue
echo '{"a": "string"}' > a.json
cue vet a.json struct-disjunction.cue
)
But it succeeds in validating cd $(mktemp -d)
(
echo "close({ a: string }) | close({ b: int })" > struct-disjunction.cue
echo '{"b": 42}' > b.json
cue vet b.json struct-disjunction.cue
) |
Beta Was this translation helpful? Give feedback.
-
@mboogerd welcome to the CUE project! There are a few things going on here, one of which might well be a bug 😄 so I'll try to pick things apart. In the expression:
the structs that are part of the disjunction,
(I assume from your description above that Both structs in the disjunction are open, hence this value unifies with both of them to give the values you see in the error message (depending on whether you are validating
and hence neither of the elements of the struct is eliminated. Notice that in each, there is an incomplete value, because This also explains why by comparison the following passes validation:
That value unifies with both structs to give a complete and identical value in each case, hence the result is unambiguous.
And herein lies the answer (as you have discovered). The structs above are not closed. They can either by closed by the
The output from So given what I believe you are trying to achieve (you are trying to describe limits on the struct of the JSON), I think definitions are your way forward. But it's at this point that I think we run into a bug, as you have demonstrated in your reply:
This gives the output:
This corresponds with what you observed in your reply. What's also problematic is that error has no details as part of it. I will raise an issue to capture both issues. (Note the final command doesn't run because of an error in the preceding command, but it succeeds as expected, just as you observed). One other point to note is that CUE does not currently handle the concept of a protobuf oneof all that well. This will be addressed in #822 (search for the "oneof" heading for relevant references - apologies, I can't link to heading from an issue)
Thank you for including this, reproducers make our lives much easier! My reproduction above using the
It has no effect in a non-closed struct.
This is covered by my explanation above, that the result must be complete and unambiguous.
This is the bug you have run into 😄 |
Beta Was this translation helpful? Give feedback.
-
Hi, I've been playing around with Cue for a few days now and most seems to work pretty intuitively. Really nice!
I have one weird case with struct disjunctions that I don't fully understand. Take the following CUE expression (reproducer below):
struct-disjunction.cue
I would expect the following two JSON payloads to pass vet/validation:
a.json
and
b.json
But it doesn't:
It does pass for a JSON value of
but that is the evaluation result I would expect for a conjunction CUE expression
{ a: string } & { b: int }
, or a disjunction of open structs{ a: string, ... } | { b: int, ... }
, but not a disjunction of closed structs.A potentially related, second question: if I employ CUE definitions, the evaluation also turns into something incomprehensible:
union.cue
I'm not sure if these two things are related at all, but they appear around the same construction. Can someone clarify for me what is happening here? Thanks a bunch in advance!
Reproducer script
Beta Was this translation helpful? Give feedback.
All reactions