Restrict instance-identifier in must statement

I am trying to write must statement to restrict the instance-identifier, if ref is for instance of “/container/foo”, range should be from 1 to 100, if ref is for instance of “/ifs/if”, range should be from 101 to 200.
I am not sure how to write the must statement in container test below.

yang example:
container ifs {
list if {
key “name”;
leaf name {
type string;
}
}
}
container foo {
list bar {
key “name”;
leaf name {
type string;
}
}
}

container test {
/if ref is for instance of “/container/foo”, range should be from 1 to 100./
/if ref is for instance of “/ifs/if”, range should be from 101 to 200./
must “??” {}
error-message;
}
leaf ref {
type instance-identifier;
}
leaf range {
type uint32;
}
}

You can write your must statement as follows for the leaf node named range:

leaf range {
  type uint32;
  must "(starts-with (.., '/ifs:ifs') and . > 100 and . < 201)
         or (starts-with(.., '/ifs:foo') and . > 0 and . < 101)";
}

in which the first ifs after the / is the prefix for your YANG module.

1 Like

thanks, it works. thanks for helping on this, with tailf:annotate statement, there is a clear approach to have such validation logic yang model without polluting the original data model.
btw:do we have some typical use cases of using xpath for must statement? I don’t have much more knowledge on it.

The use of must statements in YANG models are very common. For example, it is being used in an IETF standardized YANG model called ietf-system.yang which is part of the linuxcfg example in the ConfD distribution.

Thanks for helping this :slight_smile:

if the case is instance-identifier to point to a deep path, which more list data node in that path (e.g. /foo/bar[name=n1]/bar2[name=n2]/bar3[name=n3]), I guess there is no way to use regex function from xpath 1.0 to match that path.
I know we have valdation callpoint can help on this, if it is can be done in yang model it could be better.

I found re-match function in manual, it could help this case.

Check out the extension tailf:path-filters in the tailf_yang_extensions(5) manual page.