README
¶
genrules - EN 16931 Business Rule Generator
Auto-generates Go code for EN 16931 electronic invoicing business rules from official schematron specifications.
Overview
genrules parses EN 16931 schematron XML files and generates type-safe Go constants for business rule validation. It extracts rule codes, BT-/BG- field references, and descriptions from the official CEN/TC 434 specifications.
Installation
go install github.com/speedata/einvoice/cmd/genrules@latest
Or build from source:
go build -o genrules cmd/genrules/main.go
Usage
Basic Usage
Generate rules from a URL:
genrules \
--source https://raw.githubusercontent.com/ConnectingEurope/eInvoicing-EN16931/master/cii/schematron/abstract/EN16931-CII-model.sch \
--version v1.3.14.1 \
--package rules \
--output rules/en16931.go
Generate rules from a local file:
genrules \
--source path/to/EN16931-CII-model.sch \
--version v1.3.14.1 \
--package rules \
--output rules/en16931.go
Using go generate
The recommended workflow uses go generate for reproducible builds:
# Generate from rules/ directory
cd rules && go generate
# Or generate all packages
go generate ./...
Flags
--source(required): Path or URL to schematron XML file--version(required): Source file version (e.g., "v1.3.14.1")--package(default: "rules"): Target Go package name--output(default: "rules/en16931.go"): Output file path
Output Format
Generates Go code with:
- Package declaration and Rule struct definition
- Generation metadata (source, version, timestamp)
- 203 business rule constants from EN 16931 specification
- Sorted by rule code (BR-1, BR-2, ..., BR-CO-10, ..., BR-S-1, ...)
Example output:
// Code generated by genrules from EN16931-CII-model.sch; DO NOT EDIT.
// Source: https://github.com/ConnectingEurope/eInvoicing-EN16931
// Version: v1.3.14.1
// Generated: 2025-10-07T12:31:26Z
package rules
type Rule struct {
Code string
Fields []string
Description string
}
var (
BR1 = Rule{
Code: "BR-01",
Fields: []string{"BT-24"},
Description: `An Invoice shall have a Specification identifier (BT-24).`,
}
// ... 202 more rules
)
Rule Naming Conventions
The generator converts schematron rule codes to Go identifiers:
BR-01→BR1(remove leading zeros)BR-S-08→BRS8(remove dashes and leading zeros)BR-CO-14→BRCO14(remove all dashes)BR-AE-1→BRAE1(standard format)
Architecture
Parsing
- Fetch schematron XML from URL or file
- Parse
<pattern>,<rule>, and<assert>elements - Extract rule code from assert @id attribute
- Extract description from assert test text
- Extract BT-/BG- field references using regex
\(B[TG]-\d+\)
Code Generation
- Clean descriptions (remove [BR-XX]- prefix, normalize whitespace)
- Deduplicate and sort field references
- Convert rule codes to Go identifiers
- Sort rules alphabetically by code
- Generate Go code using text/template
- Format output with gofmt
Custom Rules
The generated rules/en16931.go file includes a custom rules section that must be manually preserved:
- Check: Line total calculation validation
- BR34-40: Non-negative amount validations (not in official spec)
- BRIG1-10: IGIC aliases (BR-AF-* in spec)
- BRIP1-10: IPSI aliases (BR-AG-* in spec)
⚠️ Important: When regenerating rules, manually add back the custom rules section marked by the comment banner.
Testing
The generator includes built-in validation:
- Verifies 200+ rules are extracted
- Checks rule code format
- Validates field references (BT-/BG- format)
- Ensures descriptions are non-empty
- Reports generation statistics
Sources
- EN 16931 Specification: ConnectingEurope/eInvoicing-EN16931
- Schematron File:
cii/schematron/abstract/EN16931-CII-model.sch - Current Version: v1.3.14.1
Maintenance
To update rules to a new EN 16931 version:
- Check for new releases at ConnectingEurope/eInvoicing-EN16931
- Update
--versionflag inrules/en16931.gogo:generate directive - Run
cd rules && go generate - Manually re-add custom rules section
- Run tests:
go test ./... - Commit changes with version number in message
License
Generated rules are derived from the EN 16931 specification, which is maintained by CEN/TC 434.