bupkis 0.1.2 → 0.3.0

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.
Files changed (198) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +16 -16
  3. package/dist/commonjs/assertion/assertion-async.d.ts +2 -1
  4. package/dist/commonjs/assertion/assertion-async.d.ts.map +1 -1
  5. package/dist/commonjs/assertion/assertion-async.js +84 -2
  6. package/dist/commonjs/assertion/assertion-async.js.map +1 -1
  7. package/dist/commonjs/assertion/assertion-sync.d.ts +1 -1
  8. package/dist/commonjs/assertion/assertion-sync.d.ts.map +1 -1
  9. package/dist/commonjs/assertion/assertion-sync.js +5 -1
  10. package/dist/commonjs/assertion/assertion-sync.js.map +1 -1
  11. package/dist/commonjs/assertion/assertion-types.d.ts +39 -84
  12. package/dist/commonjs/assertion/assertion-types.d.ts.map +1 -1
  13. package/dist/commonjs/assertion/assertion.d.ts +1 -1
  14. package/dist/commonjs/assertion/assertion.d.ts.map +1 -1
  15. package/dist/commonjs/assertion/assertion.js +1 -14
  16. package/dist/commonjs/assertion/assertion.js.map +1 -1
  17. package/dist/commonjs/assertion/create.d.ts +5 -33
  18. package/dist/commonjs/assertion/create.d.ts.map +1 -1
  19. package/dist/commonjs/assertion/create.js +17 -6
  20. package/dist/commonjs/assertion/create.js.map +1 -1
  21. package/dist/commonjs/assertion/impl/async.d.ts +122 -21
  22. package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
  23. package/dist/commonjs/assertion/impl/async.js +114 -90
  24. package/dist/commonjs/assertion/impl/async.js.map +1 -1
  25. package/dist/commonjs/assertion/impl/callback.d.ts +104 -0
  26. package/dist/commonjs/assertion/impl/callback.d.ts.map +1 -0
  27. package/dist/commonjs/assertion/impl/callback.js +694 -0
  28. package/dist/commonjs/assertion/impl/callback.js.map +1 -0
  29. package/dist/commonjs/assertion/impl/index.d.ts +1 -1
  30. package/dist/commonjs/assertion/impl/index.d.ts.map +1 -1
  31. package/dist/commonjs/assertion/impl/index.js.map +1 -1
  32. package/dist/commonjs/assertion/impl/sync-esoteric.js +1 -1
  33. package/dist/commonjs/assertion/impl/sync-esoteric.js.map +1 -1
  34. package/dist/commonjs/assertion/impl/sync-parametric.d.ts +37 -34
  35. package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
  36. package/dist/commonjs/assertion/impl/sync-parametric.js +32 -47
  37. package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
  38. package/dist/commonjs/assertion/impl/sync.d.ts +105 -58
  39. package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
  40. package/dist/commonjs/assertion/impl/sync.js +4 -1
  41. package/dist/commonjs/assertion/impl/sync.js.map +1 -1
  42. package/dist/commonjs/bootstrap.d.ts +199 -85
  43. package/dist/commonjs/bootstrap.d.ts.map +1 -1
  44. package/dist/commonjs/bootstrap.js +19 -10
  45. package/dist/commonjs/bootstrap.js.map +1 -1
  46. package/dist/commonjs/constant.js +7 -1
  47. package/dist/commonjs/constant.js.map +1 -1
  48. package/dist/commonjs/error.d.ts +32 -5
  49. package/dist/commonjs/error.d.ts.map +1 -1
  50. package/dist/commonjs/error.js +60 -5
  51. package/dist/commonjs/error.js.map +1 -1
  52. package/dist/commonjs/expect.d.ts +130 -3
  53. package/dist/commonjs/expect.d.ts.map +1 -1
  54. package/dist/commonjs/expect.js +116 -1
  55. package/dist/commonjs/expect.js.map +1 -1
  56. package/dist/commonjs/guards.d.ts +45 -20
  57. package/dist/commonjs/guards.d.ts.map +1 -1
  58. package/dist/commonjs/guards.js +56 -40
  59. package/dist/commonjs/guards.js.map +1 -1
  60. package/dist/commonjs/index.d.ts +241 -86
  61. package/dist/commonjs/index.d.ts.map +1 -1
  62. package/dist/commonjs/index.js +44 -42
  63. package/dist/commonjs/index.js.map +1 -1
  64. package/dist/commonjs/metadata.d.ts +1 -27
  65. package/dist/commonjs/metadata.d.ts.map +1 -1
  66. package/dist/commonjs/metadata.js +16 -15
  67. package/dist/commonjs/metadata.js.map +1 -1
  68. package/dist/commonjs/schema.d.ts +76 -33
  69. package/dist/commonjs/schema.d.ts.map +1 -1
  70. package/dist/commonjs/schema.js +77 -34
  71. package/dist/commonjs/schema.js.map +1 -1
  72. package/dist/commonjs/types.d.ts +480 -39
  73. package/dist/commonjs/types.d.ts.map +1 -1
  74. package/dist/commonjs/types.js +12 -2
  75. package/dist/commonjs/types.js.map +1 -1
  76. package/dist/commonjs/util.d.ts +72 -49
  77. package/dist/commonjs/util.d.ts.map +1 -1
  78. package/dist/commonjs/util.js +175 -155
  79. package/dist/commonjs/util.js.map +1 -1
  80. package/dist/commonjs/value-to-schema.d.ts +122 -0
  81. package/dist/commonjs/value-to-schema.d.ts.map +1 -0
  82. package/dist/commonjs/value-to-schema.js +309 -0
  83. package/dist/commonjs/value-to-schema.js.map +1 -0
  84. package/dist/esm/assertion/assertion-async.d.ts +2 -1
  85. package/dist/esm/assertion/assertion-async.d.ts.map +1 -1
  86. package/dist/esm/assertion/assertion-async.js +85 -3
  87. package/dist/esm/assertion/assertion-async.js.map +1 -1
  88. package/dist/esm/assertion/assertion-sync.d.ts +1 -1
  89. package/dist/esm/assertion/assertion-sync.d.ts.map +1 -1
  90. package/dist/esm/assertion/assertion-sync.js +6 -2
  91. package/dist/esm/assertion/assertion-sync.js.map +1 -1
  92. package/dist/esm/assertion/assertion-types.d.ts +39 -84
  93. package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
  94. package/dist/esm/assertion/assertion.d.ts +1 -1
  95. package/dist/esm/assertion/assertion.d.ts.map +1 -1
  96. package/dist/esm/assertion/assertion.js +1 -14
  97. package/dist/esm/assertion/assertion.js.map +1 -1
  98. package/dist/esm/assertion/create.d.ts +5 -33
  99. package/dist/esm/assertion/create.d.ts.map +1 -1
  100. package/dist/esm/assertion/create.js +14 -4
  101. package/dist/esm/assertion/create.js.map +1 -1
  102. package/dist/esm/assertion/impl/async.d.ts +122 -21
  103. package/dist/esm/assertion/impl/async.d.ts.map +1 -1
  104. package/dist/esm/assertion/impl/async.js +113 -89
  105. package/dist/esm/assertion/impl/async.js.map +1 -1
  106. package/dist/esm/assertion/impl/callback.d.ts +104 -0
  107. package/dist/esm/assertion/impl/callback.d.ts.map +1 -0
  108. package/dist/esm/assertion/impl/callback.js +691 -0
  109. package/dist/esm/assertion/impl/callback.js.map +1 -0
  110. package/dist/esm/assertion/impl/index.d.ts +1 -1
  111. package/dist/esm/assertion/impl/index.d.ts.map +1 -1
  112. package/dist/esm/assertion/impl/index.js +1 -1
  113. package/dist/esm/assertion/impl/index.js.map +1 -1
  114. package/dist/esm/assertion/impl/sync-esoteric.js +2 -2
  115. package/dist/esm/assertion/impl/sync-esoteric.js.map +1 -1
  116. package/dist/esm/assertion/impl/sync-parametric.d.ts +37 -34
  117. package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
  118. package/dist/esm/assertion/impl/sync-parametric.js +32 -47
  119. package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
  120. package/dist/esm/assertion/impl/sync.d.ts +105 -58
  121. package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
  122. package/dist/esm/assertion/impl/sync.js +3 -1
  123. package/dist/esm/assertion/impl/sync.js.map +1 -1
  124. package/dist/esm/bootstrap.d.ts +199 -85
  125. package/dist/esm/bootstrap.d.ts.map +1 -1
  126. package/dist/esm/bootstrap.js +19 -10
  127. package/dist/esm/bootstrap.js.map +1 -1
  128. package/dist/esm/constant.js +6 -0
  129. package/dist/esm/constant.js.map +1 -1
  130. package/dist/esm/error.d.ts +32 -5
  131. package/dist/esm/error.d.ts.map +1 -1
  132. package/dist/esm/error.js +59 -5
  133. package/dist/esm/error.js.map +1 -1
  134. package/dist/esm/expect.d.ts +130 -3
  135. package/dist/esm/expect.d.ts.map +1 -1
  136. package/dist/esm/expect.js +117 -2
  137. package/dist/esm/expect.js.map +1 -1
  138. package/dist/esm/guards.d.ts +45 -20
  139. package/dist/esm/guards.d.ts.map +1 -1
  140. package/dist/esm/guards.js +48 -31
  141. package/dist/esm/guards.js.map +1 -1
  142. package/dist/esm/index.d.ts +241 -86
  143. package/dist/esm/index.d.ts.map +1 -1
  144. package/dist/esm/index.js +46 -7
  145. package/dist/esm/index.js.map +1 -1
  146. package/dist/esm/metadata.d.ts +1 -27
  147. package/dist/esm/metadata.d.ts.map +1 -1
  148. package/dist/esm/metadata.js +2 -1
  149. package/dist/esm/metadata.js.map +1 -1
  150. package/dist/esm/schema.d.ts +76 -33
  151. package/dist/esm/schema.d.ts.map +1 -1
  152. package/dist/esm/schema.js +77 -34
  153. package/dist/esm/schema.js.map +1 -1
  154. package/dist/esm/types.d.ts +480 -39
  155. package/dist/esm/types.d.ts.map +1 -1
  156. package/dist/esm/types.js +12 -2
  157. package/dist/esm/types.js.map +1 -1
  158. package/dist/esm/util.d.ts +72 -49
  159. package/dist/esm/util.d.ts.map +1 -1
  160. package/dist/esm/util.js +159 -153
  161. package/dist/esm/util.js.map +1 -1
  162. package/dist/esm/value-to-schema.d.ts +122 -0
  163. package/dist/esm/value-to-schema.d.ts.map +1 -0
  164. package/dist/esm/value-to-schema.js +305 -0
  165. package/dist/esm/value-to-schema.js.map +1 -0
  166. package/package.json +94 -17
  167. package/src/assertion/assertion-async.ts +113 -3
  168. package/src/assertion/assertion-sync.ts +5 -2
  169. package/src/assertion/assertion-types.ts +52 -45
  170. package/src/assertion/assertion.ts +2 -17
  171. package/src/assertion/create.ts +16 -65
  172. package/src/assertion/impl/async.ts +132 -92
  173. package/src/assertion/impl/callback.ts +882 -0
  174. package/src/assertion/impl/index.ts +1 -1
  175. package/src/assertion/impl/sync-esoteric.ts +2 -2
  176. package/src/assertion/impl/sync-parametric.ts +41 -49
  177. package/src/assertion/impl/sync.ts +3 -0
  178. package/src/bootstrap.ts +21 -11
  179. package/src/constant.ts +8 -0
  180. package/src/error.ts +75 -4
  181. package/src/expect.ts +275 -20
  182. package/src/guards.ts +74 -69
  183. package/src/index.ts +72 -11
  184. package/src/metadata.ts +3 -4
  185. package/src/schema.ts +80 -36
  186. package/src/types.ts +625 -72
  187. package/src/util.ts +174 -222
  188. package/src/value-to-schema.ts +464 -0
  189. package/dist/commonjs/api.d.ts +0 -93
  190. package/dist/commonjs/api.d.ts.map +0 -1
  191. package/dist/commonjs/api.js +0 -8
  192. package/dist/commonjs/api.js.map +0 -1
  193. package/dist/esm/api.d.ts +0 -93
  194. package/dist/esm/api.d.ts.map +0 -1
  195. package/dist/esm/api.js +0 -7
  196. package/dist/esm/api.js.map +0 -1
  197. package/src/api.ts +0 -149
  198. package/src/schema.md +0 -15
