import type { Meta, StoryObj } from '@storybook/nextjs' import { z } from 'zod' import withValidation from '.' // Sample components to wrap with validation type UserCardProps = { name: string email: string age: number role?: string } const UserCard = ({ name, email, age, role }: UserCardProps) => { return (
Check console for validation error. Component won't render.
Age must be between 0 and 150. Check console.
Price must be positive. Check console.
Only valid products render. Invalid ones return null (check console).
Wraps React components with Zod schema validation for their props. Returns null and logs errors if validation fails.
{`import { z } from 'zod'
import withValidation from './withValidation'
// Define your component
const UserCard = ({ name, email, age }) => (
{name} - {email} - {age}
)
// Define validation schema
const schema = z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().min(0).max(150),
})
// Wrap with validation
const ValidatedUserCard = withValidation(UserCard, schema)
// Use validated component
`}
Open browser console to see validation errors