YANG pattern statement: regex constraint on string values

300-435 By islever Editorial Last updated

Short answer

A The YANG pattern statement imposes a regular-expression constraint on string-typed values rather than defining a list, container, or leaf.

Question

What is YANG pattern statement for?
  1. Regex constraint on string values Correct Answer
  2. List definition
  3. Container
  4. Leaf

Key Takeaways

In YANG, the pattern statement is a type-level restriction that limits a string-typed value to text matching a regular expression. It is therefore best described as a regex constraint on string values, not as a list, container, or leaf declaration. That distinction matters because pattern narrows the value space while the other statements build the schema tree.

Expert Analysis

Because pattern sits under a string type and narrows the values that type may carry, the operative description is a regular-expression constraint on string values rather than any schema-node declaration.

String-typed schema nodes turn pattern into a value-space restriction

In YANG, pattern is a substatement of type, most commonly attached to string or to a typedef derived from string. Its argument is a regular expression written in the XML Schema regex dialect, and it limits which strings are legal for that leaf, leaf-list, or typedef. The decisive property is that pattern does not add a node to the schema tree: it modifies the value space of an existing string type. A leaf such as interface-name can have type string { pattern '...'; }, and the leaf still exists as a scalar node; what changes is that a value outside the expression is invalid. The constraint can also be inherited through a typedef and can be combined with other restrictions such as length or additional pattern statements, which are ANDed. The phrase 'regex constraint on string values' captures exactly that role. The alternatives describe data-definition statements: list creates a collection of entries, container creates a grouping node, and leaf creates a scalar node. None of those statements evaluates a value against a regular expression. The option that names a regex restriction is therefore the only one consistent with the substatement's placement under type and with its effect on instance validation.

Validation happens after schema resolution, when the server checks an instance value

The pattern argument is not a routing or access-control expression, and it is not evaluated when a client builds a payload; the server enforces it when it resolves the leaf's type and validates an encoded instance. During module load, the server compiles the type and records the regex along with other restrictions. When a NETCONF <edit-config> or a RESTCONF JSON body supplies a string value, the server checks that value against the effective type. A mismatch is rejected as an invalid-value error rather than accepted and silently corrected. This placement explains why pattern is a constraint: it operates at the value-validation stage, after the schema has already determined that the data node is a leaf of that type. The mechanism also explains inheritance. If a typedef declares pattern, a leaf that uses the typedef inherits the restriction; a local pattern under the leaf's type narrows the value space further. Multiple pattern statements must all match, so adding one cannot loosen an earlier one. For automation, this matters when a Python script builds a payload from user input: the regex protects the device from malformed strings, but the script must still supply a value that satisfies every inherited and local pattern or the request fails.

Choosing list, container or leaf replaces a regex restriction with schema structure

Option B, list, defines a repeatable collection of entries, normally with keys and possibly min-elements or max-elements; it answers a structure question and cannot enforce a character pattern. Option C, container, creates an interior grouping node that holds children but has no value of its own, so a regex has nothing to test against. Option D, leaf, declares a named scalar data node and chooses its type; the pattern would be placed under that leaf's type, not identified with the leaf itself. All three are data-definition statements, and confusing them with a restriction leads to the wrong schema design: a list where a constrained string was intended produces keyed entries instead of one validated value, a container adds an unnecessary branch, and a leaf without the type restriction accepts strings the device should reject. The cost is not merely a naming error; an invalid module may fail to compile if pattern is attached where the grammar does not allow it, or it may compile but admit values outside the intended format. The regex option avoids that structural detour and keeps the constraint at the type layer.

Declaration-focused wording would make leaf the answer, but pattern's placement prevents that

If the requirement were to declare a named scalar value in the schema tree, leaf would be correct: a leaf statement introduces the node, gives it a name and a type, and appears in the module's data-definition hierarchy. That reading is not available because the keyword under discussion is pattern, and the grammar places pattern beneath type, not beneath module, container, or list. The same test disposes of list: if the goal were a repeatable set of records with keys, list would be the right construct. The same test also disposes of container: if the goal were an interior node grouping related children, container would be the right construct. Each alternative becomes plausible only when the wording shifts from 'what does the statement do' to 'what schema node should be created'. Here the wording asks for the purpose of a type restriction, and the only option that describes a restriction on string content is the regex constraint. A further condition would make the choice even clearer: if the stem mentioned XML Schema regex, invalid-value rejection, or a typedef-derived string, those signals would point directly to pattern and away from any data-definition statement.

Official Reference

Exam Strategy

When a YANG keyword appears next to data-definition words such as leaf, list, or container, classify it by its grammatical role: data-definition statements build the schema tree, while type substatements such as pattern, range, length, and fraction-digits restrict values. Map the keyword to the layer it acts on before reading the answer choices; regex, bounds, and enumeration language points to a constraint, while node, key, or collection language points to a schema node. Apply the same role test to any similar YANG question.

Practice All 300-435 Questions

Access 150 questions with complete answers and detailed explanations.

View Full 300-435 Practice Test →

← Back to 300-435 Study Guide