Only plain objects, and a few built-ins, can be passed to Client Components from Server Components
Unanswered
Cornish Rex posted this in #help-forum
Cornish RexOP
Hey guys im trying to pass zod schema in an object to the client but i keep getting that only plain objects... can e passed to client. i want validation on server, client and its easier to pass schema in the object so i can reuse it for all other objects how can i actually pass it to client without an issue?
page.tsx
SearchFilter.tsx
import { firstNameSchema, lastNameSchema } from "@/util/validation";
import { ZodSchema } from "zod";
export interface SearchField {
db: string;
label: string;
dataType: string;
zodValidation: ZodSchema;
}
export type SearchData = Record<string, SearchField>;
export const doctorSearchData = {
firstName: {
db: "firstName",
label: "First Name",
dataType: "string",
zodValidation: firstNameSchema,
},
lastName: {
db: "lastName",
label: "Last name",
dataType: "string",
zodValidation: lastNameSchema,
},
};
page.tsx
import { doctorSearchData } from "@/app/constants/searchData";
const page = async ({ searchParams }: ISearchParamsProps) => {
return <SearchFilter searchData={doctorSearchData} />
}
SearchFilter.tsx
const SearchFilter = ({ searchData }: SearchFilterProps) => {
const searchParams = useSearchParams();
return (
<Select defaultValue={Object.keys(searchData)[0]}>
<SelectTrigger style={{ direction: "rtl" }} className="w-fit rounded-l-none">
<ListFilter size={16} className="mx-1" />
<SelectValue placeholder={Object.keys(searchData)[0]} />
<span className="w-[10px]"></span>
</SelectTrigger>
<SelectContent style={{ direction: "rtl" }}>
{Object.keys(searchData).map((param, index) => (
<SelectItem key={index + param} value={param}>
{searchData[param].label}
</SelectItem>
))}
</SelectContent>
</Select>
)
10 Replies
Cornish RexOP
if i json stringify it it works but is it actually safe to stringify it?
I think it has to do with the zod schema
did you try using it without the schema?
@Yi Lon Ma did you try using it without the schema?
Cornish RexOP
yeah i know it is the schema im trying to pass the schema to the client thats my question
@Cornish Rex yeah i know it is the schema im trying to pass the schema to the client thats my question
I don't think that's possible. You can import it directly in client though
Cornish RexOP
will importing it to the client actually expose the logic behind the validation?
in shadcn it does use the schema too:
but is it actually expose on the frontend and can people view the validation process?
in shadcn it does use the schema too:
const onSubmit = async (data: z.infer<typeof CreateDoctorValidation>) => {}
but is it actually expose on the frontend and can people view the validation process?
Cornish RexOP
i also need to import it dynamically any ideas on how? each page has its own schema
@Cornish Rex i also need to import it dynamically any ideas on how? each page has its own schema
if there are diff page for each, you can just import the schema in each page
@Yi Lon Ma if there are diff page for each, you can just import the schema in each page
Cornish RexOP
That what i did but i get that i cannot pass it to client as you saw abovr