Skip to content

Commit

Permalink
Tweak x.custom example
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 5, 2023
1 parent e599966 commit 950bd17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 41 deletions.
23 changes: 8 additions & 15 deletions README.md
Expand Up @@ -1817,21 +1817,14 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta
You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals.

```ts
const px = z.custom<`${number}px`>(
(x) =>
z
.string()
.regex(/^\d+px$/)
.safeParse(x).success
);
type Px = z.infer<typeof px>;
// type Px = `${number}px`

console.log(px.safeParse("42px").success); // true
console.log(px.safeParse("42vw").success); // false
console.log(px.safeParse(42).success); // false
console.log(px.safeParse(42n).success); // false
console.log(px.safeParse(null).success); // false
const px = z.custom<`${number}px`>((val) => {
return /^\d+px$/.test(val as string);
});

type px = z.infer<typeof px>; // `${number}px`

px.parse("42px"); // "42px"
px.parse("42vw"); // throws;
```

If you don't provide a validation function, Zod will allow any value. This can be dangerous!
Expand Down
23 changes: 8 additions & 15 deletions deno/lib/README.md
Expand Up @@ -1817,21 +1817,14 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta
You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals.

```ts
const px = z.custom<`${number}px`>(
(x) =>
z
.string()
.regex(/^\d+px$/)
.safeParse(x).success
);
type Px = z.infer<typeof px>;
// type Px = `${number}px`

console.log(px.safeParse("42px").success); // true
console.log(px.safeParse("42vw").success); // false
console.log(px.safeParse(42).success); // false
console.log(px.safeParse(42n).success); // false
console.log(px.safeParse(null).success); // false
const px = z.custom<`${number}px`>((val) => {
return /^\d+px$/.test(val as string);
});

type px = z.infer<typeof px>; // `${number}px`

px.parse("42px"); // "42px"
px.parse("42vw"); // throws;
```

If you don't provide a validation function, Zod will allow any value. This can be dangerous!
Expand Down
14 changes: 3 additions & 11 deletions playground.ts
@@ -1,14 +1,6 @@
import { z } from "./src";

const a = z.object({ a: z.string() });
const b = z.object({ a, b: z.string() });
const c = z.object({ a, b, c: z.string() });
const d = z.object({ a, b, c, d: z.string() });
const e = z.object({ a, b, c, d, e: z.string() });
const f = z.object({ a, b, c, d, e, f: z.string() });
const g = z.object({ a, b, c, d, e, f, g: z.string() });
const h = z.object({ a, b, c, d, e, f, g, h: z.string() });
const i = z.object({ a, b, c, d, e, f, g, h, i: z.string() });
const j = z.object({ a, b, c, d, e, f, g, h, i, j: z.string() });

z.number().catch((ctx) => {
ctx;
});
export const arg = j.parse({});

0 comments on commit 950bd17

Please sign in to comment.