typeshi 2.1.2 → 2.1.4

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.
@@ -23,8 +23,25 @@ export declare class Restrict {
23
23
  */
24
24
  static toPicked: typeof picked;
25
25
  }
26
+ /**
27
+ * define a `TransformationSchema<T>` to use in `sanitizeAndMap` to convert
28
+ * an initial object `any` to `T`. the `"initial"` object is assumed to share props with `T`
29
+ * (hence the need to transform its values)
30
+ *
31
+ * values are either:
32
+ * 1. a `function` where only need to pass in `initial[K]` to get the transformed value `T[K]`
33
+ * 2. an `object` with props:
34
+ * - `transform (function, optional)` - function that uses `initial[K]` & `args` to compute `T[K]`
35
+ * - `args (any[], optional)` - the array of arguments passed into `transform` using the spread operator.
36
+ * - `defaultValue (T[K], optional)` - assign this value to `K` when `transform` is `undefined`
37
+ * and `initial[K]` is `undefined`
38
+ */
26
39
  export type TransformationSchema<T> = {
27
- [K in keyof T]?: (val: any) => T[K];
40
+ [K in keyof T]?: ((val: any) => T[K]) | {
41
+ transform?: (val: any, ...args: any[]) => T[K];
42
+ args?: any[];
43
+ defaultValue?: T[K];
44
+ };
28
45
  };
29
46
  /**
30
47
  * @param obj `any` - source object (e.g., Request Body)
@@ -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
@@ -50,8 +51,19 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
50
51
  const data = {};
51
52
  // 1. Handle explicit transformations
52
53
  for (const key in schema) {
53
- if (schema[key] && hasDefinedEntry(obj, key)) {
54
- data[key] = schema[key](obj[key]);
54
+ const schemaValue = schema[key];
55
+ if (!schemaValue)
56
+ continue;
57
+ if (hasDefinedEntry(obj, key)) {
58
+ if ((0, typeValidation_1.isFunction)(schemaValue)) {
59
+ data[key] = schemaValue(obj[key]);
60
+ }
61
+ else if ((0, typeValidation_1.isFunction)(schemaValue.transform)) {
62
+ data[key] = schemaValue.transform(obj[key], ...(schemaValue.args ?? []));
63
+ }
64
+ }
65
+ else if (!(0, typeValidation_1.isFunction)(schemaValue) && 'defaultValue' in schemaValue) { // can apply defaultValue
66
+ data[key] = schemaValue.defaultValue;
55
67
  }
56
68
  }
57
69
  // 2. Handle simple pass-throughs (Identity mapping)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",