0

Why can't the leaf wibble be assigned a default value "-"in the following schema (comments not in schema, added to post for clarity)

module type
{
    namespace "example.com";
    prefix "foo";
    
    typedef optional-value {
        type  union {
            type uint8 {
                range "0 .. 99";
            }
            
            type string {
                pattern "^-$";
            }
        }
    }
    
    container bar {
        leaf wibble {
            type optional-value;
            default "-"; ### NOT OKAY
        }
        
        leaf wobble {
            type optional-value;
            default 42;  ### OKAY
        }
    }
}

yanglint (version 0.16.105) does not validate the above schema and returns the error message:

err : Invalid value "-" in "wibble" element. (/type:wibble)
err : Module "type" parsing failed.

Done some more experimenting and it appears that strings with patterns cannot be assigned default values

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 output:

err : Value "x" does not satisfy the constraint "^x$" (range, length, or pattern). (/tmp:wibble)
err : Module "tmp" parsing failed.
Olumide
  • 5,397
  • 10
  • 55
  • 104

1 Answers1

1

In YANG one uses the regex flavor from XML Schema which doesn't require ^$ to anchor expressions so that they math the entire value. All XSD expressions are implicitly anchored. You could try to remove these characters from your patterns and give it another try.

Piotr Babij
  • 837
  • 5
  • 12
  • What you meant to say here is that a `^` character at the beginning of a YANG pattern or a `$` at the end of a YANG pattern are not interpreted as anchors like in some other regex flavors. Instead they are interpreted literally, making "^x$" the only possible valid value for leaf `wibble` in OP's example. – predi Nov 05 '21 at 07:42