-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TheAppleFreak/v2.0.0
Added support for param transformations
- Loading branch information
Showing
10 changed files
with
185 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
# 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 |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"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.", | ||
"author": "TheAppleFreak <[email protected]>", | ||
"main": "build/index.js", | ||
|
@@ -24,7 +24,7 @@ | |
"typescript" | ||
], | ||
"files": [ | ||
"build/", | ||
"build/**/*", | ||
"CHANGELOG.md" | ||
], | ||
"license": "MIT", | ||
|
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,70 @@ | ||
/** | ||
* Performs a deep mutation of an existing object to equal a given source object. | ||
* | ||
* Adapted from this package: https://github.com/jeremyhewett/mutate-object | ||
* @param {object} destination - The object to mutate | ||
* @param {object} source - The object that the destination should be mutated to equal | ||
* @param {boolean} [preserveRootProps=true] - Whether undefined props on the root level should be deleted | ||
*/ | ||
export function mutateObject( | ||
destination: any, | ||
source: any, | ||
preserveRootProps: boolean = false, | ||
): void { | ||
if (isEnumerable(source)) { | ||
const isSrcArray = Array.isArray(source); | ||
destination = | ||
(isEnumerable(destination) && | ||
Array.isArray(destination) === isSrcArray && | ||
destination) || | ||
(isSrcArray ? [] : {}); | ||
|
||
if (!preserveRootProps) { | ||
isSrcArray | ||
? cleanArray(destination, source) | ||
: cleanObject(destination, source); | ||
} | ||
|
||
if (isSrcArray) { | ||
source.map((value, i) => { | ||
if (destination.length < i + 1) { | ||
destination.push(); | ||
} | ||
destination[i] = mutateObject(destination[i], value, false); | ||
}); | ||
} else { | ||
for (let i in source) { | ||
if (source.hasOwnProperty(i)) { | ||
destination[i] = mutateObject( | ||
destination[i], | ||
source[i], | ||
false, | ||
); | ||
} | ||
} | ||
} | ||
|
||
return destination; | ||
} | ||
|
||
return source; | ||
} | ||
|
||
function isEnumerable(value: unknown) { | ||
return value !== null && typeof value === "object"; | ||
} | ||
|
||
function cleanArray(oldArray: any[], newArray: any[]) { | ||
if (newArray.length < oldArray.length) { | ||
oldArray.splice(newArray.length, oldArray.length - newArray.length); | ||
} | ||
return oldArray; | ||
} | ||
|
||
function cleanObject(oldObj: any, newObj: any) { | ||
for (let prop in oldObj) { | ||
if (oldObj.hasOwnProperty(prop) && !newObj.hasOwnProperty(prop)) { | ||
delete oldObj[prop]; | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
], | ||
], | ||
"exclude": [ | ||
"node_modules/**" | ||
] | ||
|