@@ -1,67 +1,90 @@
1
1
  /**
2
- * Utility functions for object satisfaction and shape validation.
2
+ * Utility functions.
3
3
  *
4
- * This module provides core utility functions for checking if objects satisfy
5
- * expected shapes, including `satisfies` for partial matching,
6
- * `exhaustivelySatisfies` for exact matching, and `shallowSatisfiesShape` for
7
- * converting shapes to Zod schemas. All functions handle circular references
8
- * safely.
4
+ * @category API
5
+ * @example
6
+ *
7
+ * ```ts
8
+ * import * as util from 'bupkis/util';
9
+ * ```
9
10
  *
10
11
  * @packageDocumentation
11
12
  */
12
13
  import { type StringKeyOf } from 'type-fest';
13
- import { z } from 'zod/v4';
14
- export declare function keyBy<const T extends readonly Record<PropertyKey, any>[], K extends StringKeyOf<T[number]>>(collection: T, key: K): Record<string, T[number]>;
14
+ export * from './value-to-schema.js';
15
15
  /**
16
- * Recursively converts an arbitrary value to a Zod v4 schema that would
17
- * validate values with the same structure.
16
+ * _Recursively_ searches within an object, array, or any nested structure to
17
+ * find whether a specific key exists.
18
18
  *
19
- * This function analyzes the runtime value and generates a corresponding Zod
20
- * schema that captures the value's structure and type information. It handles
21
- * primitives, objects, arrays, functions, and various built-in types, with
22
- * support for circular reference detection.
19
+ * Handles circular references by tracking visited objects to prevent infinite
20
+ * recursion.
23
21
  *
24
22
  * @example
25
23
  *
26
- * ```typescript
27
- * // Primitive types
28
- * valueToSchema('hello'); // z.string()
29
- * valueToSchema(42); // z.number()
30
- * valueToSchema(true); // z.boolean()
24
+ * ```ts
25
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] } };
26
+ *
27
+ * hasKey(obj, 'c'); // true
28
+ * hasKey(obj, 'e'); // true
29
+ * hasKey(obj, 'x'); // false (key not found)
30
+ * ```
31
31
  *
32
- * // Objects
33
- * valueToSchema({ name: 'John', age: 30 });
34
- * // z.object({ name: z.string(), age: z.number() })
32
+ * @param obj The object, array, or value to search within
33
+ * @param key The key to search for
34
+ * @param visited Internal set for circular reference detection
35
+ * @returns True if the key is found anywhere in the structure, false otherwise
36
+ */
37
+ export declare function hasKey(obj: unknown, key: PropertyKey, visited?: WeakSet<object>): boolean;
38
+ /**
39
+ * _Recursively_ searches within an object, array, or any nested structure to
40
+ * find whether a specific value exists.
41
+ *
42
+ * Uses strict equality (===) to compare values, with special handling for empty
43
+ * objects. Handles circular references by tracking visited objects to prevent
44
+ * infinite recursion.
45
+ *
46
+ * @example
35
47
  *
36
- * // Arrays
37
- * valueToSchema(['a', 'b', 'c']); // z.array(z.string())
38
- * valueToSchema([1, 'mixed']); // z.array(z.union([z.number(), z.string()]))
48
+ * ```ts
49
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] }, empty: {} };
39
50
  *
40
- * // Nested structures
41
- * valueToSchema({ users: [{ name: 'John' }] });
42
- * // z.object({ users: z.array(z.object({ name: z.string() })) })
51
+ * hasValue(obj, 2); // true (found in obj.b.c)
52
+ * hasValue(obj, 3); // true (found in obj.b.d[0].e)
53
+ * hasValue(obj, {}); // true (found in obj.empty, matches empty objects)
54
+ * hasValue(obj, '1'); // false (strict equality, 1 !== '1')
55
+ * hasValue(obj, 999); // false (value not found)
43
56
  * ```
44
57
  *
45
- * @param value - The value to convert to a schema
46
- * @param options - Configuration options for schema generation
47
- * @param visited - Internal WeakSet for circular reference detection
48
- * @returns A Zod schema that validates values matching the input's structure
58
+ * @param obj The object, array, or value to search within
59
+ * @param value The value to search for (using strict equality, with special
60
+ * empty object handling)
61
+ * @param visited Internal set for circular reference detection
62
+ * @returns True if the value is found anywhere in the structure, false
63
+ * otherwise
49
64
  */
