From ab319f0a253d3216ec67eb2a2f23da3ec1444ee3 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Sat, 9 Sep 2023 13:09:20 +0200 Subject: [PATCH] version 0.5.1: resolving relative references in jzodElementSchemaToZodSchemaAndDescription --- package.json | 4 ++-- src/Jzod.ts | 54 +++++++++++++++++++++------------------------------- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 0fc90b1..d1f2aa3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@miroir-framework/jzod", "private": false, - "version": "0.5.0", + "version": "0.5.1", "description": "The Json bootstrapper for Zod.", "main": "dist/bundle.js", "types": "dist/bundle.d.ts", @@ -21,7 +21,7 @@ "@babel/plugin-transform-modules-commonjs": "^7.20.11", "@babel/preset-env": "^7.22.5", "@jest/globals": "^29.3.1", - "@miroir-framework/jzod-ts": "0.5.0", + "@miroir-framework/jzod-ts": "0.5.1", "@rollup/plugin-commonjs": "^24.0.1", "@rollup/plugin-json": "^6.0.0", "@types/jest": "^29.2.4", diff --git a/src/Jzod.ts b/src/Jzod.ts index ebcbf69..8b3d8c2 100644 --- a/src/Jzod.ts +++ b/src/Jzod.ts @@ -396,33 +396,6 @@ export function jzodElementSchemaToZodSchemaAndDescription( } } - const resolveLazyReference = ( - // targetObject: any, - lazyReference: string, - ) => { - // const eagerReferences = {...getSchemaEagerReferences(), ...contextSubObjectSchemaAndDescriptionRecord}; - const lazyReferences = getLazyReferences(); - // console.log("parsing schemaReference relativeReferences",Object.keys(relativeReferences)); - // let targetObject: ZodSchemaAndDescriptionRecord; - // targetObject = Object.fromEntries( - // Object.entries( - // (lazyReferences[lazyReference].zodSchema as AnyZodObject).shape as any - // ).map((e: [string, any]) => [e[0], { zodSchema: e[1], zodText: "" }]) as [string, any][] - // ); - if (lazyReferences[lazyReference]) { - return lazyReferences[lazyReference] - } else { - throw new Error( - "when converting Jzod to Zod, could not find lazy reference " + - lazyReference + - " in passed references " + - Object.keys(lazyReferences) + - "from element " + JSON.stringify(element) - ); - } - // return targetObject; - } - const lazyResolverZodSchema: ZodLazy = z.lazy( () => { /** @@ -434,11 +407,28 @@ export function jzodElementSchemaToZodSchemaAndDescription( // return z.any(); } else { - // const targetObject = resolveLazyReference(element.definition.absolutePath); + const lazyReferences = getLazyReferences(); + + if (element.definition.absolutePath && lazyReferences[element.definition.absolutePath]) { + const absoluteRef = element.definition.absolutePath ? lazyReferences[element.definition.absolutePath].zodSchema : z.any(); + const relativeRef = element.definition.relativePath ? resolveEagerReference(element.definition.relativePath).zodSchema: absoluteRef + return relativeRef; + } else { + if (element.definition.relativePath) { + const relativeRef = resolveEagerReference(element.definition.relativePath).zodSchema + return relativeRef; + } else { + throw new Error( + "when converting Jzod to Zod, could not find lazy reference " + + element.definition.absolutePath + + " in passed references " + + Object.keys(lazyReferences) + + "from element " + JSON.stringify(element) + ); + } + } + // console.log("converting schemaReference relative path targetObject", targetObject, element.definition.relativePath,Object.keys(targetObject),); - const absoluteRef = element.definition.absolutePath ? resolveLazyReference(element.definition.absolutePath).zodSchema : z.any(); - const relativeRef = element.definition.relativePath ? resolveEagerReference(element.definition.relativePath).zodSchema: absoluteRef - return relativeRef; } } );// resolveReference @@ -602,7 +592,7 @@ export function jzodElementSchemaToZodSchemaAndDescription( } default: throw new Error( - "jzodElementSchemaToZodSchemaAndDescription could not convert given json Zod schema, unknown type:" + + "jzodElementSchemaToZodSchemaAndDescription could not convert given Jzod schema, unknown type:" + element["type"] + ", element: " + JSON.stringify(element)