ietf-netmod-yangyang

Yang model mandatory node only when condition is true


I have an XML file:

<a>
    <b>true</b>
    <c>foo</c>
</a>

and a Yang model:

container a {
   leaf b {
      type boolean;
   }

   leaf c {
      type string;
   }
}

Node 'c' is mandatory only when node 'b' equals 'true'. If I add a mandatory: true constraint to node 'c' it will become mandatory for all values of 'b'.

How to change the Yang model so that node 'c' is mandatory when 'b' is 'true' and optional when 'b' is false?


Solution

  • You can use the must statement with an XPath condition:

    container a {
       must 'not(b) or boolean(c)'
      
       leaf b {
          type boolean;
       }
    
       leaf c {
          type string;
       }
    }