Is it possible to retrieve the body schema for an alias? #473
Replies: 4 comments
-
Yes, just do an array find on you api definition |
Beta Was this translation helpful? Give feedback.
-
I came up with this strongly typed version. It would be nice if Zodius provided this helper so that schema's don't have to be declared separately from the Api to be retrieved later on. In our case we need it later for a form library that validates forms using zod schemas. |
Beta Was this translation helpful? Give feedback.
-
Can you elaborate the use case for this ? |
Beta Was this translation helpful? Give feedback.
-
We currently use react-form-hook which allows you to provide a zod schema and the form will use that for validation. Right now this requires us to separate the schema definition from the api definition like the following: const createUserSchema = z.object({
id: z.number(),
name: z.string(),
age: z.number().positive(),
email: z.string().email(),
});
const apiClient = new Zodios('/api', [
{
method: "post",
path: "/users",
alias: "createUser",
parameters: [
{
name: "user",
type: "Body",
schema: createUserSchema
},
],
response: user,
errors,
},
]);
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(createUserSchema),
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
...
</form>
);
}; It would be nice to be able to define the api and extract the schema instead, something like const apiClient = new Zodios('/api', [
{
method: "post",
path: "/users",
alias: "createUser",
parameters: [
{
name: "user",
type: "Body",
schema: z.object({
id: z.number(),
name: z.string(),
age: z.number().positive(),
email: z.string().email(),
})
},
],
response: user,
errors,
},
]);
const App = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(bodySchemaForAlias('createUser'))
});
return (
<form onSubmit={handleSubmit((d) => console.log(d))}>
...
</form>
);
}; |
Beta Was this translation helpful? Give feedback.
-
Is it possible to retrieve the body schema for an alias?
Beta Was this translation helpful? Give feedback.
All reactions