schemaietf-netmod-yangyang

Default string values for union types in Yang Schema


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.

Solution

  • 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.