Define a new type that includes the existing one into its definition and therefore expands its value space. YANG has a union
built-in type that allows you to do just that. A value is considered to be valid if it matches at least one type in the union of types.
In YANG version 1.1:
typedef mac-address-type {
type string {
pattern "[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}";
}
}
leaf mac-or-empty {
type union {
type aye:mac-address-type;
type empty;
}
default "";
}
In YANG version 1:
typedef mac-address-type {
type string {
pattern "[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}";
}
}
leaf mac-or-empty {
type union {
type mac-address-type;
type string {
pattern ''; // length 0; // as an alternative
}
}
default "";
}
This would mean that the valid value space for the mac-or-empty
leaf would be MAC addresses or an empty string.
Note: a MAC address type has already been published by the IETF as part of ietf-inet-types YANG module (ietf-inet-types:mac-address), RFC 6991, so there's no need to define your own.
The union
built-in type is described in detail in RFC 7950, Section 9.12.