50
- export declare const valueToSchema: (value: unknown, options?: {
51
- /** Current depth (internal) */
52
- _currentDepth?: number;
53
- /** Whether to allow mixed types in arrays (default: true) */
54
- allowMixedArrays?: boolean;
55
- /** If `true`, use `z.literal()` for primitive values instead of type schemas */
56
- literalPrimitives?: boolean;
57
- /**
58
- * If `true`, treat `RegExp` literals as `RegExp` literals; otherwise treat
59
- * as strings and attempt match
60
- */
61
- literalRegExp?: boolean;
62
- /** Maximum nesting depth to prevent stack overflow (default: 10) */
63
- maxDepth?: number;
64
- /** If `true`, will disallow unknown properties in objects */
65
- strict?: boolean;
66
- }, visited?: WeakSet<object>) => z.ZodType;
65
+ export declare function hasValue(obj: unknown, value: unknown, visited?: WeakSet<object>): boolean;
66
+ /**
67
+ * _Recursively_ searches for a key-value pair within an object or array.
68
+ *
69
+ * Uses strict equality (===) to compare values. Handles circular references by
70
+ * tracking visited objects to prevent infinite recursion.
71
+ *
72
+ * @example
73
+ *
74
+ * ```ts
75
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] } };
76
+ *
77
+ * hasKeyValue(obj, 'c', 2); // true
78
+ * hasKeyValue(obj, 'e', 3); // true
79
+ * hasKeyValue(obj, 'a', '1'); // false (strict equality)
80
+ * hasKeyValue(obj, 'x', 1); // false (key not found)
81
+ * /**
82
+ * Maps an array of objects to an object keyed by the specified key.
83
+ *
84
+ * @param collection Array of objects
85
+ * @param key Name of key
86
+ * @returns Object mapping key values to objects
87
+ * ```
88
+ */
89
+ export declare function keyBy<const T extends readonly Record<PropertyKey, any>[], K extends StringKeyOf<T[number]>>(collection: T, key: K): Record<string, T[number]>;
67
90
  //# sourceMappingURL=util.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,KAA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EACZ,KAAK,EAAE,OAAO,EACd,OAAO,kBAAwB,GAC9B,OAAO,CAyDT;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,SAAS,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,EACnD,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,EAChC,UAAU,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAelD"}
