New @settlekit/operator-console (Next.js 14) scaffold and its server-side libraries: typed operator API client (key sent only on private routes, envelope/error mapping), HMAC-signed owner session tokens with constant-time password check, proof view model with per-decision anchored status, policy drift rows, decision paging, bill form validation, attempt limiter, and the connect-your-business onboarding over the existing API. Tests include an onboarding run against the real API app. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
2.1 KiB
TypeScript
46 lines
2.1 KiB
TypeScript
/** Validation for the console's add-bill form (manual entry or pasted invoice). */
|
|
import type { BillInput } from "./api-client";
|
|
import type { FieldErrors, ParseResult } from "./onboarding";
|
|
|
|
const ADDRESS_RE = /^0x[a-fA-F0-9]{40}$/;
|
|
const USDC_RE = /^\d{1,12}(\.\d{1,6})?$/;
|
|
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
export const MAX_INVOICE_CHARS = 50_000;
|
|
|
|
const text = (form: Readonly<Record<string, unknown>>, key: string): string => {
|
|
const v = form[key];
|
|
return typeof v === "string" ? v.trim() : "";
|
|
};
|
|
|
|
export function parseBillForm(form: Readonly<Record<string, unknown>>): ParseResult<BillInput> {
|
|
const mode = text(form, "mode");
|
|
if (mode === "invoice") {
|
|
const invoiceText = text(form, "invoiceText");
|
|
if (invoiceText.length === 0) return { ok: false, errors: { invoiceText: "Paste the invoice text." } };
|
|
if (invoiceText.length > MAX_INVOICE_CHARS) return { ok: false, errors: { invoiceText: `Invoice text is limited to ${MAX_INVOICE_CHARS} characters.` } };
|
|
return { ok: true, value: { invoiceText } };
|
|
}
|
|
const errors: Record<string, string> = {};
|
|
const payee = text(form, "payee");
|
|
const amountUsdc = text(form, "amountUsdc");
|
|
const dueDate = text(form, "dueDate");
|
|
const description = text(form, "description");
|
|
const vendor = text(form, "vendor");
|
|
if (!ADDRESS_RE.test(payee)) errors.payee = "Payee must be a 0x wallet address.";
|
|
if (!USDC_RE.test(amountUsdc) || /^0+(\.0+)?$/.test(amountUsdc)) errors.amountUsdc = "Enter a positive USDC amount.";
|
|
if (!DATE_RE.test(dueDate) || Number.isNaN(Date.parse(`${dueDate}T00:00:00Z`))) errors.dueDate = "Enter a due date (YYYY-MM-DD).";
|
|
if (description.length === 0 || description.length > 500) errors.description = "Describe the bill (up to 500 characters).";
|
|
if (vendor.length > 200) errors.vendor = "Vendor name is limited to 200 characters.";
|
|
if (Object.keys(errors).length > 0) return { ok: false, errors: errors as FieldErrors };
|
|
return {
|
|
ok: true,
|
|
value: {
|
|
payee,
|
|
amountUsdc,
|
|
dueAt: new Date(`${dueDate}T00:00:00Z`).toISOString(),
|
|
description,
|
|
...(vendor ? { vendor } : {}),
|
|
},
|
|
};
|
|
}
|