Skip to content

fix(yang): enforce pattern, enum/identityref, integers, choice/case and wrong-level paths - #126

Merged
roc-ops merged 7 commits into
mainfrom
fix/yang-validator
Nov 28, 2026
Merged

roc-ops merged 7 commits into
mainfrom
fix/yang-validator

Conversation

@roc-ops

@roc-ops roc-ops commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Summary

Five YANG validator bugs where invalid data passed silently. They are fixed in internal/yang, one commit per issue. Each fix has a failing-first test at the Validator seam (internal/yang/validator_rfc7950_test.go) and an end-to-end test through CompiledTemplate.ParseWithValidation with a yang= group (yang_validation_test.go).

Issue Construct Before After yangson 1.7.9
#50 uint8 = 12.5 ✅ valid ❌ invalid type float64 … expected uint8 ❌ agrees
#55 pattern "[a-z]+" = "ABC" / "abc1" ✅ valid (TODO) ❌ value "ABC" does not match pattern '[a-z]+' ❌ agrees
#56 enum up/down = "sideways"; union{uint8; enum auto} = "zzz" ✅ valid ❌ invalid value "sideways" … expected one of up, down ❌ agrees
#57 yang= names the container, not the list ✅ Valid=true + warnings only ❌ one error: … defines none of the fields cpu-id, usage-5s … did you mean t-cpuinfo:show_cpuinfo/cpu? ❌ (stricter, see below)
#59 leaf under choice/case ⚠️ "not defined" warning, unchecked type, range, length and pattern checked like any leaf ❌ agrees

How it works:

  • YANG: integer types accept fractional floats (12.5 passes as uint8) #50: int/uint kinds accept a float64 only if it is a whole number. A whole float64 is what a JSON round trip makes of an int.
  • YANG: leaves inside choice/case are never type-checked #59: a lookupChild helper resolves data-node names through choice/case entries, which are schema-only (RFC 7950 7.9). It handles nested choices and shorthand cases. Mandatory leaves in an inactive case are still not required (RFC 7950 7.6.5).
  • YANG: enumeration and identityref membership is not checked #56: enum values must be declared names (RFC 7950 9.6). Identityref values must name an identity derived from the base, and the base itself is excluded (9.10.2). They use the RFC 7951 6.8 JSON form: module:identity, where the module can be left out only for an identity in the leaf's own module. A prefix is not accepted where a module name is expected (te:aes fails).
  • YANG: pattern statements are never enforced #55: new file internal/yang/pattern.go. It translates XSD regex to an anchored RE2 regexp: ^/$ become literals, . becomes [^\n\r], \d/\s/\w get their XSD Unicode meaning, and \S or \w inside a positive class becomes an alternation. Compiled patterns are cached. All of a type's patterns must match (goyang already merges typedef chains). goyang drops modifier invert-match, so the modifier is recovered from the parsed statements. RE2 can't express \i, \c, \p{IsBlock}, class subtraction, or a negated class containing \S or \w. For those the validator gives a warning, once per Validate call: Field 'x' pattern '…' cannot be checked: …; values are not checked against it. The value is not checked, but the validator says so instead of passing silently. A union member's patterns decide whether a value belongs to that member.
  • YANG: a yang= path at the wrong level passes silently with Valid=true #57: if the data has fields and none of them is defined under the path (this includes a path that names a leaf), Validate adds one error for the group. The error lists the fields and suggests the child paths that do define them. If at least one field matches, the old behaviour stays: unknown fields are warnings.

Evidence

All before/after results come from the new tests: each failed on origin/main and passes on this branch.

#50. Template CPU {{ id | to_float }} into leaf id { type uint8; }, input CPU 12.5:

  • before: Valid=true, no errors
  • after: Field 'id' has invalid type float64 in item 0, expected uint8. CPU 12 (→ 12.0) is still valid.

