Skip to content

Commit

Permalink
3.19.0 (#1383)
Browse files Browse the repository at this point in the history
* Add locales, fix abstract classes in instanceof, fix ZodFunction constructor, remove circular import

* Add z.NEVER

* Remove circular import

* Fix #1300

* Add runtime check for z.tuple, closes #1291

* Document .rest

* Stop implicitly adding rest arg to function schemas

* Revert to simple constructor

* 3.19.0

* Update readme

* Update tsversion

Co-authored-by: Colin McDonnell <[email protected]>
  • Loading branch information
colinhacks and Colin McDonnell authored Sep 6, 2022
1 parent e75f348 commit f1dfabb
Show file tree
Hide file tree
Showing 25 changed files with 541 additions and 346 deletions.
56 changes: 35 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`remix-domains`](https://github.com/SeasonedSoftware/remix-domains/): Improves end-to-end type safety in [Remix](https://remix.run/) by leveraging Zod to parse the framework's inputs such as FormData, URLSearchParams, etc.
- [`@zodios/core`](https://github.com/ecyrbe/zodios): A typescript API client with runtime and compile time validation backed by axios and zod.
- [`@runtyping/zod`](https://github.com/johngeorgewright/runtyping/tree/master/packages/zod): Generate zod from static types & JSON schema.
- [`slonik`](https://github.com/gajus/slonik/tree/gajus/add-zod-validation-backwards-compatible#runtime-validation-and-static-type-inference): Node.js Postgres client with strong Zod integration

#### Form integrations

Expand Down Expand Up @@ -1073,6 +1074,14 @@ type Athlete = z.infer<typeof athleteSchema>;
// type Athlete = [string, number, { pointsScored: number }]
```

A variadic ("rest") argument can be added with the `.rest` method.

```ts
const variadicTuple = z.tuple([z.string()]).rest(z.number());
const result = variadicTuple.parse(["hello", 1, 2, 3]);
// => [string, ...number[]];
```

## Unions

Zod includes a built-in `z.union` method for composing "OR" types.
Expand Down Expand Up @@ -1683,34 +1692,33 @@ const Strings = z.array(z.string()).superRefine((val, ctx) => {
});
```

You can add as many issues as you like. If `ctx.addIssue` is NOT called during the execution of the function, validation passes.
You can add as many issues as you like. If `ctx.addIssue` is _not_ called during the execution of the function, validation passes.

Normally refinements always create issues with a `ZodIssueCode.custom` error code, but with `superRefine` you can create any issue of any code. Each issue code is described in detail in the Error Handling guide: [ERROR_HANDLING.md](ERROR_HANDLING.md).

#### Abort early

By default, parsing will continue even after a refinement check fails. For instance, if you chain together multiple refinements, they will all be executed. However, it may be desirable to _abort early_ to prevent later refinements from being executed. To achieve this, pass the `fatal` flag to `ctx.addIssue`:
By default, parsing will continue even after a refinement check fails. For instance, if you chain together multiple refinements, they will all be executed. However, it may be desirable to _abort early_ to prevent later refinements from being executed. To achieve this, pass the `fatal` flag to `ctx.addIssue` and return `z.NEVER`.

```ts
const Strings = z
.number()
.superRefine((val, ctx) => {
if (val < 10) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "foo",
fatal: true,
});
}
})
.superRefine((val, ctx) => {
if (val !== " ") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "bar",
});
}
});
const schema = z.number().superRefine((val, ctx) => {
if (val < 10) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "should be >= 10",
fatal: true,
});
return z.NEVER;
}
if (val !== 12) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "should be twelve",
});
}
});
```

### `.transform`
Expand Down Expand Up @@ -1749,6 +1757,12 @@ const Strings = z.string().transform((val, ctx) => {
code: z.ZodIssueCode.custom,
message: "Not a number",
});
// This is a special symbol you can use to
// return early from the transform function.
// It has type `never` so it does not affect the
// inferred return type.
return z.NEVER;
}
return parsed;
});
Expand Down
64 changes: 39 additions & 25 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`tRPC`](https://github.com/trpc/trpc): Build end-to-end typesafe APIs without GraphQL.
- [`ts-to-zod`](https://github.com/fabien0102/ts-to-zod): Convert TypeScript definitions into Zod schemas.
- [`zod-to-ts`](https://github.com/sachinraja/zod-to-ts): Generate TypeScript definitions from Zod schemas.
- [`@anatine/zod-openapi`](https://github.com/anatine/zod-plugins/tree/main/libs/zod-openapi): Converts a Zod schema to an OpenAPI v3.x `SchemaObject`.
- [`@anatine/zod-mock`](https://github.com/anatine/zod-plugins/tree/main/libs/zod-mock): Generate mock data from a Zod schema. Powered by [faker.js](https://github.com/Marak/Faker.js).
- [`@anatine/zod-nestjs`](https://github.com/anatine/zod-plugins/tree/main/libs/zod-nestjs): Helper methods for using Zod in a NestJS project.
- [`@anatine/zod-openapi`](https://github.com/anatine/zod-plugins/tree/main/packages/zod-openapi): Converts a Zod schema to an OpenAPI v3.x `SchemaObject`.
- [`@anatine/zod-mock`](https://github.com/anatine/zod-plugins/tree/main/packages/zod-mock): Generate mock data from a Zod schema. Powered by [faker.js](https://github.com/Marak/Faker.js).
- [`@anatine/zod-nestjs`](https://github.com/anatine/zod-plugins/tree/main/packages/zod-nestjs): Helper methods for using Zod in a NestJS project.
- [`zod-mocking`](https://github.com/dipasqualew/zod-mocking): Generate mock data from your Zod schemas.
- [`zod-fast-check`](https://github.com/DavidTimms/zod-fast-check): Generate `fast-check` arbitraries from Zod schemas.
- [`zod-endpoints`](https://github.com/flock-community/zod-endpoints): Contract-first strictly typed endpoints with Zod. OpenAPI compatible.
Expand All @@ -330,6 +330,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`remix-domains`](https://github.com/SeasonedSoftware/remix-domains/): Improves end-to-end type safety in [Remix](https://remix.run/) by leveraging Zod to parse the framework's inputs such as FormData, URLSearchParams, etc.
- [`@zodios/core`](https://github.com/ecyrbe/zodios): A typescript API client with runtime and compile time validation backed by axios and zod.
- [`@runtyping/zod`](https://github.com/johngeorgewright/runtyping/tree/master/packages/zod): Generate zod from static types & JSON schema.
- [`slonik`](https://github.com/gajus/slonik/tree/gajus/add-zod-validation-backwards-compatible#runtime-validation-and-static-type-inference): Node.js Postgres client with strong Zod integration

#### Form integrations

Expand Down Expand Up @@ -799,7 +800,7 @@ Dog.shape.age; // => number schema

### `.keyof`

Use `.key` to create a `ZodEnum` schema from the keys of an object schema.
Use `.keyof` to create a `ZodEnum` schema from the keys of an object schema.

```ts
const keySchema = Dog.keyof();
Expand Down Expand Up @@ -1073,6 +1074,14 @@ type Athlete = z.infer<typeof athleteSchema>;
// type Athlete = [string, number, { pointsScored: number }]
```

A variadic ("rest") argument can be added with the `.rest` method.

```ts
const variadicTuple = z.tuple([z.string()]).rest(z.number());
const result = variadicTuple.parse(["hello", 1, 2, 3]);
// => [string, ...number[]];
```

## Unions

Zod includes a built-in `z.union` method for composing "OR" types.
Expand Down Expand Up @@ -1683,34 +1692,33 @@ const Strings = z.array(z.string()).superRefine((val, ctx) => {
});
```

You can add as many issues as you like. If `ctx.addIssue` is NOT called during the execution of the function, validation passes.
You can add as many issues as you like. If `ctx.addIssue` is _not_ called during the execution of the function, validation passes.

Normally refinements always create issues with a `ZodIssueCode.custom` error code, but with `superRefine` you can create any issue of any code. Each issue code is described in detail in the Error Handling guide: [ERROR_HANDLING.md](ERROR_HANDLING.md).

#### Abort early

By default, parsing will continue even after a refinement check fails. For instance, if you chain together multiple refinements, they will all be executed. However, it may be desirable to _abort early_ to prevent later refinements from being executed. To achieve this, pass the `fatal` flag to `ctx.addIssue`:
By default, parsing will continue even after a refinement check fails. For instance, if you chain together multiple refinements, they will all be executed. However, it may be desirable to _abort early_ to prevent later refinements from being executed. To achieve this, pass the `fatal` flag to `ctx.addIssue` and return `z.NEVER`.

```ts
const Strings = z
.number()
.superRefine((val, ctx) => {
if (val < 10) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "foo",
fatal: true,
});
}
})
.superRefine((val, ctx) => {
if (val !== " ") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "bar",
});
}
});
const schema = z.number().superRefine((val, ctx) => {
if (val < 10) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "should be >= 10",
fatal: true,
});
return z.NEVER;
}
if (val !== 12) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "should be twelve",
});
}
});
```

### `.transform`
Expand Down Expand Up @@ -1749,6 +1757,12 @@ const Strings = z.string().transform((val, ctx) => {
code: z.ZodIssueCode.custom,
message: "Not a number",
});
// This is a special symbol you can use to
// return early from the transform function.
// It has type `never` so it does not affect the
// inferred return type.
return z.NEVER;
}
return parsed;
});
Expand Down
131 changes: 4 additions & 127 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TypeOf, ZodType } from "./index.ts";
import { jsonStringifyReplacer } from "./helpers/parseUtil.ts";
import { Primitive } from "./helpers/typeAliases.ts";
import { util, ZodParsedType } from "./helpers/util.ts";

Expand Down Expand Up @@ -251,7 +250,7 @@ export class ZodError<T = any> extends Error {
return this.message;
}
get message() {
return JSON.stringify(this.issues, jsonStringifyReplacer, 2);
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
}

get isEmpty(): boolean {
Expand Down Expand Up @@ -299,134 +298,12 @@ export type IssueData = stripPath<ZodIssueOptionalMessage> & {
};
export type MakeErrorData = IssueData;

type ErrorMapCtx = {
export type ErrorMapCtx = {
defaultError: string;
data: any;
};

export type ZodErrorMap = typeof defaultErrorMap;
export const defaultErrorMap = (
export type ZodErrorMap = (
issue: ZodIssueOptionalMessage,
_ctx: ErrorMapCtx
): { message: string } => {
let message: string;
switch (issue.code) {
case ZodIssueCode.invalid_type:
if (issue.received === ZodParsedType.undefined) {
message = "Required";
} else {
message = `Expected ${issue.expected}, received ${issue.received}`;
}
break;
case ZodIssueCode.invalid_literal:
message = `Invalid literal value, expected ${JSON.stringify(
issue.expected,
jsonStringifyReplacer
)}`;
break;
case ZodIssueCode.unrecognized_keys:
message = `Unrecognized key(s) in object: ${util.joinValues(
issue.keys,
", "
)}`;
break;
case ZodIssueCode.invalid_union:
message = `Invalid input`;
break;
case ZodIssueCode.invalid_union_discriminator:
message = `Invalid discriminator value. Expected ${util.joinValues(
issue.options
)}`;
break;
case ZodIssueCode.invalid_enum_value:
message = `Invalid enum value. Expected ${util.joinValues(
issue.options
)}, received '${issue.received}'`;
break;
case ZodIssueCode.invalid_arguments:
message = `Invalid function arguments`;
break;
case ZodIssueCode.invalid_return_type:
message = `Invalid function return type`;
break;
case ZodIssueCode.invalid_date:
message = `Invalid date`;
break;
case ZodIssueCode.invalid_string:
if (typeof issue.validation === "object") {
if ("startsWith" in issue.validation) {
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
} else if ("endsWith" in issue.validation) {
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
} else {
util.assertNever(issue.validation);
}
} else if (issue.validation !== "regex") {
message = `Invalid ${issue.validation}`;
} else {
message = "Invalid";
}
break;
case ZodIssueCode.too_small:
if (issue.type === "array")
message = `Array must contain ${
issue.inclusive ? `at least` : `more than`
} ${issue.minimum} element(s)`;
else if (issue.type === "string")
message = `String must contain ${
issue.inclusive ? `at least` : `over`
} ${issue.minimum} character(s)`;
else if (issue.type === "number")
message = `Number must be greater than ${
issue.inclusive ? `or equal to ` : ``
}${issue.minimum}`;
else if (issue.type === "date")
message = `Date must be greater than ${
issue.inclusive ? `or equal to ` : ``
}${new Date(issue.minimum)}`;
else message = "Invalid input";
break;
case ZodIssueCode.too_big:
if (issue.type === "array")
message = `Array must contain ${
issue.inclusive ? `at most` : `less than`
} ${issue.maximum} element(s)`;
else if (issue.type === "string")
message = `String must contain ${
issue.inclusive ? `at most` : `under`
} ${issue.maximum} character(s)`;
else if (issue.type === "number")
message = `Number must be less than ${
issue.inclusive ? `or equal to ` : ``
}${issue.maximum}`;
else if (issue.type === "date")
message = `Date must be smaller than ${
issue.inclusive ? `or equal to ` : ``
}${new Date(issue.maximum)}`;
else message = "Invalid input";
break;
case ZodIssueCode.custom:
message = `Invalid input`;
break;
case ZodIssueCode.invalid_intersection_types:
message = `Intersection results could not be merged`;
break;
case ZodIssueCode.not_multiple_of:
message = `Number must be a multiple of ${issue.multipleOf}`;
break;
default:
message = _ctx.defaultError;
util.assertNever(issue);
}
return { message };
};

let overrideErrorMap = defaultErrorMap;

export function setErrorMap(map: ZodErrorMap) {
overrideErrorMap = map;
}

export function getErrorMap() {
return overrideErrorMap;
}
) => { message: string };
2 changes: 2 additions & 0 deletions deno/lib/__tests__/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ test("output validation error", () => {
expect(checker).toThrow();
});

z.function(z.tuple([z.string()])).args()._def.args;

test("special function error codes", () => {
const checker = z
.function(z.tuple([z.string()]), z.boolean())
Expand Down
7 changes: 7 additions & 0 deletions deno/lib/__tests__/instanceof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import * as z from "../index.ts";
test("instanceof", async () => {
class Test {}
class Subtest extends Test {}
abstract class AbstractBar {
constructor(public val: string) {}
}
class Bar extends AbstractBar {}

const TestSchema = z.instanceof(Test);
const SubtestSchema = z.instanceof(Subtest);
const BarSchema = z.instanceof(Bar);

TestSchema.parse(new Test());
TestSchema.parse(new Subtest());
SubtestSchema.parse(new Subtest());
const bar = BarSchema.parse(new Bar("asdf"));
expect(bar.val).toEqual("asdf");

await expect(() => SubtestSchema.parse(new Test())).toThrow(
/Input not instance of Subtest/
Expand Down
Loading

0 comments on commit f1dfabb

Please sign in to comment.