ConfD User Community

Check if element exists in a leaf-list and return error

Is it possible to check for an element in a leaf list and return error-message in yang. Please provide an example.

It is a bit unclear what you want to achieve. If you want to:

  1. make sure that the leaf-list has at least one element:

    leaf-list data {
       type string;
       min-elements 1;
     }
    
  2. make sure that a certain element exists:

    leaf-list data {
      type string;
    }
    must "data = 'myvalue'" {
      error-message "element 'myvalue' must be present";
    }
    

    This can be used event for 1. using simple must "data", the advantage is that it allows you to customize the error message; it might have a bit worse performance though;

  3. have a complex constraint on every element in the list - this is possible too, depending on the constraint you may be able to achieve that with a must statement like this:

    leaf-list data {
      type string;
      must "my-xpath-expr";
    }
    

    or you may need to resort to semantic validation. There are examples of this in intro/validation examples (and others), one full chapter of the ConfD user guide is dedicated to this topic too.