#55. typedef mac { type string { pattern "[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}"; } }:

  • before: 0011.2233.44zz and 0011.2233.4455.6677 both valid
  • after: Field 'mac' value "0011.2233.44zz" does not match pattern '…' in item 0. The 4-group value fails too, because a substring match isn't enough. 0011.2233.4455 is valid.
  • TestXSDToRE2MatchesYangson checks the translation on 74 pattern/value pairs, with expectations from yangson's own regex translation (elementpath 5.1.4 translate_pattern, called the way yangson calls it).

#56. leaf state { type enumeration { enum up; enum down; } }, leaf speed { type union { type uint32; type enumeration { enum auto; } } }:

  • before: state=sideways and speed=fast both valid
  • after: Field 'state' has invalid value "sideways" in item 0, expected one of up, down and Field 'speed' has invalid value "fast" …

#57. The research doc's show cpuinfo example with yang="t-cpuinfo:show_cpuinfo", input CPU0 12.50% / CPU1 140.00%:

  • before: Valid=true, 4 "not defined" warnings, and the out-of-range 140.00 is never seen
  • after: Valid=false, YANG path 't-cpuinfo:show_cpuinfo' defines none of the fields cpu-id, usage-5s; the path must name the node each match maps to (a list, not its container): did you mean t-cpuinfo:show_cpuinfo/cpu?. With the suggested path, Field 'usage-5s' value is out of range in item 1.

#59. choice addr { case v4 { leaf mtu { type uint16 { range "68..9216"; } } } }, input eth0 mtu 20:

  • before: Valid=true, warning Field 'mtu' not defined in YANG schema
  • after: Field 'mtu' value is out of range in item 0. mtu 1500 is valid with no warnings.

Agreement with yangson (what Python TTP validates with)

