Metadata-Version: 2.5
Name: isorailkit
Version: 0.1.1
Summary: Independent developer toolkit derived from the ISO 20022 financial messaging standard: safe parsing, base XSD validation, readable errors, identifier checks and MT103 to pacs.008 translation.
Project-URL: Homepage, https://github.com/OpenPayOps/isorailkit
Project-URL: Repository, https://github.com/OpenPayOps/isorailkit
Project-URL: Documentation, https://github.com/OpenPayOps/isorailkit/blob/main/docs/ARCHITECTURE.md
Project-URL: Issues, https://github.com/OpenPayOps/isorailkit/issues
Project-URL: Changelog, https://github.com/OpenPayOps/isorailkit/releases
Author: Aldrine Einsteen
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: bic,iban,iso20022,lei,mt103,pacs.008,payments,validation,xsd
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Office/Business :: Financial
Classifier: Topic :: Text Processing :: Markup :: XML
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: lxml>=5.0
Requires-Dist: pydantic>=2.6
Requires-Dist: rich>=13.0
Requires-Dist: typer>=0.12
Description-Content-Type: text/markdown

# isorailkit

[![CI](https://github.com/OpenPayOps/isorailkit/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/OpenPayOps/isorailkit/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/isorailkit.svg)](https://pypi.org/project/isorailkit/)
[![Python versions](https://img.shields.io/pypi/pyversions/isorailkit.svg)](https://pypi.org/project/isorailkit/)

An independent developer toolkit derived from the ISO 20022 financial messaging standard.

`isorailkit` is stateless and works offline. It provides:

- **Safe XML parsing.** DTDs, external entities and entity expansion are rejected.
- **Validation against the base ISO 20022 XSDs, or your own.** A set of base schemas is bundled from `iso20022.org` (`src/isorailkit/schemas/iso20022_base/`). You can add any other XSD at runtime, such as newer message versions or usage-guideline schemas you are licensed to use. Every message part is checked, including parts wrapped in an envelope.
- **Plain-English errors.** Each problem comes back as a structured `ValidationIssue` with a readable XPath, line number and remediation hint. Raw `lxml` output never reaches you.
- **Pure-Python identifier checks.** IBAN (ISO 7064 Mod-97 and registry lengths), BIC (ISO 9362), LEI (ISO 17442), UETR (UUIDv4) and ISO 4217 minor-unit precision.
- **MT103 → `pacs.008.001.08` translation.** Every field that can't be carried over faithfully is reported.
- **A CLI** with stable exit codes and clean `--json` output.

It contains no web framework, no database and no market-infrastructure rules (CBPR+, SEPA, CHAPS, FedNow).

> isorailkit is not affiliated with, endorsed by, or certified by ISO or SWIFT.

**Documentation:** [Architecture](https://github.com/OpenPayOps/isorailkit/blob/main/docs/ARCHITECTURE.md) covers the layers, the validation pipeline, the XML security model, the schema registry, the error contract and the release process.

## Install

```bash
pip install isorailkit
```

Requires Python 3.11 or later. Runtime dependencies: `lxml`, `typer`, `rich`, `pydantic`.

## CLI

```bash
isorailkit validate payment.xml            # human-readable report
isorailkit validate payment.xml --json     # ValidationResult as JSON
isorailkit validate - < payment.xml        # read from stdin
isorailkit validate msg.xml --schema pacs.008.001.08   # force a schema by message ID
isorailkit validate msg.xml -S ./my-schemas   # add your own XSDs (file or directory)
isorailkit schemas                         # list available message types
isorailkit check iban "DE89 3704 0044 0532 0130 00"
isorailkit translate mt103 mt103.txt -o pacs008.xml
```

| Exit code | Meaning |
|---|---|
| `0` | Valid. Warnings may still be reported. |
| `1` | Schema or semantic validation failed. |
| `2` | The input could not be validated: unreadable file, malformed XML, a forbidden DTD, a non-ISO 20022 document, a message version with no available schema, or a supplied schema that cannot be loaded (`SCHEMA_UNREADABLE` / `SCHEMA_INVALID`). |

With `--json`, stdout contains exactly one JSON document and no Rich formatting.

## Python API

```python
from isorailkit import validate_file, translate_mt103, is_valid_iban

result = validate_file("payment.xml")
print(result.status)  # "valid" | "invalid" | "error"
for issue in result.issues:
    print(issue.code, issue.xpath, issue.line)
    print(issue.message)
    print("fix:", issue.remediation_hint)

is_valid_iban("GB82WEST12345698765432")  # True

translation = translate_mt103(open("mt103.txt").read())
print(translation.xml)
for issue in translation.issues:  # unmapped / truncated / lossy fields
    print(issue.code, issue.message)
```

Example issue:

```json
{
  "code": "XSD_ENUMERATION",
  "severity": "error",
  "xpath": "/Document/FIToFICstmrCdtTrf/CdtTrfTxInf/ChrgBr",
  "line": 34,
  "message": "The value 'XXXX' of <ChrgBr> is not an allowed code. Allowed: DEBT, CRED, SHAR, SLEV.",
  "remediation_hint": "Use one of DEBT (debtor pays all), CRED (creditor pays all), SHAR (shared) or SLEV (per service level).",
  "part": "pacs.008.001.08"
}
```

Everything exported from `isorailkit` (see `isorailkit.__all__`) is part of the semantically versioned public API.

## Bundled schemas

| Family | Versions |
|---|---|
| pacs.008 | 001.08, 001.14 |
| pacs.009 | 001.08, 001.13 |
| pacs.002 | 001.10, 001.16 |
| pacs.004 | 001.09, 001.15 |
| pain.001 | 001.09, 001.13 |
| pain.002 | 001.10, 001.15 |
| camt.053 | 001.08, 001.14 |
| camt.054 | 001.08, 001.14 |
| head.001 | 001.02 |

The schemas are discovered at runtime with `importlib.resources`, so dropping a new base XSD into `iso20022_base/` is enough to support it. The test suite then requires a matching valid fixture for it.

Only freely redistributable base schemas are bundled. For anything else, supply the XSD yourself (see below).

## Using your own schemas

In practice you'll need schemas that cannot ship with an open-source package: other message versions, market-infrastructure usage-guideline XSDs you are licensed to use, or your bank's own variants. Load them at runtime from local files. isorailkit never fetches schemas over the network.

```bash
isorailkit validate payment.xml --schema-path ./schemas/cbpr        # a directory (scanned recursively)
isorailkit validate payment.xml -S pacs.008.001.10.xsd -S ./more    # files and directories, repeatable
export ISORAILKIT_SCHEMA_PATH=./schemas/cbpr:./schemas/bank          # ';' on Windows
isorailkit validate payment.xml --no-bundled -S ./schemas/cbpr       # only your schemas
isorailkit schemas -S ./schemas/cbpr                                 # shows where each schema comes from
```

`--schema-path`, `--no-bundled` and the environment variable work with `validate`, `schemas` and `translate mt103`.

```python
from isorailkit import SchemaRegistry, validate_file

registry = SchemaRegistry(["./schemas/cbpr"])  # or SchemaRegistry.from_env([...])
result = validate_file("payment.xml", schemas=registry)
print(result.parts[0].schema_source)  # "bundled" or the XSD's path
```

- **Precedence.** Your schemas override bundled ones with the same message ID. For example, a usage-guideline `pacs.008.001.08` XSD replaces the base one. Among your paths, the first one that declares an ID wins, and `--schema-path` entries come before `ISORAILKIT_SCHEMA_PATH` entries. Two files in the same directory that declare the same ID are an error.
- **Discovery.** A schema must have a `urn:iso:std:iso:20022:tech:xsd:<message id>` target namespace and a global element (`Document` or `AppHdr`). While scanning a directory, files without one are treated as include/import fragments and skipped.
- **Includes and imports.** `xs:include` and `xs:import` can load local files inside the directory you supplied (for a single file, inside its directory). URLs, paths outside that directory (including through symlinks) and files with a `DOCTYPE` are refused.
- **Errors.** A schema that cannot be loaded never raises a stack trace. You get a `SCHEMA_UNREADABLE` or `SCHEMA_INVALID` issue with exit code `2`.
- **Library default.** The Python API never reads the environment variable unless you call `SchemaRegistry.from_env()`. Without `schemas=`, validation uses the bundled set only.

## MT103 translation

The translator is an independent mapping onto the base ISO `pacs.008.001.08` schema. It is not an official SWIFT translation, and it applies no CBPR+ or other usage guidelines.

- **Coverage:** it handles header blocks 1–3 (sender and receiver BICs, priority, UETR) and every MT103 field: 13C, 20, 23B, 23E, 26T, 32A, 33B, 36, 50a, 51A, 52a–57a, 59a, 70, 71A, 71F, 71G, 72, 77B and 77T.
- **Reporting:** anything dropped, truncated or approximated is reported as `MT_FIELD_UNMAPPED`, `MT_FIELD_TRUNCATED` or `MT_FIELD_LOSSY`.
- **Output check:** every output is validated against the bundled XSD.

## Security

All XML, including bundled and user-supplied XSDs, goes through `isorailkit.parser.xml_loader.create_safe_parser()`. Any `DOCTYPE` is rejected before libxml2 sees the input. The parser carries a resolver that refuses every external load, and entity expansion and network access are disabled. User-supplied XSDs are the only exception: they get a sandboxed resolver that allows local includes and imports inside the directory you supplied.

## Development

```bash
uv sync
uv run ruff check . && uv run ruff format --check .
uv run mypy src
uv run pytest
./scripts/verify_wheel.sh   # build, then install in an isolated venv and smoke-test
```

CI runs all of these on every pull request: lint and type checks, tests on Python 3.11–3.14 on Linux plus macOS and Windows, and the wheel check. See the [architecture document](https://github.com/OpenPayOps/isorailkit/blob/main/docs/ARCHITECTURE.md) for how the pieces fit together.

### Releasing

Releases are published to PyPI by GitHub Actions using Trusted Publishing, so no API tokens are stored.

1. Bump the version in `pyproject.toml` and `src/isorailkit/__init__.py`, then run `uv lock`.
2. Merge to `main` with CI green.
3. Publish a GitHub Release tagged `v<version>`, for example `v0.2.0`. The workflow:
   - runs the full CI gate;
   - checks that the tag matches the package version;
   - builds and verifies the wheel;
   - uploads to TestPyPI;
   - after a maintainer approves the `pypi` environment, uploads to PyPI.

## License

Apache-2.0
