HealthCheckOutput
import { HealthCheckOutput } from '@app/core/schemas/HealthCheckOutput'
Location
- HealthCheckOutput.ts
Zod Schema
What the schema would look like when defined with z.object()
in Zod V3:
const HealthCheckOutput = z.object({
echo: z
.string()
.optional()
.describe("Echoes back the input argument"),
status: z
.literal("OK")
.describe("Health status of the server"),
alive: z
.boolean(),
kicking: z
.boolean(),
now: z
.string(),
aliveTime: z
.number()
.describe("Time since the server started in milliseconds"),
aliveSince: z
.date()
.describe("Time when the server started"),
serverTimezone: z
.string(),
requestHost: z
.string()
.optional(),
requestProtocol: z
.string()
.optional(),
requestURL: z
.string()
.optional(),
baseURL: z
.string()
.optional(),
backendURL: z
.string()
.optional(),
apiURL: z
.string()
.optional(),
graphURL: z
.string()
.optional(),
port: z
.number()
.int()
.nullable(),
debugPort: z
.number()
.int()
.nullable(),
nodeVersion: z
.string()
.optional(),
v8Version: z
.string()
.optional(),
systemArch: z
.string()
.optional(),
systemPlatform: z
.string()
.optional(),
systemRelease: z
.string()
.optional(),
systemFreeMemory: z
.number()
.optional(),
systemTotalMemory: z
.number()
.optional(),
systemLoadAverage: z
.array(z.number())
.optional(),
context: z
.record(z.string(), z.unknown())
.nullish(),
})
(💡 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 HealthCheckOutput = z.input<typeof HealthCheckOutput>
What the resulting TypeScript type would look like:
{
/** Echoes back the input argument */
echo?: string | undefined;
/** Health status of the server */
status: "OK";
alive: boolean;
kicking: boolean;
now: string;
/** Time since the server started in milliseconds */
aliveTime: number;
/** Time when the server started */
aliveSince: Date;
serverTimezone: string;
requestHost?: string | undefined;
requestProtocol?: string | undefined;
requestURL?: string | undefined;
baseURL?: string | undefined;
backendURL?: string | undefined;
apiURL?: string | undefined;
graphURL?: string | undefined;
port: number | null;
debugPort: number | null;
nodeVersion?: string | undefined;
v8Version?: string | undefined;
systemArch?: string | undefined;
systemPlatform?: string | undefined;
systemRelease?: string | undefined;
systemFreeMemory?: number | undefined;
systemTotalMemory?: number | undefined;
systemLoadAverage?: number[] | undefined;
context?: ({
[x: string]: unknown;
} | 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 healthCheckOutput = HealthCheckOutput.parse(data)
// Returns { success: boolean, data?: T, error?: ZodError }
const healthCheckOutput = HealthCheckOutput.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(HealthCheckOutput, {
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 ‘HealthCheckOutput’ schema is to type component props, provide default values and generate documentation for that component:
export const HealthCheckOutputComponentProps = HealthCheckOutput.extend({
// Add any additional props here
})
export type HealthCheckOutputComponentProps = z.input<typeof HealthCheckOutputComponentProps>
/* --- <HealthCheckOutputComponent/> --------------- */
export const HealthCheckOutputComponent = (rawProps: HealthCheckOutputComponentProps) => {
// Extract the props and apply defaults + infer resulting type
const props = ComponentProps.applyDefaults(rawProps)
// ... rest of the component logic ...
}
/* --- Documentation --------------- */
export const documentationProps = HealthCheckOutputComponentProps.documentationProps('HealthCheckOutputComponent')
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.