-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from iway1/zod-3.20-stability-and-refactor
Zod 3.20 stability and refactor
- Loading branch information
Showing
31 changed files
with
577 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export { parseNode as parseRouter } from "./parse/parse-router"; | ||
export { mapZodObjectToNode as mapZodObject } from "./parse/input-mappers/zod"; | ||
export { renderTrpcPanel as renderTrpcPanel } from "./render"; |
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodArrayDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ZodArrayDef } from "zod"; | ||
import { ArrayNode, ParseFunction } from "../../../parsed-node-types"; | ||
import { zodSelectorFunction } from "../selector"; | ||
|
||
export const parseZodArrayDef: ParseFunction<ZodArrayDef, ArrayNode> = (def, refs) => { | ||
const {type} = def | ||
const childType = zodSelectorFunction(type._def, refs) | ||
return { | ||
type: "array", | ||
childType, | ||
...refs | ||
}; | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodBooleanFieldDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ZodBooleanDef } from "zod"; | ||
import { BooleanNode, ParseFunction } from "../../../parsed-node-types"; | ||
|
||
export const parseZodBooleanFieldDef: ParseFunction<ZodBooleanDef, BooleanNode> = (_, ref)=>{ | ||
return { type: "boolean", ...ref }; | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodDiscriminatedUnionDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { AnyZodObject, ZodFirstPartyTypeKind } from "zod"; | ||
import { | ||
DiscriminatedUnionNode, | ||
ParseFunction, | ||
} from "../../../parsed-node-types"; | ||
import { zodSelectorFunction } from "../selector"; | ||
|
||
type OptionsMap = Map<string, AnyZodObject>; | ||
|
||
type ZodDiscriminatedUnionThreePointTwenty = { | ||
optionsMap: OptionsMap; | ||
discriminator: string; | ||
}; | ||
|
||
type ZodDiscriminatedUnionPreThreePointTwenty = { | ||
options: OptionsMap; | ||
discriminator: string; | ||
}; | ||
|
||
export type ZodDiscriminatedUnionDefUnversioned = | ||
| ZodDiscriminatedUnionPreThreePointTwenty | ||
| ZodDiscriminatedUnionThreePointTwenty; | ||
|
||
function isZodThreePointTwenty( | ||
def: ZodDiscriminatedUnionDefUnversioned | ||
): def is ZodDiscriminatedUnionThreePointTwenty { | ||
return "optionsMap" in def; | ||
} | ||
|
||
function makeDefConsistent(def: ZodDiscriminatedUnionDefUnversioned): { | ||
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion; | ||
discriminator: string; | ||
options: Map<string, AnyZodObject>; | ||
} { | ||
const optionsMap = isZodThreePointTwenty(def) | ||
? def.optionsMap | ||
: def.options; | ||
return { | ||
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion, | ||
discriminator: def.discriminator, | ||
options: optionsMap, | ||
}; | ||
} | ||
|
||
export const parseZodDiscriminatedUnionDef: ParseFunction< | ||
ZodDiscriminatedUnionDefUnversioned, | ||
DiscriminatedUnionNode | ||
> = (def, refs) => { | ||
const defConsistent = makeDefConsistent(def); | ||
const entries = Array.from(defConsistent.options.entries()); | ||
const nodeEntries = entries.map(([discriminatorValue, zodObj]) => [ | ||
discriminatorValue, | ||
zodSelectorFunction(zodObj._def, refs), | ||
]); | ||
// Not sure why this is here but seems important | ||
// if (nodeEntries.some((e) => e[1] === null)) return null; | ||
|
||
const nodesMap = Object.fromEntries(nodeEntries); | ||
|
||
return { | ||
type: "discriminated-union", | ||
discriminatedUnionValues: entries.map(([n]) => n), | ||
discriminatedUnionChildrenMap: nodesMap, | ||
discriminatorName: def.discriminator, | ||
...refs, | ||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodEnumDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ZodEnumDef } from "zod"; | ||
import { EnumNode, ParseFunction } from "../../../parsed-node-types"; | ||
|
||
export const parseZodEnumDef: ParseFunction<ZodEnumDef, EnumNode> = ( | ||
def, | ||
refs | ||
) => { | ||
const values = def.values as string[]; | ||
return { type: "enum", enumValues: values, ...refs }; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodLiteralDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ZodLiteralDef } from "zod"; | ||
import { LiteralNode, ParseFunction } from "../../../parsed-node-types"; | ||
|
||
export const parseZodLiteralDef: ParseFunction<ZodLiteralDef, LiteralNode> = (def, refs)=>{ | ||
return { | ||
type :'literal', | ||
value: def.value, | ||
...refs, | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodNumberDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NumberNode, ParseFunction } from "../../../parsed-node-types"; | ||
import { ZodNumberDef } from "zod"; | ||
|
||
export const parseZodNumberDef: ParseFunction<ZodNumberDef, NumberNode> = ( | ||
_, | ||
references | ||
) => { | ||
return { | ||
type: "number", | ||
path: references.path, | ||
optional: references.optional, | ||
}; | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodObjectDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ZodObjectDef } from "zod"; | ||
import { | ||
ObjectNode, | ||
ParsedInputNode, | ||
ParseFunction, | ||
UnsupportedNode, | ||
} from "../../../parsed-node-types"; | ||
import { zodSelectorFunction } from "../selector"; | ||
|
||
export const parseZodObjectDef: ParseFunction< | ||
ZodObjectDef, | ||
ObjectNode | UnsupportedNode | ||
> = (def, refs) => { | ||
const shape = def.shape(); | ||
const children: { [propertyName: string]: ParsedInputNode } = {}; | ||
for (var propertyName of Object.keys(shape)) { | ||
const node = zodSelectorFunction(shape[propertyName]!._def, { | ||
...refs, | ||
path: refs.path.concat([propertyName]), | ||
}); | ||
children[propertyName] = node; | ||
} | ||
return { | ||
type: "object", | ||
children, | ||
path: refs.path, | ||
optional: refs.optional, | ||
}; | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodOptionalDef.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ZodOptionalDef } from "zod"; | ||
import { ParsedInputNode, ParseFunction } from "../../../parsed-node-types"; | ||
import { zodSelectorFunction } from "../selector"; | ||
|
||
|
||
export const parseZodOptionalDef: ParseFunction<ZodOptionalDef, ParsedInputNode & {optional: true}> = (def, refs)=>{ | ||
const parsedInner = zodSelectorFunction(def.innerType._def, refs) | ||
return { | ||
...parsedInner, | ||
optional: true, | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodStringDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ParseFunction, StringNode } from "../../../parsed-node-types"; | ||
import { ZodStringDef } from "zod"; | ||
|
||
export const parseZodStringDef: ParseFunction<ZodStringDef, StringNode> = (_, refs)=>{ | ||
return { | ||
type: 'string', | ||
path: refs.path, | ||
optional: refs.optional | ||
} | ||
} |
Oops, something went wrong.