Skip to content

Commit

Permalink
add carry-on types, heteronomous unions, and manyfold object union ty…
Browse files Browse the repository at this point in the history
…pes #8. Testing heteronomous unions and adding manyfold object union types (discriminators are ignored upon conversion to Zod schemas).
  • Loading branch information
miroir-framework committed Jun 18, 2024
1 parent b200e78 commit 76cfdd2
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 198 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@miroir-framework/jzod",
"private": false,
"version": "0.7.0",
"version": "0.8.0",
"description": "The Json bootstrapper for Zod.",
"main": "dist/bundle.js",
"types": "dist/bundle.d.ts",
Expand Down
40 changes: 37 additions & 3 deletions src/JzodInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZodTypeAny } from "zod";
import { ZodTypeAny, union } from "zod";


import { JzodReference } from "@miroir-framework/jzod-ts";
Expand All @@ -18,6 +18,7 @@ export type ZodSchemaAndDescriptionRecord = { [k: string]: ZodSchemaAndDescripti
// ##############################################################################################################
// ##############################################################################################################
// ##############################################################################################################
// export const jzodBootstrapElementSchema: any = {
export const jzodBootstrapElementSchema: JzodReference = {
type: "schemaReference",
context: {
Expand Down Expand Up @@ -194,7 +195,10 @@ export const jzodBootstrapElementSchema: JzodReference = {
},
jzodElement: {
type: "union",
discriminator: "type",
discriminator: {
discriminatorType: "string",
value: "type"
},
definition: [
{ type: "schemaReference", definition: { relativePath: "jzodArray" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttribute" } },
Expand Down Expand Up @@ -419,7 +423,37 @@ export const jzodBootstrapElementSchema: JzodReference = {
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "union" },
discriminator: { type: "string", optional: true },
discriminator: {
type: "union",
optional: true,
definition: [
{
type: "object",
definition: {
discriminatorType: {
type: "literal",
definition: "string"
},
value: { type: "string" }
}
},
{
type: "object",
definition: {
discriminatorType: {
type: "literal",
definition: "array"
},
value: {
type: "array",
definition: {
type: "string"
}
}
}
},
]
},
definition: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodElement" } },
Expand Down
4 changes: 2 additions & 2 deletions src/JzodToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ export function jzodElementSchemaToZodSchemaAndDescription(
}
case "object": {
const castElement = element as JzodObject;
const extendsSubObject: ZodSchemaAndDescription | undefined = castElement.extend
const extendsSubObject: ZodSchemaAndDescription | undefined = castElement?.extend
? jzodElementSchemaToZodSchemaAndDescription(
castElement.extend,
castElement.extend,
getSchemaEagerReferences,
getLazyReferences,
typeScriptLazyReferenceConverter
Expand Down
17 changes: 11 additions & 6 deletions src/ZodToJzod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,22 @@ export const zodToJzod = (zod: ZodTypeAny, identifier: string): JzodElement => {
return { type: "union", definition: jzodUnionElements };
}
case "ZodDiscriminatedUnion": {
console.warn(
"zodToJzod: Zod discriminated unions are converted to unions in Jzod, which are converted back as plain unions in Zod, not discriminated unions as the original Zod Schema.",
JSON.stringify(zod)
);
// console.warn(
// "zodToJzod: Zod discriminated unions are converted to unions in Jzod, which are converted back as plain unions in Zod, not discriminated unions as the original Zod Schema.",
// JSON.stringify(zod)
// );

const jzodUnionElements: JzodElement[] = [...zod._def.options.values()].map((option: ZodTypeAny) =>
zodToJzod(option, identifier)
);
return zod._def.discriminator
? { type: "union", discriminator: zod._def.discriminator, definition: jzodUnionElements }
: { type: "union", definition: jzodUnionElements };
? {
type: "union",
discriminator: { discriminatorType: "string", value: zod._def.discriminator },
definition: jzodUnionElements,
}
: { type: "union", definition: jzodUnionElements }
;
}
case "ZodEffects": {
console.warn("zodToJzod: Zod effects are ignored.", JSON.stringify(zod));
Expand Down
Loading

0 comments on commit 76cfdd2

Please sign in to comment.