I ran every unit-test case through yangson 1.7.9 in a scratch venv. They all agree except for these deliberate differences:

  1. 12.0 for an integer leaf: yangson rejects it because it is a JSON float. gottp accepts it because gottp's own data can arrive through encoding/json, which turns ints into float64.
  2. XSD \s and \w outside a class: elementpath passes them to Python's re unchanged, so for yangson \s matches \f and \w matches _. This PR follows XSD 1.0 Part 2 F.1.1, because RFC 7950 9.4.5 defers to it: \s = [#x20\t\n\r], and \w excludes \p{P}, which includes _. Inside a class, elementpath already uses XSD semantics, so yangson and gottp agree that [\w\-]+ rejects a_b. The 3 cases are marked differs in pattern_test.go.
  3. RE2-inexpressible patterns: yangson enforces them through Python re. gottp warns and skips them (see YANG: pattern statements are never enforced #55 above).
  4. Unknown fields: yangson rejects any member that isn't in the schema. gottp keeps them as warnings, and since YANG: a yang= path at the wrong level passes silently with Valid=true #57 it gives an error only when no field matches, which is the scope of the issue.

Checks

  • go vet ./..., go test ./..., go test -race (all packages except test/memory, with GOTTP_PYTHON set), staticcheck 2025.1.1 and GOOS=js GOARCH=wasm go build ./... all pass locally.
  • ttpsuite is unchanged: no case uses yang=.
TTP 0.10.1 suite: pass 48 / skipped-by-issue 148 / design 4 of 200   (before and after)
gottp repros: pass 1 / skipped-by-issue 9 / design 0 of 10
source-map parity: same 199 / diverge 11 of 210

Merge Danger

Stricter validation means output that passed before can now fail. The parsed Data never changes; only ParseResult.ValidationResults (and the editor's markers and notification) can go from Valid=true to Valid=false. CompiledTemplate.Parse() discards validation results, so it is unaffected. Who can see a change:

  • Templates whose yang= path is one level too high (YANG: a yang= path at the wrong level passes silently with Valid=true #57). These were checking nothing, and they now fail every parse until the path is fixed. The error names the fix. This is the most likely visible change.
  • Schemas with pattern (YANG: pattern statements are never enforced #55). This includes ietf-inet-types and ietf-yang-types typedefs (ipv4-address, mac-address, …). Captured values that don't match exactly now fail. For example, a MAC captured in Cisco dotted form against yang:mac-address used to pass and now fails, correctly.
  • Enum and identityref leaves (YANG: enumeration and identityref membership is not checked #56) that receive raw device strings (for example Up vs up) now fail. Identityrefs from another module must be written as module:identity.
  • Choice/case leaves (YANG: leaves inside choice/case are never type-checked #59) are now checked, and their "not defined" warnings go away.
  • New warnings: one per field whose pattern RE2 can't check.
  • Performance: a regex match per patterned value (patterns are compiled once and cached process-wide), plus an RLock per pattern check to read the invert-match table. No allocation per value beyond the regexp match.
  • ValidationError gets an unexported field (once). JSON output is unchanged.

Related bugs found and filed, not fixed here: #122 (a mandatory choice is reported missing even when a case is present, and mandatory leaves of the active case aren't checked), #123 (union members' range/length aren't checked), #124 (a yang= path can't go through choice/case).

Fixes #50
Fixes #55
Fixes #56
Fixes #57
Fixes #59

🤖 Generated with Claude Code

@roc-ops roc-ops added this to the Baseline v0.1.14 milestone Nov 28, 2026
roc-ops and others added 7 commits November 27, 2026 19:55
validateValueType accepted any float64 for int/uint kinds, so 12.5
validated against a uint8 leaf. RFC 7950 9.2 integer types admit whole
numbers only. A whole float64 (what a JSON round trip makes of an int)
is still accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Field lookups used schemaEntry.Dir[name] directly, so a leaf under a
choice/case came back as a 'not defined' warning and was never checked.
choice and case are schema-only nodes (RFC 7950 7.9); the lookup now
descends through them, including nested choices and shorthand cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Enum and identityref leaves accepted any string, so a union with an
enum member accepted anything. An enum value must now be one of the
declared names (RFC 7950 9.6). An identityref value must name an
identity derived from the base, the base itself excluded (RFC 7950
9.10.2), in the RFC 7951 6.8 JSON form: 'module:identity', with the
module optional only for an identity in the leaf's own module.

Errors name the bad value and the allowed set instead of reporting an
'invalid type string'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A leaf's pattern statements were a TODO, so any string passed. They are
now checked with XSD regex semantics (RFC 7950 9.4.5): the whole value
must match, all patterns must hold, and 'modifier invert-match' inverts
one. The XSD expression is translated to an anchored RE2 regexp: ^ and $
are literals, '.' excludes \n and \r, and \d, \s and \w get their
XSD (Unicode) meaning. goyang drops the modifier, so it is recovered
from the parsed statements.

Patterns RE2 can't express (\i, \c, Unicode block escapes, class
subtraction, a negated class with \S or \w) are reported as a warning,
once per Validate call, instead of passing silently. A union member's
patterns decide whether a value belongs to that member.

The translator is checked against yangson 1.7.9 (what Python TTP uses);
the three differing cases follow XSD, where elementpath hands \s and \w
to Python's re unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A path one level too high (the container instead of the list each match
maps to) turned every field into a 'not defined' warning and returned
Valid=true, so nothing was checked. When the data has fields and none
of them is defined under the path (or the path names a leaf), Validate
now adds one error for the group, naming the fields and suggesting the
child paths that do define them. A path matching some fields keeps the
old behaviour: unknown fields stay warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#59)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
So the editor validates yang= groups the same way as the library after
#50, #55, #56, #57 and #59.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@roc-ops

roc-ops commented Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto main after #116 and #125 merged. I kept all CHANGELOG entries, and rebuilt editor/gottp.wasm and editor/js/gottp.wasm (identical) so the editor's YANG validation matches the library. Ran locally: go vet, go test, go test -race (without test/memory), staticcheck and the wasm build. All pass.

@roc-ops
roc-ops merged commit 9182fa1 into main Nov 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant