A lightweight, modular, rules-based EDI validation system for X12 documents with web interface and multi-format reporting.
The EDI Compliance Rules Engine validates EDI X12 documents against industry standards and retailer-specific requirements. Built for speed and extensibility, it provides both CLI and web-based interfaces for validation workflows.
Validate EDI documents against:
- ✅ X12 standard requirements (ISA/GS/ST envelope structure)
- ✅ Document-specific rules (850 PO, 856 ASN, 810 Invoice)
- ✅ Segment-level cardinality and required elements
- ✅ Conditional logic (if-then rules)
- ✅ Data type, length, and format validation
- ✅ Retailer-specific compliance (Walmart, Amazon, Target)
Use Cases:
- 📋 EDI onboarding validation for new trading partners
- 🔍 Production compliance checks before transmission
- ✅ Trading partner certification and testing
- 🚀 Pre-transmission quality gates in integration pipelines
- 🛠️ Development and debugging of EDI documents
Many teams think “we’re compliant” because their EDI passes basic validation.
Retailers disagree.
This project simulates the reality that causes rejections and chargebacks:
- mandatory references
- conditional segments
- code list enforcement
- cross-field consistency checks
It’s built to demonstrate how production onboarding teams actually evaluate documents.
- Requirements thinking (“what does the customer actually need to pass?”)
- Explaining constraints to non-technical stakeholders
- Turning rules into testable checks
- Producing customer-friendly outputs (reports + next actions)
✅ Lightweight Custom Parser
- No heavy dependencies (no pyx12 or external libraries)
- Fast segment/element extraction (>10,000 segments/second)
- Line number tracking for precise error reporting
- Handles ISA/GS/ST envelope structures
✅ Rules-Driven Architecture
- JSON-based rule definitions (easy to read and extend)
- Three-tier hierarchy: Core → Document → Retailer
- Priority-based rule merging with override support
- No hardcoded validation logic
✅ Multi-Level Validation
- Required Segments: Enforces mandatory segments and cardinality
- Element Validation: Data type, length, allowed values, regex patterns
- Conditional Rules: If-then logic for context-dependent requirements
- Cross-Segment: Validates relationships between segments (e.g., CTT count matches PO1 count)
✅ Professional Reporting
- Text Report: Human-readable detailed analysis
- JSON Export: Machine-readable for API integration
- CSV Export: Spreadsheet-compatible issue list
- Dashboard: Visual summary with box-drawing graphics
✅ Severity Classification
- ERROR: Blocking issues that fail compliance
- WARNING: Review recommended but not blocking
- INFO: Best practice suggestions
✅ Web Interface (Streamlit)
- File upload, text paste, or sample file input
- Document type and retailer selection
- Real-time validation with progress indication
- Interactive results with 4-tab display
- Download all report formats
| Document | Description | Transaction Set |
|---|---|---|
| 850 | Purchase Order | X12 850 |
| 856 | Advance Ship Notice | X12 856 |
| 810 | Invoice | X12 810 |
Pre-configured validation rules for major retailers:
| Retailer | Rules | Severity Escalations | Custom Validations |
|---|---|---|---|
| Walmart | 15 rules | Ship-To address required (ERROR) | 10-digit PO format |
| Amazon | 12 rules | ASN references required | Amazon-specific codes |
| Target | 14 rules | Address validation stricter | Target PO format |
┌─────────────────────────────────────────────────────────┐
│ Input Layer │
│ - File upload / Text input / Sample files │
└────────────────────┬────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ EDI Parser │
│ - Segment splitting │
│ - Element extraction │
│ - Line number tracking │
│ - Metadata extraction │
└────────────────────┬────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Rule Loader │
│ - Load Core rules (X12 standard) │
│ - Load Document rules (850/856/810) │
│ - Load Retailer rules (Walmart/Amazon/Target) │
│ - Merge with priority: Retailer > Document > Core │
└────────────────────┬────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Validation Engine │
│ - Required Segment Validator │
│ - Element Validator │
│ - Conditional Rule Validator │
│ - Cross-Segment Validator │
│ - Error Collector │
└────────────────────┬────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Report Generator │
│ - Text Formatter (human-readable) │
│ - JSON Formatter (API integration) │
│ - CSV Formatter (spreadsheet) │
│ - Dashboard Formatter (visual summary) │
└────────────────────┬────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Output Layer │
│ - Console display / File export / Web UI │
└─────────────────────────────────────────────────────────┘
In real EDI operations, documents often pass basic X12 validation but still fail retailer onboarding or incur chargebacks due to retailer-specific business rules.
This project simulates how EDI compliance is validated in production environments before documents are exchanged with trading partners.
A mid-market supplier receives an 850 Purchase Order from a large retailer and automatically processes it into their ERP.
Although the document is syntactically valid X12, the retailer later rejects it because:
- required reference segments are missing,
- invalid code values are used, or
- conditional rules are violated (rules that apply only when certain segments or values are present).
This engine detects those issues upstream, preventing failed transmissions, onboarding delays, and retailer chargebacks.
Retailers commonly enforce stricter requirements than the base X12 specification.
This project models that reality through retailer-specific rule packs layered on top of core X12 and document-level rules.
Typical enforcement patterns include:
- Stricter required segments: segments optional in X12 may be mandatory for a specific retailer.
- Code list enforcement: only approved values are allowed for certain elements.
- Conditional logic: if segment A or value X is present, segment B becomes required.
- Cross-segment consistency checks: values must align across related segments and loops.
- Walmart — stricter reference and address enforcement
- Amazon — conditional requirements and code validation
- Target — segment cardinality and identifier constraints
These rule packs reflect real-world retailer onboarding behavior and demonstrate how the same document can pass base validation but fail retailer-specific compliance.
- Python 3.8 or higher
- pip package manager
# Clone the repository
git clone https://github.com/itsbrianhughes/edi-compliance-rules-engine.git
cd edi-compliance-rules-engine
# Install dependencies
pip install -r requirements.txt# Validate with base rules only
python -c "from src.validator.validation_engine import ValidationEngine; \
engine = ValidationEngine(); \
result = engine.validate_file('samples/edi_850_valid.txt', '850'); \
print(f'Compliant: {result.is_compliant()}, Errors: {result.error_count()}')"
# Validate with Walmart rules
python -c "from src.validator.validation_engine import ValidationEngine; \
engine = ValidationEngine(); \
result = engine.validate_file('samples/edi_850_valid.txt', '850', 'walmart'); \
print(f'Compliant: {result.is_compliant()}, Errors: {result.error_count()}')"# Start the Streamlit UI
streamlit run src/ui/streamlit_app.py
# Access at http://localhost:8501from src.parser.edi_parser import EDIParser
from src.rules.rule_loader import RuleLoader
from src.validator.validation_engine import ValidationEngine
from src.reporting.report_generator import ReportGenerator
# Parse EDI document
parser = EDIParser()
parsed_edi = parser.parse_file("samples/edi_850_valid.txt")
# Load rules (850 Purchase Order + Walmart requirements)
loader = RuleLoader()
rules = loader.load_rules("850", "walmart")
# Validate
engine = ValidationEngine()
result = engine.validate(parsed_edi, rules, "walmart")
# Generate reports
generator = ReportGenerator(result)
# Get text report
text_report = generator.generate_text_report()
print(text_report)
# Save all formats
generator.save_all_formats("output", "validation_report")
# Check compliance
if result.is_compliant():
print("✅ Document is compliant")
else:
print(f"❌ Found {result.error_count()} errors")
for error in result.get_errors():
print(f" - Line {error.line_number}: {error.message}")edi-compliance-rules-engine/
├── src/
│ ├── parser/
│ │ ├── edi_parser.py # Main EDI parser class
│ │ └── segment_utils.py # Low-level parsing utilities
│ ├── rules/
│ │ ├── rule_loader.py # Rule loading and merging
│ │ └── rule_definitions/
│ │ ├── x12_core.json # X12 standard rules
│ │ ├── doc_850.json # Purchase Order rules
│ │ ├── doc_856.json # ASN rules
│ │ ├── doc_810.json # Invoice rules
│ │ └── retailer_overrides/
│ │ ├── walmart.json # Walmart-specific rules
│ │ ├── amazon.json # Amazon-specific rules
│ │ └── target.json # Target-specific rules
│ ├── validator/
│ │ ├── validation_engine.py # Main validation orchestrator
│ │ ├── rule_evaluators.py # Specialized validators
│ │ └── error_collector.py # Error aggregation
│ ├── reporting/
│ │ ├── report_generator.py # Report generation interface
│ │ └── formatters.py # Text/JSON/CSV/Dashboard formatters
│ └── ui/
│ └── streamlit_app.py # Web interface
├── samples/
│ ├── edi_850_valid.txt # Valid Purchase Order
│ ├── edi_850_invalid.txt # Invalid PO (for testing)
│ ├── edi_856_valid.txt # Valid ASN
│ └── edi_810_valid.txt # Valid Invoice
├── tests/
│ ├── test_parser.py # Parser unit tests
│ ├── test_rules.py # Rule loader tests
│ └── test_validator.py # Validation engine tests
├── docs/
│ ├── architecture.md # System architecture
│ ├── rule_schema.md # Rule definition schema
│ ├── parser_usage.md # Parser documentation
│ ├── ui_guide.md # Web UI user guide
│ ├── PART_7_SUMMARY.md # Part 7 technical summary
│ └── deployment_guide.md # Deployment instructions
├── config/
│ └── settings.py # Configuration constants
├── output/ # Generated reports (gitignored)
├── demo_parser.py # Parser demonstration
├── demo_reports.py # Report generation demo
├── demo_ui_workflow.py # UI workflow demonstration
├── requirements.txt # Python dependencies
└── README.md # This file
streamlit run src/ui/streamlit_app.py- Select Document Type: 850 - Purchase Order
- Select Retailer: Walmart
- Choose Upload File and select your EDI file
- Click Run Validation
- View results in the Dashboard tab
- Download Text Report for detailed analysis
from src.validator.validation_engine import ValidationEngine
engine = ValidationEngine()
files = [
("samples/edi_850_valid.txt", "850", "walmart"),
("samples/edi_856_valid.txt", "856", "amazon"),
("samples/edi_810_valid.txt", "810", "target")
]
for file_path, doc_type, retailer in files:
result = engine.validate_file(file_path, doc_type, retailer)
print(f"{file_path}: {result.error_count()} errors")from src.validator.validation_engine import ValidationEngine
from src.reporting.report_generator import ReportGenerator
# Validate
engine = ValidationEngine()
result = engine.validate_file("samples/edi_850_valid.txt", "850", "walmart")
# Generate all reports
generator = ReportGenerator(result)
files = generator.save_all_formats("output", "walmart_850_validation")
# Files created:
# - output/walmart_850_validation.txt (text report)
# - output/walmart_850_validation.json (JSON export)
# - output/walmart_850_validation.csv (CSV issue list)
# - output/walmart_850_validation_dashboard.txt (dashboard)# Parser tests
python tests/test_parser.py
# Rule loader tests
python tests/test_rules.py
# Validation engine tests
python tests/test_validator.py# Parser demonstration
python demo_parser.py
# Report generation demonstration
python demo_reports.py
# UI workflow demonstration
python demo_ui_workflow.pyAll tests passing:
- ✅ Parser: 10/10 tests
- ✅ Rules: 17/17 tests
- ✅ Validator: 12/12 tests
Performance:
- Parse 21-segment EDI: < 2ms
- Load 3-tier rules: < 50ms
- Run validation: < 1ms
- Generate reports: < 10ms
| Operation | Time | Throughput |
|---|---|---|
| Parse 21-segment EDI | < 2ms | >10,000 segments/sec |
| Load rules (3-tier merge) | < 50ms | - |
| Validate document | < 1ms | >1,000 docs/sec |
| Generate 4 reports | < 10ms | - |
| End-to-end validation | < 200ms | >5 docs/sec |
Tested on: Standard development machine (4-core CPU, 8GB RAM)
Rules are defined in JSON format with five categories:
{
"rule_id": "850_REQ_BEG",
"segment_id": "BEG",
"severity": "ERROR",
"min_occurrences": 1,
"max_occurrences": 1
}{
"rule_id": "850_BEG03_PO_NUMBER",
"segment_id": "BEG",
"element_position": "03",
"severity": "ERROR",
"validations": {
"min_length": 1,
"max_length": 22,
"regex": "^[A-Z0-9]+$"
}
}{
"rule_id": "850_COND_N1_ST_ADDRESS",
"condition": {
"if_segment": "N1",
"if_element": "01",
"if_value": "ST"
},
"then": {
"required_segments": ["N3", "N4"]
}
}{
"rule_id": "850_CROSS_CTT_COUNT",
"validation_logic": {
"type": "count_match",
"count_segment": "CTT",
"count_element": "01",
"target_segment": "PO1"
}
}{
"rule_id": "850_COND_N1_ST_ADDRESS",
"override_type": "severity_escalation",
"original_severity": "WARNING",
"new_severity": "ERROR"
}See docs/rule_schema.md for complete documentation.
- Create rule file:
src/rules/rule_definitions/doc_XXX.json - Define required segments, element rules, conditionals
- Add sample file:
samples/edi_XXX_valid.txt - Update UI dropdown in
src/ui/streamlit_app.py
- Create override file:
src/rules/rule_definitions/retailer_overrides/RETAILER.json - Define retailer-specific rules and overrides
- Update UI dropdown in
src/ui/streamlit_app.py - Test with sample documents
- Create validator in
src/validator/rule_evaluators.py - Integrate into
ValidationEngineorchestrator - Update rule schema documentation
- Add unit tests in
tests/test_validator.py
See Deployment Guide for detailed instructions on:
- Production deployment
- Docker containerization
- Environment configuration
- CI/CD integration
This is a portfolio project demonstrating EDI integration engineering capabilities. If you'd like to use or extend it:
- Fork the repository
- Create a feature branch
- Make your changes with tests
- Submit a pull request
- No support for EDIFACT (only X12)
- Single document validation (no batch processing in UI)
- No real-time validation (must click "Run Validation")
- No validation history/logging
- No rule editing interface (rules are JSON files)
These are candidates for future enhancements.
Solution:
# Ensure you're in the project root directory
cd edi-compliance-rules-engine
# Install dependencies
pip install -r requirements.txtSolution:
# Install Streamlit
pip install streamlit>=1.28.0
# Run with explicit path
streamlit run src/ui/streamlit_app.pySolution:
- Check document type selection matches file content
- Verify retailer selection (retailer rules are stricter)
- Review detailed report for specific rule violations
MIT License - See LICENSE file for details
Built by Brian Hughes as part of an EDI Integration Engineering portfolio.
Contact:
- GitHub: @itsbrianhughes
- LinkedIn: https://www.linkedin.com/in/b-hughes/
- X12 EDI standards from ASC X12
- Inspired by real-world EDI integration challenges
- Built with Python, Streamlit, and open-source tools
Ready to validate your EDI documents? Get started:
git clone https://github.com/itsbrianhughes/edi-compliance-rules-engine.git
cd edi-compliance-rules-engine
pip install -r requirements.txt
streamlit run src/ui/streamlit_app.pyVisit http://localhost:8501 and start validating!



