Skip to content

Commit 3809d54

Browse files
committed
Add superforms
1 parent 56ace68 commit 3809d54

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ There are a growing number of tools that are built atop or support Zod natively!
426426
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): Useful for translating Zod error messages.
427427
- [`@modular-forms/solid`](https://github.com/fabian-hiller/modular-forms): Modular form library for SolidJS that supports Zod for validation.
428428
- [`houseform`](https://github.com/crutchcorn/houseform/): A React form library that uses Zod for validation.
429+
- [`sveltekit-superforms`](https://github.com/ciscoheat/sveltekit-superforms): Supercharged form library for SvelteKit with Zod validation.
429430

430431
#### Zod to X
431432

@@ -2399,51 +2400,55 @@ The `.pipe()` method returns a `ZodPipeline` instance.
23992400
You can constrain the input to types that work well with your chosen coercion. Then use `.pipe()` to apply the coercion.
24002401

24012402
without constrained input:
2403+
24022404
```ts
2403-
const toDate = z.coerce.date()
2405+
const toDate = z.coerce.date();
24042406

24052407
// works intuitively
2406-
console.log(toDate.safeParse('2023-01-01').success) // true
2408+
console.log(toDate.safeParse("2023-01-01").success); // true
24072409

24082410
// might not be what you want
2409-
console.log(toDate.safeParse(null).success) // true
2411+
console.log(toDate.safeParse(null).success); // true
24102412
```
24112413

24122414
with constrained input:
2415+
24132416
```ts
2414-
const datelike = z.union([z.number(), z.string(), z.date()])
2415-
const datelikeToDate = datelike.pipe(z.coerce.date())
2417+
const datelike = z.union([z.number(), z.string(), z.date()]);
2418+
const datelikeToDate = datelike.pipe(z.coerce.date());
24162419

24172420
// still works intuitively
2418-
console.log(datelikeToDate.safeParse('2023-01-01').success) // true
2421+
console.log(datelikeToDate.safeParse("2023-01-01").success); // true
24192422

24202423
// more likely what you want
2421-
console.log(datelikeToDate.safeParse(null).success) // false
2424+
console.log(datelikeToDate.safeParse(null).success); // false
24222425
```
24232426

24242427
You can also use this technique to avoid coercions that throw uncaught errors.
24252428

24262429
without constrained input:
2430+
24272431
```ts
2428-
const toBigInt = z.coerce.bigint()
2432+
const toBigInt = z.coerce.bigint();
24292433

24302434
// works intuitively
2431-
console.log( toBigInt.safeParse( '42' ) ) // true
2435+
console.log(toBigInt.safeParse("42")); // true
24322436

24332437
// probably not what you want
2434-
console.log( toBigInt.safeParse( null ) ) // throws uncaught error
2438+
console.log(toBigInt.safeParse(null)); // throws uncaught error
24352439
```
24362440

24372441
with constrained input:
2442+
24382443
```ts
2439-
const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
2440-
const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
2444+
const toNumber = z.number().or(z.string()).pipe(z.coerce.number());
2445+
const toBigInt = z.bigint().or(toNumber).pipe(z.coerce.bigint());
24412446

24422447
// still works intuitively
2443-
console.log( toBigInt.safeParse( '42' ).success ) // true
2448+
console.log(toBigInt.safeParse("42").success); // true
24442449

24452450
// error handled by zod, more likely what you want
2446-
console.log( toBigInt.safeParse( null ).success ) // false
2451+
console.log(toBigInt.safeParse(null).success); // false
24472452
```
24482453

24492454
## Guides and concepts

deno/lib/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ There are a growing number of tools that are built atop or support Zod natively!
426426
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): Useful for translating Zod error messages.
427427
- [`@modular-forms/solid`](https://github.com/fabian-hiller/modular-forms): Modular form library for SolidJS that supports Zod for validation.
428428
- [`houseform`](https://github.com/crutchcorn/houseform/): A React form library that uses Zod for validation.
429+
- [`sveltekit-superforms`](https://github.com/ciscoheat/sveltekit-superforms): Supercharged form library for SvelteKit with Zod validation.
429430

430431
#### Zod to X
431432

@@ -2399,51 +2400,55 @@ The `.pipe()` method returns a `ZodPipeline` instance.
23992400
You can constrain the input to types that work well with your chosen coercion. Then use `.pipe()` to apply the coercion.
24002401

24012402
without constrained input:
2403+
24022404
```ts
2403-
const toDate = z.coerce.date()
2405+
const toDate = z.coerce.date();
24042406

24052407
// works intuitively
2406-
console.log(toDate.safeParse('2023-01-01').success) // true
2408+
console.log(toDate.safeParse("2023-01-01").success); // true
24072409

24082410
// might not be what you want
2409-
console.log(toDate.safeParse(null).success) // true
2411+
console.log(toDate.safeParse(null).success); // true
24102412
```
24112413

24122414
with constrained input:
2415+
24132416
```ts
2414-
const datelike = z.union([z.number(), z.string(), z.date()])
2415-
const datelikeToDate = datelike.pipe(z.coerce.date())
2417+
const datelike = z.union([z.number(), z.string(), z.date()]);
2418+
const datelikeToDate = datelike.pipe(z.coerce.date());
24162419

24172420
// still works intuitively
2418-
console.log(datelikeToDate.safeParse('2023-01-01').success) // true
2421+
console.log(datelikeToDate.safeParse("2023-01-01").success); // true
24192422

24202423
// more likely what you want
2421-
console.log(datelikeToDate.safeParse(null).success) // false
2424+
console.log(datelikeToDate.safeParse(null).success); // false
24222425
```
24232426

24242427
You can also use this technique to avoid coercions that throw uncaught errors.
24252428

24262429
without constrained input:
2430+
24272431
```ts
2428-
const toBigInt = z.coerce.bigint()
2432+
const toBigInt = z.coerce.bigint();
24292433

24302434
// works intuitively
2431-
console.log( toBigInt.safeParse( '42' ) ) // true
2435+
console.log(toBigInt.safeParse("42")); // true
24322436

24332437
// probably not what you want
2434-
console.log( toBigInt.safeParse( null ) ) // throws uncaught error
2438+
console.log(toBigInt.safeParse(null)); // throws uncaught error
24352439
```
24362440

24372441
with constrained input:
2442+
24382443
```ts
2439-
const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
2440-
const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
2444+
const toNumber = z.number().or(z.string()).pipe(z.coerce.number());
2445+
const toBigInt = z.bigint().or(toNumber).pipe(z.coerce.bigint());
24412446

24422447
// still works intuitively
2443-
console.log( toBigInt.safeParse( '42' ).success ) // true
2448+
console.log(toBigInt.safeParse("42").success); // true
24442449

24452450
// error handled by zod, more likely what you want
2446-
console.log( toBigInt.safeParse( null ).success ) // false
2451+
console.log(toBigInt.safeParse(null).success); // false
24472452
```
24482453

24492454
## Guides and concepts

playground.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
import { z } from "./src";
22
z;
3+
4+
function recursive<T extends z.ZodTypeAny>(
5+
callback: <G extends z.ZodTypeAny>(schema: G) => T
6+
): T {
7+
return "asdf" as any;
8+
}
9+
10+
const cat = recursive((type) => {
11+
return z.object({
12+
name: z.string(),
13+
subcategories: type,
14+
});
15+
});
16+
type cat = z.infer<typeof cat>; //["subcategories"];
17+
declare let fido: cat;
18+
fido;
19+
fido.subcategories![0];

0 commit comments

Comments
 (0)