Skip to content

Commit e2389b0

Browse files
author
Colin McDonnell
committed
Add describe
1 parent 6fcd0e4 commit e2389b0

7 files changed

Lines changed: 59 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
</p>
1313
<p align="center">
14-
⭐️ smash that star button ⭐️
14+
by [@colinhacks](https://twitter.com/colinhacks)
1515
</p>
1616

17-
> Like typesafety? Check out [tRPC](https://trpc.io) — a better way to build end-to-end typesafe APIs without GraphQL or code generation — just TypeScript.
17+
> Hi! Colin here, creator of Zod. I hope you find it easy to use and powerful enough for all your use cases. If you have any issues or suggestions, please [open an issue](https://github.com/colinhacks/zod/issues/new)!
18+
>
19+
> If you like typesafety, check out my other library [tRPC](https://trpc.io). It works in concert with Zod to provide a seamless way to build end-to-end typesafe APIs without GraphQL or code generation — just TypeScript.
20+
>
21+
> Colin (AKA [@colinhacks](https://twitter.com/colinhacks))
1822
1923
<br/>
2024

coverage.svg

Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-ignore TS6133
2+
import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";
3+
const test = Deno.test;
4+
5+
import * as z from "../index.ts";
6+
7+
test("description", () => {
8+
const schema: any = z.string();
9+
const DESC = "asdlfkjasdf";
10+
expect(schema.describe(DESC).description).toEqual(DESC);
11+
});

deno/lib/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type { TypeOf as infer };
5454
export type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
5555
export interface ZodTypeDef {
5656
errorMap?: ZodErrorMap;
57+
description?: string;
5758
}
5859

5960
const handleResult = <Input, Output>(
@@ -78,9 +79,10 @@ type RawCreateParams =
7879
errorMap?: ZodErrorMap;
7980
invalid_type_error?: string;
8081
required_error?: string;
82+
description?: string;
8183
}
8284
| undefined;
83-
type ProcessedCreateParams = { errorMap?: ZodErrorMap };
85+
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string };
8486
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
8587
if (!params) return {};
8688
if (params.errorMap && (params.invalid_type_error || params.required_error)) {
@@ -110,6 +112,10 @@ export abstract class ZodType<
110112
readonly _input!: Input;
111113
readonly _def!: Def;
112114

115+
get description() {
116+
return this._def.description;
117+
}
118+
113119
abstract _parse(input: ParseInput): ParseReturnType<Output>;
114120

115121
_processInputParams(
@@ -336,6 +342,14 @@ export abstract class ZodType<
336342
}) as any;
337343
}
338344

345+
describe(description: string): this {
346+
const This = (this as any).constructor;
347+
return new This({
348+
...this._def,
349+
description,
350+
});
351+
}
352+
339353
isOptional(): boolean {
340354
return this.safeParse(undefined).success;
341355
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zod",
3-
"version": "3.11.2",
3+
"version": "3.11.4",
44
"description": "TypeScript-first schema declaration and validation library with static type inference",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/__tests__/description.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @ts-ignore TS6133
2+
import { expect, test } from "@jest/globals";
3+
4+
import * as z from "../index";
5+
6+
test("description", () => {
7+
const schema: any = z.string();
8+
const DESC = "asdlfkjasdf";
9+
expect(schema.describe(DESC).description).toEqual(DESC);
10+
});

src/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type { TypeOf as infer };
5454
export type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
5555
export interface ZodTypeDef {
5656
errorMap?: ZodErrorMap;
57+
description?: string;
5758
}
5859

5960
const handleResult = <Input, Output>(
@@ -78,9 +79,10 @@ type RawCreateParams =
7879
errorMap?: ZodErrorMap;
7980
invalid_type_error?: string;
8081
required_error?: string;
82+
description?: string;
8183
}
8284
| undefined;
83-
type ProcessedCreateParams = { errorMap?: ZodErrorMap };
85+
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string };
8486
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
8587
if (!params) return {};
8688
if (params.errorMap && (params.invalid_type_error || params.required_error)) {
@@ -110,6 +112,10 @@ export abstract class ZodType<
110112
readonly _input!: Input;
111113
readonly _def!: Def;
112114

115+
get description() {
116+
return this._def.description;
117+
}
118+
113119
abstract _parse(input: ParseInput): ParseReturnType<Output>;
114120

115121
_processInputParams(
@@ -336,6 +342,14 @@ export abstract class ZodType<
336342
}) as any;
337343
}
338344

345+
describe(description: string): this {
346+
const This = (this as any).constructor;
347+
return new This({
348+
...this._def,
349+
description,
350+
});
351+
}
352+
339353
isOptional(): boolean {
340354
return this.safeParse(undefined).success;
341355
}

0 commit comments

Comments
 (0)