typeshi 2.1.2 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/object.d.ts +10 -1
- package/dist/utils/object.js +7 -1
- package/package.json +1 -1
package/dist/utils/object.d.ts
CHANGED
|
@@ -23,8 +23,17 @@ export declare class Restrict {
|
|
|
23
23
|
*/
|
|
24
24
|
static toPicked: typeof picked;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* **`values`** are either:
|
|
28
|
+
* - a `function` where only need to pass in single value to get the transformed value `T[K]`
|
|
29
|
+
* - an object with two props: `transform` & `args` where `args` is the array of arguments
|
|
30
|
+
* passed into `transform` using the spread operator.
|
|
31
|
+
*/
|
|
26
32
|
export type TransformationSchema<T> = {
|
|
27
|
-
[K in keyof T]?: (val: any) => T[K]
|
|
33
|
+
[K in keyof T]?: ((val: any) => T[K]) | {
|
|
34
|
+
transform: (val: any, ...args: any[]) => T[K];
|
|
35
|
+
args: any[];
|
|
36
|
+
};
|
|
28
37
|
};
|
|
29
38
|
/**
|
|
30
39
|
* @param obj `any` - source object (e.g., Request Body)
|
package/dist/utils/object.js
CHANGED
|
@@ -9,6 +9,7 @@ exports.hasValidKeysOnly = hasValidKeysOnly;
|
|
|
9
9
|
exports.picked = picked;
|
|
10
10
|
exports.sanitizeAndMap = sanitizeAndMap;
|
|
11
11
|
exports.hasDefinedEntry = hasDefinedEntry;
|
|
12
|
+
const typeValidation_1 = require("./typeValidation");
|
|
12
13
|
/**
|
|
13
14
|
* @returns `boolean`
|
|
14
15
|
* - `true` if all keys in obj are also in validKeys
|
|
@@ -51,7 +52,12 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
|
|
|
51
52
|
// 1. Handle explicit transformations
|
|
52
53
|
for (const key in schema) {
|
|
53
54
|
if (schema[key] && hasDefinedEntry(obj, key)) {
|
|
54
|
-
|
|
55
|
+
if ((0, typeValidation_1.isFunction)(schema[key])) {
|
|
56
|
+
data[key] = schema[key](obj[key]);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
data[key] = schema[key].transform(obj[key], ...schema[key].args);
|
|
60
|
+
}
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
// 2. Handle simple pass-throughs (Identity mapping)
|