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;
}
YANG has a built-in type named
enumeration, which serves the exact purpose you describe.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
stringbuilt-in type and restrict it with apatternthat takes a regex argument such asapple|orange|strawberry, but you'd be using the wrong tool for the job.Advantages of using
enumerationinstead of above:enumerationvalue is a string - one of theenumsenummay be documented withdescription/referenceenumis (either explicitly or implicitly) assigned avalue, an integer that may be used internally by implementations to identify the associated stringenummay get anif-featurestatement (YANG version 1.1)