Skip to content

fix(template_builder): tighten suggested BIC/VAT/IBAN value patterns - #761

Open
bosd wants to merge 1 commit into
invoice-x:masterfrom
bosd:fix/template-builder-tight-identifier-patterns
Open

bosd wants to merge 1 commit into
invoice-x:masterfrom
bosd:fix/template-builder-tight-identifier-patterns

Conversation

@bosd

@bosd bosd commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

`_VALUE_PATTERNS` in `src/invoice2data/extract/template_builder.py` used loose regexes:

```
'iban': r'[A-Z0-9 ]+',
'vat': r'[A-Z0-9]+',
'bic': r'[A-Z0-9]+',
```

These patterns are embedded into every field regex the CLI builder / db_templates "Suggest fields" wizard produces. On the sample document a suggestion is built from, the candidate detector's own strict pattern anchors things correctly — but when the same suggested template is later applied to a fresh PDF, the loose pattern happily grabs any adjacent uppercase-alnum blob near the label. An order code like `ORDER12345` matches the `bic` slot; downstream (Odoo's `base_business_document_import`) then rejects the field and the import fails.

Tighten each pattern to mirror the strict validators in `validators.py`:

kind old new
BIC `[A-Z0-9]+` `[A-Z]{6}[A-Z0-9]{2}(?:[A-Z0-9]{3})?` — ISO 9362
VAT `[A-Z0-9]+` `[A-Z]{2}[A-Z0-9]{8,14}`
IBAN `[A-Z0-9 ]+` `[A-Z]{2}\d{2}(?:[ \-]?[A-Z0-9]){11,30}`

Test

`tests/test_template_builder.py::test_bic_value_pattern_only_captures_bic_shape` — a doc containing both an order code and a valid BIC. Old pattern captured both; the tight one captures only the BIC.

All six template-builder tests green locally.

Test plan

  • `pytest tests/test_template_builder.py` — 6/6
  • `ruff check` / `ruff format --check` clean
  • CI: full matrix

Reported downstream while iterating on the account_invoice_import_invoice2data_db_templates "Suggest Fields" wizard (OCA/edi#1365) — Suggest was producing a BIC regex that captured an unrelated order code, so re-applying the template on a fresh PDF grabbed the wrong value.

The value patterns in _VALUE_PATTERNS get embedded into every regex the
CLI builder / db_templates Suggest Fields wizard generates for a
matched identifier candidate. They were loose:

    'iban': r'[A-Z0-9 ]+',
    'vat':  r'[A-Z0-9]+',
    'bic':  r'[A-Z0-9]+',

A suggestion built on a valid candidate is therefore fine on the
sample document (the candidate detector's own regex is strict, and the
label anchor picks the right value there), but when the same suggested
template is later applied to a fresh PDF the loose pattern grabs any
adjacent uppercase-alphanumeric blob near the label -- an order code
'ORDER12345' can match 'bic' shaped just fine, so downstream consumers
(Odoo's base_business_document_import) reject the field and the import
fails.

Tighten each pattern to mirror the strict validators in validators.py:

- BIC: ISO 9362 shape [A-Z]{6}[A-Z0-9]{2}(?:[A-Z0-9]{3})? (8 or 11
  chars, first 6 letters).
- VAT: 2 alpha country + 8-14 alphanumerics.
- IBAN: country + 2 digits + 11-30 alphanumeric body, tolerating
  embedded single-spaces/hyphens.

Regression test in tests/test_template_builder.py covers the BIC case
end-to-end (suggested template on a doc containing both an order code
and a valid BIC -> regex captures ONLY the BIC).

Reported downstream while iterating on the account_invoice_import_
invoice2data_db_templates Suggest Fields wizard (OCA/edi#1365).
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/invoice2data/extract
  template_builder.py
  tests
  test_template_builder.py
Project Total  

This report was generated by python-coverage-comment-action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant