Skip to content

Commit

Permalink
add name() to define displayName
Browse files Browse the repository at this point in the history
  • Loading branch information
Piet Groot Kormelink committed Apr 28, 2022
1 parent 8688e9f commit dc9c438
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ These docs have been translated into [Chinese](./README_ZH.md).
- [.and](#and)
- [.describe](#describe)
- [.localize](#localize)
- [.name](#name)
- [Guides and concepts](#guides-and-concepts)
- [Type inference](#type-inference)
- [Writing generic functions](#writing-generic-functions)
Expand Down Expand Up @@ -1696,6 +1697,14 @@ const nullishString = z.string().nullish(); // string | null | undefined
z.string().optional().nullable();
```

### `.describe`

A schema method that adds a description to the schema for documentation purpose.

```ts
const description = z.string().describe('usefull description').description; // string
```

### `.localize`

A schema method that defines that the schema should be translated.
Expand All @@ -1704,12 +1713,12 @@ A schema method that defines that the schema should be translated.
const isLocalized = z.string().localize(true).localized; // boolean
```

### `.describe`
### `.name`

A schema method that adds a description to the schema for documentation purpose.
A schema method that defines the displayName.

```ts
const description = z.string().describe('usefull description').description; // string
const name = z.string().name(true).displayName;
```

### `.array`
Expand Down
11 changes: 11 additions & 0 deletions deno/lib/__tests__/name.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/expect@v0.2.6/mod.ts";
const test = Deno.test;

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

test("name", () => {
const displayName = "myDisplayName";
const schema = z.string().name(displayName);
expect(schema.displayName).toEqual(displayName);
});
19 changes: 18 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface ZodTypeDef {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
displayName?: string;
}

class ParseInputLazyPath implements ParseInput {
Expand Down Expand Up @@ -106,12 +107,14 @@ type RawCreateParams =
required_error?: string;
description?: string;
localized?: boolean;
displayName?: string;
}
| undefined;
type ProcessedCreateParams = {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
displayName?: string;
};
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {};
Expand All @@ -121,6 +124,7 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
required_error,
description,
localized,
displayName,
} = params;
if (errorMap && (invalid_type_error || required_error)) {
throw new Error(
Expand All @@ -136,7 +140,7 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
return { message: params.invalid_type_error };
return { message: ctx.defaultError };
};
return { errorMap: customMap, description, localized };
return { errorMap: customMap, description, localized, displayName };
}

export type SafeParseSuccess<Output> = { success: true; data: Output };
Expand Down Expand Up @@ -164,6 +168,10 @@ export abstract class ZodType<
return this._def.localized;
}

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

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

_getType(input: ParseInput): string {
Expand Down Expand Up @@ -391,6 +399,7 @@ export abstract class ZodType<
this.isNullable = this.isNullable.bind(this);
this.isOptional = this.isOptional.bind(this);
this.localize = this.localize.bind(this);
this.name = this.name.bind(this);
}

optional(): ZodOptional<this> {
Expand Down Expand Up @@ -455,6 +464,14 @@ export abstract class ZodType<
});
}

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

isOptional(): boolean {
return this.safeParse(undefined).success;
}
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/name.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("name", () => {
const displayName = "myDisplayName";
const schema = z.string().name(displayName);
expect(schema.displayName).toEqual(displayName);
});
19 changes: 18 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface ZodTypeDef {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
displayName?: string;
}

class ParseInputLazyPath implements ParseInput {
Expand Down Expand Up @@ -106,12 +107,14 @@ type RawCreateParams =
required_error?: string;
description?: string;
localized?: boolean;
displayName?: string;
}
| undefined;
type ProcessedCreateParams = {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
displayName?: string;
};
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {};
Expand All @@ -121,6 +124,7 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
required_error,
description,
localized,
displayName,
} = params;
if (errorMap && (invalid_type_error || required_error)) {
throw new Error(
Expand All @@ -136,7 +140,7 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
return { message: params.invalid_type_error };
return { message: ctx.defaultError };
};
return { errorMap: customMap, description, localized };
return { errorMap: customMap, description, localized, displayName };
}

export type SafeParseSuccess<Output> = { success: true; data: Output };
Expand Down Expand Up @@ -164,6 +168,10 @@ export abstract class ZodType<
return this._def.localized;
}

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

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

_getType(input: ParseInput): string {
Expand Down Expand Up @@ -391,6 +399,7 @@ export abstract class ZodType<
this.isNullable = this.isNullable.bind(this);
this.isOptional = this.isOptional.bind(this);
this.localize = this.localize.bind(this);
this.name = this.name.bind(this);
}

optional(): ZodOptional<this> {
Expand Down Expand Up @@ -455,6 +464,14 @@ export abstract class ZodType<
});
}

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

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

0 comments on commit dc9c438

Please sign in to comment.