Releases: samchungy/zod-openapi
v2.13.0
What's Changed
New Features 🎉
-
Introduce
same
effectType by @samchungy in #217This library now has a new
effectType
when dealing with Zod Effects. In most scenarios, our users were usingtransform
to sanitise or transform while ensuring the type did not change, however, this library could not handle this.The new
same
option allows this library to pick the input Zod Type to produce a schema while ensuring that the input and output remain the same type.For example: when creating a response (output) schema for the following type this library cannot hook into the TypeScript
compiler at runtime to understand that you wanted a string schema generated:z.string().transform((string) => string.toUpperCase());
which meant that you had to declare the following in order to get it to work:
z.string().transform((string) => string.toUpperCase()).openapi({ effectType: 'input' }); // or z.string().transform((string) => string.toUpperCase()).pipe(z.string());
However, in the first example this could be dangerous if a user was to change the transform to no longer be the same type:
z.string().transform((string) => string.length).openapi({ effectType: 'input' });
The new
same
type allows us to avoid this mistake:✅ z.string().transform((string) => string.toUpperCase()).openapi({ effectType: 'same' }); // Type '"same"' is not assignable to type 'CreationType | undefined'. ts(2322) ❌ z.string().transform((string) => string.length).openapi({ effectType: 'same' });
-
Refactor Effect Handling by @samchungy in #211, #214
This library now validates that effects used within registered Lazy schemas are not included in both input and output schemas. This could possibly be a breaking change if you were accidentally doing this.
This library should also provide clearer errors when using Zod Effects with registered components which should make it easier to deal with effects.
Before:
Error: {"_def":{"in":{"_def":{"checks":[],"typeName":"ZodString","coerce":false}},"out":{"_def":{"checks":[],"typeName":"ZodNumber","coerce":false}},"typeName":"ZodPipeline","openapi":{"description":"A name that describes the job","example":"Mid level developer"}}} at /job > post > request body > content > application/json > schema > property: title contains a transformation but is used in both an input and an output. This is likely a mistake. Set an `effectType`, wrap it in a ZodPipeline or assign it a manual type to resolve
After:
Error: The ZodPipeline at /job > post > request body > content > application/json > schema > property: title is used within a registered compoment schema (jobTitle) and contains an output transformation (ZodPipeline) defined at /job > get > responses > 200 > content > application/json > schema > property: title which is also used in an input schema. This may cause the schema to render incorrectly and is most likely a mistake. You can resolve this by: 1. Setting an `effectType` on the transformation to `same` or `input` eg. `.openapi({type: 'same'})` 2. Wrapping the transformation in a ZodPipeline 3. Assigning a manual type to the transformation eg. `.openapi({type: 'string'})` 4. Removing the transformation 5. Deregistering the component containing the transformation
-
Add default as an effect by @samchungy in #215, #216
#175 added a change which would generate differences in the schema for input and outputs for a default schema. This may be a breaking change if you were using
.default()
in a registered schema which is referenced in both a request and response schema. As such,effectType
can now also be used to change the rendered type.
Other Changes
- Fix ESM importing on Node.js by @jaens in #208
- Fix duplicate parameter component check by @jaens in #207
- Remove duplicate descriptions on referenced schemas by @samchungy in #212
New Contributors
Full Changelog: v2.12.0...v2.13.0
v2.12.0
What's Changed
New Features 🎉
- Duplicate description on schemas used in parameters by @samchungy in #200
- Support ZodNever and ZodUndefined by @samchungy in #202
Other Changes
- Bump openapi3-ts from 4.1.2 to 4.2.1 by @dependabot in #197
- Update ZodTransform
effectType
error messages by @samchungy in #203
New Contributors
Full Changelog: v2.11.0...v2.12.0
v2.11.0
What's Changed
New Features 🎉
-
Fix Nullable Behaviour by @samchungy in #187
.nullable()
now renders differently for OpenAPI < 3.1.0Previously a registered schema which was made nullable would render as:
{ oneOf: [ { $ref: '#/components/schemas/a' }, { nullable: true } ] }
will now be rendered as:
{ allOf: [ { $ref: '#/components/schemas/a' } ], nullable: true }
Similarly with nullable unions
{ anyOf: [ { $ref: '#/components/schemas/a' }, { $ref: '#/components/schemas/b' }, { nullable: true } ] }
Will now be rendered as:
{ anyOf: [ { $ref: '#/components/schemas/a' }, { $ref: '#/components/schemas/b' } ], nullable: true }
Full Changelog: v2.10.0...v2.11.0
v2.10.0
What's Changed
New Features 🎉
-
Add unionOneOf by @samchungy in #184
You can now use
unionOneOf
to override the output of a ZodUnion to be aoneOf
schema instead ofallOf
(default). Previously rendering an oneOf schema was only possible with a ZodDiscriminatedUnion. -
Bundle Package with esbuild by @samchungy in #178
This should result in no changes to the public API, however, if you were previously reaching into the package paths internally you will now see an error.
Full Changelog: v2.9.1...v2.10.0
v2.10.0-beta.1
What's Changed
New Features 🎉
-
Bundle Package with esbuild by @samchungy in #178
This should result in no changes to the public API, however, if you were previously reaching into the internal files you will now see an error.
Full Changelog: v2.9.1...v2.10.0-beta.1
v2.9.1
What's Changed
Other Changes
-
fix: default should be nonoptional in output state by @tomwwright in #175
When
.default()
is used on a ZodObject property field in a response schema, it is now correctly reflected as a required field.
New Contributors
- @tomwwright made their first contribution in #175
Full Changelog: v2.9.0...v2.9.1
v2.9.0
What's Changed
New Features 🎉
- Handle
.startsWith()
,.endsWith()
,.includes()
and multiple string patterns by @samchungy in #164
Other Changes
- Eslint type tweaks by @dependabot in #161
Full Changelog: v2.8.0...v2.9.0
v2.9.0-beta.0
What's Changed
New Features 🎉
- Handle
.startsWith()
,.endsWith()
,.includes()
and multiple string patterns by @samchungy in #164
Other Changes
- Eslint type tweaks by @dependabot in #161
Full Changelog: v2.8.0...v2.9.0-beta.0
v2.8.0
What's Changed
New Features 🎉
- Support ZodReadonly by @samchungy in #159
Other Changes
- Add stricter typing to Zod Type check by @samchungy in #152
Full Changelog: v2.7.4...v2.8.0
v2.7.4
What's Changed
Other Changes
- Avoid instanceof checks and rely on typeName by @samchungy in #150
Full Changelog: v2.7.3...v2.7.4