Skip to content

Commit

Permalink
version 0.5.1: resolving relative references in jzodElementSchemaToZo…
Browse files Browse the repository at this point in the history
…dSchemaAndDescription
  • Loading branch information
arnaud-bailly committed Sep 9, 2023
1 parent 90402af commit ab319f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
54 changes: 22 additions & 32 deletions src/Jzod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = z.lazy(
() => {
/**
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ab319f0

Please sign in to comment.