From e9298e26777f58bb2c95dc3fcebd80602a89bae2 Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Thu, 23 Feb 2023 22:12:09 +0000 Subject: [PATCH] BRAND Record to Non Partial --- deno/lib/README.md | 20 +++++++++++++------- deno/lib/types.ts | 2 ++ src/types.ts | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/deno/lib/README.md b/deno/lib/README.md index 6c2711ec5..d7b5f62bb 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -1409,7 +1409,7 @@ type NumberSet = z.infer; // type NumberSet = Set ``` -Set schemas can be further contrainted with the following utility methods. +Set schemas can be further constrained with the following utility methods. ```ts z.set(z.string()).nonempty(); // must contain at least one item @@ -1714,6 +1714,12 @@ If you don't provide a validation function, Zod will allow any value. This can b z.custom<{ arg: string }>(); // performs no validation ``` +You can customize the error message and other options by passing a second argument. This parameter works the same way as the params parameter of [`.refine`](#refine). + +```ts +z.custom<...>((val) => ..., "custom error message"); +``` + ## Schema methods All Zod schemas contain certain methods. @@ -2349,14 +2355,14 @@ makeSchemaOptional(z.number()); Zod provides a subclass of Error called `ZodError`. ZodErrors contain an `issues` array containing detailed information about the validation problems. ```ts -const data = z +const result = z .object({ name: z.string(), }) .safeParse({ name: 12 }); -if (!data.success) { - data.error.issues; +if (!result.success) { + result.error.issues; /* [ { "code": "invalid_type", @@ -2378,14 +2384,14 @@ Zod's error reporting emphasizes _completeness_ and _correctness_. If you are lo You can use the `.format()` method to convert this error into a nested object. ```ts -const data = z +const result = z .object({ name: z.string(), }) .safeParse({ name: 12 }); -if (!data.success) { - const formatted = data.error.format(); +if (!result.success) { + const formatted = result.error.format(); /* { name: { _errors: [ 'Expected string, received number' ] } } */ diff --git a/deno/lib/types.ts b/deno/lib/types.ts index df6c1391d..364911222 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -3066,6 +3066,8 @@ export type RecordType = [ ? Record : [symbol] extends [K] ? Record + : K extends BRAND + ? Record : Partial>; export class ZodRecord< Key extends KeySchema = ZodString, diff --git a/src/types.ts b/src/types.ts index 2af24c0a9..9f3f7150d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3066,6 +3066,8 @@ export type RecordType = [ ? Record : [symbol] extends [K] ? Record + : K extends BRAND + ? Record : Partial>; export class ZodRecord< Key extends KeySchema = ZodString,