wobsoriano / h3-zod Goto Github PK
View Code? Open in Web Editor NEWTypeScript-first schema validation for h3 and Nuxt applications
License: MIT License
TypeScript-first schema validation for h3 and Nuxt applications
License: MIT License
sorry for this is not an issue but a question, but I am new to zod and I've been trying to use formkit with the zod plugin in nuxt and wanted to use zod in the backend for validation. but I felt like using zod directly does the job.
so I was wondering what advantages does this package offer to my codebase and is it worth adding another dependency to my project?
Have you seen sidebase/sidebase#17 (comment) ?
First of all big thanks for actively maintaining this package.
Zod schema can use multiple refine validations, which produces type with nested ZodEffects.
Passing this schema to h3-zod gives TS error.
Reproduction:
https://codesandbox.io/s/ecstatic-surf-0u8wo2?file=/server/api/submit.post.ts
Tried this with [email protected], which handled this case OK, but further improvements were made down the line, changing the behaviour.
EDIT: Also the resulting body type is unknown
,
I am using what you are saying in your doc. The one says should send a 400 status code.
But nothing happened when using it. Is the doc missing something ?
H3 version: 1.8.2
h3-zod's H3 version: 1.8.1
import { useSafeValidatedQuery } from "h3-zod";
import { z } from "zod";
export default defineEventHandler((event) => {
const querySchema = z.object({ s: z.string() });
const query = await useSafeValidatedQuery(event, querySchema);
^ Here
if (!query.success)
throw createError({ status: 400, message: "search_bad_query" });
return query.data.s;
});
First of all thanks for this very useful library.
The issue I'd like to report deals with using schema with type ZodEffects
. Such schema can be produced by using z.refine
on z.object
as shown in example below.
This produces a TS error. See https://codesandbox.io/s/morning-bash-oi6h35?file=/server/api/submit.post.ts
import { useValidatedBody, z } from "h3-zod";
import { defineEventHandler } from "h3";
import dayjs from "dayjs";
const bodySchema = z
.object({
fromDate: z.string().optional(),
toDate: z.string().optional()
})
.refine(
(schema) =>
!!schema.fromDate && !!schema.toDate
? dayjs(schema.fromDate).isBefore(schema.toDate)
: true,
"'fromDate' has to be before 'toDate'"
);
export default defineEventHandler(async (event) => {
const body = await useValidatedBody(event, bodySchema);
return;
});
The solution would be to extend TQuery type like so:
type TQuery<U extends UnknownKeysParam = any> =
| z.ZodObject<any, U>
| z.ZodUnion<[TQuery<U>, ...TQuery<U>[]]>
| z.ZodIntersection<TQuery<U>, TQuery<U>>
| z.ZodDiscriminatedUnion<string, z.Primitive, z.ZodObject<any, U>>
| z.ZodEffects<z.ZodTypeAny>; // <-- add this line
Would you like me to open a PR?
zod: 3.21.4
h3-zod: 0.4.0
Property 'error' does not exist on type 'SafeParseReturnType<ZodObject<{ email: ZodString; password: ZodString; firstName: ZodOptional; lastName: ZodOptional; name: ZodOptional<...>; tags: ZodArray<...>; }, UnknownKeysParam, ZodTypeAny, { ...; }, { ...; }>, { ...; }>'. Property 'error' does not exist on type 'SafeParseSuccess<{ email?: string; password?: string; firstName?: string; lastName?: string; name?: string; tags?: string[]; }>'.
When doing a wrong body request, the router below return an status and error property, but typescript give us an error
export default defineEventHandler(async (event) => {
const body = await zh.useSafeValidatedBody(event, {
email: z.string().email(),
password: z.string().min(8),
firstName: z.string().optional(),
lastName: z.string().optional(),
name: z.string().optional(),
tags: z.array(z.string()).min(1)
});
// typescript warning here
console.log('body', body.error);
return body;
});
Hi Thanks for you great job
It can be interesting to have a dedicated method to parse cookie content.
With the usage of getCookie() method if data is stored as json
zodSchema.parse(JSON.stringify(getCookie(event, 'cookie_name')))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.