Repository navigation
fix(yang): enforce pattern, enum/identityref, integers, choice/case and wrong-level paths - #126
Merged
Merged
Conversation
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>
roc-ops
force-pushed
the
fix/yang-validator
branch
from
November 28, 2026 01:58
f3b3495 to
4019758
Compare
Owner
Author
|
Rebased onto main after #116 and #125 merged. I kept all CHANGELOG entries, and rebuilt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theValidatorseam (internal/yang/validator_rfc7950_test.go) and an end-to-end test throughCompiledTemplate.ParseWithValidationwith ayang=group (yang_validation_test.go).uint8=12.5invalid type float64 … expected uint8pattern "[a-z]+"="ABC"/"abc1"value "ABC" does not match pattern '[a-z]+'enum up/down="sideways";union{uint8; enum auto}="zzz"invalid value "sideways" … expected one of up, downyang=names the container, not the listValid=true+ warnings only… defines none of the fields cpu-id, usage-5s … did you mean t-cpuinfo:show_cpuinfo/cpu?choice/caseHow it works:
float64only if it is a whole number. A wholefloat64is what a JSON round trip makes of an int.lookupChildhelper resolves data-node names throughchoice/caseentries, 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).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:aesfails).internal/yang/pattern.go. It translates XSD regex to an anchored RE2 regexp:^/$become literals,.becomes[^\n\r],\d/\s/\wget their XSD Unicode meaning, and\Sor\winside a positive class becomes an alternation. Compiled patterns are cached. All of a type's patterns must match (goyang already merges typedef chains). goyang dropsmodifier 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\Sor\w. For those the validator gives a warning, once perValidatecall: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.Validateadds 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/mainand passes on this branch.#50. Template
CPU {{ id | to_float }}intoleaf id { type uint8; }, inputCPU 12.5:Valid=true, no errorsField '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}"; } }:0011.2233.44zzand0011.2233.4455.6677both validField '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.4455is valid.TestXSDToRE2MatchesYangsonchecks the translation on 74 pattern/value pairs, with expectations from yangson's own regex translation (elementpath 5.1.4translate_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; } } }:state=sidewaysandspeed=fastboth validField 'state' has invalid value "sideways" in item 0, expected one of up, downandField 'speed' has invalid value "fast" …#57. The research doc's
show cpuinfoexample withyang="t-cpuinfo:show_cpuinfo", inputCPU0 12.50%/CPU1 140.00%:Valid=true, 4 "not defined" warnings, and the out-of-range140.00is never seenValid=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"; } } } }, inputeth0 mtu 20:Valid=true, warningField 'mtu' not defined in YANG schemaField 'mtu' value is out of range in item 0.mtu 1500is 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:
12.0for an integer leaf: yangson rejects it because it is a JSON float. gottp accepts it because gottp's own data can arrive throughencoding/json, which turns ints intofloat64.\sand\woutside a class: elementpath passes them to Python'sreunchanged, so for yangson\smatches\fand\wmatches_. 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\wexcludes\p{P}, which includes_. Inside a class, elementpath already uses XSD semantics, so yangson and gottp agree that[\w\-]+rejectsa_b. The 3 cases are markeddiffersinpattern_test.go.re. gottp warns and skips them (see YANG: pattern statements are never enforced #55 above).Checks
go vet ./...,go test ./...,go test -race(all packages excepttest/memory, withGOTTP_PYTHONset), staticcheck 2025.1.1 andGOOS=js GOARCH=wasm go build ./...all pass locally.yang=.Merge Danger
Stricter validation means output that passed before can now fail. The parsed
Datanever changes; onlyParseResult.ValidationResults(and the editor's markers and notification) can go fromValid=truetoValid=false.CompiledTemplate.Parse()discards validation results, so it is unaffected. Who can see a change: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.pattern(YANG: pattern statements are never enforced #55). This includesietf-inet-typesandietf-yang-typestypedefs (ipv4-address,mac-address, …). Captured values that don't match exactly now fail. For example, a MAC captured in Cisco dotted form againstyang:mac-addressused to pass and now fails, correctly.Upvsup) now fail. Identityrefs from another module must be written asmodule:identity.RLockper pattern check to read the invert-match table. No allocation per value beyond theregexpmatch.ValidationErrorgets 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