moleculer-zod-validator
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -9,10 +9,8 @@ import { z } from "zod"; | ||
$$$options: { | ||
passthrough?: boolean | undefined; | ||
strict?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
catchall?: any; | ||
deepPartial?: boolean | undefined; | ||
partial?: boolean | undefined; | ||
compiledValidatorOnly?: { | ||
passthrough?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
} | undefined; | ||
}; | ||
@@ -29,32 +27,20 @@ }; | ||
catchall: z.ZodOptional<z.ZodAny>; | ||
compiledValidatorOnly: z.ZodOptional<z.ZodObject<{ | ||
passthrough: z.ZodOptional<z.ZodBoolean>; | ||
strip: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
passthrough?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
}, { | ||
passthrough?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
}>>; | ||
passthrough: z.ZodOptional<z.ZodBoolean>; | ||
strip: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
passthrough?: boolean | undefined; | ||
strict?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
catchall?: any; | ||
deepPartial?: boolean | undefined; | ||
partial?: boolean | undefined; | ||
compiledValidatorOnly?: { | ||
passthrough?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
} | undefined; | ||
}, { | ||
passthrough?: boolean | undefined; | ||
strict?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
catchall?: any; | ||
deepPartial?: boolean | undefined; | ||
partial?: boolean | undefined; | ||
compiledValidatorOnly?: { | ||
passthrough?: boolean | undefined; | ||
strip?: boolean | undefined; | ||
} | undefined; | ||
}>; | ||
export declare type ZodParamsOptionsType = z.infer<typeof ZodParamsOptions>; | ||
export {}; |
@@ -13,13 +13,11 @@ "use strict"; | ||
catchall: undefined, | ||
compiledValidatorOnly: { | ||
strip: false, | ||
passthrough: false, | ||
}, | ||
strip: false, | ||
passthrough: false, | ||
}, options); | ||
const opts = ZodParamsOptions.parse(options); | ||
let validator; | ||
if (opts.compiledValidatorOnly.strip) { | ||
if (opts.strip) { | ||
validator = zod_1.z.object(this._rawSchema).strip(); | ||
} | ||
else if (opts.compiledValidatorOnly.passthrough) { | ||
else if (opts.passthrough) { | ||
validator = zod_1.z.object(this._rawSchema).passthrough(); | ||
@@ -67,7 +65,5 @@ } | ||
catchall: zod_1.z.any(), | ||
compiledValidatorOnly: zod_1.z.object({ | ||
passthrough: zod_1.z.boolean(), | ||
strip: zod_1.z.boolean(), | ||
}), | ||
passthrough: zod_1.z.boolean(), | ||
strip: zod_1.z.boolean(), | ||
}) | ||
.deepPartial(); |
import { z } from "zod"; | ||
import { Validators } from "moleculer"; | ||
import type { ZodParamsOptionsType } from "."; | ||
import type { ZodParamsOptionsType } from "./params"; | ||
export declare class ZodValidator extends Validators.Base { | ||
@@ -5,0 +5,0 @@ constructor(); |
@@ -6,2 +6,3 @@ "use strict"; | ||
const moleculer_1 = require("moleculer"); | ||
const helpers_1 = require("./helpers"); | ||
class ZodValidator extends moleculer_1.Validators.Base { | ||
@@ -18,19 +19,13 @@ constructor() { | ||
let compiled; | ||
// Uncomment when I figure out how to apply transformations from the validator | ||
/* | ||
if (opts.strip) { | ||
compiled = z.object(schema).strip(); | ||
} else if (opts.strict) { | ||
compiled = z.object(schema).strict(); | ||
} else if (opts.passthrough) { | ||
compiled = z.object(schema).passthrough(); | ||
} else { | ||
compiled = z.object(schema); | ||
compiled = zod_1.z.object(schema).strip(); | ||
} | ||
*/ | ||
if (opts.strict) { | ||
else if (opts.strict) { | ||
compiled = zod_1.z.object(schema).strict(); | ||
} | ||
else if (opts.passthrough) { | ||
compiled = zod_1.z.object(schema).passthrough(); | ||
} | ||
else { | ||
compiled = zod_1.z.object(schema); | ||
compiled = zod_1.z.object(schema).strip(); | ||
} | ||
@@ -46,3 +41,8 @@ if (opts.partial) { | ||
} | ||
compiled.parse(params); | ||
const results = compiled.parse(params); | ||
// params is passed by reference, meaning that we can apply transformations | ||
// to what gets passed to the validator from here. However, simply setting | ||
// it to a new value will just change the pointer to the object in function | ||
// scope, so we need to actually mutate the original ourselves. | ||
(0, helpers_1.mutateObject)(params, results, false); | ||
return true; | ||
@@ -49,0 +49,0 @@ } |
# CHANGELOG | ||
# 1.0.2 (2022/1/17) | ||
## 2.0.0 (2022/1/24) | ||
* Fixed package.json to actually include the build this time, for real | ||
* Added transformation support to the validator itself, which will allow for features such as defaults and different passthrough modes for unrecognized parameters. This has several side effects that are listed below. | ||
* **BREAKING CHANGE** - Removed the `compiledValidatorOnly` nested options object. The two settings, `strip` and `passthrough`, are now present in the top level options object. | ||
* **BREAKING CHANGE** - The default validation mode is now `strip` as opposed to `passthrough`. | ||
# 1.0.1 (2022/1/17) | ||
## 1.0.2 (2022/1/17) | ||
* Fixed package.json to actually include the build this time | ||
* Fixed package.json and continuous integration scripts to actually include the build this time, for real | ||
# 1.0.0 (2022/1/17) | ||
## 1.0.1 (2022/1/17) | ||
* Fixed package.json and continuous integration scripts to actually include the build this time | ||
## 1.0.0 (2022/1/17) | ||
* Initial release |
{ | ||
"name": "moleculer-zod-validator", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "A validator for the Moleculer microservice framework to allow the use of Zod.", | ||
@@ -27,3 +27,3 @@ "author": "TheAppleFreak <TheAppleFreak@gmail.com>", | ||
"files": [ | ||
"build/", | ||
"build/**/*", | ||
"CHANGELOG.md" | ||
@@ -30,0 +30,0 @@ ], |
@@ -41,15 +41,14 @@ # moleculer-zod-validator | ||
The `ZodParams` constructor takes one to two arguments, `schema` and optionally `options`. | ||
The `ZodParams` constructor takes one or two arguments, `schema` and optionally `options`. | ||
* `schema` - This is a schema object that gets passed directly into `z.object`. [For all available schema options, please look at the Zod documentation.](https://github.com/colinhacks/zod#defining-schemas) Please note that transformation options (such as `default`) are currently unavailable in the main validator; please read the below note for more information. | ||
* `options` - This provides access to some of the different functions available on a standard Zod object. All booleans default to `false`. | ||
* `schema` - This is a schema object that gets passed directly into `z.object`. [For all available schema options, please look at the Zod documentation.](https://github.com/colinhacks/zod#defining-schemas) | ||
* `options` - This provides access to some of the different functions available on a standard Zod object. All booleans default to `false` except for `strip`, which is implicitly set to `true`. | ||
* `partial` (boolean) - Shallowly makes all properties optional. ([docs](https://github.com/colinhacks/zod#partial)) | ||
* `deepPartial` (boolean) - Deeply makes all properties optional. ([docs](https://github.com/colinhacks/zod#deepPartial)) | ||
* `strict` (boolean) - Throws an error if unrecognized keys are present. ([docs](https://github.com/colinhacks/zod#strict)) | ||
* `catchall` (Zod validator) - Validates all unknown keys against this schema. Obviates `strict`. ([docs](https://github.com/colinhacks/zod#catchall)) | ||
* `compiledValidatorOnly` - This provides access to settings that transform the inputs that is currently not supported natively in the validator itself. Please read the note below for more information on this. | ||
* `passthrough` (boolean) - Passes through unrecognized keys. Equivalent to the current default behavior in this validator. | ||
* `strip` (boolean) - Removes unrecognized keys from the parsed input. Equivalent to Zod's default behavior. | ||
* `strip` (boolean) - Removes unrecognized keys from the parsed input. This is Zod's default behavior and this validator's default behavior. Mutually exclusive with `passthrough` and `strict`, and will override them if set. ([docs](https://github.com/colinhacks/zod#strip)) | ||
* `strict` (boolean) - Throws an error if unrecognized keys are present. Mutually exclusive with `passthrough` and `strip`. ([docs](https://github.com/colinhacks/zod#strict)) | ||
* `passthrough` (boolean) - Passes through unrecognized keys. Mutually exclusive with `strict` and `strip`. ([docs](https://github.com/colinhacks/zod#passthrough)) | ||
* `catchall` (Zod validator) - Validates all unknown keys against this schema. Obviates `strict`, `passthrough`, and `strip`. ([docs](https://github.com/colinhacks/zod#catchall)) | ||
**Important note**: As of this writing (v1.0.0), there is currently no support for transformation options (`strip`, `passthrough`, and `default` primarily), meaning that this validator only checks to see whether the shape of the incoming data is valid and then sends it to the action as received. I'm not sure how the default implementation of fastest-validator performs its transformations; this section will be updated if and when that support can be added. For transformation options in the interim, you can access the compiled validator within the action and run `ctx.params` through it again to properly transform the input. There will be a major version update (currently v2.0.0) once it is added. | ||
As of v2.0.0, 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). | ||
@@ -99,4 +98,17 @@ Once constructed, there are four properties exposed on the `ZodParams` object. | ||
broker.call<returnType, typeof simpleValidator.call>({ string: "yes", number: 42 }); // calls successfully | ||
broker.call<returnType, typeof complexValidator.call>({ object: { nestedString: "not optional", nestedBoolean: false }, unrecognizedKey: 69 }); // throws ValidationError | ||
broker.call< | ||
ReturnType, | ||
typeof simpleValidator.call | ||
>({ string: "yes", number: 42 }); // calls successfully | ||
broker.call< | ||
ReturnType, | ||
typeof complexValidator.call | ||
>({ | ||
object: { | ||
nestedString: "not optional", | ||
nestedBoolean: false | ||
}, | ||
unrecognizedKey: 69 | ||
}); // throws ValidationError | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
113
0
43755
354