Debian Codegraph @

frontend/src/api/validators.ts

raw
1 import { account_hierarchy_validator } from "../charts/hierarchy";
2 import {
3 Document,
4 entryBaseValidator,
5 entryValidator,
6 Event,
7 Transaction,
8 } from "../entries";
9 import type { ValidationT } from "../lib/validation";
10 import {
11 array,
12 boolean,
13 date,
14 number,
15 object,
16 optional,
17 record,
18 string,
19 tuple,
20 unknown,
21 } from "../lib/validation";
22 import { query_validator } from "../reports/query/query_table";
23
24 /** A Beancount error that should be shown to the user in the list of errors. */
25 export interface BeancountError {
26 readonly type: string;
27 readonly message: string;
28 readonly source: {
29 readonly filename: string;
30 readonly lineno: number;
31 } | null;
32 }
33
34 /** Validator for a BeancountError. */
35 const error_validator = object<BeancountError>({
36 type: string,
37 message: string,
38 source: optional(object({ filename: string, lineno: number })),
39 });
40
41 /** Validator for the details for a single account. */
42 const account_details = record(
43 object({
44 balance_string: optional(string),
45 close_date: optional(date),
46 last_entry: optional(object({ date, entry_hash: string })),
47 uptodate_status: optional(string),
48 }),
49 );
50
51 /** Validator for the Fava options that are used in the frontend. */
52 const fava_options = object({
53 auto_reload: boolean,
54 currency_column: number,
55 conversion_currencies: array(string),
56 collapse_pattern: array(string),
57 import_config: optional(string),
58 indent: number,
59 invert_income_liabilities_equity: boolean,
60 show_closed_accounts: boolean,
61 show_accounts_with_zero_balance: boolean,
62 show_accounts_with_zero_transactions: boolean,
63 locale: optional(string),
64 uptodate_indicator_grey_lookback_days: number,
65 insert_entry: array(
66 object({ date: string, filename: string, lineno: number, re: string }),
67 ),
68 use_external_editor: boolean,
69 });
70
71 /** Validator for the Beancount options that are used in the frontend. */
72 const options = object({
73 documents: array(string),
74 filename: string,
75 include: array(string),
76 name_assets: string,
77 name_equity: string,
78 name_expenses: string,
79 name_income: string,
80 name_liabilities: string,
81 operating_currency: array(string),
82 title: string,
83 });
84
85 const extensions = array(
86 object({
87 name: string,
88 report_title: optional(string),
89 has_js_module: boolean,
90 }),
91 );
92
93 export const ledgerDataValidator = object({
94 account_details,
95 accounts: array(string),
96 base_url: string,
97 currencies: array(string),
98 currency_names: record(string),
99 errors: array(error_validator),
100 extensions,
101 fava_options,
102 have_excel: boolean,
103 incognito: boolean,
104 links: array(string),
105 options,
106 other_ledgers: array(tuple(string, string)),
107 payees: array(string),
108 precisions: record(number),
109 sidebar_links: array(tuple(string, string)),
110 tags: array(string),
111 upcoming_events_count: number,
112 user_queries: array(object({ name: string, query_string: string })),
113 years: array(string),
114 });
115
116 export type LedgerData = ValidationT<typeof ledgerDataValidator>;
117
118 const importable_files_validator = array(
119 object({
120 name: string,
121 basename: string,
122 importers: array(
123 object({
124 account: string,
125 importer_name: string,
126 date: string,
127 name: string,
128 }),
129 ),
130 }),
131 );
132
133 const date_range = object({ begin: date, end: date });
134
135 const commodities = array(
136 object({ base: string, quote: string, prices: array(tuple(date, number)) }),
137 );
138
139 export type Commodities = ValidationT<typeof commodities>;
140
141 const context = object({
142 entry: entryBaseValidator,
143 balances_before: optional(record(array(string))),
144 balances_after: optional(record(array(string))),
145 sha256sum: string,
146 slice: string,
147 });
148
149 const account_budget = object({
150 budget: record(number),
151 budget_children: record(number),
152 });
153 export type AccountBudget = ValidationT<typeof account_budget>;
154
155 /** One of the Beancount source files. */
156 export interface SourceFile {
157 readonly file_path: string;
158 readonly sha256sum: string;
159 readonly source: string;
160 }
161 const source = object<SourceFile>({
162 file_path: string,
163 sha256sum: string,
164 source: string,
165 });
166
167 const tree_report = object({
168 charts: unknown,
169 trees: array(account_hierarchy_validator),
170 date_range: optional(date_range),
171 });
172
173 export const getAPIValidators = {
174 balance_sheet: tree_report,
175 account_report: object({
176 charts: unknown,
177 journal: optional(string),
178 dates: optional(array(date_range)),
179 interval_balances: optional(array(account_hierarchy_validator)),
180 budgets: optional(record(array(account_budget))),
181 }),
182 changed: boolean,
183 commodities,
184 context,
185 documents: array(Document.validator),
186 errors: array(error_validator),
187 events: array(Event.validator),
188 extract: array(entryValidator),
189 imports: importable_files_validator,
190 income_statement: tree_report,
191 ledger_data: ledgerDataValidator,
192 move: string,
193 options: object({
194 fava_options: record(string),
195 beancount_options: record(string),
196 }),
197 payee_accounts: array(string),
198 payee_transaction: Transaction.validator,
199 query: query_validator,
200 source,
201 trial_balance: tree_report,
202 };
203
204 export type GetAPIValidators = typeof getAPIValidators;