fix(invoices): accept empty self-billing fields on invoice create (#920)
#911 added self-billing fields to the shared CreateInvoiceSchema with external_invoice_number: z.string().min(1), but the invoice form has always sent that field (plus self_billing_agreement_ref and received_date) as '' on every normal invoice. The empty string failed min(1), so every invoice create returned 400. Normalise the empty optional self-billing strings to undefined in the schema (matching the existing optionalIsoDate / deduction_brf_org_number patterns), and strip the unused empty carriers client-side before the form POSTs. Required-when-self-billed is still enforced post-parse in the v1 route, so the self-billed path is unaffected. Adds schema regression tests. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
27b88426e2
commit
cbffcd7292
+16
-3
@@ -401,9 +401,22 @@ export const CreateInvoiceSchema = z.object({
|
||||
// invoice. A plain optional flag (no schema refine) so UpdateInvoiceSchema's
|
||||
// .omit() keeps working on this object.
|
||||
is_self_billed: z.boolean().optional(),
|
||||
external_invoice_number: z.string().min(1).max(64).optional(),
|
||||
self_billing_agreement_ref: z.string().max(128).optional(),
|
||||
received_date: isoDate.optional(),
|
||||
// The dashboard invoice form always sends these self-billing fields (default
|
||||
// '' in create/edit mode) even for a normal invoice, so an empty string must
|
||||
// read as "not provided", not fail validation. Otherwise a plain
|
||||
// external_invoice_number: '' trips the min(1) and 400s every regular invoice
|
||||
// create. Required-when-self-billed is still enforced in the v1 route via a
|
||||
// falsy check after parse, so normalising '' -> undefined here is safe.
|
||||
external_invoice_number: z
|
||||
.union([z.string().min(1).max(64), z.literal('')])
|
||||
.transform((v) => v || undefined)
|
||||
.optional(),
|
||||
self_billing_agreement_ref: z
|
||||
.string()
|
||||
.max(128)
|
||||
.transform((v) => v || undefined)
|
||||
.optional(),
|
||||
received_date: optionalIsoDate,
|
||||
items: z.array(CreateInvoiceItemSchema).min(1, 'At least one item is required'),
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user