Skip to content

Commit

Permalink
feat(42): support also these witch don't have accelerator
Browse files Browse the repository at this point in the history
  • Loading branch information
mathcovax committed Sep 13, 2024
1 parent febf1f1 commit 435793f
Show file tree
Hide file tree
Showing 8 changed files with 607 additions and 5 deletions.
6 changes: 5 additions & 1 deletion scripts/accelerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z as zod } from "zod";
import { ZodAcceleratorContent } from "./content";
import { ZodAcceleratorParser } from "./parser";
import { ZodAcceleratorError } from "./error";
import { zodSchemaIsAsync } from "./utils/zodSchemaIsAsync";

declare module "zod" {
interface ZodType {
Expand Down Expand Up @@ -30,9 +31,12 @@ export abstract class ZodAccelerator {
zac.addContext({
zodSchema,
});
const isAsync = zodSchemaIsAsync(zodSchema);
const parseMethod = isAsync ? "safeParseAsync" : "safeParse";
const mayBeAwait = isAsync ? "await" : "";

zac.addContent(`
let $output = $this.zodSchema.accelerator.safeParse($input);
let $output = ${mayBeAwait} $this.zodSchema.accelerator.${parseMethod}($input);
if($output.success === false){
$output.error.message = $output.error.message.replace(".", \`$path.\`);
Expand Down
8 changes: 5 additions & 3 deletions scripts/accelerators/effects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as zod from "zod";
import { ZodAccelerator } from "../accelerator";
import type { ZodAcceleratorContent } from "../content";
import { zodSchemaIsAsync } from "..";

@ZodAccelerator.autoInstance
export class ZodEffectAccelerator extends ZodAccelerator {
Expand All @@ -10,6 +11,7 @@ export class ZodEffectAccelerator extends ZodAccelerator {

public makeAcceleratorContent(zodSchema: zod.ZodEffects<zod.ZodAny>, zac: ZodAcceleratorContent) {
const def = zodSchema._def;
const async = zodSchemaIsAsync(zodSchema);

zac.addContext({
transform: def.effect.type === "transform" ? def.effect.transform : undefined,
Expand All @@ -19,7 +21,7 @@ export class ZodEffectAccelerator extends ZodAccelerator {
});

zac.addContent(
def.effect.type !== "preprocess" || ZodEffectAccelerator.contentPart.preprocess(def.effect.transform.constructor.name === "AsyncFunction"),
def.effect.type !== "preprocess" || ZodEffectAccelerator.contentPart.preprocess(async),
[
ZodAccelerator.findAcceleratorContent(def.schema),
{
Expand All @@ -28,8 +30,8 @@ export class ZodEffectAccelerator extends ZodAccelerator {
output: "$input",
},
],
def.effect.type !== "transform" || ZodEffectAccelerator.contentPart.transform(def.effect.transform.constructor.name === "AsyncFunction"),
def.effect.type !== "refinement" || ZodEffectAccelerator.contentPart.refinement(def.effect.refinement.constructor.name === "AsyncFunction"),
def.effect.type !== "transform" || ZodEffectAccelerator.contentPart.transform(async),
def.effect.type !== "refinement" || ZodEffectAccelerator.contentPart.refinement(async),
);

return zac;
Expand Down
27 changes: 27 additions & 0 deletions scripts/accelerators/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import * as zod from "zod";
import { ZodAccelerator } from "..";
import { ZodAcceleratorError } from "@scripts/error";

describe("type", () => {
it("create parser for missing Map", () => {
const schema = zod.map(zod.string(), zod.string());
const accelerateSchema = ZodAccelerator.build(schema);
const data = new Map();
data.set("toto", "tata");

expect(accelerateSchema.parse(data)).toStrictEqual(schema.parse(data));

data.set("tata", 2);

try {
accelerateSchema.parse(data);
throw new Error();
} catch (error: any) {
const err: ZodAcceleratorError = error;
expect(err).instanceOf(ZodAcceleratorError);
expect(schema.safeParse(data).success).toBe(false);
expect(err.message).toBe(". : ZodSchema Fail parse.");
}
});
});
38 changes: 38 additions & 0 deletions scripts/accelerators/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { ZodAcceleratorContent } from "@scripts/content";
import type { ZodType } from "zod";
import { ZodAccelerator } from "../accelerator";
import { zodSchemaIsAsync } from "..";

@ZodAccelerator.autoInstance
export class ZodTypeAccelerator extends ZodAccelerator {
public get support(): any {
return ZodAccelerator.zod.ZodType;
}

public makeAcceleratorContent(zodSchema: ZodType, zac: ZodAcceleratorContent) {
const isAsync = zodSchemaIsAsync(zodSchema);
const parseMethod = isAsync ? "safeParseAsync" : "safeParse";
const mayBeAwait = isAsync ? "await" : "";

zac.addContent({
content: `
const $output = ${mayBeAwait} $this.zodSchema.${parseMethod}($input);
if($output.success === false){
$output.error.message = $output.error.message.replace(".", \`$path.\`);
return {
success: false,
error: new ZodAcceleratorError(\`$path\`, "ZodSchema Fail parse.")
};
}
$input = $output.data;
`,
ctx: {
zodSchema,
},
});

return zac;
}
}
6 changes: 6 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ import "./accelerators/bigInt";
import "./accelerators/effects";
import "./accelerators/lazy";

// must be import at last
import "./accelerators/type";

export * from "./accelerator";
export * from "./error";
export * from "./parser";

export * from "./utils/zodSchemaIsAsync";
export * from "./utils/types";

export { ZodAccelerator as default } from "./accelerator";
Loading

0 comments on commit 435793f

Please sign in to comment.