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

Validate the existence of fieldA xor fieldB #1349

Closed
ChristianKniep opened this issue Nov 1, 2021 · 3 comments
Closed

Validate the existence of fieldA xor fieldB #1349

ChristianKniep opened this issue Nov 1, 2021 · 3 comments

Comments

@ChristianKniep
Copy link

I started with cue-lang and aim to use it to validate YAML. I want either to have a field called item or items, but not both and I am wondering how I can achieve that...

My cue file looks like this:

item?: string
items?: [...string]

And I suppose I need to add a check that checks if either of them is set, but not both.
(Writing the issue is almost rubber duck debugging - I might resolve the issue tomorrow already; hope that helps someone else)

Valid YAML

item: abc

or

items: [abc, cde]

Invalid YAML

item: abc
items: [abc, cde]
@ChristianKniep ChristianKniep added the Triage Requires triage/attention label Nov 1, 2021
@seh
Copy link
Contributor

seh commented Nov 1, 2021

See the numexist builtin function proposed in #822. It's not implemented today, but that's the intended way to cover this case.

@verdverm
Copy link

verdverm commented Nov 2, 2021

You could use the anyOf work around with an upper limit as well.

https://cuetorials.com/patterns/fields/#anyof

The oneOf pattern there may be sufficient for your needs however

val: {
  { item: string } | { items: [...string] }
}

https://cuelang.org/play/?id=gdbxOmzwmMy#cue@export@cue

@myitcv
Copy link
Member

myitcv commented Nov 2, 2021

Thanks for raising this @ChristianKniep

@seh is right, this is covered in #822 so will close this as a duplicate.

@myitcv myitcv closed this as completed Nov 2, 2021
@myitcv myitcv added duplicate and removed Triage Requires triage/attention labels Nov 2, 2021
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

4 participants