Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: improve schema unwrapping

Sidwebworks7a191ec2b534
diff --git a/apps/erp/app/components/Form/AddressAutocomplete.tsx b/apps/erp/app/components/Form/AddressAutocomplete.tsx
index a842d15892..36c59f4d16 100644
--- a/apps/erp/app/components/Form/AddressAutocomplete.tsx
+++ b/apps/erp/app/components/Form/AddressAutocomplete.tsx
@@ -12,7 +12,7 @@ import {
useDebounce,
VStack
} from "@carbon/react";
-import { useLingui } from "@lingui/react/macro";
+import { Trans, useLingui } from "@lingui/react/macro";
import { useCallback, useEffect, useRef, useState } from "react";
import { useGooglePlaces } from "~/hooks/useGooglePlaces";
import Country from "./Country";
@@ -149,7 +149,7 @@ const AddressAutocomplete = ({
const addressAutocompleteField = (
<FormControl isInvalid={!!error}>
<FormLabel htmlFor={address1Field} isOptional={isAddressLine1Optional}>
- {t`Address Line 1`}
+ <Trans>Address Line 1</Trans>
</FormLabel>
<div className="relative w-full" ref={containerRef}>
<Command shouldFilter={false} className="bg-transparent">
diff --git a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx
index f2b8aed2a1..1eeab479f4 100644
--- a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx
+++ b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx
@@ -385,7 +385,9 @@ const PurchaseInvoiceLineForm = ({
/>
<FormControl className="col-span-2">
- <FormLabel isOptional>Description</FormLabel>
+ <FormLabel isOptional>
+ <Trans>Description</Trans>
+ </FormLabel>
<Input
value={itemData.description}
onChange={(e) =>
diff --git a/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceLineForm.tsx b/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceLineForm.tsx
index 8a6a9866cf..c3e72ace84 100644
--- a/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceLineForm.tsx
+++ b/apps/erp/app/modules/invoicing/ui/SalesInvoice/SalesInvoiceLineForm.tsx
@@ -373,7 +373,9 @@ const SalesInvoiceLineForm = ({
/>
<FormControl className="col-span-2">
- <FormLabel isOptional>{t`Description`}</FormLabel>
+ <FormLabel isOptional>
+ <Trans>Description</Trans>
+ </FormLabel>
<Input
value={itemData.description}
onChange={(e) =>
diff --git a/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx b/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx
index 5b2be060e6..3060e83468 100644
--- a/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx
+++ b/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx
@@ -1333,6 +1333,7 @@ function OperationForm({
<NumberControlled
name="setupTime"
label={t`Setup Time`}
+ isOptional={false}
minValue={0}
value={processData.setupTime}
onChange={(newValue) =>
@@ -1361,6 +1362,7 @@ function OperationForm({
<StandardFactor
name="setupUnit"
label={t`Setup Unit`}
+ isOptional={false}
hint={processData.setupUnitHint}
value={processData.setupUnit}
onChange={(newValue) => {
@@ -1446,6 +1448,7 @@ function OperationForm({
<NumberControlled
name="laborTime"
label={t`Labor Time`}
+ isOptional={false}
minValue={0}
value={processData.laborTime}
onChange={(newValue) =>
@@ -1474,6 +1477,7 @@ function OperationForm({
<StandardFactor
name="laborUnit"
label={t`Labor Unit`}
+ isOptional={false}
hint={processData.laborUnitHint}
value={processData.laborUnit}
onChange={(newValue) => {
@@ -1560,6 +1564,7 @@ function OperationForm({
<NumberControlled
name="machineTime"
label={t`Machine Time`}
+ isOptional={false}
minValue={0}
value={processData.machineTime}
onChange={(newValue) =>
@@ -1588,6 +1593,7 @@ function OperationForm({
<StandardFactor
name="machineUnit"
label={t`Machine Unit`}
+ isOptional={false}
hint={processData.machineUnitHint}
value={processData.machineUnit}
onChange={(newValue) => {
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx b/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
index fda96adff6..a936ce17bc 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
@@ -2478,6 +2478,7 @@ function OperationForm({
<NumberControlled
name="setupTime"
label={t`Setup Time`}
+ isOptional={false}
minValue={0}
value={processData.setupTime}
onChange={(newValue) =>
@@ -2490,6 +2491,7 @@ function OperationForm({
<StandardFactor
name="setupUnit"
label={t`Setup Unit`}
+ isOptional={false}
hint={processData.setupUnitHint}
value={processData.setupUnit}
onChange={(newValue) => {
@@ -2558,6 +2560,7 @@ function OperationForm({
<NumberControlled
name="laborTime"
label={t`Labor Time`}
+ isOptional={false}
minValue={0}
value={processData.laborTime}
onChange={(newValue) =>
@@ -2570,6 +2573,7 @@ function OperationForm({
<StandardFactor
name="laborUnit"
label={t`Labor Unit`}
+ isOptional={false}
hint={processData.laborUnitHint}
value={processData.laborUnit}
onChange={(newValue) => {
@@ -2640,6 +2644,7 @@ function OperationForm({
<NumberControlled
name="machineTime"
label={t`Machine Time`}
+ isOptional={false}
minValue={0}
value={processData.machineTime}
onChange={(newValue) =>
@@ -2652,6 +2657,7 @@ function OperationForm({
<StandardFactor
name="machineUnit"
label={t`Machine Unit`}
+ isOptional={false}
hint={processData.machineUnitHint}
value={processData.machineUnit}
onChange={(newValue) => {
diff --git a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx
index 40709a66ac..4b445c3c7b 100644
--- a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx
+++ b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx
@@ -416,7 +416,9 @@ const PurchaseOrderLineForm = ({
/>
<FormControl>
- <FormLabel isOptional>Description</FormLabel>
+ <FormLabel isOptional>
+ <Trans>Description</Trans>
+ </FormLabel>
<Input
value={itemData.description}
onChange={(e) =>
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
index 0ef4500a0b..8bdf92d7ac 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
@@ -2143,6 +2143,7 @@ function OperationForm({
<NumberControlled
name="setupTime"
label={t`Setup Time`}
+ isOptional={false}
minValue={0}
value={processData.setupTime}
onChange={(newValue) =>
@@ -2155,6 +2156,7 @@ function OperationForm({
<StandardFactor
name="setupUnit"
label={t`Setup Unit`}
+ isOptional={false}
hint={processData.setupUnitHint}
value={processData.setupUnit}
onChange={(newValue) => {
@@ -2222,6 +2224,7 @@ function OperationForm({
<NumberControlled
name="laborTime"
label={t`Labor Time`}
+ isOptional={false}
minValue={0}
value={processData.laborTime}
onChange={(newValue) =>
@@ -2234,6 +2237,7 @@ function OperationForm({
<StandardFactor
name="laborUnit"
label={t`Labor Unit`}
+ isOptional={false}
hint={processData.laborUnitHint}
value={processData.laborUnit}
onChange={(newValue) => {
@@ -2304,6 +2308,7 @@ function OperationForm({
<NumberControlled
name="machineTime"
label={t`Machine Time`}
+ isOptional={false}
minValue={0}
value={processData.machineTime}
onChange={(newValue) =>
@@ -2316,6 +2321,7 @@ function OperationForm({
<StandardFactor
name="machineUnit"
label={t`Machine Unit`}
+ isOptional={false}
hint={processData.machineUnitHint}
value={processData.machineUnit}
onChange={(newValue) => {
diff --git a/packages/form/src/internal/isFieldOptional.ts b/packages/form/src/internal/isFieldOptional.ts
index 106c2bdf09..bad06d5ffd 100644
--- a/packages/form/src/internal/isFieldOptional.ts
+++ b/packages/form/src/internal/isFieldOptional.ts
@@ -1,52 +1,104 @@
-import { z } from "zod";
+import { type ZodSchema, z } from "zod";
import { stringToPathArray } from "../utils";
-type ZodSchema = z.ZodTypeAny;
-
-function unwrapSchema(schema: ZodSchema): {
+type UnwrapResult = {
schema: ZodSchema;
isOptional: boolean;
-} {
- let current: ZodSchema = schema;
+ hasDefault: boolean;
+};
+
+function unwrapSchema(
+ schema: ZodSchema,
+ io: "input" | "output" = "output"
+): UnwrapResult {
+ let current = schema;
let isOptional = false;
+ let hasDefault = false;
+ const seen = new Set<ZodSchema>();
while (true) {
- if (
- current instanceof z.ZodOptional ||
- current instanceof z.ZodDefault ||
- current instanceof z.ZodCatch
- ) {
- isOptional = true;
- current = current._def.innerType;
- continue;
- }
-
- if (current instanceof z.ZodEffects) {
- current = current._def.schema;
- continue;
- }
-
- if (current instanceof z.ZodNullable) {
- current = current.unwrap();
- continue;
+ if (seen.has(current)) return { schema: current, isOptional, hasDefault };
+ seen.add(current);
+
+ const def: any = (current as any)._zod?.def ?? (current as any)._def;
+ const type: string | undefined = def?.type ?? def?.typeName;
+
+ switch (type) {
+ // optionality wrappers
+ case "optional":
+ case "ZodOptional":
+ isOptional = true;
+ current = def.innerType;
+ continue;
+
+ case "default":
+ case "ZodDefault":
+ isOptional = true;
+ hasDefault = true;
+ current = def.innerType;
+ continue;
+
+ case "prefault":
+ isOptional = true;
+ hasDefault = true;
+ current = def.innerType;
+ continue;
+
+ case "catch":
+ case "ZodCatch":
+ isOptional = true;
+ current = def.innerType;
+ continue;
+
+ // nullable — semantically distinct from optional
+ case "nullable":
+ case "ZodNullable":
+ current = def.innerType;
+ continue;
+
+ // transparent wrappers
+ case "readonly":
+ case "ZodReadonly":
+ current = def.innerType;
+ continue;
+
+ case "nonoptional":
+ isOptional = false;
+ current = def.innerType;
+ continue;
+
+ case "promise":
+ case "ZodPromise":
+ current = def.innerType;
+ continue;
+
+ case "ZodBranded":
+ current = def.type;
+ continue;
+
+ // lazy — resolve the thunk
+ case "lazy":
+ case "ZodLazy": {
+ const inner = (current as any)._zod?.innerType ?? def.getter?.();
+ if (!inner) return { schema: current, isOptional, hasDefault };
+ current = inner;
+ continue;
+ }
+
+ // pipe — direction matters
+ case "pipe":
+ case "ZodPipeline":
+ current = io === "input" ? def.in : def.out;
+ continue;
+
+ // effects (v3 refine/transform/preprocess)
+ case "ZodEffects":
+ current = def.schema;
+ continue;
+
+ default:
+ return { schema: current, isOptional, hasDefault };
}
-
- if (current instanceof z.ZodBranded) {
- current = current.unwrap();
- continue;
- }
-
- if (current instanceof z.ZodReadonly) {
- current = current.unwrap();
- continue;
- }
-
- if (current instanceof z.ZodPipeline) {
- current = current._def.out;
- continue;
- }
-
- return { schema: current, isOptional };
}
}
@@ -86,6 +138,8 @@ export function isFieldOptional(
schema: ZodSchema | undefined,
fieldName: string
): boolean | undefined {
+ const dir = "input"; // Can be a param
+
if (!schema || !fieldName) return undefined;
const path = stringToPathArray(fieldName);
@@ -95,7 +149,7 @@ export function isFieldOptional(
for (const segment of path) {
if (!current) return undefined;
- const unwrapped = unwrapSchema(current);
+ const unwrapped = unwrapSchema(current, dir);
current = unwrapped.schema;
optionalFromParent = optionalFromParent || unwrapped.isOptional;
@@ -104,6 +158,6 @@ export function isFieldOptional(
if (!current) return undefined;
- const final = unwrapSchema(current);
+ const final = unwrapSchema(current, dir);
return optionalFromParent || final.isOptional;
}