My yang model has two same-level lists. I need the elements on both lists to be unique. Not unique within each list, but unique in the union of the lists. Is there a way to define this with a must statement or something?
container parent {
list a {
key "name";
leaf name {
type string;
}
leaf something {
type string;
}
}
list b {
key "name";
leaf name {
type string;
}
leaf something-else{
type string;
}
}
}
So, how would I make sure that every element has a different name?
Assuming you need unique values of certain elements within a union between two lists, you can indeed achieve this using a must constraint.
(Also) assuming that you need unique values for
somethingandsomething-else, you can put a must constraint onlist bwith a condition like this:So, values of
somethingare not required to be unique across all instances oflist a, values ofsomething-elseare not required to be unique across all instances oflist b, but an intersection across distinct values ofsomethingand distinct values ofsomething-elsemust be an empty set.An example of an invalid document:
Which would fail validation with something like:
Error at (16:5): failed assert at "/nc:data/a1:parent/a1:b": Condition "not(../a1:a[a1:something=current()/a1:something-else])" must be trueNote that this is just one example of how you could approach your requirement. You could, for example, move my condition to the
something-elseleaf, which would then become something like this (did not test):