@davoodmehraban89 / Aram-2 Mirror · read-only / commits / 3b2f725
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 3, 18:15 UTC

fix(cli): validate decimal strings, policy fields and transaction company-year scope

davoodmehraban89 committed Oct 31, 2026 at 18:05 UTC 3b2f7258d6e95afeb731e12bc33980280a1968dc
1 file changed +45 -11
backend/src/aram_backend/five_statement_pipeline_cli.py
+45 -11
@@ -23,7 +23,7 @@ from aram_backend.five_statement_pipeline import (
23 FiveStatementPipelineResult, build_five_statement_pipeline,
24 )
25 from aram_backend.income_statement_candidate import IncomeStatementLinePolicy
26 -from aram_backend.reconciliation_review import NoteReference
26 +from aram_backend.reconciliation_review import NoteReference, _money
27 from aram_backend.reconciliation_review_cli import _strict_pairs
28
29
@@ -84,6 +84,7 @@ def _rows(
84 factory: type, maximum: int, *, minimum: int = 0,
85 string_fields: tuple[str, ...] = (),
86 sign_field: str | None = None,
87 + amount_fields: tuple[str, ...] = (),
88 ) -> tuple[Any, ...]:
89 items = payload[key]
90 if (not isinstance(items, list) or len(items) < minimum
@@ -93,11 +94,26 @@ def _rows(
94 for item in items:
95 if not isinstance(item, dict) or set(item) != fields:
96 raise CoverageError("PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID")
96 - if any(
97 - not isinstance(item[field], str) or not item[field]
98 - for field in string_fields
99 - ):
100 - raise CoverageError("PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID")
97 + for field in string_fields:
98 + value = item[field]
99 + if (not isinstance(value, str) or not value
100 + or value != value.strip() or len(value) > 256
101 + or any(ord(char) < 32 or ord(char) == 127 for char in value)):
102 + raise CoverageError(
103 + "PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID"
104 + )
105 + for field in amount_fields:
106 + value = item[field]
107 + if not isinstance(value, str) or value != value.strip():
108 + raise CoverageError(
109 + "PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID"
110 + )
111 + try:
112 + _money(value)
113 + except CoverageError as exc:
114 + raise CoverageError(
115 + "PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID"
116 + ) from exc
117 if sign_field is not None and (
118 type(item[sign_field]) is not int or item[sign_field] not in (-1, 1)
119 ):
@@ -145,6 +161,13 @@ def parse_pipeline_manifest(content: bytes) -> ParsedPipelineManifest:
161 or any(ord(char) < 32 or ord(char) == 127 for char in value)):
162 raise CoverageError("PIPELINE_MANIFEST_CURRENCY_UNIT_INVALID")
163
164 + for item in payload["transactions"]:
165 + if (isinstance(item, dict) and
166 + (item.get("company_code") != company
167 + or type(item.get("fiscal_year")) is not int
168 + or item.get("fiscal_year") != year)):
169 + raise CoverageError("PIPELINE_MANIFEST_TRANSACTION_SCOPE_MISMATCH")
170 +
171 return ParsedPipelineManifest(
172 company_code=company,
173 fiscal_year=year,
@@ -153,39 +176,50 @@ def parse_pipeline_manifest(content: bytes) -> ParsedPipelineManifest:
176 references=_rows(
177 payload, "references", REFERENCE_FIELDS, NoteReference,
178 MAX_POLICY_ROWS, minimum=1,
156 - string_fields=("expected_closing_exposure",),
179 + string_fields=("statement", "section", "note_number", "evidence_ref"),
180 + amount_fields=("expected_closing_exposure",),
181 ),
182 balance_sheet_policies=_rows(
183 payload, "balance_sheet_policies", BS_POLICY_FIELDS,
184 BalanceSheetLinePolicy, MAX_POLICY_ROWS, sign_field="sign_multiplier",
185 + string_fields=("statement_section", "note_number", "category",
186 + "line_label", "policy_evidence_ref"),
187 ),
188 income_statement_policies=_rows(
189 payload, "income_statement_policies", IS_POLICY_FIELDS,
190 IncomeStatementLinePolicy, MAX_POLICY_ROWS, sign_field="sign_multiplier",
191 + string_fields=("statement_section", "note_number", "category",
192 + "line_label", "policy_evidence_ref"),
193 ),
194 equity_movement_policies=_rows(
195 payload, "equity_movement_policies", EQ_POLICY_FIELDS,
196 EquityMovementPolicy, MAX_POLICY_ROWS, sign_field="sign_multiplier",
197 + string_fields=("statement_section", "note_number", "equity_component",
198 + "movement_category", "line_label", "policy_evidence_ref"),
199 ),
200 cash_accounts=_rows(
201 payload, "cash_accounts", CASH_ACCOUNT_FIELDS,
202 CashAccountPolicy, MAX_CASH_ACCOUNTS, minimum=1,
203 + string_fields=("account_code", "evidence_ref"),
204 ),
205 counterpart_policies=_rows(
206 payload, "counterpart_policies", COUNTERPART_FIELDS,
207 CashCounterpartPolicy, MAX_CASH_COUNTERPART_POLICIES,
208 + string_fields=("account_code", "activity", "evidence_ref"),
209 ),
210 transactions=_rows(
211 payload, "transactions", TRANSACTION_FIELDS,
212 CashFlowEntry, MAX_CASH_TRANSACTIONS,
181 - string_fields=(
182 - "cash_debit", "cash_credit",
183 - "counterpart_debit", "counterpart_credit",
184 - ),
213 + string_fields=("company_code", "line_id", "transaction_id",
214 + "posting_date", "cash_account_code",
215 + "counterpart_account_code", "evidence_ref"),
216 + amount_fields=("cash_debit", "cash_credit",
217 + "counterpart_debit", "counterpart_credit"),
218 ),
219 cash_balance_notes=_rows(
220 payload, "cash_balance_notes", CASH_NOTE_FIELDS,
221 CashBalanceNoteSelection, MAX_POLICY_ROWS, minimum=1,
222 + string_fields=("statement_section", "note_number", "evidence_ref"),
223 ),
224 )
225