rules: document tests outside rules/is, prefer plain rules - #911
Open
samlown wants to merge 1 commit into
Open
Conversation
Add an inventory of the rules.Test implementations exported by num, cal, cbc, tax, org, bill, currency, head and uuid, and state the preference order: generic tests, then domain tests, then a custom function. Add a section showing nested Field, num and tax tests, and per-field assertions in place of custom is.Func helpers. Correct the documented fault path format to the JSON Paths rooted at $ that HasPath matches, and note that faults sharing a code and message merge into one entry. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #911 +/- ##
=======================================
Coverage 94.87% 94.87%
=======================================
Files 320 320
Lines 17431 17431
=======================================
Hits 16537 16537
Misses 553 553
Partials 341 341 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Docs-only change to
rules/README.md, prompted by review of gobl.dk.oioubl#8, where a large number ofis.Funchelpers were written for checks the framework and domain packages already cover.Tests exist outside
rules/isThe "Available tests" section only documented
rules/is, so there was nothing pointing addon authors at the tests each domain package already exports. Added a preference order (generic → domain → custom function) and a new Domain tests outsiderules/issection inventorying every exportedrules.Testin the repo:num—Positive,ZeroOrPositive,NotZero,Min/Max/Equals,.Exclusive()cal—DateNotZero/After/Beforeplus theDateTimeandTimestampequivalentscbc—InCodes,InCodeDefs,InKeyDefs,HasValidKeyIn,CodeMapHastax— the sixExtensions*rules,ExtensionHasValidCode,SetHasCategory/SetHasOneOf,RegimeIn,IdentityIn,HasAddon/AddonIn,TagsInorg,bill,currency,head,uuid— identity and attribute tests, the*TypeInWhenguards,RequireLineTaxCategory,CanConvertTo,StampsHas, UUID version testsCloses with a note asking authors to push generally-useful tests down into the type's own package rather than keeping them private to an addon.
Prefer plain rules over helper functions
New section leading "Common patterns", with three before/after pairs drawn from the patterns in that PR:
is.Functhat type-asserts to reach a nested value → nestedrules.Field+AssertIfPresent(is.In(...))num.NotZero,tax.SetHasCategorytotals.payableandtotals.dueseparately instead of collapsing ontototalsThe point worth the most: a helper's
val.(*bill.Tax)guard returnstrueon a type mismatch, so a field rename silently disables the rule with no test failure, whereasFieldvalidates its name against the struct at initialisation and panics. The section ends by saying custom functions remain the right answer for genuine cross-field logic and domain lookups.All three "Prefer" examples were compile-run against real
bill.Invoicedata before being documented — the five rules fire with exactly the codes and paths shown, and an emptyroundingis correctly skipped.Fault format correction
Found while verifying the above: the Testing section documented
faults.HasPath("field")and an error format of[CODE] field: description.HasPathmatches JSON Paths rooted at$exactly, andFault.Errorrenders[CODE] ($.field) description. Corrected both, and documented that faults sharing a code and message merge into one entry listing every path.No CHANGELOG entry — docs only.
🤖 Generated with Claude Code