@onetyped/zod
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,2 +0,2 @@ | ||
import { CustomTypeNode, AnyNode, Infer } from '@onetyped/core'; | ||
import { CustomTypeNode, AnyNode, DefinitionMap, Infer } from '@onetyped/core'; | ||
import { z } from 'zod'; | ||
@@ -7,3 +7,3 @@ | ||
type ToZodSchema<TNode extends AnyNode> = z.ZodType<Infer<TNode>>; | ||
declare const toZodSchema: <TNode extends AnyNode>(node: TNode) => ToZodSchema<TNode>; | ||
declare const toZodSchema: <TNode extends AnyNode>(node: TNode, definitions?: DefinitionMap) => ToZodSchema<TNode>; | ||
@@ -10,0 +10,0 @@ declare const toZodString: (node: AnyNode) => string; |
@@ -113,4 +113,25 @@ // src/from-schema.ts | ||
import { z as z2 } from "zod"; | ||
var toZodSchema = (node) => { | ||
var toZodSchema = (node, definitions = /* @__PURE__ */ new Map()) => { | ||
const zodSchemaDefinitions = /* @__PURE__ */ new Map(); | ||
const schema = toZodSchemaInternal(node, definitions, zodSchemaDefinitions); | ||
return schema; | ||
}; | ||
var toZodSchemaInternal = (node, nodeDefinitions, zodSchemaDefinitions) => { | ||
switch (node.typeName) { | ||
case "definitionReference": { | ||
const schemaBag = zodSchemaDefinitions.get(node.identifier); | ||
if (schemaBag) { | ||
return z2.lazy(() => { | ||
return schemaBag.schema; | ||
}); | ||
} | ||
const nodeDefinition = nodeDefinitions.get(node.identifier); | ||
const resolvedSchemaBag = { schema: z2.unknown() }; | ||
zodSchemaDefinitions.set( | ||
node.identifier, | ||
resolvedSchemaBag | ||
); | ||
resolvedSchemaBag.schema = toZodSchemaInternal(nodeDefinition, nodeDefinitions, zodSchemaDefinitions); | ||
return z2.lazy(() => resolvedSchemaBag.schema); | ||
} | ||
case "string": { | ||
@@ -153,23 +174,31 @@ return z2.string(); | ||
case "object": { | ||
const propertySchemas = Object.fromEntries(mapObjectNode(node, toZodSchema)); | ||
const propertySchemas = Object.fromEntries( | ||
mapObjectNode(node, (node2) => toZodSchemaInternal(node2, nodeDefinitions, zodSchemaDefinitions)) | ||
); | ||
return z2.object(propertySchemas); | ||
} | ||
case "array": { | ||
return z2.array(toZodSchema(node.type)); | ||
return z2.array(toZodSchemaInternal(node.type, nodeDefinitions, zodSchemaDefinitions)); | ||
} | ||
case "set": { | ||
return z2.set(toZodSchema(node.type)); | ||
return z2.set(toZodSchemaInternal(node.type, nodeDefinitions, zodSchemaDefinitions)); | ||
} | ||
case "record": { | ||
return z2.record(toZodSchema(node.key), toZodSchema(node.value)); | ||
return z2.record( | ||
toZodSchemaInternal(node.key, nodeDefinitions, zodSchemaDefinitions), | ||
toZodSchemaInternal(node.value, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
} | ||
case "map": { | ||
return z2.map(toZodSchema(node.key), toZodSchema(node.value)); | ||
return z2.map( | ||
toZodSchemaInternal(node.key, nodeDefinitions, zodSchemaDefinitions), | ||
toZodSchemaInternal(node.value, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
} | ||
case "function": { | ||
const argumentSchemas = z2.tuple( | ||
node.arguments.map((argument) => toZodSchema(argument)) | ||
node.arguments.map((argument) => toZodSchemaInternal(argument, nodeDefinitions, zodSchemaDefinitions)) | ||
); | ||
if (node.return) { | ||
return z2.function(argumentSchemas, toZodSchema(node.return)); | ||
return z2.function(argumentSchemas, toZodSchemaInternal(node.return, nodeDefinitions, zodSchemaDefinitions)); | ||
} | ||
@@ -180,8 +209,14 @@ return z2.function(argumentSchemas); | ||
if (node.types.length === 1) | ||
return toZodSchema(node.types[0]); | ||
const schemas = mapMultipleInnerNode(node, toZodSchema); | ||
return toZodSchemaInternal(node.types[0], nodeDefinitions, zodSchemaDefinitions); | ||
const schemas = mapMultipleInnerNode( | ||
node, | ||
(node2) => toZodSchemaInternal(node2, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
return z2.union(schemas); | ||
} | ||
case "tuple": { | ||
const schemas = mapMultipleInnerNode(node, toZodSchema); | ||
const schemas = mapMultipleInnerNode( | ||
node, | ||
(node2) => toZodSchemaInternal(node2, nodeDefinitions, zodSchemaDefinitions) | ||
); | ||
return z2.tuple(schemas); | ||
@@ -191,5 +226,5 @@ } | ||
const types = [...node.types]; | ||
let schema = toZodSchema(types.pop()); | ||
let schema = toZodSchemaInternal(types.pop(), nodeDefinitions, zodSchemaDefinitions); | ||
for (const type of types) { | ||
schema = schema.and(toZodSchema(type)); | ||
schema = schema.and(toZodSchemaInternal(type, nodeDefinitions, zodSchemaDefinitions)); | ||
} | ||
@@ -206,2 +241,5 @@ return schema; | ||
switch (node.typeName) { | ||
case "definitionReference": { | ||
return createZodMethod("lazy", `() => ${node.identifier}`); | ||
} | ||
case "string": { | ||
@@ -208,0 +246,0 @@ return createZodMethod("string"); |
{ | ||
"name": "@onetyped/zod", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"type": "module", | ||
@@ -38,3 +38,3 @@ "description": "onetyped Zod integration", | ||
"devDependencies": { | ||
"@onetyped/core": "0.0.3", | ||
"@onetyped/core": "0.0.4", | ||
"zod": "3.19.1" | ||
@@ -41,0 +41,0 @@ }, |
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
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
25064
659