Skip to content

Commit

Permalink
Add describe
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed Oct 24, 2021
1 parent 6fcd0e4 commit e2389b0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

</p>
<p align="center">
⭐️ smash that star button ⭐️
by [@colinhacks](https://twitter.com/colinhacks)
</p>

> 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.
> 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)!
>
> 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.
>
> Colin (AKA [@colinhacks](https://twitter.com/colinhacks))
<br/>

Expand Down
2 changes: 1 addition & 1 deletion coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions deno/lib/__tests__/description.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-ignore TS6133
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
const test = Deno.test;

import * as z from "../index.ts";

test("description", () => {
const schema: any = z.string();
const DESC = "asdlfkjasdf";
expect(schema.describe(DESC).description).toEqual(DESC);
});
16 changes: 15 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type { TypeOf as infer };
export type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
export interface ZodTypeDef {
errorMap?: ZodErrorMap;
description?: string;
}

const handleResult = <Input, Output>(
Expand All @@ -78,9 +79,10 @@ type RawCreateParams =
errorMap?: ZodErrorMap;
invalid_type_error?: string;
required_error?: string;
description?: string;
}
| undefined;
type ProcessedCreateParams = { errorMap?: ZodErrorMap };
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string };
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {};
if (params.errorMap && (params.invalid_type_error || params.required_error)) {
Expand Down Expand Up @@ -110,6 +112,10 @@ export abstract class ZodType<
readonly _input!: Input;
readonly _def!: Def;

get description() {
return this._def.description;
}

abstract _parse(input: ParseInput): ParseReturnType<Output>;

_processInputParams(
Expand Down Expand Up @@ -336,6 +342,14 @@ export abstract class ZodType<
}) as any;
}

describe(description: string): this {
const This = (this as any).constructor;
return new This({
...this._def,
description,
});
}

isOptional(): boolean {
return this.safeParse(undefined).success;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod",
"version": "3.11.2",
"version": "3.11.4",
"description": "TypeScript-first schema declaration and validation library with static type inference",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/description.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";

import * as z from "../index";

test("description", () => {
const schema: any = z.string();
const DESC = "asdlfkjasdf";
expect(schema.describe(DESC).description).toEqual(DESC);
});
16 changes: 15 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type { TypeOf as infer };
export type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
export interface ZodTypeDef {
errorMap?: ZodErrorMap;
description?: string;
}

const handleResult = <Input, Output>(
Expand All @@ -78,9 +79,10 @@ type RawCreateParams =
errorMap?: ZodErrorMap;
invalid_type_error?: string;
required_error?: string;
description?: string;
}
| undefined;
type ProcessedCreateParams = { errorMap?: ZodErrorMap };
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string };
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {};
if (params.errorMap && (params.invalid_type_error || params.required_error)) {
Expand Down Expand Up @@ -110,6 +112,10 @@ export abstract class ZodType<
readonly _input!: Input;
readonly _def!: Def;

get description() {
return this._def.description;
}

abstract _parse(input: ParseInput): ParseReturnType<Output>;

_processInputParams(
Expand Down Expand Up @@ -336,6 +342,14 @@ export abstract class ZodType<
}) as any;
}

describe(description: string): this {
const This = (this as any).constructor;
return new This({
...this._def,
description,
});
}

isOptional(): boolean {
return this.safeParse(undefined).success;
}
Expand Down

0 comments on commit e2389b0

Please sign in to comment.