0

Yang pattern does not accept valid Regex when it contains caret ^ or dollar $.

The bellow pattern is valid on Regex validators, but when run inside a Yang pattern template in an is-types.yang file my application errors.

However when i take off the ^ and $ from each part of the expression it works fine. However this now allows the regex to accept unwanted values.

I am trying to match either 1, 11, 1/1/1, 1/1/1/1.

Trying the pattern:

^([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})$|^([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})$|^([0-9]{1,2})$

Yang typedef:

typedef interface_number_value {
    type string {
        pattern "([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})|([0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3}/[0-9]{1,3})|([0-9]{1,2})" {
            error-message "error";
        }
    } 
    description
        "description";
}

Also I am not sure where to raise issues/bugs with Yang.

  • This is not a bug. YANG uses XSD flavor of regular expressions, where ^ and $ represent literals that must appear in a matching string. The expressions are always implicitly anchored (entire value must match), so what you are saying about unwanted values is not possible, unless your YANG validator implementation is non-standard (and therefore isn't a YANG validator). – predi Dec 15 '21 at 07:40
  • Similar question: https://stackoverflow.com/questions/69825229/default-string-values-for-union-types-in-yang-schema – predi Dec 15 '21 at 07:44
  • Since you are also asking where bugs in YANG specifications are discussed, that would be the NETMOD WG mailing lists (https://www.ietf.org/mailman/listinfo/netmod). If you suspect that there is an issue, that would be the place to go to. Each IETF RFC document also has an errata page, where you can check if the issue has been reported already (https://www.rfc-editor.org/errata_search.php?rfc=7950&rec_status=0). – predi Dec 15 '21 at 08:22

1 Answers1

2

This is what RFC7950 says about the "pattern" statement:

The "pattern" statement, which is an optional substatement to the "type" statement, takes as an argument a regular expression string, as defined in [XSD-TYPES]. It is used to restrict the built-in type "string", or types derived from "string", to values that match the pattern.

If you follow the normative reference XSD-TYPES, you eventually get to:

[XSD-TYPES] Biron, P. and A. Malhotra, "XML Schema Part 2: Datatypes Second Edition", World Wide Web Consortium Recommendation REC-xmlschema-2-20041028, October 2004, http://www.w3.org/TR/2004/REC-xmlschema-2-20041028.

Therefore the regular expressions in YANG "pattern" statements are defined by XML Schema (XSD) type specs (and that specific revision of them for YANG 1.0 and 1.1).

Unlike some other regex flavors, a ^ character at the beginning of a YANG "pattern" or a $ at the end of a YANG "pattern" are not interpreted as anchors. Instead they are interpreted literally as characters that must match. All "pattern" expressions are implicitly anchored and therefore match the entire value.

You can find out more about the specifics of XML Schema Regular Expressions here.

predi
  • 5,528
  • 32
  • 60