From 3809d54fc8c5dd0a0ce367bd2575fe3fdadf087d Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Thu, 9 Mar 2023 12:05:12 -0800 Subject: [PATCH] Add superforms --- README.md | 33 +++++++++++++++++++-------------- deno/lib/README.md | 33 +++++++++++++++++++-------------- playground.ts | 17 +++++++++++++++++ 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index ec988f6ee..b01b6121a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/deno/lib/README.md b/deno/lib/README.md index ec988f6ee..b01b6121a 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -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 @@ -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 diff --git a/playground.ts b/playground.ts index d17b18eb4..72c92c0c8 100644 --- a/playground.ts +++ b/playground.ts @@ -1,2 +1,19 @@ import { z } from "./src"; z; + +function recursive( + callback: (schema: G) => T +): T { + return "asdf" as any; +} + +const cat = recursive((type) => { + return z.object({ + name: z.string(), + subcategories: type, + }); +}); +type cat = z.infer; //["subcategories"]; +declare let fido: cat; +fido; +fido.subcategories![0];