Comments (8)
Both will be in the next release
from orval.
Sure use Discord?
from orval.
On a related note, is there a way to type-safe the response as well?
from orval.
I am on it and for the response. A zod schema is already generated but not automatically checked. I am not sure how to do it properly if you have an idea or example I am open to add it
from orval.
something like this would do the job I will add it as an option and we can iterate on it
const responseValidator = factory.createMiddleware(async (c, next) => {
await next();
const data = c.res.json();
const result = await createPetsResponse.safeParseAsync(data);
if (!result.success) {
c.res = new Response(JSON.stringify(result), {
status: 400,
headers: {
'Content-Type': 'application/json',
},
});
}
c.res = new Response(c.res.body, c.res);
});
from orval.
I ended with something like this
// based on https://github.com/honojs/middleware/blob/main/packages/zod-validator/src/index.ts
import type { z, ZodSchema, ZodError } from 'zod';
import {
Context,
Env,
Input,
MiddlewareHandler,
TypedResponse,
ValidationTargets,
} from 'hono';
type HasUndefined<T> = undefined extends T ? true : false;
type Hook<T, E extends Env, P extends string, O = {}> = (
result:
| { success: true; data: T }
| { success: false; error: ZodError; data: T },
c: Context<E, P>,
) =>
| Response
| Promise<Response>
| void
| Promise<Response | void>
| TypedResponse<O>;
export const zResponseValidtor =
<
T extends ZodSchema,
Target extends keyof ValidationTargets,
E extends Env,
P extends string,
In = z.input<T>,
Out = z.output<T>,
I extends Input = {
in: HasUndefined<In> extends true
? {
[K in Target]?: K extends 'json'
? In
: HasUndefined<keyof ValidationTargets[K]> extends true
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
: { [K2 in keyof In]: ValidationTargets[K][K2] };
}
: {
[K in Target]: K extends 'json'
? In
: HasUndefined<keyof ValidationTargets[K]> extends true
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
: { [K2 in keyof In]: ValidationTargets[K][K2] };
};
out: { [K in Target]: Out };
},
V extends I = I,
>(
schema: T,
hook?: Hook<z.infer<T>, E, P>,
): MiddlewareHandler<E, P, V> =>
async (c, next) => {
await next();
const clonedResponse = c.res.clone();
let value: unknown;
try {
value = await clonedResponse.json();
} catch {
const message = 'Malformed JSON in response';
c.res = new Response(message, { status: 400 });
}
const result = await schema.safeParseAsync(value);
if (hook) {
const hookResult = hook({ data: value, ...result }, c);
if (hookResult) {
if (hookResult instanceof Response || hookResult instanceof Promise) {
const hookResponse = await hookResult;
if (hookResponse instanceof Response) {
c.res = new Response(hookResponse.body, hookResponse);
}
}
if (
'response' in hookResult &&
hookResult.response instanceof Response
) {
c.res = new Response(hookResult.response.body, hookResult.response);
}
}
}
if (!result.success) {
c.res = new Response(JSON.stringify(result), {
status: 400,
headers: {
'Content-Type': 'application/json',
},
});
}
};
from orval.
Thx @anymaniax , looking forward to it.
For the response typing, to be honest, I had no idea how to do it; so anything would be good ;-)
My main problem with responses in general is that to much is send to the client; e.g. fields with data that is not meant to be exposed.
from orval.
Thx @anymaniax I can confirm the issue Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)
is solved with latest release 💪
For response validation I have some questions, but I think the best place would be Discord?
from orval.
Related Issues (20)
- NonReadonly type incorrectly includes readonly fields HOT 4
- Prettier: true fails for zod when not generating a schema
- Unstable return reference of generated hook when using a custom mutator hook.
- Zod: unstable/breaking variable names for validation lengths HOT 7
- Type Issue: RegEx is not accepted in `defineConfig.[name].input.filters.tags` HOT 1
- Feature request: support different enum key
- Question: Limitations of client instance customisation
- Add support to nested `useInfiniteQueryParam`
- Non-reactive query params in Vue when using useInfiniteQueryParam HOT 2
- Zod: only generates 200 response bodies HOT 6
- Types in generated react-query schema have unstable order, causing noisy diffs HOT 1
- [Feature] Add ability to pass custom types for a string given a format hint
- Strange enum generation while using anyOf in schema HOT 2
- support `fetch` client
- MSW: overrideResponse should allow async functions
- Non-requred requestBody results in TS error
- Faker types are incompatible with contract definition for number enums
- [Feature]Enable `faker.helpers.fromRegExp()` to be applied to msw mock from pattern keyword
- support faker for API schemas HOT 2
- Zod: type string format date should be "YYYY-MM-DD" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from orval.