package/dist/esm/util.js CHANGED
@@ -1,182 +1,188 @@
1
1
  /**
2
- * Utility functions for object satisfaction and shape validation.
2
+ * Utility functions.
3
3
  *
4
- * This module provides core utility functions for checking if objects satisfy
5
- * expected shapes, including `satisfies` for partial matching,
6
- * `exhaustivelySatisfies` for exact matching, and `shallowSatisfiesShape` for
7
- * converting shapes to Zod schemas. All functions handle circular references
8
- * safely.
4
+ * @category API
5
+ * @example
6
+ *
7
+ * ```ts
8
+ * import * as util from 'bupkis/util';
9
+ * ```
9
10
  *
10
11
  * @packageDocumentation
11
12
  */
12
- import { z } from 'zod/v4';
13
- import { isNonNullObject, isPromiseLike, isString } from './guards.js';
14
- import { FunctionSchema, RegExpSchema, StrongMapSchema, StrongSetSchema, WrappedPromiseLikeSchema, } from './schema.js';
15
- export function keyBy(collection, key) {
16
- const result = {};
17
- for (const item of collection) {
18
- const keyValue = item[key];
19
- if (typeof keyValue === 'string' ||
20
- typeof keyValue === 'number' ||
21
- typeof keyValue === 'symbol') {
22
- result[String(keyValue)] = item;
13
+ export * from './value-to-schema.js';
14
+ /**
15
+ * _Recursively_ searches within an object, array, or any nested structure to
16
+ * find whether a specific key exists.
17
+ *
18
+ * Handles circular references by tracking visited objects to prevent infinite
19
+ * recursion.
20
+ *
21
+ * @example
22
+ *
23
+ * ```ts
24
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] } };
25
+ *
26
+ * hasKey(obj, 'c'); // true
27
+ * hasKey(obj, 'e'); // true
28
+ * hasKey(obj, 'x'); // false (key not found)
29
+ * ```
30
+ *
31
+ * @param obj The object, array, or value to search within
32
+ * @param key The key to search for
33
+ * @param visited Internal set for circular reference detection
34
+ * @returns True if the key is found anywhere in the structure, false otherwise
35
+ */
36
+ export function hasKey(obj, key, visited = new WeakSet()) {
37
+ // Handle primitives that can't contain keys
38
+ if (typeof obj !== 'object' || obj === null) {
39
+ return false;
40
+ }
41
+ // Prevent infinite recursion with circular references
42
+ if (visited.has(obj)) {
43
+ return false;
44
+ }
45
+ visited.add(obj);
46
+ try {
47
+ // Check if this object has the key
48
+ if (Object.hasOwn(obj, key)) {
49
+ return true;
23
50
  }
51
+ // Recursively search in object/array values
52
+ if (Array.isArray(obj)) {
53
+ // For arrays, search in each element
54
+ for (const item of obj) {
55
+ if (hasKey(item, key, visited)) {
56
+ return true;
57
+ }
58
+ }
59
+ }
60
+ else {
61
+ // For objects, search in each property value
62
+ for (const propValue of Object.values(obj)) {
63
+ if (hasKey(propValue, key, visited)) {
64
+ return true;
65
+ }
66
+ }
67
+ }
68
+ return false;
69
+ }
70
+ finally {
71
+ visited.delete(obj);
24
72
  }
25
- return result;
26
73
  }
27
74
  /**
28
- * Recursively converts an arbitrary value to a Zod v4 schema that would
29
- * validate values with the same structure.
75
+ * _Recursively_ searches within an object, array, or any nested structure to
76
+ * find whether a specific value exists.
30
77
  *
31
- * This function analyzes the runtime value and generates a corresponding Zod
32
- * schema that captures the value's structure and type information. It handles
33
- * primitives, objects, arrays, functions, and various built-in types, with
34
- * support for circular reference detection.
78
+ * Uses strict equality (===) to compare values, with special handling for empty
79
+ * objects. Handles circular references by tracking visited objects to prevent
80
+ * infinite recursion.
35
81
  *
36
82
  * @example
37
83
  *
38
- * ```typescript
39
- * // Primitive types
40
- * valueToSchema('hello'); // z.string()
41
- * valueToSchema(42); // z.number()
42
- * valueToSchema(true); // z.boolean()
43
- *
44
- * // Objects
45
- * valueToSchema({ name: 'John', age: 30 });
46
- * // z.object({ name: z.string(), age: z.number() })
84
+ * ```ts
85
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] }, empty: {} };
47
86
  *
48
- * // Arrays
49
- * valueToSchema(['a', 'b', 'c']); // z.array(z.string())
50
- * valueToSchema([1, 'mixed']); // z.array(z.union([z.number(), z.string()]))
51
- *
52
- * // Nested structures
53
- * valueToSchema({ users: [{ name: 'John' }] });
54
- * // z.object({ users: z.array(z.object({ name: z.string() })) })
87
+ * hasValue(obj, 2); // true (found in obj.b.c)
88
+ * hasValue(obj, 3); // true (found in obj.b.d[0].e)
89
+ * hasValue(obj, {}); // true (found in obj.empty, matches empty objects)
90
+ * hasValue(obj, '1'); // false (strict equality, 1 !== '1')
91
+ * hasValue(obj, 999); // false (value not found)
55
92
  * ```
56
93
  *
57
- * @param value - The value to convert to a schema
58
- * @param options - Configuration options for schema generation
59
- * @param visited - Internal WeakSet for circular reference detection
60
- * @returns A Zod schema that validates values matching the input's structure
94
+ * @param obj The object, array, or value to search within
95
+ * @param value The value to search for (using strict equality, with special
96
+ * empty object handling)
97
+ * @param visited Internal set for circular reference detection
98
+ * @returns True if the value is found anywhere in the structure, false
99
+ * otherwise
61
100
  */
62
- export const valueToSchema = (value, options = {}, visited = new WeakSet()) => {
63
- const { _currentDepth = 0, allowMixedArrays = true, literalPrimitives = false, literalRegExp = false, maxDepth = 10, strict = false, } = options;
64
- // Prevent infinite recursion
65
- if (_currentDepth >= maxDepth) {
66
- return z.unknown();
67
- }
68
- // Handle primitives
69
- if (value === null) {
70
- return z.null();
101
+ export function hasValue(obj, value, visited = new WeakSet()) {
102
+ // Direct value comparison
103
+ if (obj === value) {
104
+ return true;
71
105
  }
72
- if (value === undefined) {
73
- return z.undefined();
74
- }
75
- if (Number.isNaN(value)) {
76
- return z.nan();
106
+ // Special case: Check for empty objects
107
+ if (typeof obj === 'object' &&
108
+ obj !== null &&
109
+ !Array.isArray(obj) &&
110
+ typeof value === 'object' &&
111
+ value !== null &&
112
+ !Array.isArray(value)) {
113
+ const objKeys = Object.keys(obj);
114
+ const valueKeys = Object.keys(value);
115
+ // Both are empty objects
116
+ if (objKeys.length === 0 && valueKeys.length === 0) {
117
+ return true;
118
+ }
77
119
  }
78
- if (value === Infinity || value === -Infinity) {
79
- return z.literal(value);
120
+ // Handle primitives that can't contain nested values
121
+ if (typeof obj !== 'object' || obj === null) {
122
+ return false;
80
123
  }
81
- const valueType = typeof value;
82
- switch (valueType) {
83
- case 'bigint':
84
- return literalPrimitives ? z.literal(value) : z.bigint();
85
- case 'boolean':
86
- return literalPrimitives ? z.literal(value) : z.boolean();
87
- case 'function':
88
- return FunctionSchema;
89
- case 'number':
90
- return literalPrimitives ? z.literal(value) : z.number();
91
- case 'string':
92
- return literalPrimitives ? z.literal(value) : z.string();
93
- case 'symbol':
94
- return z.symbol();
124
+ // Prevent infinite recursion with circular references
125
+ if (visited.has(obj)) {
126
+ return false;
95
127
  }
96
- // Handle objects
97
- if (typeof value === 'object' && value !== null) {
98
- // Check for circular references
99
- if (visited.has(value)) {
100
- // Return a recursive schema reference or unknown for circular refs
101
- return z.unknown();
102
- }
103
- visited.add(value);
104
- try {
105
- // Handle built-in object types
106
- if (value instanceof Date) {
107
- return z.date();
108
- }
109
- if (value instanceof RegExp) {
110
- if (literalRegExp) {
111
- return RegExpSchema;
112
- }
113
- return z.coerce.string().regex(value);
114
- }
115
- if (value instanceof Map) {
116
- return StrongMapSchema;
117
- }
118
- if (value instanceof Set) {
119
- return StrongSetSchema;
120
- }
121
- if (value instanceof WeakMap) {
122
- return z.instanceof(WeakMap);
123
- }
124
- if (value instanceof WeakSet) {
125
- return z.instanceof(WeakSet);
126
- }
127
- if (value instanceof Error) {
128
- return z.instanceof(Error);
129
- }
130
- if (isPromiseLike(value)) {
131
- return WrappedPromiseLikeSchema;
132
- }
133
- // Handle arrays
134
- if (Array.isArray(value)) {
135
- if (value.length === 0) {
136
- return z.array(z.unknown());
137
- }
138
- const elementSchemas = value.map((item) => valueToSchema(item, {
139
- ...options,
140
- _currentDepth: _currentDepth + 1,
141
- }, visited));
142
- if (allowMixedArrays) {
143
- // Create a union of all unique element types
144
- const uniqueSchemas = Array.from(new Set(elementSchemas.map((schema) => schema.constructor.name))).map((_, index) => elementSchemas[index]);
145
- if (uniqueSchemas.length === 1) {
146
- return z.array(uniqueSchemas[0]);
147
- }
148
- else {
149
- return z.array(z.union(uniqueSchemas));
150
- }
151
- }
152
- else {
153
- // Use the first element's schema for all elements
154
- return z.array(elementSchemas[0]);
128
+ visited.add(obj);
129
+ try {
130
+ // Recursively search in object/array values
131
+ if (Array.isArray(obj)) {
132
+ // For arrays, search in each element
133
+ for (const item of obj) {
134
+ if (hasValue(item, value, visited)) {
135
+ return true;
155
136
  }
156
137
  }
157
- // Handle plain objects
158
- if (isNonNullObject(value)) {
159
- const schemaShape = {};
160
- for (const [key, val] of Object.entries(value)) {
161
- if (isString(key)) {
162
- schemaShape[key] = valueToSchema(val, {
163
- ...options,
164
- _currentDepth: _currentDepth + 1,
165
- }, visited);
166
- }
138
+ }
139
+ else {
140
+ // For objects, search in each property value
141
+ for (const propValue of Object.values(obj)) {
142
+ if (hasValue(propValue, value, visited)) {
143
+ return true;
167
144
  }
168
- return strict
169
- ? z.strictObject(schemaShape)
170
- : z.looseObject(schemaShape);
171
145
  }
172
- // Handle other object types (ArrayBuffer, etc.)
173
- return z.custom((val) => typeof val === 'object' && val !== null, { message: 'Expected an object' });
174
146
  }
175
- finally {
176
- visited.delete(value);
147
+ return false;
148
+ }
149
+ finally {
150
+ visited.delete(obj);
151
+ }
152
+ }
153
+ /**
154
+ * _Recursively_ searches for a key-value pair within an object or array.
155
+ *
156
+ * Uses strict equality (===) to compare values. Handles circular references by
157
+ * tracking visited objects to prevent infinite recursion.
158
+ *
159
+ * @example
160
+ *
161
+ * ```ts
162
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] } };
163
+ *
164
+ * hasKeyValue(obj, 'c', 2); // true
165
+ * hasKeyValue(obj, 'e', 3); // true
166
+ * hasKeyValue(obj, 'a', '1'); // false (strict equality)
167
+ * hasKeyValue(obj, 'x', 1); // false (key not found)
168
+ * /**
169
+ * Maps an array of objects to an object keyed by the specified key.
170
+ *
171
+ * @param collection Array of objects
172
+ * @param key Name of key
173
+ * @returns Object mapping key values to objects
174
+ * ```
175
+ */
176
+ export function keyBy(collection, key) {
177
+ const result = {};
178
+ for (const item of collection) {
179
+ const keyValue = item[key];
180
+ if (typeof keyValue === 'string' ||
181
+ typeof keyValue === 'number' ||
182
+ typeof keyValue === 'symbol') {
183
+ result[String(keyValue)] = item;
177
184
  }
178
185
  }
179
- // Fallback for unknown types
180
- return z.unknown();
181
- };
186
+ return result;
187
+ }
182
188
  //# sourceMappingURL=util.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAE3B,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EACL,cAAc,EACd,YAAY,EACZ,eAAe,EACf,eAAe,EACf,wBAAwB,GACzB,MAAM,aAAa,CAAC;AAErB,MAAM,UAAU,KAAK,CAGnB,UAAa,EAAE,GAAM;IACrB,MAAM,MAAM,GAAG,EAA+B,CAAC;IAE/C,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;QAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QAC3B,IACE,OAAO,QAAQ,KAAK,QAAQ;YAC5B,OAAO,QAAQ,KAAK,QAAQ;YAC5B,OAAO,QAAQ,KAAK,QAAQ,EAC5B,CAAC;YACD,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC;QAClC,CAAC;IACH,CAAC;IAED,OAA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IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,CAAC;oBACnC,OAAO,IAAI,CAAC;gBACd,CAAC;YACH,CAAC;QACH,CAAC;aAAM,CAAC;YACN,6CAA6C;YAC7C,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC3C,IAAI,QAAQ,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,CAAC;oBACxC,OAAO,IAAI,CAAC;gBACd,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;YAAS,CAAC;QACT,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,KAAK,CAGnB,UAAa,EAAE,GAAM;IACrB,MAAM,MAAM,GAAG,EAA+B,CAAC;IAE/C,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;QAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QAC3B,IACE,OAAO,QAAQ,KAAK,QAAQ;YAC5B,OAAO,QAAQ,KAAK,QAAQ;YAC5B,OAAO,QAAQ,KAAK,QAAQ,EAC5B,CAAC;YACD,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC;QAClC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,122 @@
1
+ import { z } from 'zod/v4';
2
+ /**
3
+ * Recursively converts an arbitrary value to a Zod v4 schema that would
4
+ * validate values with the same structure.
5
+ *
6
+ * This function analyzes the runtime value and generates a corresponding Zod
7
+ * schema that captures the value's structure and type information. It handles
8
+ * primitives, objects, arrays, functions, and various built-in types, with
9
+ * support for circular reference detection.
10
+ *
11
+ * @example
12
+ *
13
+ * ```typescript
14
+ * // Primitive types
15
+ * valueToSchema('hello'); // z.string()
16
+ * valueToSchema(42); // z.number()
17
+ * valueToSchema(true); // z.boolean()
18
+ *
19
+ * // Objects
20
+ * valueToSchema({ name: 'John', age: 30 });
21
+ * // z.object({ name: z.string(), age: z.number() })
22
+ *
23
+ * // Arrays
24
+ * valueToSchema(['a', 'b', 'c']); // z.array(z.string())
25
+ * valueToSchema([1, 'mixed']); // z.array(z.union([z.number(), z.string()]))
26
+ *
27
+ * // Nested structures
28
+ * valueToSchema({ users: [{ name: 'John' }] });
29
+ * // z.object({ users: z.array(z.object({ name: z.string() })) })
30
+ * ```
31
+ *
32
+ * @param value - The value to convert to a schema
33
+ * @param options - Configuration options for schema generation
34
+ * @param visited - Internal WeakSet for circular reference detection
35
+ * @returns A Zod schema that validates values matching the input's structure.
36
+ * This value is unfortunately untyped due to the complexity involved. But the
37
+ * schema works!
38
+ */
39
+ export declare const valueToSchema: (value: unknown, options?: ValueToSchemaOptions, visited?: WeakSet<object>) => z.ZodType<any>;
40
+ /**
41
+ * Options for {@link valueToSchema}
42
+ */
43
+ export interface ValueToSchemaOptions {
44
+ /**
45
+ * If `true`, treat empty objects `{}` as literal empty objects that only
46
+ * match objects with zero own properties
47
+ *
48
+ * @defaultValue false
49
+ */
50
+ literalEmptyObjects?: boolean;
51
+ /**
52
+ * If `true`, use literal schema for primitive values instead of type schema
53
+ *
54
+ * @defaultValue false
55
+ */
56
+ literalPrimitives?: boolean;
57
+ /**
58
+ * If `true`, treat `RegExp` literals as `RegExp` literals; otherwise treat as
59
+ * strings and attempt match
60
+ *
61
+ * @defaultValue false
62
+ */
63
+ literalRegExp?: boolean;
64
+ /**
65
+ * If `true`, treat arrays as tuples wherever possible.
66
+ *
67
+ * Implies `false` for {@link noMixedArrays}.
68
+ *
69
+ * @defaultValue false
70
+ */
71
+ literalTuples?: boolean;
72
+ /**
73
+ * Maximum nesting depth to prevent stack overflow
74
+ *
75
+ * @defaultValue 10
76
+ */
77
+ maxDepth?: number;
78
+ /**
79
+ * Whether to allow mixed types in arrays
80
+ *
81
+ * If {@link literalTuples} is `true`, this option is ignored and treated as
82
+ * `false`.
83
+ *
84
+ * @defaultValue false
85
+ */
86
+ noMixedArrays?: boolean;
87
+ /**
88
+ * If `true`, will disallow unknown properties in parsed objects
89
+ *
90
+ * @defaultValue false
91
+ */
92
+ strict?: boolean;
93
+ }
94
+ /**
95
+ * Predefined options for {@link valueToSchema} optimized for object satisfaction
96
+ * checks.
97
+ *
98
+ * Uses literal primitives and tuples for exact matching while allowing extra
99
+ * properties.
100
+ */
101
+ export declare const valueToSchemaOptionsForSatisfies: Readonly<{
102
+ readonly literalEmptyObjects: true;
103
+ readonly literalPrimitives: true;
104
+ readonly literalRegExp: false;
105
+ readonly literalTuples: true;
106
+ readonly strict: false;
107
+ }>;
108
+ /**
109
+ * Predefined options for {@link valueToSchema} optimized for deep equality
110
+ * checks.
111
+ *
112
+ * Uses literal primitives, regexp, and tuples with strict validation for exact
113
+ * matching.
114
+ */
115
+ export declare const valueToSchemaOptionsForDeepEqual: Readonly<{
116
+ readonly literalEmptyObjects: true;
117
+ readonly literalPrimitives: true;
118
+ readonly literalRegExp: true;
119
+ readonly literalTuples: true;
120
+ readonly strict: true;
121
+ }>;
122
+ //# sourceMappingURL=value-to-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-to-schema.d.ts","sourceRoot":"","sources":["../../src/value-to-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAgB3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,eAAO,MAAM,aAAa,GACxB,OAAO,OAAO,EACd,UAAS,oBAAyB,EAClC,yBAA+B,KAC9B,CAAC,CAAC,OAAO,CAAC,GAAG,CAmTf,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,oBAAoB;IAQnC;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAE9B;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAE5B;;;;;OAKG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,gCAAgC;;;;;;EAMH,CAAC;AAE3C;;;;;;GAMG;AACH,eAAO,MAAM,gCAAgC;;;;;;EAMH,CAAC"}