typeshi 2.1.6 → 2.1.8

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.
@@ -2,27 +2,6 @@
2
2
  * @file src/utils/object.ts
3
3
  * @TODO just use zod instead
4
4
  */
5
- /**
6
- * @returns `boolean`
7
- * - `true` if all keys in obj are also in validKeys
8
- * - `false` if there exists a key in obj that is not in validKeys
9
- */
10
- export declare function hasValidKeysOnly<T extends object>(obj: object, validKeys: readonly (keyof T)[]): obj is T;
11
- /**
12
- * @returns a new object containing only the specified keys.
13
- */
14
- export declare function picked<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
15
- export declare class Restrict {
16
- /**
17
- * `hasValidKeysOnly`
18
- * @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
19
- */
20
- static keys: typeof hasValidKeysOnly;
21
- /**
22
- * @returns a new object containing only the specified keys.
23
- */
24
- static toPicked: typeof picked;
25
- }
26
5
  /**
27
6
  * define a `TransformationSchema<T, S>` to use in `sanitizeAndMap`, where entries from
28
7
  * a `source` object `S` are mapped to a new object `T`
@@ -38,10 +17,10 @@ export declare class Restrict {
38
17
  * and `source[sourceKey]` is `undefined`
39
18
  */
40
19
  export type TransformationSchema<T extends object, S extends object = any> = {
41
- [K in keyof T]?: ((val: S[keyof S]) => T[K]) | TransformOptions<T, K, S>;
20
+ [K in keyof T]?: ((val: any) => T[K]) | TransformOptions<T, K, S>;
42
21
  };
43
22
  export type TransformOptions<T extends object, K extends keyof T, S extends object = any> = {
44
- transform?: (val: S[keyof S], ...args: any[]) => T[K];
23
+ transform?: (val: any, ...args: any[]) => T[K];
45
24
  args?: any[];
46
25
  defaultValue?: T[K];
47
26
  sourceKey?: keyof S;
@@ -57,3 +36,24 @@ export declare function sanitizeAndMap<T extends object, S extends object = any>
57
36
  * @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
58
37
  */
59
38
  export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T | keyof any): boolean;
39
+ /**
40
+ * @returns `boolean`
41
+ * - `true` if all keys in obj are also in validKeys
42
+ * - `false` if there exists a key in obj that is not in validKeys
43
+ */
44
+ export declare function hasValidKeysOnly<T extends object>(obj: object, validKeys: readonly (keyof T)[]): obj is T;
45
+ /**
46
+ * @returns a new object containing only the specified keys.
47
+ */
48
+ export declare function picked<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
49
+ export declare class Restrict {
50
+ /**
51
+ * `hasValidKeysOnly`
52
+ * @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
53
+ */
54
+ static keys: typeof hasValidKeysOnly;
55
+ /**
56
+ * @returns a new object containing only the specified keys.
57
+ */
58
+ static toPicked: typeof picked;
59
+ }
@@ -5,42 +5,11 @@
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.Restrict = void 0;
8
- exports.hasValidKeysOnly = hasValidKeysOnly;
9
- exports.picked = picked;
10
8
  exports.sanitizeAndMap = sanitizeAndMap;
11
9
  exports.hasDefinedEntry = hasDefinedEntry;
10
+ exports.hasValidKeysOnly = hasValidKeysOnly;
11
+ exports.picked = picked;
12
12
  const typeValidation_1 = require("./typeValidation");
