@onetyped/zod
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -69,3 +69,6 @@ // src/from-schema.ts | ||
const propertySchemas = Object.fromEntries( | ||
Object.entries(schema._def.shape()).map(([key, value]) => [key, fromZodSchema(value)]) | ||
Object.entries(schema._def.shape()).map(([key, value]) => [ | ||
key, | ||
fromZodSchema(value) | ||
]) | ||
); | ||
@@ -81,22 +84,36 @@ return object(propertySchemas); | ||
case ZodFirstPartyTypeKind.ZodRecord: { | ||
return record(fromZodSchema(schema._def.keyType), fromZodSchema(schema._def.valueType)); | ||
return record( | ||
fromZodSchema(schema._def.keyType), | ||
fromZodSchema(schema._def.valueType) | ||
); | ||
} | ||
case ZodFirstPartyTypeKind.ZodMap: { | ||
return map(fromZodSchema(schema._def.keyType), fromZodSchema(schema._def.valueType)); | ||
return map( | ||
fromZodSchema(schema._def.keyType), | ||
fromZodSchema(schema._def.valueType) | ||
); | ||
} | ||
case ZodFirstPartyTypeKind.ZodFunction: { | ||
const argumentSchemas = schema._def.args.map((argument) => fromZodSchema(argument)); | ||
const returnSchema = typeof schema._def.returns !== "undefined" ? fromZodSchema(schema._def.returns) : void 0; | ||
const argumentSchemas = schema._def.args.map( | ||
(argument) => fromZodSchema(argument) | ||
); | ||
const returnSchema = typeof schema._def.returns === "undefined" ? void 0 : fromZodSchema(schema._def.returns); | ||
return func({ arguments: argumentSchemas, return: returnSchema }); | ||
} | ||
case ZodFirstPartyTypeKind.ZodUnion: { | ||
const schemas = schema._def.options.map((option) => fromZodSchema(option)); | ||
const schemas = schema._def.options.map( | ||
(option) => fromZodSchema(option) | ||
); | ||
return union(schemas); | ||
} | ||
case ZodFirstPartyTypeKind.ZodTuple: { | ||
const schemas = schema._def.items.map((item) => fromZodSchema(item)); | ||
const schemas = schema._def.items.map( | ||
(item) => fromZodSchema(item) | ||
); | ||
return tuple(schemas); | ||
} | ||
case ZodFirstPartyTypeKind.ZodIntersection: { | ||
const schemas = schema._def.options.map((option) => fromZodSchema(option)); | ||
const schemas = schema._def.options.map( | ||
(option) => fromZodSchema(option) | ||
); | ||
return intersection(schemas); | ||
@@ -117,3 +134,6 @@ } | ||
// src/to-schema.ts | ||
import { mapMultipleInnerNode, mapObjectNode } from "@onetyped/core"; | ||
import { | ||
mapMultipleInnerNode, | ||
mapObjectNode | ||
} from "@onetyped/core"; | ||
import { z as z2 } from "zod"; | ||
@@ -136,7 +156,8 @@ var toZodSchema = (node, definitions = /* @__PURE__ */ new Map()) => { | ||
const resolvedSchemaBag = { schema: z2.unknown() }; | ||
zodSchemaDefinitions.set( | ||
node.identifier, | ||
resolvedSchemaBag | ||
zodSchemaDefinitions.set(node.identifier, resolvedSchemaBag); | ||
resolvedSchemaBag.schema = toZodSchemaInternal( | ||
nodeDefinition, | ||
nodeDefinitions, | ||
zodSchemaDefinitions | ||
); | ||
resolvedSchemaBag.schema = toZodSchemaInternal(nodeDefinition, nodeDefinitions, zodSchemaDefinitions); | ||
return z2.lazy(() => resolvedSchemaBag.schema); | ||
@@ -185,3 +206,6 @@ } | ||
const propertySchemas = Object.fromEntries( | ||
mapObjectNode(node, (node2) => toZodSchemaInternal(node2, nodeDefinitions, zodSchemaDefinitions)) | ||
mapObjectNode( | ||
node, | ||
(node2) => toZodSchemaInternal(node2, nodeDefinitions, zodSchemaDefinitions) | ||
) | ||
); | ||
@@ -191,6 +215,10 @@ return z2.object(propertySchemas); | ||
case "array": { | ||
return z2.array(toZodSchemaInternal(node.type, nodeDefinitions, zodSchemaDefinitions)); | ||
return z2.array( | ||
toZodSchemaInternal(node.type, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
} | ||
case "set": { | ||
return z2.set(toZodSchemaInternal(node.type, nodeDefinitions, zodSchemaDefinitions)); | ||
return z2.set( | ||
toZodSchemaInternal(node.type, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
} | ||
@@ -211,6 +239,15 @@ case "record": { | ||
const argumentSchemas = z2.tuple( | ||
node.arguments.map((argument) => toZodSchemaInternal(argument, nodeDefinitions, zodSchemaDefinitions)) | ||
node.arguments.map( | ||
(argument) => toZodSchemaInternal(argument, nodeDefinitions, zodSchemaDefinitions) | ||
) | ||
); | ||
if (node.return) { | ||
return z2.function(argumentSchemas, toZodSchemaInternal(node.return, nodeDefinitions, zodSchemaDefinitions)); | ||
return z2.function( | ||
argumentSchemas, | ||
toZodSchemaInternal( | ||
node.return, | ||
nodeDefinitions, | ||
zodSchemaDefinitions | ||
) | ||
); | ||
} | ||
@@ -221,3 +258,7 @@ return z2.function(argumentSchemas); | ||
if (node.types.length === 1) | ||
return toZodSchemaInternal(node.types[0], nodeDefinitions, zodSchemaDefinitions); | ||
return toZodSchemaInternal( | ||
node.types[0], | ||
nodeDefinitions, | ||
zodSchemaDefinitions | ||
); | ||
const schemas = mapMultipleInnerNode( | ||
@@ -238,8 +279,21 @@ node, | ||
const types = [...node.types]; | ||
let schema = toZodSchemaInternal(types.pop(), nodeDefinitions, zodSchemaDefinitions); | ||
let schema = toZodSchemaInternal( | ||
types.pop(), | ||
nodeDefinitions, | ||
zodSchemaDefinitions | ||
); | ||
for (const type of types) { | ||
schema = schema.and(toZodSchemaInternal(type, nodeDefinitions, zodSchemaDefinitions)); | ||
schema = schema.and( | ||
toZodSchemaInternal(type, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
} | ||
return schema; | ||
} | ||
case "named": { | ||
return toZodSchemaInternal( | ||
node.node, | ||
nodeDefinitions, | ||
zodSchemaDefinitions | ||
); | ||
} | ||
} | ||
@@ -249,3 +303,8 @@ }; | ||
// src/to-string.ts | ||
import { mapMultipleInnerNode as mapMultipleInnerNode2, mapObjectNode as mapObjectNode2, serializePrimitive } from "@onetyped/core"; | ||
import { | ||
mapMultipleInnerNode as mapMultipleInnerNode2, | ||
mapObjectNode as mapObjectNode2, | ||
serializePrimitive, | ||
UnsupportedError | ||
} from "@onetyped/core"; | ||
var createZodMethod = (method, argument_) => `z.${method}(${argument_ ?? ""})`; | ||
@@ -307,6 +366,12 @@ var toZodString = (node) => { | ||
case "record": { | ||
return createZodMethod("record", `${toZodString(node.key)}, ${toZodString(node.value)}`); | ||
return createZodMethod( | ||
"record", | ||
`${toZodString(node.key)}, ${toZodString(node.value)}` | ||
); | ||
} | ||
case "map": { | ||
return createZodMethod("map", `${toZodString(node.key)}, ${toZodString(node.value)}`); | ||
return createZodMethod( | ||
"map", | ||
`${toZodString(node.key)}, ${toZodString(node.value)}` | ||
); | ||
} | ||
@@ -316,4 +381,9 @@ case "function": { | ||
return createZodMethod("function"); | ||
const argumentSchemas = node.arguments.map((argument) => toZodString(argument)); | ||
const tupleArguments = createZodMethod("tuple", `[${argumentSchemas.join(", ")}]`); | ||
const argumentSchemas = node.arguments.map( | ||
(argument) => toZodString(argument) | ||
); | ||
const tupleArguments = createZodMethod( | ||
"tuple", | ||
`[${argumentSchemas.join(", ")}]` | ||
); | ||
if (typeof node.return === "undefined") | ||
@@ -342,2 +412,5 @@ return createZodMethod("function", tupleArguments); | ||
} | ||
case "named": { | ||
throw new UnsupportedError("zod", "named"); | ||
} | ||
} | ||
@@ -344,0 +417,0 @@ }; |
{ | ||
"name": "@onetyped/zod", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"type": "module", | ||
@@ -36,6 +36,16 @@ "description": "onetyped Zod integration", | ||
], | ||
"eslintConfig": { | ||
"extends": [ | ||
"@trpc-labs/eslint-config" | ||
] | ||
}, | ||
"sideEffects": false, | ||
"devDependencies": { | ||
"@onetyped/core": "0.0.6", | ||
"zod": "3.19.1" | ||
"@onetyped/core": "0.0.7", | ||
"@trpc-labs/eslint-config": "0.1.1", | ||
"@trpc-labs/tsconfig": "0.1.1", | ||
"@trpc-labs/tsup-config": "0.1.0", | ||
"tsup": "6.7.0", | ||
"vitest": "0.32.0", | ||
"zod": "3.21.4" | ||
}, | ||
@@ -53,4 +63,8 @@ "peerDependencies": { | ||
"build": "tsup", | ||
"type-check": "tsc" | ||
"dev": "tsup --watch", | ||
"lint": "eslint src --max-warnings 0", | ||
"test": "vitest", | ||
"test:run": "vitest run", | ||
"typecheck": "tsc --noEmit" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27322
816
7
1