Skip to content

Commit

Permalink
Add superforms
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 9, 2023
1 parent 56ace68 commit 3809d54
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): Useful for translating Zod error messages.
- [`@modular-forms/solid`](https://github.com/fabian-hiller/modular-forms): Modular form library for SolidJS that supports Zod for validation.
- [`houseform`](https://github.com/crutchcorn/houseform/): A React form library that uses Zod for validation.
- [`sveltekit-superforms`](https://github.com/ciscoheat/sveltekit-superforms): Supercharged form library for SvelteKit with Zod validation.

#### Zod to X

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

without constrained input:

```ts
const toDate = z.coerce.date()
const toDate = z.coerce.date();

// works intuitively
console.log(toDate.safeParse('2023-01-01').success) // true
console.log(toDate.safeParse("2023-01-01").success); // true

// might not be what you want
console.log(toDate.safeParse(null).success) // true
console.log(toDate.safeParse(null).success); // true
```

with constrained input:

```ts
const datelike = z.union([z.number(), z.string(), z.date()])
const datelikeToDate = datelike.pipe(z.coerce.date())
const datelike = z.union([z.number(), z.string(), z.date()]);
const datelikeToDate = datelike.pipe(z.coerce.date());

// still works intuitively
console.log(datelikeToDate.safeParse('2023-01-01').success) // true
console.log(datelikeToDate.safeParse("2023-01-01").success); // true

// more likely what you want
console.log(datelikeToDate.safeParse(null).success) // false
console.log(datelikeToDate.safeParse(null).success); // false
```

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

without constrained input:

```ts
const toBigInt = z.coerce.bigint()
const toBigInt = z.coerce.bigint();

// works intuitively
console.log( toBigInt.safeParse( '42' ) ) // true
console.log(toBigInt.safeParse("42")); // true

// probably not what you want
console.log( toBigInt.safeParse( null ) ) // throws uncaught error
console.log(toBigInt.safeParse(null)); // throws uncaught error
```

with constrained input:

```ts
const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
const toNumber = z.number().or(z.string()).pipe(z.coerce.number());
const toBigInt = z.bigint().or(toNumber).pipe(z.coerce.bigint());

// still works intuitively
console.log( toBigInt.safeParse( '42' ).success ) // true
console.log(toBigInt.safeParse("42").success); // true

// error handled by zod, more likely what you want
console.log( toBigInt.safeParse( null ).success ) // false
console.log(toBigInt.safeParse(null).success); // false
```

## Guides and concepts
Expand Down
33 changes: 19 additions & 14 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): Useful for translating Zod error messages.
- [`@modular-forms/solid`](https://github.com/fabian-hiller/modular-forms): Modular form library for SolidJS that supports Zod for validation.
- [`houseform`](https://github.com/crutchcorn/houseform/): A React form library that uses Zod for validation.
- [`sveltekit-superforms`](https://github.com/ciscoheat/sveltekit-superforms): Supercharged form library for SvelteKit with Zod validation.

#### Zod to X

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

without constrained input:

```ts
const toDate = z.coerce.date()
const toDate = z.coerce.date();

// works intuitively
console.log(toDate.safeParse('2023-01-01').success) // true
console.log(toDate.safeParse("2023-01-01").success); // true

// might not be what you want
console.log(toDate.safeParse(null).success) // true
console.log(toDate.safeParse(null).success); // true
```

with constrained input:

```ts
const datelike = z.union([z.number(), z.string(), z.date()])
const datelikeToDate = datelike.pipe(z.coerce.date())
const datelike = z.union([z.number(), z.string(), z.date()]);
const datelikeToDate = datelike.pipe(z.coerce.date());

// still works intuitively
console.log(datelikeToDate.safeParse('2023-01-01').success) // true
console.log(datelikeToDate.safeParse("2023-01-01").success); // true

// more likely what you want
console.log(datelikeToDate.safeParse(null).success) // false
console.log(datelikeToDate.safeParse(null).success); // false
```

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

without constrained input:

```ts
const toBigInt = z.coerce.bigint()
const toBigInt = z.coerce.bigint();

// works intuitively
console.log( toBigInt.safeParse( '42' ) ) // true
console.log(toBigInt.safeParse("42")); // true

// probably not what you want
console.log( toBigInt.safeParse( null ) ) // throws uncaught error
console.log(toBigInt.safeParse(null)); // throws uncaught error
```

with constrained input:

```ts
const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
const toNumber = z.number().or(z.string()).pipe(z.coerce.number());
const toBigInt = z.bigint().or(toNumber).pipe(z.coerce.bigint());

// still works intuitively
console.log( toBigInt.safeParse( '42' ).success ) // true
console.log(toBigInt.safeParse("42").success); // true

// error handled by zod, more likely what you want
console.log( toBigInt.safeParse( null ).success ) // false
console.log(toBigInt.safeParse(null).success); // false
```

## Guides and concepts
Expand Down
17 changes: 17 additions & 0 deletions playground.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
import { z } from "./src";
z;

function recursive<T extends z.ZodTypeAny>(
callback: <G extends z.ZodTypeAny>(schema: G) => T
): T {
return "asdf" as any;
}

const cat = recursive((type) => {
return z.object({
name: z.string(),
subcategories: type,
});
});
type cat = z.infer<typeof cat>; //["subcategories"];
declare let fido: cat;
fido;
fido.subcategories![0];

0 comments on commit 3809d54

Please sign in to comment.