CheckoutLineItem
import { CheckoutLineItem } from '@payments/driver/schemas/CheckoutLineItem.schema'Location
- CheckoutLineItem.schema.ts
Zod Schema
What the schema would look like when defined with z.object() in Zod V3:
const CheckoutLineItem = z.object({
priceVariantId: z
.string()
.index()
.describe("Payment provider price / variant / sku ID linked to this purchase / checkout. Can be the same as productId for certain providers."),
productId: z
.string()
.index()
.nullish()
.describe("Payment provider product ID linked to this purchase / checkout."),
quantity: z
.number()
.default(1)
.describe("Quantity of units purchased"),
metadata: z
.record(z.string(), z.any())
.nullish()
.describe("Optional metadata to attach to the purchase item"),
})(💡 Could be handy to copy-paste this schema info into an AI chat assistant)
Type Definition
You can extract the TypeScript type from the schema using z.input(), z.output() or z.infer() methods. e.g.:
type CheckoutLineItem = z.input<typeof CheckoutLineItem>What the resulting TypeScript type would look like:
{
/** Payment provider price / variant / sku ID linked to this purchase / checkout. Can be the same as productId for certain providers. */
priceVariantId: string;
/** Payment provider product ID linked to this purchase / checkout. */
productId?: (string | null) | undefined;
/** Quantity of units purchased */
quantity?: number;
/** Optional metadata to attach to the purchase item */
metadata?: ({
[x: string]: any;
} | null) | undefined;
}(💡 Could be handy to copy-paste this type info into an AI chat assistant)
Usage - Validation
To validate data against this schema, you have a few options:
// Throws if invalid
const checkoutLineItem = CheckoutLineItem.parse(data)
// Returns { success: boolean, data?: T, error?: ZodError }
const checkoutLineItem = CheckoutLineItem.safeParse(data)
This might be useful for parsing API input data or validating form data before submission.
You can also directly integrate this schema with form state managers like our own:
Usage - Form State
import { useFormState } from '@green-stack/forms/useFormState'
const formState = useFormState(CheckoutLineItem, {
initialValues: { /* ... */ }, // Provide initial values?
validateOnMount: true, // Validate on component mount?
})
Learn more about using schemas for form state in our Form Management Docs.
Usage - Component Props / Docs
Another potential use case for the ‘CheckoutLineItem’ schema is to type component props, provide default values and generate documentation for that component:
export const CheckoutLineItemComponentProps = CheckoutLineItem.extend({
// Add any additional props here
})
export type CheckoutLineItemComponentProps = z.input<typeof CheckoutLineItemComponentProps>
/* --- <CheckoutLineItemComponent/> --------------- */
export const CheckoutLineItemComponent = (rawProps: CheckoutLineItemComponentProps) => {
// Extract the props and apply defaults + infer resulting type
const props = ComponentProps.applyDefaults(rawProps)
// ... rest of the component logic ...
}
/* --- Documentation --------------- */
export const documentationProps = CheckoutLineItemComponentProps.documentationProps('CheckoutLineItemComponent')
Other
Disclaimer - Automatic Docgen
These dynamic schema docs were auto-generated with npm run regenerate-docs. This happens automatically for schema files in any \schemas\ folder. You can opt-out of this by adding // export const optOut = true somewhere in the file.
