Alt description missing in image
Beta: Plugins coming soon!

Customer

import { Customer } from '@payments/driver/schemas/Customer.schema'

Location

        • Customer.schema.ts

Zod Schema

What the schema would look like when defined with z.object() in Zod V3:

const Customer = z.object({
    userId: z
        .string()
        .index()
        .describe("Auth provider user ID the plan is associated with"),
    provider: z
        .enum(["stripe", "revenuecat", "polar", "lemonsqueezy", "paddle", "clerk", "custom"])
        .describe("Payment provider / driver name, e.g. 'stripe' or 'revenuecat'"),
    customerId: z
        .string()
        .index()
        .unique()
        .describe("Payment provider customer ID, limited to 1 unique customerId per provider")
        .sensitive() // = stripped in API responses, serverside only,
    subscriptionId: z
        .string()
        .nullish()
        .describe("Payment provider subscription ID")
        .sensitive() // = stripped in API responses, serverside only,
    email: z
        .string()
        .email()
        .nullish()
        .describe("Customer email, if available from the payment provider"),
    name: z
        .string()
        .nullish()
        .describe("Customer full name, if available from the payment provider"),
    raw: z
        .string()
        .nullish()
        .describe("JSON stringified raw customer object as received from the payment provider")
        .sensitive() // = stripped in API responses, serverside only,
})

(💡 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 Customer = z.input<typeof Customer>

What the resulting TypeScript type would look like:

{
    /** Auth provider user ID the plan is associated with */
    userId: string;
    /** Payment provider / driver name, e.g. 'stripe' or 'revenuecat' */
    provider: "stripe" | "revenuecat" | "polar" | "lemonsqueezy" | "paddle" | "clerk" | "custom";
    /** Payment provider customer ID, limited to 1 unique customerId per provider */
    customerId: string;
    /** Payment provider subscription ID */
    subscriptionId?: (string | null) | undefined;
    /** Customer email, if available from the payment provider */
    email?: (string | null) | undefined;
    /** Customer full name, if available from the payment provider */
    name?: (string | null) | undefined;
    /** JSON stringified raw customer object as received from the payment provider */
    raw?: (string | 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 customer = Customer.parse(data)
 
// Returns { success: boolean, data?: T, error?: ZodError }
const customer = Customer.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(Customer, {
    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 ‘Customer’ schema is to type component props, provide default values and generate documentation for that component:

export const CustomerComponentProps = Customer.extend({
    // Add any additional props here
})
 
export type CustomerComponentProps = z.input<typeof CustomerComponentProps>
 
/* --- <CustomerComponent/> --------------- */
 
export const CustomerComponent = (rawProps: CustomerComponentProps) => {
 
    // Extract the props and apply defaults + infer resulting type
    const props = ComponentProps.applyDefaults(rawProps)
 
    // ... rest of the component logic ...
 
}
 
/* --- Documentation --------------- */
 
export const documentationProps = CustomerComponentProps.documentationProps('CustomerComponent')
 

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.