Skip to content

Commit

Permalink
add localized bool schema support
Browse files Browse the repository at this point in the history
  • Loading branch information
Piet Groot Kormelink committed Apr 27, 2022
1 parent f6981f8 commit 8688e9f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ These docs have been translated into [Chinese](./README_ZH.md).
- [.promise](#promise)
- [.or](#or)
- [.and](#and)
- [.describe](#describe)
- [.localize](#localize)
- [Guides and concepts](#guides-and-concepts)
- [Type inference](#type-inference)
- [Writing generic functions](#writing-generic-functions)
Expand Down Expand Up @@ -1694,6 +1696,22 @@ const nullishString = z.string().nullish(); // string | null | undefined
z.string().optional().nullable();
```

### `.localize`

A schema method that defines that the schema should be translated.

```ts
const isLocalized = z.string().localize(true).localized; // boolean
```

### `.describe`

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

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

### `.array`

A convenience method that returns an array schema for the given type:
Expand Down
11 changes: 11 additions & 0 deletions deno/lib/__tests__/localize.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("localize", () => {
const localized = true;
const schema = z.string().localize(localized);
expect(schema.localized).toEqual(localized);
});
31 changes: 28 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
export interface ZodTypeDef {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
}

class ParseInputLazyPath implements ParseInput {
Expand Down Expand Up @@ -104,12 +105,23 @@ type RawCreateParams =
invalid_type_error?: string;
required_error?: string;
description?: string;
localized?: boolean;
}
| undefined;
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string };
type ProcessedCreateParams = {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
};
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {};
const { errorMap, invalid_type_error, required_error, description } = params;
const {
errorMap,
invalid_type_error,
required_error,
description,
localized,
} = params;
if (errorMap && (invalid_type_error || required_error)) {
throw new Error(
`Can't use "invalid" or "required" in conjunction with custom error map.`
Expand All @@ -124,7 +136,7 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
return { message: params.invalid_type_error };
return { message: ctx.defaultError };
};
return { errorMap: customMap, description };
return { errorMap: customMap, description, localized };
}

export type SafeParseSuccess<Output> = { success: true; data: Output };
Expand All @@ -148,6 +160,10 @@ export abstract class ZodType<
return this._def.description;
}

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

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

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

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

localize(localized: boolean): this {
const This = (this as any).constructor;
return new This({
...this._def,
localized,
});
}

isOptional(): boolean {
return this.safeParse(undefined).success;
}
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/localize.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("localize", () => {
const localized = true;
const schema = z.string().localize(localized);
expect(schema.localized).toEqual(localized);
});
31 changes: 28 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
export interface ZodTypeDef {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
}

class ParseInputLazyPath implements ParseInput {
Expand Down Expand Up @@ -104,12 +105,23 @@ type RawCreateParams =
invalid_type_error?: string;
required_error?: string;
description?: string;
localized?: boolean;
}
| undefined;
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string };
type ProcessedCreateParams = {
errorMap?: ZodErrorMap;
description?: string;
localized?: boolean;
};
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {};
const { errorMap, invalid_type_error, required_error, description } = params;
const {
errorMap,
invalid_type_error,
required_error,
description,
localized,
} = params;
if (errorMap && (invalid_type_error || required_error)) {
throw new Error(
`Can't use "invalid" or "required" in conjunction with custom error map.`
Expand All @@ -124,7 +136,7 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
return { message: params.invalid_type_error };
return { message: ctx.defaultError };
};
return { errorMap: customMap, description };
return { errorMap: customMap, description, localized };
}

export type SafeParseSuccess<Output> = { success: true; data: Output };
Expand All @@ -148,6 +160,10 @@ export abstract class ZodType<
return this._def.description;
}

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

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

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

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

localize(localized: boolean): this {
const This = (this as any).constructor;
return new This({
...this._def,
localized,
});
}

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

0 comments on commit 8688e9f

Please sign in to comment.