-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(42): support also these witch don't have accelerator
- Loading branch information
Showing
8 changed files
with
607 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.