13
- /**
14
- * @returns `boolean`
15
- * - `true` if all keys in obj are also in validKeys
16
- * - `false` if there exists a key in obj that is not in validKeys
17
- */
18
- function hasValidKeysOnly(obj, validKeys) {
19
- return Object.keys(obj).every(key => validKeys.includes(key));
20
- }
21
- /**
22
- * @returns a new object containing only the specified keys.
23
- */
24
- function picked(obj, keys) {
25
- const result = {};
26
- for (const key of keys) {
27
- if (key in obj && obj[key] !== undefined)
28
- result[key] = obj[key];
29
- }
30
- return result;
31
- }
32
- class Restrict {
33
- }
34
- exports.Restrict = Restrict;
35
- /**
36
- * `hasValidKeysOnly`
37
- * @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
38
- */
39
- Restrict.keys = hasValidKeysOnly;
40
- /**
41
- * @returns a new object containing only the specified keys.
42
- */
43
- Restrict.toPicked = picked;
44
13
  /**
45
14
  * @param obj `S` - source object (e.g., Request Body)
46
15
  * @param schema {@link TransformationSchema}`<T, S>` - map of keys to transformation functions.
@@ -58,10 +27,10 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
58
27
  ? schemaValue.sourceKey
59
28
  : key);
60
29
  if (hasDefinedEntry(obj, sourceKey)) {
61
- if ((0, typeValidation_1.isFunction)(schemaValue)) { // schemaValue is ((val: S[keyof S]) => T[K])
30
+ if ((0, typeValidation_1.isFunction)(schemaValue)) { // schemaValue is ((val: any) => T[K])
62
31
  data[key] = schemaValue(obj[sourceKey]);
63
32
  }
64
- else if ((0, typeValidation_1.isFunction)(schemaValue.transform)) { // schemaValue is { transform?: (val: S[keyof S], ...args: any[]) => T[K]; args?: any[]; sourceKey?: keyof S}
33
+ else if ((0, typeValidation_1.isFunction)(schemaValue.transform)) { // schemaValue is { transform?: (val: any, ...args: any[]) => T[K]; args?: any[]; sourceKey?: keyof S}
65
34
  data[key] = schemaValue.transform(obj[sourceKey], ...(schemaValue.args ?? []));
66
35
  }
67
36
  }
@@ -72,9 +41,17 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
72
41
  }
73
42
  // 2. Handle simple pass-throughs (Identity mapping)
74
43
  for (const key of passThroughKeys) {
75
- if (!data[key] && hasDefinedEntry(obj, key)) {
44
+ if (key in data)
45
+ continue; // already handled
46
+ if (hasDefinedEntry(obj, key)) {
76
47
  data[key] = obj[key];
77
48
  }
49
+ else if (key in schema && schema[key]) { // no value available to pass through, but can assign defaultValue
50
+ const schemaValue = schema[key];
51
+ if (!(0, typeValidation_1.isFunction)(schemaValue) && 'defaultValue' in schemaValue) {
52
+ data[key] = schemaValue.defaultValue;
53
+ }
54
+ }
78
55
  }
79
56
  return data;
80
57
  }
@@ -84,3 +61,34 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
84
61
  function hasDefinedEntry(obj, key) {
85
62
  return Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;
86
63
  }
64
+ /**
65
+ * @returns `boolean`
66
+ * - `true` if all keys in obj are also in validKeys
67
+ * - `false` if there exists a key in obj that is not in validKeys
68
+ */
69
+ function hasValidKeysOnly(obj, validKeys) {
70
+ return Object.keys(obj).every(key => validKeys.includes(key));
71
+ }
72
+ /**
73
+ * @returns a new object containing only the specified keys.
74
+ */
75
+ function picked(obj, keys) {
76
+ const result = {};
77
+ for (const key of keys) {
78
+ if (key in obj && obj[key] !== undefined)
79
+ result[key] = obj[key];
80
+ }
81
+ return result;
82
+ }
83
+ class Restrict {
84
+ }
85
+ exports.Restrict = Restrict;
86
+ /**
87
+ * `hasValidKeysOnly`
88
+ * @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
89
+ */
90
+ Restrict.keys = hasValidKeysOnly;
91
+ /**
92
+ * @returns a new object containing only the specified keys.
93
+ */
94
+ Restrict.toPicked = picked;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",