@tanstack/zod-adapter 1.81.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present Tanner Linsley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const zod = require("zod");
4
+ const zodValidator = (options) => {
5
+ const input = "input" in options ? options.input : "input";
6
+ const output = "output" in options ? options.output : "output";
7
+ const _input = "schema" in options ? options.schema._input : options._input;
8
+ const _output = "schema" in options ? options.schema._output : options._output;
9
+ return {
10
+ types: {
11
+ input: input === "output" ? _output : _input,
12
+ output: output === "input" ? _input : _output
13
+ },
14
+ parse: (input2) => "schema" in options ? options.schema.parse(input2) : options.parse(input2)
15
+ };
16
+ };
17
+ const fallback = (schema, fallback2) => {
18
+ return zod.z.custom().pipe(schema.catch(fallback2));
19
+ };
20
+ exports.fallback = fallback;
21
+ exports.zodValidator = zodValidator;
22
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["import { z } from 'zod'\nimport type { ValidatorAdapter } from '@tanstack/react-router'\n\nexport interface ZodTypeLike {\n _input: any\n _output: any\n parse: (input: any) => any\n}\n\nexport type InputOutputOption = 'input' | 'output'\n\nexport interface zodValidatorOptions {\n readonly schema: ZodTypeLike\n readonly input?: InputOutputOption\n readonly output?: InputOutputOption\n}\n\nexport type zodValidatorInput<\n TOptions extends ZodTypeLike | zodValidatorOptions,\n> = TOptions extends zodValidatorOptions\n ? 'input' extends TOptions['input']\n ? TOptions['schema']['_input']\n : TOptions['schema']['_output']\n : TOptions extends ZodTypeLike\n ? TOptions['_input']\n : never\n\nexport type zodValidatorOutput<\n TOptions extends ZodTypeLike | zodValidatorOptions,\n> = TOptions extends zodValidatorOptions\n ? 'output' extends TOptions['output']\n ? TOptions['schema']['_output']\n : TOptions['schema']['_input']\n : TOptions extends ZodTypeLike\n ? TOptions['_output']\n : never\n\nexport type zodValidatorAdapter<\n TOptions extends ZodTypeLike | zodValidatorOptions,\n> = ValidatorAdapter<zodValidatorInput<TOptions>, zodValidatorOutput<TOptions>>\n\nexport const zodValidator = <\n TOptions extends ZodTypeLike | zodValidatorOptions,\n>(\n options: TOptions,\n): zodValidatorAdapter<TOptions> => {\n const input = 'input' in options ? options.input : 'input'\n const output = 'output' in options ? options.output : 'output'\n const _input = 'schema' in options ? options.schema._input : options._input\n const _output = 'schema' in options ? options.schema._output : options._output\n return {\n types: {\n input: input === 'output' ? _output : _input,\n output: output === 'input' ? _input : _output,\n },\n parse: (input) =>\n 'schema' in options ? options.schema.parse(input) : options.parse(input),\n }\n}\n\nexport const fallback = <TSchema extends z.ZodTypeAny>(\n schema: TSchema,\n fallback: TSchema['_input'],\n): z.ZodPipeline<\n z.ZodType<TSchema['_input'], z.ZodTypeDef, TSchema['_input']>,\n z.ZodCatch<TSchema>\n> => {\n return z.custom<TSchema['_input']>().pipe(schema.catch(fallback))\n}\n"],"names":["input","fallback","z"],"mappings":";;;AAyCa,MAAA,eAAe,CAG1B,YACkC;AAClC,QAAM,QAAQ,WAAW,UAAU,QAAQ,QAAQ;AACnD,QAAM,SAAS,YAAY,UAAU,QAAQ,SAAS;AACtD,QAAM,SAAS,YAAY,UAAU,QAAQ,OAAO,SAAS,QAAQ;AACrE,QAAM,UAAU,YAAY,UAAU,QAAQ,OAAO,UAAU,QAAQ;AAChE,SAAA;AAAA,IACL,OAAO;AAAA,MACL,OAAO,UAAU,WAAW,UAAU;AAAA,MACtC,QAAQ,WAAW,UAAU,SAAS;AAAA,IACxC;AAAA,IACA,OAAO,CAACA,WACN,YAAY,UAAU,QAAQ,OAAO,MAAMA,MAAK,IAAI,QAAQ,MAAMA,MAAK;AAAA,EAC3E;AACF;AAEa,MAAA,WAAW,CACtB,QACAC,cAIG;AACH,SAAOC,IAAAA,EAAE,SAA4B,KAAK,OAAO,MAAMD,SAAQ,CAAC;AAClE;;;"}
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod';
2
+ import { ValidatorAdapter } from '@tanstack/react-router';
3
+ export interface ZodTypeLike {
4
+ _input: any;
5
+ _output: any;
6
+ parse: (input: any) => any;
7
+ }
8
+ export type InputOutputOption = 'input' | 'output';
9
+ export interface zodValidatorOptions {
10
+ readonly schema: ZodTypeLike;
11
+ readonly input?: InputOutputOption;
12
+ readonly output?: InputOutputOption;
13
+ }
14
+ export type zodValidatorInput<TOptions extends ZodTypeLike | zodValidatorOptions> = TOptions extends zodValidatorOptions ? 'input' extends TOptions['input'] ? TOptions['schema']['_input'] : TOptions['schema']['_output'] : TOptions extends ZodTypeLike ? TOptions['_input'] : never;
15
+ export type zodValidatorOutput<TOptions extends ZodTypeLike | zodValidatorOptions> = TOptions extends zodValidatorOptions ? 'output' extends TOptions['output'] ? TOptions['schema']['_output'] : TOptions['schema']['_input'] : TOptions extends ZodTypeLike ? TOptions['_output'] : never;
16
+ export type zodValidatorAdapter<TOptions extends ZodTypeLike | zodValidatorOptions> = ValidatorAdapter<zodValidatorInput<TOptions>, zodValidatorOutput<TOptions>>;
17
+ export declare const zodValidator: <TOptions extends ZodTypeLike | zodValidatorOptions>(options: TOptions) => zodValidatorAdapter<TOptions>;
18
+ export declare const fallback: <TSchema extends z.ZodTypeAny>(schema: TSchema, fallback: TSchema["_input"]) => z.ZodPipeline<z.ZodType<TSchema["_input"], z.ZodTypeDef, TSchema["_input"]>, z.ZodCatch<TSchema>>;
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod';
2
+ import { ValidatorAdapter } from '@tanstack/react-router';
3
+ export interface ZodTypeLike {
4
+ _input: any;
5
+ _output: any;
6
+ parse: (input: any) => any;
7
+ }
8
+ export type InputOutputOption = 'input' | 'output';
9
+ export interface zodValidatorOptions {
10
+ readonly schema: ZodTypeLike;
11
+ readonly input?: InputOutputOption;
12
+ readonly output?: InputOutputOption;
13
+ }
14
+ export type zodValidatorInput<TOptions extends ZodTypeLike | zodValidatorOptions> = TOptions extends zodValidatorOptions ? 'input' extends TOptions['input'] ? TOptions['schema']['_input'] : TOptions['schema']['_output'] : TOptions extends ZodTypeLike ? TOptions['_input'] : never;
15
+ export type zodValidatorOutput<TOptions extends ZodTypeLike | zodValidatorOptions> = TOptions extends zodValidatorOptions ? 'output' extends TOptions['output'] ? TOptions['schema']['_output'] : TOptions['schema']['_input'] : TOptions extends ZodTypeLike ? TOptions['_output'] : never;
16
+ export type zodValidatorAdapter<TOptions extends ZodTypeLike | zodValidatorOptions> = ValidatorAdapter<zodValidatorInput<TOptions>, zodValidatorOutput<TOptions>>;
17
+ export declare const zodValidator: <TOptions extends ZodTypeLike | zodValidatorOptions>(options: TOptions) => zodValidatorAdapter<TOptions>;
18
+ export declare const fallback: <TSchema extends z.ZodTypeAny>(schema: TSchema, fallback: TSchema["_input"]) => z.ZodPipeline<z.ZodType<TSchema["_input"], z.ZodTypeDef, TSchema["_input"]>, z.ZodCatch<TSchema>>;
@@ -0,0 +1,22 @@
1
+ import { z } from "zod";
2
+ const zodValidator = (options) => {
3
+ const input = "input" in options ? options.input : "input";
4
+ const output = "output" in options ? options.output : "output";
5
+ const _input = "schema" in options ? options.schema._input : options._input;
6
+ const _output = "schema" in options ? options.schema._output : options._output;
7
+ return {
8
+ types: {
9
+ input: input === "output" ? _output : _input,
10
+ output: output === "input" ? _input : _output
11
+ },
12
+ parse: (input2) => "schema" in options ? options.schema.parse(input2) : options.parse(input2)
13
+ };
14
+ };
15
+ const fallback = (schema, fallback2) => {
16
+ return z.custom().pipe(schema.catch(fallback2));
17
+ };
18
+ export {
19
+ fallback,
20
+ zodValidator
21
+ };
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import { z } from 'zod'\nimport type { ValidatorAdapter } from '@tanstack/react-router'\n\nexport interface ZodTypeLike {\n _input: any\n _output: any\n parse: (input: any) => any\n}\n\nexport type InputOutputOption = 'input' | 'output'\n\nexport interface zodValidatorOptions {\n readonly schema: ZodTypeLike\n readonly input?: InputOutputOption\n readonly output?: InputOutputOption\n}\n\nexport type zodValidatorInput<\n TOptions extends ZodTypeLike | zodValidatorOptions,\n> = TOptions extends zodValidatorOptions\n ? 'input' extends TOptions['input']\n ? TOptions['schema']['_input']\n : TOptions['schema']['_output']\n : TOptions extends ZodTypeLike\n ? TOptions['_input']\n : never\n\nexport type zodValidatorOutput<\n TOptions extends ZodTypeLike | zodValidatorOptions,\n> = TOptions extends zodValidatorOptions\n ? 'output' extends TOptions['output']\n ? TOptions['schema']['_output']\n : TOptions['schema']['_input']\n : TOptions extends ZodTypeLike\n ? TOptions['_output']\n : never\n\nexport type zodValidatorAdapter<\n TOptions extends ZodTypeLike | zodValidatorOptions,\n> = ValidatorAdapter<zodValidatorInput<TOptions>, zodValidatorOutput<TOptions>>\n\nexport const zodValidator = <\n TOptions extends ZodTypeLike | zodValidatorOptions,\n>(\n options: TOptions,\n): zodValidatorAdapter<TOptions> => {\n const input = 'input' in options ? options.input : 'input'\n const output = 'output' in options ? options.output : 'output'\n const _input = 'schema' in options ? options.schema._input : options._input\n const _output = 'schema' in options ? options.schema._output : options._output\n return {\n types: {\n input: input === 'output' ? _output : _input,\n output: output === 'input' ? _input : _output,\n },\n parse: (input) =>\n 'schema' in options ? options.schema.parse(input) : options.parse(input),\n }\n}\n\nexport const fallback = <TSchema extends z.ZodTypeAny>(\n schema: TSchema,\n fallback: TSchema['_input'],\n): z.ZodPipeline<\n z.ZodType<TSchema['_input'], z.ZodTypeDef, TSchema['_input']>,\n z.ZodCatch<TSchema>\n> => {\n return z.custom<TSchema['_input']>().pipe(schema.catch(fallback))\n}\n"],"names":["input","fallback"],"mappings":";AAyCa,MAAA,eAAe,CAG1B,YACkC;AAClC,QAAM,QAAQ,WAAW,UAAU,QAAQ,QAAQ;AACnD,QAAM,SAAS,YAAY,UAAU,QAAQ,SAAS;AACtD,QAAM,SAAS,YAAY,UAAU,QAAQ,OAAO,SAAS,QAAQ;AACrE,QAAM,UAAU,YAAY,UAAU,QAAQ,OAAO,UAAU,QAAQ;AAChE,SAAA;AAAA,IACL,OAAO;AAAA,MACL,OAAO,UAAU,WAAW,UAAU;AAAA,MACtC,QAAQ,WAAW,UAAU,SAAS;AAAA,IACxC;AAAA,IACA,OAAO,CAACA,WACN,YAAY,UAAU,QAAQ,OAAO,MAAMA,MAAK,IAAI,QAAQ,MAAMA,MAAK;AAAA,EAC3E;AACF;AAEa,MAAA,WAAW,CACtB,QACAC,cAIG;AACH,SAAO,EAAE,SAA4B,KAAK,OAAO,MAAMA,SAAQ,CAAC;AAClE;"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@tanstack/zod-adapter",
3
+ "version": "1.81.6",
4
+ "description": "Modern and scalable routing for React applications",
5
+ "author": "Tanner Linsley",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/TanStack/router.git",
10
+ "directory": "packages/zod-adapter"
11
+ },
12
+ "homepage": "https://tanstack.com/router",
13
+ "funding": {
14
+ "type": "github",
15
+ "url": "https://github.com/sponsors/tannerlinsley"
16
+ },
17
+ "keywords": [
18
+ "react",
19
+ "location",
20
+ "router",
21
+ "routing",
22
+ "async",
23
+ "async router",
24
+ "typescript"
25
+ ],
26
+ "type": "module",
27
+ "types": "dist/esm/index.d.ts",
28
+ "main": "dist/cjs/index.cjs",
29
+ "module": "dist/esm/index.js",
30
+ "exports": {
31
+ ".": {
32
+ "import": {
33
+ "types": "./dist/esm/index.d.ts",
34
+ "default": "./dist/esm/index.js"
35
+ },
36
+ "require": {
37
+ "types": "./dist/cjs/index.d.cts",
38
+ "default": "./dist/cjs/index.cjs"
39
+ }
40
+ },
41
+ "./package.json": "./package.json"
42
+ },
43
+ "sideEffects": false,
44
+ "files": [
45
+ "dist",
46
+ "src"
47
+ ],
48
+ "engines": {
49
+ "node": ">=12"
50
+ },
51
+ "devDependencies": {
52
+ "@testing-library/jest-dom": "^6.6.3",
53
+ "@testing-library/react": "^16.0.1",
54
+ "zod": "^3.23.8",
55
+ "react": "^18.3.1",
56
+ "react-dom": "^18.3.1",
57
+ "@tanstack/react-router": "1.81.6"
58
+ },
59
+ "peerDependencies": {
60
+ "zod": "^3.23.8",
61
+ "@tanstack/react-router": ">=1.43.2"
62
+ },
63
+ "scripts": {}
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { z } from 'zod'
2
+ import type { ValidatorAdapter } from '@tanstack/react-router'
3
+
4
+ export interface ZodTypeLike {
5
+ _input: any
6
+ _output: any
7
+ parse: (input: any) => any
8
+ }
9
+
10
+ export type InputOutputOption = 'input' | 'output'
11
+
12
+ export interface zodValidatorOptions {
13
+ readonly schema: ZodTypeLike
14
+ readonly input?: InputOutputOption
15
+ readonly output?: InputOutputOption
16
+ }
17
+
18
+ export type zodValidatorInput<
19
+ TOptions extends ZodTypeLike | zodValidatorOptions,
20
+ > = TOptions extends zodValidatorOptions
21
+ ? 'input' extends TOptions['input']
22
+ ? TOptions['schema']['_input']
23
+ : TOptions['schema']['_output']
24
+ : TOptions extends ZodTypeLike
25
+ ? TOptions['_input']
26
+ : never
27
+
28
+ export type zodValidatorOutput<
29
+ TOptions extends ZodTypeLike | zodValidatorOptions,
30
+ > = TOptions extends zodValidatorOptions
31
+ ? 'output' extends TOptions['output']
32
+ ? TOptions['schema']['_output']
33
+ : TOptions['schema']['_input']
34
+ : TOptions extends ZodTypeLike
35
+ ? TOptions['_output']
36
+ : never
37
+
38
+ export type zodValidatorAdapter<
39
+ TOptions extends ZodTypeLike | zodValidatorOptions,
40
+ > = ValidatorAdapter<zodValidatorInput<TOptions>, zodValidatorOutput<TOptions>>
41
+
42
+ export const zodValidator = <
43
+ TOptions extends ZodTypeLike | zodValidatorOptions,
44
+ >(
45
+ options: TOptions,
46
+ ): zodValidatorAdapter<TOptions> => {
47
+ const input = 'input' in options ? options.input : 'input'
48
+ const output = 'output' in options ? options.output : 'output'
49
+ const _input = 'schema' in options ? options.schema._input : options._input
50
+ const _output = 'schema' in options ? options.schema._output : options._output
51
+ return {
52
+ types: {
53
+ input: input === 'output' ? _output : _input,
54
+ output: output === 'input' ? _input : _output,
55
+ },
56
+ parse: (input) =>
57
+ 'schema' in options ? options.schema.parse(input) : options.parse(input),
58
+ }
59
+ }
60
+
61
+ export const fallback = <TSchema extends z.ZodTypeAny>(
62
+ schema: TSchema,
63
+ fallback: TSchema['_input'],
64
+ ): z.ZodPipeline<
65
+ z.ZodType<TSchema['_input'], z.ZodTypeDef, TSchema['_input']>,
66
+ z.ZodCatch<TSchema>
67
+ > => {
68
+ return z.custom<TSchema['_input']>().pipe(schema.catch(fallback))
69
+ }