Replies: 7 comments 8 replies
-
Hello @digimuza Currently when you create a import {OpenAPI} from 'express-zod-api';
const specification = new OpenAPI({
routing,
version: '1.2.3',
title: 'Example API',
serverUrl: 'https://example.com'
});
const yamlString = specification.getSpecAsYaml(); It provides you with specification.addExample(...); And besides that you have a direct access to the specification properties, for example: specification.rootDoc
.paths['/v1/path'].get.responses['200']
.content['application/json'].example // and .examples I did not try it yet, but there is definitely a possibility to add something to the specification.
I'm glad to hear that, and if you have any ideas on how the necessary functionality could be implemented, please let me know. |
Beta Was this translation helpful? Give feedback.
-
I thought a little about this problem, and I realized that the only thing that confuses me is the requirement of the second parameter of I decided to try to keep the factory interface using a slightly different approach. But I'm not sure if there are any disadvantages or risks. Please take a look and tell me what you think of this solution? Personally, I like how the application of type ExampleProp<T extends z.ZodTypeAny> = T['_output'];
type DescriptionProp = string;
type WithMeta<T extends z.ZodTypeAny> = T & {
example: (value: ExampleProp<T>) => WithMeta<T>;
description: (description: DescriptionProp) => WithMeta<T>;
}
const withMeta = <T extends z.ZodTypeAny>(schema: T) => {
schema._def.meta = {};
Object.defineProperty(schema, 'example', {
get() {
return (example: ExampleProp<T>) => {
this._def.meta.example = example;
return this;
};
}
});
Object.defineProperty(schema, 'description', {
get() {
return (description: DescriptionProp) => {
this._def.meta.description = description;
return this;
};
}
});
return schema as WithMeta<T>;
};
// usage:
const myType = withMeta(z.object({
id: z.string()
})).example({
id: 'test'
}).description('something'); |
Beta Was this translation helpful? Give feedback.
-
While I've been working on the implementation the ability to add description to the schema has been released in Zod 3.11.x |
Beta Was this translation helpful? Give feedback.
-
@RobinTail You are amazing ❤️ ❤️ ❤️ ❤️ ❤️ |
Beta Was this translation helpful? Give feedback.
-
One additional thing that would be amazing is ability to generate models that can be referenced across multiple endpoints. This is necessary if you want generate any useful API client with open-api generators. Is it possible to have this kinda feature? |
Beta Was this translation helpful? Give feedback.
-
For example, we have this swagger documentation If we try to generate typescript-axios client with https://www.npmjs.com/package/@openapitools/openapi-generator-cli We get a nice API client Try to generate typescript-axios client from a schema that is generated by express-zod-api and you will see problem |
Beta Was this translation helpful? Give feedback.
-
@digimuza |
Beta Was this translation helpful? Give feedback.
-
Currently, I don't see any possible ways how I could add additional meta information to swager generated file. Do you have any suggestions?
I really like the library so far and I would like to help :D
Beta Was this translation helpful? Give feedback.
All reactions