Subject: [netmod] Yang string with patterns cannot be assigned default values

From: Olumide <[email-masked]>
Date: Wed, 3 Nov 2021 16:06:34 +0000

Dear List,

yanglint (version 0.16.105) does not validate the schema below when the
a default value is assigned to string that has a pattern. Is this the
specified behavior; and if so is there a workaround? (Comments not in
schema, added for emphasis)

     module tmp
     {
         namespace "example.com";
         prefix "foo";

         container bar {
             leaf wibble {
                 type string {
                     pattern "^x$";
                 }
                 default "x";      ### NOT OKAY
             }

             leaf wobble {
                 type string;
                 default "y";     ### OKAY
             }
         }
     }

yanglint error message:

err : Value "x" does not satisfy the constraint "^x$" (range, length, or
pattern). (/tmp:wibble)
err : Module "tmp" parsing failed.

Regards,

- Olumide

---

From: Mahesh Jethanandani <[email-masked]>
Date: Wed, 3 Nov 2021 13:11:06 -0400

I believe if you remove the anchor characters ^ and & from the pattern statement, the default value will work.

> On Nov 3, 2021, at 12:06 PM, Olumide <[email-masked]> wrote:
> 
> Dear List,
> 
> _______________________________________________
> netmod mailing list
> [email-masked]
> https://www.ietf.org/mailman/listinfo/netmod

Mahesh Jethanandani
[email-masked]

---

From: Carsten Bormann <[email-masked]>
Date: Wed, 3 Nov 2021 18:20:25 +0100

> I believe if you remove the anchor characters ^ and & from the pattern statement, the default value will work.

Indeed.  And the reason is that YANG patterns are not PCRE/Javascript type regular expressions (which always require anchors unless used for searching), but W3C XSD type regular expressions, which are implicitly anchored.

https://www.rfc-editor.org/rfc/rfc7950.html#section-9.4.5

Most people learn about PCRE/Javascript type regular expressions first, which is a source of a common misunderstanding of YANG (and, CDDL, for that matter) regular expressions.

Grüße, Carsten

>> On Nov 3, 2021, at 12:06 PM, Olumide <[email-masked]> wrote:
[…]
>>                default "x";      ### NOT OKAY
[…]