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

v3.1.0 - Fixes broken type inference #6

Merged
merged 5 commits into from
Apr 21, 2023
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# CHANGELOG

(all dates use the ISO-8601 format, which is YYYY/MM/DD)

## 3.1.0 (2023/4/21)

* Type inference now works (mostly) as expected! While the runtime behavior of the package worked as expected during work on 3.0.0 and was what I had been testing, I somehow was completely unaware that type inference was completely broken. It's a bit embarrassing for me, to be honest. It took more energy than I had anticipated to make it work, but type inferences now behave when using `typeof .call` and `typeof .context` almost exactly as they do in Zod with `z.input<typeof validator>` and `z.infer<typeof validator>`/`z.output<typeof validator>`. This closes [Issue #5](https://github.com/TheAppleFreak/moleculer-zod-validator/issues/5).

I do say almost, as there is an important note that is worth mentioning. [There's a known upstream bug in Zod where using `.catchall()` results in bugged TypeScript type inference](https://github.com/colinhacks/zod/issues/1949). While I could have left that behavior in ZodParams for parity with Zod, the sudden introduction of that behavior could break builds (it already broke one of our tests, through no fault of my own), so for the time being I've disabled catchall type inference in ZodParams. When the bug is fixed upstream, I'll reenable that behavior here.

Beyond all of this, there should be no other changes and everything should Just Work™ as expected.
* Updated dev dependencies

## 3.0.1 (2023/4/13)

* No changes were made; NPM just didn't get the memo that there was a README.

## 3.0.0 (2023/4/13)

* **BREAKING CHANGE** - The minimum compatible Node.js version is now Node.js v17.0.0. This is due to the requirement of the `structuredClone` function in the fastest-validator fallback (described below), which in Node.js is only available in v17.0.0 and above. I tried using Lodash's `cloneDeep` method like Moleculer itself uses, but for a reason unknown to me it kept causing crashes during unit testing that I couldn't figure out. `structuredClone` does work, however, which should solve the same problem.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ The `ZodParams` constructor takes one or two arguments, `schema` and optionally
* `strict` (boolean) - Throws an error if unrecognized keys are present. Mutually exclusive with `passthrough` and `strip`. ([docs](https://github.com/colinhacks/zod#strict))
* `catchall` (Zod validator) - Validates all unknown keys against this schema. Obviates `strict`, `passthrough`, and `strip`. ([docs](https://github.com/colinhacks/zod#catchall))

**NOTE**: [There is currently an upstream bug in Zod that prevents `catchall` type inference from working correctly.](https://github.com/colinhacks/zod/issues/1949) Type inference for catchall in ZodParams is disabled for the time being until that is fixed. If you wish to emulate the type inference, you can do so by using a type union when using `broker.call` or `ctx.call`.

```ts
broker.call<
ReturnType,
typeof zodParamObject.call & {[index: string]: string}
>({ ... })
```

Additionally, support for object transformations is present, allowing for the use of features such as [preprocessing](https://github.com/colinhacks/zod#preprocess), [refinements](https://github.com/colinhacks/zod#refine), [transforms](https://github.com/colinhacks/zod#transform), and [defaults](https://github.com/colinhacks/zod#default).

Once constructed, there are four properties exposed on the `ZodParams` object.
Expand Down
148 changes: 112 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moleculer-zod-validator",
"version": "3.0.1",
"version": "3.1.0",
"description": "A validator for the Moleculer microservice framework to allow the use of Zod.",
"author": "TheAppleFreak <TheAppleFreak@gmail.com>",
"main": "build/index.js",
Expand Down Expand Up @@ -38,8 +38,8 @@
},
"homepage": "https://github.com/TheAppleFreak/moleculer-zod-validator#readme",
"devDependencies": {
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@types/jest": "^29.5.1",
"@types/node": "^18.15.13",
"fastest-validator": "^1.16.0",
"husky": "^8.0.3",
"jest": "^29.5.0",
Expand Down
Loading