yang

Can you restrict a string to a set of permitted strings in YANG?


Can we have a statement in YANG which constraints the user input string to one of the strings which are permitted? I saw that one can use pattern to match a certain regex, but what if we have strings like "apple", "orange", "strawberry" which are permitted, and the product below must be a member of this set of permissible strings?

leaf product{
  type string;
}

Solution

  • YANG has a built-in type named enumeration, which serves the exact purpose you describe.

    leaf product {
      type enumeration {
        enum apple;
        enum orange;
        enum strawberry;
      }
    }
    

    This is the type you should use if permitted values are members of a set of string constants. The specifics of the type may be found in RFC 7950, Section 9.6.

    Technically speaking, you could also use the string built-in type and restrict it with a pattern that takes a regex argument such as apple|orange|strawberry, but you'd be using the wrong tool for the job.

    Advantages of using enumeration instead of above: