Skip to content

Commit 36786e7

Browse files
committed
Storing deprecated metadata on the top level of global registry.
1 parent c42bbd0 commit 36786e7

5 files changed

Lines changed: 9 additions & 15 deletions

File tree

express-zod-api/src/documentation-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ export const depictRequestParams = ({
706706
return acc.concat({
707707
name,
708708
in: location,
709-
deprecated: globalRegistry.get(paramSchema)?.[metaSymbol]?.isDeprecated,
709+
deprecated: globalRegistry.get(paramSchema)?.deprecated,
710710
required: !(paramSchema as z.ZodType).isOptional(),
711711
description: depicted.description || description,
712712
schema: result,
@@ -769,7 +769,7 @@ export const onEach: SchemaHandler<
769769
schema.isNullable();
770770
const result: SchemaObject = {};
771771
if (description) result.description = description;
772-
if (schema.meta()?.[metaSymbol]?.isDeprecated) result.deprecated = true;
772+
if (schema.meta()?.deprecated) result.deprecated = true;
773773
if (isActuallyNullable) result.type = makeNullableType(prev);
774774
if (!shouldAvoidParsing) {
775775
const examples = getExamples({

express-zod-api/src/metadata.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export interface Metadata {
99
/** @override ZodDefault::_zod.def.defaultValue() in depictDefault */
1010
defaultLabel?: string;
1111
brand?: string | number | symbol;
12-
isDeprecated?: boolean;
1312
}
1413

1514
export const copyMeta = <A extends z.ZodType, B extends z.ZodType>(

express-zod-api/src/zod-plugin.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { $ZodType, $ZodShape } from "@zod/core";
1717
declare module "@zod/core" {
1818
interface GlobalMeta {
1919
[metaSymbol]?: Metadata;
20+
deprecated?: boolean;
2021
}
2122
}
2223

@@ -66,11 +67,8 @@ const exampleSetter = function (this: z.ZodType, value: z.input<typeof this>) {
6667
const deprecationSetter = function (this: z.ZodType) {
6768
return this.meta({
6869
description: this.description,
69-
[metaSymbol]: {
70-
examples: [],
71-
...this.meta()?.[metaSymbol],
72-
isDeprecated: true,
73-
},
70+
deprecated: true,
71+
[metaSymbol]: this.meta()?.[metaSymbol],
7472
});
7573
};
7674

express-zod-api/src/zts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { hasCoercion, getTransformedType } from "./common-helpers";
2222
import { ezDateInBrand } from "./date-in-schema";
2323
import { ezDateOutBrand } from "./date-out-schema";
2424
import { ezFileBrand, FileSchema } from "./file-schema";
25-
import { metaSymbol } from "./metadata";
2625
import { ProprietaryBrand } from "./proprietary-schemas";
2726
import { ezRawBrand, RawSchema } from "./raw-schema";
2827
import { FirstPartyKind, HandlingRules, walkSchema } from "./schema-walker";
@@ -83,11 +82,12 @@ const onObject: Producer = (
8382
: value instanceof z.ZodPromise
8483
? false
8584
: (value as z.ZodType).isOptional();
86-
const { description: comment, ...meta } = globalRegistry.get(value) || {};
85+
const { description: comment, deprecated: isDeprecated } =
86+
globalRegistry.get(value) || {};
8787
return makeInterfaceProp(key, next(value), {
8888
comment,
89+
isDeprecated,
8990
isOptional: isOptional && hasQuestionMark,
90-
isDeprecated: meta[metaSymbol]?.isDeprecated,
9191
});
9292
},
9393
);

express-zod-api/tests/zod-plugin.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ describe("Zod Runtime Plugin", () => {
6767
test("should set the corresponding metadata in the schema definition", () => {
6868
const schema = z.string();
6969
const schemaWithMeta = schema.deprecated();
70-
expect(schemaWithMeta.meta()?.[metaSymbol]).toHaveProperty(
71-
"isDeprecated",
72-
true,
73-
);
70+
expect(schemaWithMeta.meta()).toHaveProperty("deprecated", true);
7471
});
7572
});
7673

0 commit comments

Comments
 (0)