Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnyOf Pattern doesn't work when fields are optional and array #72

Open
kak-bo-che opened this issue Apr 11, 2023 · 3 comments
Open

AnyOf Pattern doesn't work when fields are optional and array #72

kak-bo-che opened this issue Apr 11, 2023 · 3 comments

Comments

@kak-bo-che
Copy link

import "list"

#test: this={
	a?:        [...string]
	b?:        string
	c?:        string
	#AnyOfABC: true & list.MinItems([ for label, val in this if list.Contains(["a", "b", "c"], label) {label}], 1)
}
{
	"a": ["bar"],
	"b": "foo"
}
cue eval -c ./test.cue ./testany.json -d "#test"
#test.#AnyOfABC: conflicting values false and true:
    ./test.cue:7:13
    ./test.cue:7:20

changing from array a to string works or changing the array field from optional to default works as well. This is slightly outside of the content of the page, bug a simple derivative.

@verdverm
Copy link
Member

yeah, I never tested that. There are better solutions on the horizon in https://cuetorials.com/cueology/futurology/builtin-helpers/

@kak-bo-che
Copy link
Author

Changing the list to non-optional only works because the empty list will have a label and the constraint will pass as the label is always available. The underlying issue seems to be that the simplified example as given in the tutorial doesn't seem to work when providing data to validate instead of embedding it in the definition:

test.cue

import "list"

#test: this={
	a?:        string
	b?:        string
	c?:        string
	#AnyOfABC: true & list.MinItems([ for label, _ in this if list.Contains(["a", "b", "c"], label) {label}], 1)
}

test.json

{
	"b": "foo"
}
cue eval -c ./test.cue ./testany.json -d "#test"
#test.#AnyOfABC: conflicting values false and true:
    ./test.cue:7:13
    ./test.cue:7:20

@verdverm
Copy link
Member

verdverm commented Apr 14, 2023

I think this is because this in the definition is local to the definition, thus the data is not part of it. In other words, there is only one this, not per data value to which it is applied. Also note, that the way cue handles json/yaml is slightly different from how it handles CUE values.

So I suspect there are some scoping complexities at play here.

The optional list works with the example from the page: https://cuelang.org/play/?id=yDm35mVv9iR#cue@export@cue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants