bupkis 0.2.0 → 0.4.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 (187) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +35 -11
  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 +6 -2
  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/impl/async.d.ts +122 -21
  18. package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
  19. package/dist/commonjs/assertion/impl/async.js +118 -90
  20. package/dist/commonjs/assertion/impl/async.js.map +1 -1
  21. package/dist/commonjs/assertion/impl/callback.d.ts +104 -0
  22. package/dist/commonjs/assertion/impl/callback.d.ts.map +1 -0
  23. package/dist/commonjs/assertion/impl/callback.js +694 -0
  24. package/dist/commonjs/assertion/impl/callback.js.map +1 -0
  25. package/dist/commonjs/assertion/impl/index.d.ts +1 -1
  26. package/dist/commonjs/assertion/impl/index.d.ts.map +1 -1
  27. package/dist/commonjs/assertion/impl/index.js.map +1 -1
  28. package/dist/commonjs/assertion/impl/sync-basic.d.ts.map +1 -1
  29. package/dist/commonjs/assertion/impl/sync-basic.js +1 -1
  30. package/dist/commonjs/assertion/impl/sync-basic.js.map +1 -1
  31. package/dist/commonjs/assertion/impl/sync-collection.d.ts +1 -1
  32. package/dist/commonjs/assertion/impl/sync-collection.js +3 -3
  33. package/dist/commonjs/assertion/impl/sync-collection.js.map +1 -1
  34. package/dist/commonjs/assertion/impl/sync-esoteric.js +1 -1
  35. package/dist/commonjs/assertion/impl/sync-esoteric.js.map +1 -1
  36. package/dist/commonjs/assertion/impl/sync-parametric.d.ts +22 -28
  37. package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
  38. package/dist/commonjs/assertion/impl/sync-parametric.js +35 -50
  39. package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
  40. package/dist/commonjs/assertion/impl/sync.d.ts +68 -30
  41. package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
  42. package/dist/commonjs/assertion/impl/sync.js +4 -1
  43. package/dist/commonjs/assertion/impl/sync.js.map +1 -1
  44. package/dist/commonjs/bootstrap.d.ts +147 -52
  45. package/dist/commonjs/bootstrap.d.ts.map +1 -1
  46. package/dist/commonjs/bootstrap.js +2 -3
  47. package/dist/commonjs/bootstrap.js.map +1 -1
  48. package/dist/commonjs/constant.d.ts +1 -1
  49. package/dist/commonjs/constant.d.ts.map +1 -1
  50. package/dist/commonjs/constant.js +8 -1
  51. package/dist/commonjs/constant.js.map +1 -1
  52. package/dist/commonjs/error.d.ts +22 -2
  53. package/dist/commonjs/error.d.ts.map +1 -1
  54. package/dist/commonjs/error.js +44 -4
  55. package/dist/commonjs/error.js.map +1 -1
  56. package/dist/commonjs/expect.d.ts.map +1 -1
  57. package/dist/commonjs/expect.js +1 -1
  58. package/dist/commonjs/expect.js.map +1 -1
  59. package/dist/commonjs/guards.d.ts +96 -5
  60. package/dist/commonjs/guards.d.ts.map +1 -1
  61. package/dist/commonjs/guards.js +104 -25
  62. package/dist/commonjs/guards.js.map +1 -1
  63. package/dist/commonjs/index.d.ts +146 -51
  64. package/dist/commonjs/index.d.ts.map +1 -1
  65. package/dist/commonjs/index.js.map +1 -1
  66. package/dist/commonjs/schema.d.ts +84 -18
  67. package/dist/commonjs/schema.d.ts.map +1 -1
  68. package/dist/commonjs/schema.js +107 -22
  69. package/dist/commonjs/schema.js.map +1 -1
  70. package/dist/commonjs/types.d.ts +171 -9
  71. package/dist/commonjs/types.d.ts.map +1 -1
  72. package/dist/commonjs/use.d.ts.map +1 -1
  73. package/dist/commonjs/use.js +15 -1
  74. package/dist/commonjs/use.js.map +1 -1
  75. package/dist/commonjs/util.d.ts +66 -50
  76. package/dist/commonjs/util.d.ts.map +1 -1
  77. package/dist/commonjs/util.js +169 -156
  78. package/dist/commonjs/util.js.map +1 -1
  79. package/dist/commonjs/value-to-schema.d.ts +122 -0
  80. package/dist/commonjs/value-to-schema.d.ts.map +1 -0
  81. package/dist/commonjs/value-to-schema.js +329 -0
  82. package/dist/commonjs/value-to-schema.js.map +1 -0
  83. package/dist/esm/assertion/assertion-async.d.ts +2 -1
  84. package/dist/esm/assertion/assertion-async.d.ts.map +1 -1
  85. package/dist/esm/assertion/assertion-async.js +85 -3
  86. package/dist/esm/assertion/assertion-async.js.map +1 -1
  87. package/dist/esm/assertion/assertion-sync.d.ts +1 -1
  88. package/dist/esm/assertion/assertion-sync.d.ts.map +1 -1
  89. package/dist/esm/assertion/assertion-sync.js +6 -2
  90. package/dist/esm/assertion/assertion-sync.js.map +1 -1
  91. package/dist/esm/assertion/assertion-types.d.ts +6 -2
  92. package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
  93. package/dist/esm/assertion/assertion.d.ts +1 -1
  94. package/dist/esm/assertion/assertion.d.ts.map +1 -1
  95. package/dist/esm/assertion/assertion.js +1 -14
  96. package/dist/esm/assertion/assertion.js.map +1 -1
  97. package/dist/esm/assertion/impl/async.d.ts +122 -21
  98. package/dist/esm/assertion/impl/async.d.ts.map +1 -1
  99. package/dist/esm/assertion/impl/async.js +118 -90
  100. package/dist/esm/assertion/impl/async.js.map +1 -1
  101. package/dist/esm/assertion/impl/callback.d.ts +104 -0
  102. package/dist/esm/assertion/impl/callback.d.ts.map +1 -0
  103. package/dist/esm/assertion/impl/callback.js +691 -0
  104. package/dist/esm/assertion/impl/callback.js.map +1 -0
  105. package/dist/esm/assertion/impl/index.d.ts +1 -1
  106. package/dist/esm/assertion/impl/index.d.ts.map +1 -1
  107. package/dist/esm/assertion/impl/index.js +1 -1
  108. package/dist/esm/assertion/impl/index.js.map +1 -1
  109. package/dist/esm/assertion/impl/sync-basic.d.ts.map +1 -1
  110. package/dist/esm/assertion/impl/sync-basic.js +2 -2
  111. package/dist/esm/assertion/impl/sync-basic.js.map +1 -1
  112. package/dist/esm/assertion/impl/sync-collection.d.ts +1 -1
  113. package/dist/esm/assertion/impl/sync-collection.js +3 -3
  114. package/dist/esm/assertion/impl/sync-collection.js.map +1 -1
  115. package/dist/esm/assertion/impl/sync-esoteric.js +2 -2
  116. package/dist/esm/assertion/impl/sync-esoteric.js.map +1 -1
  117. package/dist/esm/assertion/impl/sync-parametric.d.ts +22 -28
  118. package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
  119. package/dist/esm/assertion/impl/sync-parametric.js +36 -51
  120. package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
  121. package/dist/esm/assertion/impl/sync.d.ts +68 -30
  122. package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
  123. package/dist/esm/assertion/impl/sync.js +3 -1
  124. package/dist/esm/assertion/impl/sync.js.map +1 -1
  125. package/dist/esm/bootstrap.d.ts +147 -52
  126. package/dist/esm/bootstrap.d.ts.map +1 -1
  127. package/dist/esm/bootstrap.js +1 -2
  128. package/dist/esm/bootstrap.js.map +1 -1
  129. package/dist/esm/constant.d.ts +1 -1
  130. package/dist/esm/constant.d.ts.map +1 -1
  131. package/dist/esm/constant.js +7 -0
  132. package/dist/esm/constant.js.map +1 -1
  133. package/dist/esm/error.d.ts +22 -2
  134. package/dist/esm/error.d.ts.map +1 -1
  135. package/dist/esm/error.js +43 -4
  136. package/dist/esm/error.js.map +1 -1
  137. package/dist/esm/expect.d.ts.map +1 -1
  138. package/dist/esm/expect.js +2 -2
  139. package/dist/esm/expect.js.map +1 -1
  140. package/dist/esm/guards.d.ts +96 -5
  141. package/dist/esm/guards.d.ts.map +1 -1
  142. package/dist/esm/guards.js +98 -21
  143. package/dist/esm/guards.js.map +1 -1
  144. package/dist/esm/index.d.ts +146 -51
  145. package/dist/esm/index.d.ts.map +1 -1
  146. package/dist/esm/index.js.map +1 -1
  147. package/dist/esm/schema.d.ts +84 -18
  148. package/dist/esm/schema.d.ts.map +1 -1
  149. package/dist/esm/schema.js +107 -22
  150. package/dist/esm/schema.js.map +1 -1
  151. package/dist/esm/types.d.ts +171 -9
  152. package/dist/esm/types.d.ts.map +1 -1
  153. package/dist/esm/use.d.ts.map +1 -1
  154. package/dist/esm/use.js +15 -1
  155. package/dist/esm/use.js.map +1 -1
  156. package/dist/esm/util.d.ts +66 -50
  157. package/dist/esm/util.d.ts.map +1 -1
  158. package/dist/esm/util.js +153 -154
  159. package/dist/esm/util.js.map +1 -1
  160. package/dist/esm/value-to-schema.d.ts +122 -0
  161. package/dist/esm/value-to-schema.d.ts.map +1 -0
  162. package/dist/esm/value-to-schema.js +325 -0
  163. package/dist/esm/value-to-schema.js.map +1 -0
  164. package/package.json +16 -13
  165. package/src/assertion/assertion-async.ts +113 -3
  166. package/src/assertion/assertion-sync.ts +5 -2
  167. package/src/assertion/assertion-types.ts +14 -4
  168. package/src/assertion/assertion.ts +2 -17
  169. package/src/assertion/impl/async.ts +137 -93
  170. package/src/assertion/impl/callback.ts +882 -0
  171. package/src/assertion/impl/index.ts +1 -1
  172. package/src/assertion/impl/sync-basic.ts +5 -2
  173. package/src/assertion/impl/sync-collection.ts +3 -3
  174. package/src/assertion/impl/sync-esoteric.ts +2 -2
  175. package/src/assertion/impl/sync-parametric.ts +47 -54
  176. package/src/assertion/impl/sync.ts +3 -0
  177. package/src/bootstrap.ts +1 -2
  178. package/src/constant.ts +10 -0
  179. package/src/error.ts +57 -3
  180. package/src/expect.ts +6 -2
  181. package/src/guards.ts +125 -18
  182. package/src/index.ts +3 -0
  183. package/src/schema.ts +121 -23
  184. package/src/types.ts +205 -10
  185. package/src/use.ts +22 -0
  186. package/src/util.ts +168 -223
  187. package/src/value-to-schema.ts +489 -0
@@ -1,11 +1,5 @@
1
1
  /**
2
- * Utility functions for object satisfaction and shape validation.
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.
2
+ * Utility functions.
9
3
  *
10
4
  * @category API
11
5
  * @example
@@ -17,58 +11,80 @@
17
11
  * @packageDocumentation
18
12
  */
19
13
  import { type StringKeyOf } from 'type-fest';
20
- import { z } from 'zod/v4';
21
- 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';
22
15
  /**
23
- * Recursively converts an arbitrary value to a Zod v4 schema that would
24
- * 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.
25
18
  *
26
- * This function analyzes the runtime value and generates a corresponding Zod
27
- * schema that captures the value's structure and type information. It handles
28
- * primitives, objects, arrays, functions, and various built-in types, with
29
- * support for circular reference detection.
19
+ * Handles circular references by tracking visited objects to prevent infinite
20
+ * recursion.
30
21
  *
31
22
  * @example
32
23
  *
33
- * ```typescript
34
- * // Primitive types
35
- * valueToSchema('hello'); // z.string()
36
- * valueToSchema(42); // z.number()
37
- * valueToSchema(true); // z.boolean()
24
+ * ```ts
25
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] } };
38
26
  *
39
- * // Objects
40
- * valueToSchema({ name: 'John', age: 30 });
41
- * // z.object({ name: z.string(), age: z.number() })
27
+ * hasKey(obj, 'c'); // true
28
+ * hasKey(obj, 'e'); // true
29
+ * hasKey(obj, 'x'); // false (key not found)
30
+ * ```
42
31
  *
43
- * // Arrays
44
- * valueToSchema(['a', 'b', 'c']); // z.array(z.string())
45
- * valueToSchema([1, 'mixed']); // z.array(z.union([z.number(), z.string()]))
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
47
+ *
48
+ * ```ts
49
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] }, empty: {} };
46
50
  *
47
- * // Nested structures
48
- * valueToSchema({ users: [{ name: 'John' }] });
49
- * // 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)
50
56
  * ```
51
57
  *
52
- * @param value - The value to convert to a schema
53
- * @param options - Configuration options for schema generation
54
- * @param visited - Internal WeakSet for circular reference detection
55
- * @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
56
64
  */
57
- export declare const valueToSchema: (value: unknown, options?: {
58
- /** Current depth (internal) */
59
- _currentDepth?: number;
60
- /** Whether to allow mixed types in arrays (default: true) */
61
- allowMixedArrays?: boolean;
62
- /** If `true`, use `z.literal()` for primitive values instead of type schemas */
63
- literalPrimitives?: boolean;
64
- /**
65
- * If `true`, treat `RegExp` literals as `RegExp` literals; otherwise treat
66
- * as strings and attempt match
67
- */
68
- literalRegExp?: boolean;
69
- /** Maximum nesting depth to prevent stack overflow (default: 10) */
70
- maxDepth?: number;
71
- /** If `true`, will disallow unknown properties in objects */
72
- strict?: boolean;
73
- }, 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]>;
74
90
  //# sourceMappingURL=util.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAW3B,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,SAAS,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,EACnD,CAAC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AAE,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,11 +1,5 @@
1
1
  /**
2
- * Utility functions for object satisfaction and shape validation.
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.
2
+ * Utility functions.
9
3
  *
10
4
  * @category API
11
5
  * @example
@@ -16,174 +10,179 @@
16
10
  *
17
11
  * @packageDocumentation
18
12
  */
19
- import { z } from 'zod/v4';
20
- import { isNonNullObject, isPromiseLike, isString } from './guards.js';
21
- import { FunctionSchema, RegExpSchema, StrongMapSchema, StrongSetSchema, WrappedPromiseLikeSchema, } from './schema.js';
22
- export function keyBy(collection, key) {
23
- const result = {};
24
- for (const item of collection) {
25
- const keyValue = item[key];
26
- if (typeof keyValue === 'string' ||
27
- typeof keyValue === 'number' ||
28
- typeof keyValue === 'symbol') {
29
- 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;
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
+ }
30
67
  }
68
+ return false;
69
+ }
70
+ finally {
71
+ visited.delete(obj);
31
72
  }
32
- return result;
33
73
  }
34
74
  /**
35
- * Recursively converts an arbitrary value to a Zod v4 schema that would
36
- * 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.
37
77
  *
38
- * This function analyzes the runtime value and generates a corresponding Zod
39
- * schema that captures the value's structure and type information. It handles
40
- * primitives, objects, arrays, functions, and various built-in types, with
41
- * 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.
42
81
  *
43
82
  * @example
44
83
  *
45
- * ```typescript
46
- * // Primitive types
47
- * valueToSchema('hello'); // z.string()
48
- * valueToSchema(42); // z.number()
49
- * valueToSchema(true); // z.boolean()
50
- *
51
- * // Objects
52
- * valueToSchema({ name: 'John', age: 30 });
53
- * // z.object({ name: z.string(), age: z.number() })
54
- *
55
- * // Arrays
56
- * valueToSchema(['a', 'b', 'c']); // z.array(z.string())
57
- * valueToSchema([1, 'mixed']); // z.array(z.union([z.number(), z.string()]))
84
+ * ```ts
85
+ * const obj = { a: 1, b: { c: 2, d: [{ e: 3 }] }, empty: {} };
58
86
  *
59
- * // Nested structures
60
- * valueToSchema({ users: [{ name: 'John' }] });
61
- * // 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)
62
92
  * ```
63
93
  *
64
- * @param value - The value to convert to a schema
65
- * @param options - Configuration options for schema generation
66
- * @param visited - Internal WeakSet for circular reference detection
67
- * @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
68
100
  */
69
- export const valueToSchema = (value, options = {}, visited = new WeakSet()) => {
70
- const { _currentDepth = 0, allowMixedArrays = true, literalPrimitives = false, literalRegExp = false, maxDepth = 10, strict = false, } = options;
71
- // Prevent infinite recursion
72
- if (_currentDepth >= maxDepth) {
73
- return z.unknown();
101
+ export function hasValue(obj, value, visited = new WeakSet()) {
102
+ // Direct value comparison
103
+ if (obj === value) {
104
+ return true;
74
105
  }
75
- // Handle primitives
76
- if (value === null) {
77
- return z.null();
78
- }
79
- if (value === undefined) {
80
- return z.undefined();
81
- }
82
- if (Number.isNaN(value)) {
83
- 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
+ }
84
119
  }
85
- if (value === Infinity || value === -Infinity) {
86
- return z.literal(value);
120
+ // Handle primitives that can't contain nested values
121
+ if (typeof obj !== 'object' || obj === null) {
122
+ return false;
87
123
  }
88
- const valueType = typeof value;
89
- switch (valueType) {
90
- case 'bigint':
91
- return literalPrimitives ? z.literal(value) : z.bigint();
92
- case 'boolean':
93
- return literalPrimitives ? z.literal(value) : z.boolean();
94
- case 'function':
95
- return FunctionSchema;
96
- case 'number':
97
- return literalPrimitives ? z.literal(value) : z.number();
98
- case 'string':
99
- return literalPrimitives ? z.literal(value) : z.string();
100
- case 'symbol':
101
- return z.symbol();
124
+ // Prevent infinite recursion with circular references
125
+ if (visited.has(obj)) {
126
+ return false;
102
127
  }
103
- // Handle objects
104
- if (typeof value === 'object' && value !== null) {
105
- // Check for circular references
106
- if (visited.has(value)) {
107
- // Return a recursive schema reference or unknown for circular refs
108
- return z.unknown();
109
- }
110
- visited.add(value);
111
- try {
112
- // Handle built-in object types
113
- if (value instanceof Date) {
114
- return z.date();
115
- }
116
- if (value instanceof RegExp) {
117
- if (literalRegExp) {
118
- return RegExpSchema;
119
- }
120
- return z.coerce.string().regex(value);
121
- }
122
- if (value instanceof Map) {
123
- return StrongMapSchema;
124
- }
125
- if (value instanceof Set) {
126
- return StrongSetSchema;
127
- }
128
- if (value instanceof WeakMap) {
129
- return z.instanceof(WeakMap);
130
- }
131
- if (value instanceof WeakSet) {
132
- return z.instanceof(WeakSet);
133
- }
134
- if (value instanceof Error) {
135
- return z.instanceof(Error);
136
- }
137
- if (isPromiseLike(value)) {
138
- return WrappedPromiseLikeSchema;
139
- }
140
- // Handle arrays
141
- if (Array.isArray(value)) {
142
- if (value.length === 0) {
143
- return z.array(z.unknown());
144
- }
145
- const elementSchemas = value.map((item) => valueToSchema(item, {
146
- ...options,
147
- _currentDepth: _currentDepth + 1,
148
- }, visited));
149
- if (allowMixedArrays) {
150
- // Create a union of all unique element types
151
- const uniqueSchemas = Array.from(new Set(elementSchemas.map((schema) => schema.constructor.name))).map((_, index) => elementSchemas[index]);
152
- if (uniqueSchemas.length === 1) {
153
- return z.array(uniqueSchemas[0]);
154
- }
155
- else {
156
- return z.array(z.union(uniqueSchemas));
157
- }
158
- }
159
- else {
160
- // Use the first element's schema for all elements
161
- 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;
162
136
  }
163
137
  }
164
- // Handle plain objects
165
- if (isNonNullObject(value)) {
166
- const schemaShape = {};
167
- for (const [key, val] of Object.entries(value)) {
168
- if (isString(key)) {
169
- schemaShape[key] = valueToSchema(val, {
170
- ...options,
171
- _currentDepth: _currentDepth + 1,
172
- }, visited);
173
- }
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;
174
144
  }
175
- return strict
176
- ? z.strictObject(schemaShape)
177
- : z.looseObject(schemaShape);
178
145
  }
179
- // Handle other object types (ArrayBuffer, etc.)
180
- return z.custom((val) => typeof val === 'object' && val !== null, { message: 'Expected an object' });
181
146
  }
182
- finally {
183
- 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;
184
184
  }
185
185
  }
186
- // Fallback for unknown types
187
- return z.unknown();
188
- };
186
+ return result;
187
+ }
189
188
  //# sourceMappingURL=util.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;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,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,KAAc,EACd,UAgBI,EAAE,EACN,UAAU,IAAI,OAAO,EAAU,EACpB,EAAE;IACb,MAAM,EACJ,aAAa,G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C;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;AAiB3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,eAAO,MAAM,aAAa,GACxB,OAAO,OAAO,EACd,UAAS,oBAAyB,EAClC,yBAA+B,KAC9B,CAAC,CAAC,OAAO,CAAC,GAAG,CA2Uf,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"}