-
Notifications
You must be signed in to change notification settings - Fork 262
Open
Labels
enhancementNew feature or requestNew feature or request
Description
What is the feature you are proposing?
I have a nested query like ?filter[color]=red&filter[size]=large&user[post][id]=385&sort=-createdAt&include=author,comments&active=true and I want to validate. In zod-validator I use qs lib to make the query like an object and after that i parse with zod. In zod-openapi I love to get query to make the same. See my example above.
const route = createRoute({
...
request: {
query: (query) => {
// query example: "?filter[color]=red&filter[size]=large&user[post][id]=385&sort=-createdAt&include=author,comments&active=true"
const queryObject = qs.parse(query, {
ignoreQueryPrefix: true,
decoder: (str: string, defaultDecoder: qs.defaultDecoder, charset: string, type: "key" | "value") => {
if (type === "value" && !!Number(str)) {
return Number(str);
}
if (type === "value" && str === "true") {
return true;
}
if (type === "value" && str === "false") {
return false;
}
if (type === "value" && str.includes(",")) {
return str.split(",").map((s: string) => s.trim());
}
return defaultDecoder(str);
}
});
const schema = z.object({
filter: z.object({
color: z.string().optional(),
size: z.string().optional(),
}).optional(),
user: z.object({
post: z.object({
id: z.int().optional(),
}).optional(),
}).optional(),
sort: z.string().optional(),
include: z.array(z.string()).optional(),
active: z.boolean().optional(),
});
return schema.parse(queryObject);
},
},
...
});Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request