Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish: 2.5.0 #56

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# @duplojs/zod-accelerator
[![NPM version](https://img.shields.io/npm/v/@duplojs/zod-accelerator)](https://www.npmjs.com/package/@duplojs/zod-accelerator)

## Instalation
## Installation
```
npm i @duplojs/zod-accelerator
```

## Benchmarck
## Benchmark
![Benchmarck result](/benchmarck-result.png)

## Utilisation
## How to use it
```ts
import * as zod from "zod";
import {ZodAccelerator} from "@duplojs/zod-accelerator";
Expand Down Expand Up @@ -48,3 +48,42 @@ const inputData = Array.from({length: 10}).fill({

const outputData = zodAccelerateSchema.parse(inputData);
```

## When should I use it
ZodAccelerator is useful when the schema is used multiple times during the same session. The time to build the custom function is about twice as long as its execution. If you only need to call your schema once, it might not be the best solution.

## How does it work
The `ZodAccelerator.build` function allows you to create a custom function based on the schema passed as an argument. The function will be used to create an instance of the `ZodAcceleratorParser` object. This makes its usage similar to Zod's.

```ts
zodSchema.parse(...)
zodAccelerateSchema.parse(...)

zodSchema.parseAsync(...)
zodAccelerateSchema.parseAsync(...)

zodSchema.safeParse(...)
zodAccelerateSchema.safeParse(...)

zodSchema.safeParseAsync(...)
zodAccelerateSchema.safeParseAsync(...)
```

### Support for Zod types
To build the custom function, we have created a converter for each type. Not all types are supported. However, to avoid compatibility issues, when a type is not supported, ZodAccelerator directly uses the Zod schema. To find out which types are supported, go to the [`scripts/accelerators`](./scripts/accelerators/) folder.

### The differences
The functions created by ZodAccelerator have some key differences compared to Zod's basic functionality :
- In case of an issue, the schema analysis does not continue, it stops immediately.
- Errors are not as explicit.
- All secondary parameters, such as custom messages, may not be implemented everywhere (can be done on request).
- The Zod contexts of `zodEffect` are not as complete.

### How custom functions are created
For each type that Zod provides, we have an algorithm that translates the information from the Zod schema into a string of code. At the end, the different relevant parts are assembled and interpreted using the `eval` function (no injection possible).

### Caching system?
Would it be possible to build the schemas in a file and then call them instead of rebuilding them? Unfortunately, no. The construction is mandatory because, in addition to the content, we also build its context (`this`). In the case of a `ZodEffect` type, for example, the associated function is stored in the context. Therefore, skipping the construction phase is not possible.

### Skipping `eval` ?
As mentioned earlier, ZodAccelerator uses the eval function to interpret the code after a Node program is launched. Currently, no other method seems viable.
Binary file modified benchmarck-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions scripts/accelerators/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as zod from "zod";
import type * as zod from "zod";
import { ZodAccelerator } from "../accelerator";
import type { ZodAcceleratorContent } from "../content";

Expand Down Expand Up @@ -30,14 +30,14 @@ export class ZodObjectAccelerator extends ZodAccelerator {
Object.entries(shape).forEach(([key, zodSchema]) => {
const propsZac = ZodAccelerator.findAcceleratorContent(zodSchema as zod.ZodType);
if (
zodSchema instanceof zod.ZodUndefined
|| zodSchema instanceof zod.ZodVoid
|| zodSchema instanceof zod.ZodUnknown
|| zodSchema instanceof zod.ZodAny
|| zodSchema instanceof zod.ZodOptional
|| zodSchema instanceof zod.ZodDefault
zodSchema instanceof ZodAccelerator.zod.ZodUndefined
|| zodSchema instanceof ZodAccelerator.zod.ZodVoid
|| zodSchema instanceof ZodAccelerator.zod.ZodUnknown
|| zodSchema instanceof ZodAccelerator.zod.ZodAny
|| zodSchema instanceof ZodAccelerator.zod.ZodOptional
|| zodSchema instanceof ZodAccelerator.zod.ZodDefault
|| (
zodSchema instanceof zod.ZodLiteral
zodSchema instanceof ZodAccelerator.zod.ZodLiteral
&& zodSchema._def.value === undefined
)
) {
Expand Down
31 changes: 0 additions & 31 deletions scripts/accelerators/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,4 @@ describe("record type", () => {
]);
}
});

it("input record number string", () => {
const schema = zod.record(zod.number(), zod.string());
const accelerateSchema = ZodAccelerator.build(schema);
let data: any = {
1: "test",
2: "test",
};

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

data = {
test1: 1,
};

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.issues).toStrictEqual([
{
code: "custom",
message: ".[Record Key \"test1\"] : Input is not a Number.",
path: ["[Record Key \"test1\"]"],
},
]);
}
});
});
17 changes: 10 additions & 7 deletions scripts/accelerators/record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as zod from "zod";
import type * as zod from "zod";
import { ZodAccelerator } from "../accelerator";
import { ZodObjectAccelerator } from "./object";
import type { ZodAcceleratorContent } from "../content";
Expand All @@ -9,17 +9,20 @@
return ZodAccelerator.zod.ZodRecord;
}

public makeAcceleratorContent(zodSchema: zod.ZodRecord, zac: ZodAcceleratorContent) {
public makeAcceleratorContent(zodSchema: zod.ZodRecord<zod.KeySchema>, zac: ZodAcceleratorContent) {
const def = zodSchema._def;

const zacValueType = ZodAccelerator.findAcceleratorContent(def.valueType as zod.ZodType);

const keyType = def.keyType;
if (keyType instanceof zod.ZodNumber || keyType instanceof zod.ZodString) {
//@ts-expect-error infinity error de merde
keyType._def.coerce = true;
let keyType = def.keyType;
if (keyType instanceof ZodAccelerator.zod.ZodString) {
keyType = new ZodAccelerator.zod.ZodString({
...keyType._def,
coerce: true,
});
}
const zacKeyType = ZodAccelerator.findAcceleratorContent(def.keyType);

const zacKeyType = ZodAccelerator.findAcceleratorContent(keyType as zod.ZodType);

zac.addContent(
"let $output = {};",
Expand All @@ -28,7 +31,7 @@
[
zacKeyType,
{
path: "[Record Key \"${$id_key}\"]",

Check warning on line 34 in scripts/accelerators/record.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected template string expression
input: "$id_key",
output: "$id_key",
},
Expand Down
5 changes: 2 additions & 3 deletions scripts/utils/shadowEval.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
const { eval: badEval } = global;

export const shadowEval = badEval;
/* eslint-disable no-eval */
export const shadowEval = eval;
Loading