@octanejs/hook-form 0.1.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 (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -0
  3. package/package.json +46 -0
  4. package/src/FormProvider.tsrx +76 -0
  5. package/src/FormProvider.tsrx.d.ts +10 -0
  6. package/src/constants.ts +31 -0
  7. package/src/controller.tsrx +8 -0
  8. package/src/controller.tsrx.d.ts +52 -0
  9. package/src/form.tsrx +119 -0
  10. package/src/form.tsrx.d.ts +6 -0
  11. package/src/formStateSubscribe.tsrx +10 -0
  12. package/src/formStateSubscribe.tsrx.d.ts +16 -0
  13. package/src/index.ts +19 -0
  14. package/src/logic/appendErrors.ts +19 -0
  15. package/src/logic/createFormControl.ts +1841 -0
  16. package/src/logic/generateId.ts +14 -0
  17. package/src/logic/generateWatchOutput.ts +27 -0
  18. package/src/logic/getCheckboxValue.ts +36 -0
  19. package/src/logic/getDirtyFields.ts +120 -0
  20. package/src/logic/getEventValue.ts +12 -0
  21. package/src/logic/getFieldValue.ts +33 -0
  22. package/src/logic/getFieldValueAs.ts +22 -0
  23. package/src/logic/getFocusFieldName.ts +13 -0
  24. package/src/logic/getNodeParentName.ts +4 -0
  25. package/src/logic/getProxyFormState.ts +33 -0
  26. package/src/logic/getRadioValue.ts +24 -0
  27. package/src/logic/getResolverOptions.ts +33 -0
  28. package/src/logic/getRuleValue.ts +16 -0
  29. package/src/logic/getValidateError.ts +22 -0
  30. package/src/logic/getValidationModes.ts +11 -0
  31. package/src/logic/getValueAndMessage.ts +12 -0
  32. package/src/logic/hasPromiseValidation.ts +27 -0
  33. package/src/logic/hasValidation.ts +12 -0
  34. package/src/logic/index.ts +3 -0
  35. package/src/logic/isNameInFieldArray.ts +7 -0
  36. package/src/logic/isWatched.ts +11 -0
  37. package/src/logic/iterateFieldsByAction.ts +37 -0
  38. package/src/logic/schemaErrorLookup.ts +54 -0
  39. package/src/logic/shouldRenderFormState.ts +25 -0
  40. package/src/logic/shouldSubscribeByName.ts +18 -0
  41. package/src/logic/skipValidation.ts +21 -0
  42. package/src/logic/unsetEmptyArray.ts +6 -0
  43. package/src/logic/updateFieldArrayRootError.ts +17 -0
  44. package/src/logic/validateField.ts +273 -0
  45. package/src/types/controller.ts +98 -0
  46. package/src/types/errors.ts +56 -0
  47. package/src/types/events.ts +26 -0
  48. package/src/types/fieldArray.ts +288 -0
  49. package/src/types/fields.ts +46 -0
  50. package/src/types/form.ts +961 -0
  51. package/src/types/index.ts +12 -0
  52. package/src/types/path/common.ts +391 -0
  53. package/src/types/path/eager.ts +224 -0
  54. package/src/types/path/index.ts +16 -0
  55. package/src/types/resolvers.ts +38 -0
  56. package/src/types/utils.ts +125 -0
  57. package/src/types/validator.ts +94 -0
  58. package/src/types/watch.ts +66 -0
  59. package/src/useController.ts +268 -0
  60. package/src/useFieldArray.ts +490 -0
  61. package/src/useForm.ts +182 -0
  62. package/src/useFormContext.ts +47 -0
  63. package/src/useFormControlContext.ts +25 -0
  64. package/src/useFormState.ts +86 -0
  65. package/src/useIsomorphicLayoutEffect.ts +6 -0
  66. package/src/useWatch.ts +356 -0
  67. package/src/utils/append.ts +4 -0
  68. package/src/utils/cloneObject.ts +32 -0
  69. package/src/utils/compact.ts +2 -0
  70. package/src/utils/convertToArrayPayload.ts +2 -0
  71. package/src/utils/createSubject.ts +48 -0
  72. package/src/utils/deepEqual.ts +81 -0
  73. package/src/utils/deepMerge.ts +27 -0
  74. package/src/utils/extractFormValues.ts +28 -0
  75. package/src/utils/fillEmptyArray.ts +3 -0
  76. package/src/utils/flatten.ts +23 -0
  77. package/src/utils/get.ts +29 -0
  78. package/src/utils/index.ts +3 -0
  79. package/src/utils/insert.ts +8 -0
  80. package/src/utils/isBoolean.ts +2 -0
  81. package/src/utils/isCheckBoxInput.ts +4 -0
  82. package/src/utils/isDateObject.ts +2 -0
  83. package/src/utils/isEmptyObject.ts +7 -0
  84. package/src/utils/isFileInput.ts +4 -0
  85. package/src/utils/isFunction.ts +2 -0
  86. package/src/utils/isHTMLElement.ts +13 -0
  87. package/src/utils/isKey.ts +4 -0
  88. package/src/utils/isMultipleSelect.ts +5 -0
  89. package/src/utils/isNullOrUndefined.ts +2 -0
  90. package/src/utils/isObject.ts +8 -0
  91. package/src/utils/isPlainObject.ts +8 -0
  92. package/src/utils/isPrimitive.ts +8 -0
  93. package/src/utils/isRadioInput.ts +4 -0
  94. package/src/utils/isRadioOrCheckbox.ts +8 -0
  95. package/src/utils/isRegex.ts +2 -0
  96. package/src/utils/isString.ts +2 -0
  97. package/src/utils/isUndefined.ts +2 -0
  98. package/src/utils/isWeb.ts +4 -0
  99. package/src/utils/live.ts +6 -0
  100. package/src/utils/move.ts +15 -0
  101. package/src/utils/noop.ts +2 -0
  102. package/src/utils/objectHasFunction.ts +11 -0
  103. package/src/utils/prepend.ts +7 -0
  104. package/src/utils/remove.ts +24 -0
  105. package/src/utils/set.ts +36 -0
  106. package/src/utils/sleep.ts +2 -0
  107. package/src/utils/stringToPath.ts +4 -0
  108. package/src/utils/swap.ts +4 -0
  109. package/src/utils/unset.ts +65 -0
  110. package/src/utils/update.ts +5 -0
  111. package/src/watch.tsrx +7 -0
  112. package/src/watch.tsrx.d.ts +42 -0
@@ -0,0 +1,12 @@
1
+ // Vendored from react-hook-form@7.81.0 src/types/index.ts (octane port).
2
+ export * from './controller';
3
+ export * from './errors';
4
+ export * from './events';
5
+ export * from './fieldArray';
6
+ export * from './fields';
7
+ export * from './form';
8
+ export * from './path';
9
+ export * from './resolvers';
10
+ export * from './utils';
11
+ export * from './validator';
12
+ export * from './watch';
@@ -0,0 +1,391 @@
1
+ // Vendored from react-hook-form@7.81.0 src/types/path/common.ts (octane port).
2
+ import type { IsAny, IsNever } from '../utils';
3
+
4
+ /**
5
+ * Type alias to `string` which describes a lodash-like path through an object.
6
+ * E.g. `'foo.bar.0.baz'`
7
+ */
8
+ export type PathString = string;
9
+
10
+ /**
11
+ * Type which can be traversed through with a {@link PathString}.
12
+ * I.e. objects, arrays, and tuples
13
+ */
14
+ export type Traversable = object;
15
+
16
+ /**
17
+ * Type to query whether an array type T is a tuple type.
18
+ * @typeParam T - type which may be an array or tuple
19
+ * @example
20
+ * ```
21
+ * IsTuple<[number]> = true
22
+ * IsTuple<number[]> = false
23
+ * ```
24
+ */
25
+ export type IsTuple<T extends ReadonlyArray<any>> = number extends T['length'] ? false : true;
26
+
27
+ /**
28
+ * Type which can be used to index an array or tuple type.
29
+ */
30
+ export type ArrayKey = number;
31
+
32
+ /**
33
+ * Type which can be used to index an object.
34
+ */
35
+ export type Key = string;
36
+
37
+ /**
38
+ * Type to assert that a type is a {@link Key}.
39
+ * @typeParam T - type which may be a {@link Key}
40
+ */
41
+ export type AsKey<T> = Extract<T, Key>;
42
+
43
+ /**
44
+ * Type to convert a type to a {@link Key}.
45
+ * @typeParam T - type which may be converted to a {@link Key}
46
+ */
47
+ export type ToKey<T> = T extends ArrayKey ? `${T}` : AsKey<T>;
48
+
49
+ /**
50
+ * Type which describes a path through an object
51
+ * as a list of individual {@link Key}s.
52
+ */
53
+ export type PathTuple = Key[];
54
+
55
+ /**
56
+ * Type to assert that a type is a {@link PathTuple}.
57
+ * @typeParam T - type which may be a {@link PathTuple}
58
+ */
59
+ export type AsPathTuple<T> = Extract<T, PathTuple>;
60
+
61
+ /**
62
+ * Type to intersect a union type.
63
+ * See https://fettblog.eu/typescript-union-to-intersection/
64
+ * @typeParam U - union
65
+ * @example
66
+ * ```
67
+ * UnionToIntersection<{ foo: string } | { bar: number }>
68
+ * = { foo: string; bar: number }
69
+ * ```
70
+ */
71
+ export type UnionToIntersection<U> = (U extends any ? (_: U) => any : never) extends (
72
+ _: infer I,
73
+ ) => any
74
+ ? I
75
+ : never;
76
+
77
+ /**
78
+ * Type which appends a {@link Key} to the {@link PathTuple} only if it is not
79
+ * blank, i.e. not the empty string.
80
+ * @typeParam PT - path
81
+ * @typeParam K - key
82
+ * @example
83
+ * ```
84
+ * AppendNonBlankKey<['foo'], 'bar'> = ['foo', 'bar']
85
+ * AppendNonBlankKey<['foo'], ''> = ['foo']
86
+ * ```
87
+ */
88
+ type AppendNonBlankKey<PT extends PathTuple, K extends Key> = K extends '' ? PT : [...PT, K];
89
+
90
+ /**
91
+ * Type to implement {@link SplitPathString} tail recursively.
92
+ * @typeParam PS - remaining {@link PathString} which should be split into its
93
+ * individual {@link Key}s
94
+ * @typeParam PT - accumulator of the {@link Key}s which have been split from
95
+ * the original {@link PathString} already
96
+ */
97
+ type SplitPathStringImpl<
98
+ PS extends PathString,
99
+ PT extends PathTuple,
100
+ > = PS extends `${infer K}.${infer R}`
101
+ ? SplitPathStringImpl<R, AppendNonBlankKey<PT, K>>
102
+ : AppendNonBlankKey<PT, PS>;
103
+
104
+ /**
105
+ * Type to split a {@link PathString} into a {@link PathTuple}.
106
+ * The individual {@link Key}s may be empty strings.
107
+ * @typeParam PS - {@link PathString} which should be split into its
108
+ * individual {@link Key}s
109
+ * @example
110
+ * ```
111
+ * SplitPathString<'foo'> = ['foo']
112
+ * SplitPathString<'foo.bar.0.baz'> = ['foo', 'bar', '0', 'baz']
113
+ * SplitPathString<'.'> = []
114
+ * ```
115
+ */
116
+ export type SplitPathString<PS extends PathString> = SplitPathStringImpl<PS, []>;
117
+
118
+ /**
119
+ * Type to implement {@link JoinPathTuple} tail-recursively.
120
+ * @typeParam PT - remaining {@link Key}s which needs to be joined
121
+ * @typeParam PS - accumulator of the already joined {@link Key}s
122
+ */
123
+ type JoinPathTupleImpl<PT extends PathTuple, PS extends PathString> = PT extends [
124
+ infer K,
125
+ ...infer R,
126
+ ]
127
+ ? JoinPathTupleImpl<AsPathTuple<R>, `${PS}.${AsKey<K>}`>
128
+ : PS;
129
+
130
+ /**
131
+ * Type to join a {@link PathTuple} to a {@link PathString}.
132
+ * @typeParam PT - {@link PathTuple} which should be joined.
133
+ * @example
134
+ * ```
135
+ * JoinPathTuple<['foo']> = 'foo'
136
+ * JoinPathTuple<['foo', 'bar', '0', 'baz']> = 'foo.bar.0.baz'
137
+ * JoinPathTuple<[]> = never
138
+ * ```
139
+ */
140
+ export type JoinPathTuple<PT extends PathTuple> = PT extends [infer K, ...infer R]
141
+ ? JoinPathTupleImpl<AsPathTuple<R>, AsKey<K>>
142
+ : never;
143
+
144
+ /**
145
+ * Type which converts all keys of an object to {@link Key}s.
146
+ * @typeParam T - object type
147
+ * @example
148
+ * ```
149
+ * MapKeys<{0: string}> = {'0': string}
150
+ * ```
151
+ */
152
+ type MapKeys<T> = { [K in keyof T as ToKey<K>]: T[K] };
153
+
154
+ /**
155
+ * Type to access a type by a key.
156
+ * - Returns undefined if it can't be indexed by that key.
157
+ * - Returns null if the type is null.
158
+ * - Returns undefined if the type is not traversable.
159
+ * @typeParam T - type which is indexed by the key
160
+ * @typeParam K - key into the type
161
+ * ```
162
+ * TryAccess<{foo: string}, 'foo'> = string
163
+ * TryAccess<{foo: string}, 'bar'> = undefined
164
+ * TryAccess<null, 'foo'> = null
165
+ * TryAccess<string, 'foo'> = undefined
166
+ * ```
167
+ */
168
+ type TryAccess<T, K> = K extends keyof T ? T[K] : T extends null ? null : undefined;
169
+
170
+ /**
171
+ * Type to access an array type by a key.
172
+ * Returns undefined if the key is non-numeric.
173
+ * @typeParam T - type which is indexed by the key
174
+ * @typeParam K - key into the type
175
+ * ```
176
+ * TryAccessArray<string[], '0'> = string
177
+ * TryAccessArray<string[], 'foo'> = undefined
178
+ * ```
179
+ */
180
+ type TryAccessArray<T extends ReadonlyArray<any>, K extends Key> = K extends `${ArrayKey}`
181
+ ? T[number]
182
+ : TryAccess<T, K>;
183
+
184
+ /**
185
+ * Type to evaluate the type which the given key points to.
186
+ * @typeParam T - type which is indexed by the key
187
+ * @typeParam K - key into the type
188
+ * @example
189
+ * ```
190
+ * EvaluateKey<{foo: string}, 'foo'> = string
191
+ * EvaluateKey<[number, string], '1'> = string
192
+ * EvaluateKey<string[], '1'> = string
193
+ * ```
194
+ */
195
+ export type EvaluateKey<T, K extends Key> =
196
+ T extends ReadonlyArray<any>
197
+ ? IsTuple<T> extends true
198
+ ? TryAccess<T, K>
199
+ : TryAccessArray<T, K>
200
+ : TryAccess<MapKeys<T>, K>;
201
+
202
+ /**
203
+ * Type to evaluate the type which the given path points to.
204
+ * @typeParam T - deeply nested type which is indexed by the path
205
+ * @typeParam PT - path into the deeply nested type
206
+ * @example
207
+ * ```
208
+ * EvaluatePath<{foo: {bar: string}}, ['foo', 'bar']> = string
209
+ * EvaluatePath<[number, string], ['1']> = string
210
+ * EvaluatePath<number, []> = number
211
+ * EvaluatePath<number, ['foo']> = undefined
212
+ * ```
213
+ */
214
+ export type EvaluatePath<T, PT extends PathTuple> = PT extends [infer K, ...infer R]
215
+ ? EvaluatePath<EvaluateKey<T, AsKey<K>>, AsPathTuple<R>>
216
+ : T;
217
+
218
+ /**
219
+ * Type which given a tuple type returns its own keys, i.e. only its indices.
220
+ * @typeParam T - tuple type
221
+ * @example
222
+ * ```
223
+ * TupleKeys<[number, string]> = '0' | '1'
224
+ * ```
225
+ */
226
+ export type TupleKeys<T extends ReadonlyArray<any>> = Exclude<keyof T, keyof any[]>;
227
+
228
+ /**
229
+ * Type which extracts all numeric keys from an object.
230
+ * @typeParam T - type
231
+ * @example
232
+ * ```
233
+ * NumericObjectKeys<{0: string, '1': string, foo: string}> = '0' | '1'
234
+ * ```
235
+ */
236
+ type NumericObjectKeys<T extends Traversable> = ToKey<Extract<keyof T, ArrayKey | `${ArrayKey}`>>;
237
+
238
+ /**
239
+ * Type which extracts all numeric keys from an object, tuple, or array.
240
+ * If a union is passed, it evaluates to the overlapping numeric keys.
241
+ * @typeParam T - type
242
+ * @example
243
+ * ```
244
+ * NumericKeys<{0: string, '1': string, foo: string}> = '0' | '1'
245
+ * NumericKeys<number[]> = `${number}`
246
+ * NumericKeys<[string, number]> = '0' | '1'
247
+ * NumericKeys<{0: string, '1': string} | [number] | number[]> = '0'
248
+ * ```
249
+ */
250
+ export type NumericKeys<T extends Traversable> = UnionToIntersection<
251
+ T extends ReadonlyArray<any>
252
+ ? IsTuple<T> extends true
253
+ ? [TupleKeys<T>]
254
+ : [ToKey<ArrayKey>]
255
+ : [NumericObjectKeys<T>]
256
+ >[never];
257
+
258
+ /**
259
+ * Type which extracts all keys from an object.
260
+ * If a union is passed, it evaluates to the overlapping keys.
261
+ * @typeParam T - object type
262
+ * @example
263
+ * ```
264
+ * ObjectKeys<{foo: string, bar: string}> = 'foo' | 'bar'
265
+ * ObjectKeys<{foo: string, bar: number} | { foo: string }> = 'foo'
266
+ * ```
267
+ */
268
+ export type ObjectKeys<T extends Traversable> = Exclude<ToKey<keyof T>, `${string}.${string}` | ''>;
269
+
270
+ /**
271
+ * Type to check whether a type's property matches the constraint type
272
+ * and return its key. Converts the key to a {@link Key}.
273
+ * @typeParam T - type whose property should be checked
274
+ * @typeParam K - key of the property
275
+ * @typeParam U - constraint type
276
+ * @example
277
+ * ```
278
+ * CheckKeyConstraint<{foo: string}, 'foo', string> = 'foo'
279
+ * CheckKeyConstraint<{foo: string}, 'foo', number> = never
280
+ * CheckKeyConstraint<string[], number, string> = `${number}`
281
+ * ```
282
+ */
283
+ export type CheckKeyConstraint<T, K extends Key, U> = K extends any
284
+ ? EvaluateKey<T, K> extends U
285
+ ? K
286
+ : never
287
+ : never;
288
+
289
+ /**
290
+ * Type which evaluates to true when the type is an array or tuple or is a union
291
+ * which contains an array or tuple.
292
+ * @typeParam T - type
293
+ * @example
294
+ * ```
295
+ * ContainsIndexable<{foo: string}> = false
296
+ * ContainsIndexable<{foo: string} | number[]> = true
297
+ * ```
298
+ */
299
+ export type ContainsIndexable<T> =
300
+ IsNever<Extract<T, ReadonlyArray<any>>> extends true ? false : true;
301
+
302
+ /**
303
+ * Type to implement {@link Keys} for non-nullable values.
304
+ * @typeParam T - non-nullable type whose property should be checked
305
+ */
306
+ type KeysImpl<T> = [T] extends [Traversable]
307
+ ? ContainsIndexable<T> extends true
308
+ ? NumericKeys<T>
309
+ : ObjectKeys<T>
310
+ : never;
311
+
312
+ /**
313
+ * Type to find all properties of a type that match the constraint type
314
+ * and return their keys.
315
+ * If a union is passed, it evaluates to the overlapping keys.
316
+ * @typeParam T - type whose property should be checked
317
+ * @typeParam U - constraint type
318
+ * @example
319
+ * ```
320
+ * Keys<{foo: string, bar: string}, string> = 'foo' | 'bar'
321
+ * Keys<{foo?: string, bar?: string}> = 'foo' | 'bar'
322
+ * Keys<{foo: string, bar: number}, string> = 'foo'
323
+ * Keys<[string, number], string> = '0'
324
+ * Keys<string[], string> = `${number}`
325
+ * Keys<{0: string, '1': string} | [number] | number[]> = '0'
326
+ * ```
327
+ */
328
+ export type Keys<T, U = unknown> =
329
+ IsAny<T> extends true
330
+ ? Key
331
+ : IsNever<T> extends true
332
+ ? Key
333
+ : IsNever<NonNullable<T>> extends true
334
+ ? never
335
+ : CheckKeyConstraint<T, KeysImpl<NonNullable<T>>, U>;
336
+
337
+ /**
338
+ * Type to check whether a {@link Key} is present in a type.
339
+ * If a union of {@link Key}s is passed, all {@link Key}s have to be present
340
+ * in the type.
341
+ * @typeParam T - type which is introspected
342
+ * @typeParam K - key
343
+ * @example
344
+ * ```
345
+ * HasKey<{foo: string}, 'foo'> = true
346
+ * HasKey<{foo: string}, 'bar'> = false
347
+ * HasKey<{foo: string}, 'foo' | 'bar'> = false
348
+ * ```
349
+ */
350
+ export type HasKey<T, K extends Key> = IsNever<Exclude<K, Keys<T>>>;
351
+
352
+ /**
353
+ * Type to implement {@link ValidPathPrefix} tail recursively.
354
+ * @typeParam T - type which the path should be checked against
355
+ * @typeParam PT - path which should exist within the given type
356
+ * @typeParam VPT - accumulates the prefix of {@link Key}s which have been
357
+ * confirmed to exist already
358
+ */
359
+ type ValidPathPrefixImpl<T, PT extends PathTuple, VPT extends PathTuple> = PT extends [
360
+ infer K,
361
+ ...infer R,
362
+ ]
363
+ ? HasKey<T, AsKey<K>> extends true
364
+ ? ValidPathPrefixImpl<EvaluateKey<T, AsKey<K>>, AsPathTuple<R>, AsPathTuple<[...VPT, K]>>
365
+ : VPT
366
+ : VPT;
367
+
368
+ /**
369
+ * Type to find the longest path prefix which is still valid,
370
+ * i.e. exists within the given type.
371
+ * @typeParam T - type which the path should be checked against
372
+ * @typeParam PT - path which should exist within the given type
373
+ * @example
374
+ * ```
375
+ * ValidPathPrefix<{foo: {bar: string}}, ['foo', 'bar']> = ['foo', 'bar']
376
+ * ValidPathPrefix<{foo: {bar: string}}, ['foo', 'ba']> = ['foo']
377
+ * ```
378
+ */
379
+ export type ValidPathPrefix<T, PT extends PathTuple> = ValidPathPrefixImpl<T, PT, []>;
380
+
381
+ /**
382
+ * Type to check whether a path through a type exists.
383
+ * @typeParam T - type which the path should be checked against
384
+ * @typeParam PT - path which should exist within the given type
385
+ * @example
386
+ * ```
387
+ * HasPath<{foo: {bar: string}}, ['foo', 'bar']> = true
388
+ * HasPath<{foo: {bar: string}}, ['foo', 'ba']> = false
389
+ * ```
390
+ */
391
+ export type HasPath<T, PT extends PathTuple> = ValidPathPrefix<T, PT> extends PT ? true : false;
@@ -0,0 +1,224 @@
1
+ // Vendored from react-hook-form@7.81.0 src/types/path/eager.ts (octane port).
2
+ import type { FieldValues } from '../fields';
3
+ import type { BrowserNativeObject, IsAny, IsEqual, Primitive } from '../utils';
4
+
5
+ import type { ArrayKey, IsTuple, TupleKeys } from './common';
6
+
7
+ /**
8
+ * Helper function to break apart T1 and check if any are equal to T2
9
+ *
10
+ * See {@link IsEqual}
11
+ */
12
+ type AnyIsEqual<T1, T2> = T1 extends T2 ? (IsEqual<T1, T2> extends true ? true : never) : never;
13
+
14
+ /**
15
+ * Helper type for recursively constructing paths through a type.
16
+ * This actually constructs the strings and recurses into nested
17
+ * object types.
18
+ *
19
+ * See {@link Path}
20
+ */
21
+ type PathImpl<K extends string | number, V, TraversedTypes> = V extends
22
+ | Primitive
23
+ | BrowserNativeObject
24
+ ? `${K}`
25
+ : // Check so that we don't recurse into the same type
26
+ // by ensuring that the types are mutually assignable
27
+ // mutually required to avoid false positives of subtypes
28
+ true extends AnyIsEqual<TraversedTypes, V>
29
+ ? `${K}`
30
+ : `${K}` | `${K}.${PathInternal<V, TraversedTypes | V>}`;
31
+
32
+ /**
33
+ * Helper type for recursively constructing paths through a type.
34
+ * This obscures the internal type param TraversedTypes from exported contract.
35
+ *
36
+ * See {@link Path}
37
+ */
38
+ type PathInternal<T, TraversedTypes = T> =
39
+ T extends ReadonlyArray<infer V>
40
+ ? IsTuple<T> extends true
41
+ ? {
42
+ [K in TupleKeys<T>]-?: PathImpl<K & string, T[K], TraversedTypes>;
43
+ }[TupleKeys<T>]
44
+ : PathImpl<ArrayKey, V, TraversedTypes>
45
+ : {
46
+ [K in keyof T]-?: PathImpl<K & string, T[K], TraversedTypes>;
47
+ }[keyof T];
48
+
49
+ /**
50
+ * Type which eagerly collects all paths through a type
51
+ * @typeParam T - type which should be introspected
52
+ * @example
53
+ * ```
54
+ * Path<{foo: {bar: string}}> = 'foo' | 'foo.bar'
55
+ * ```
56
+ */
57
+ // We want to explode the union type and process each individually
58
+ // so assignable types don't leak onto the stack from the base.
59
+ export type Path<T> = T extends any ? PathInternal<T> : never;
60
+
61
+ /**
62
+ * See {@link Path}
63
+ */
64
+ export type FieldPath<TFieldValues extends FieldValues> = Path<TFieldValues>;
65
+
66
+ /**
67
+ * Helper type for recursively constructing paths through a type.
68
+ * This actually constructs the strings and recurses into nested
69
+ * object types.
70
+ *
71
+ * See {@link ArrayPath}
72
+ */
73
+ type ArrayPathImpl<K extends string | number, V, TraversedTypes> = V extends
74
+ | Primitive
75
+ | BrowserNativeObject
76
+ ? IsAny<V> extends true
77
+ ? string
78
+ : never
79
+ : V extends ReadonlyArray<infer U>
80
+ ? U extends Primitive | BrowserNativeObject
81
+ ? IsAny<V> extends true
82
+ ? string
83
+ : never
84
+ : // Check so that we don't recurse into the same type
85
+ // by ensuring that the types are mutually assignable
86
+ // mutually required to avoid false positives of subtypes
87
+ true extends AnyIsEqual<TraversedTypes, V>
88
+ ? never
89
+ : `${K}` | `${K}.${ArrayPathInternal<V, TraversedTypes | V>}`
90
+ : true extends AnyIsEqual<TraversedTypes, V>
91
+ ? never
92
+ : `${K}.${ArrayPathInternal<V, TraversedTypes | V>}`;
93
+
94
+ /**
95
+ * Helper type for recursively constructing paths through a type.
96
+ * This obscures the internal type param TraversedTypes from exported contract.
97
+ *
98
+ * See {@link ArrayPath}
99
+ */
100
+ type ArrayPathInternal<T, TraversedTypes = T> =
101
+ T extends ReadonlyArray<infer V>
102
+ ? IsTuple<T> extends true
103
+ ? {
104
+ [K in TupleKeys<T>]-?: ArrayPathImpl<K & string, T[K], TraversedTypes>;
105
+ }[TupleKeys<T>]
106
+ : ArrayPathImpl<ArrayKey, V, TraversedTypes>
107
+ : {
108
+ [K in keyof T]-?: ArrayPathImpl<K & string, T[K], TraversedTypes>;
109
+ }[keyof T];
110
+
111
+ /**
112
+ * Type which eagerly collects all paths through a type which point to an array
113
+ * type.
114
+ * @typeParam T - type which should be introspected.
115
+ * @example
116
+ * ```
117
+ * Path<{foo: {bar: string[], baz: number[]}}> = 'foo.bar' | 'foo.baz'
118
+ * ```
119
+ */
120
+ // We want to explode the union type and process each individually
121
+ // so assignable types don't leak onto the stack from the base.
122
+ export type ArrayPath<T> = T extends any ? ArrayPathInternal<T> : never;
123
+
124
+ /**
125
+ * See {@link ArrayPath}
126
+ */
127
+ export type FieldArrayPath<TFieldValues extends FieldValues> = ArrayPath<TFieldValues>;
128
+
129
+ /**
130
+ * Type to evaluate the type which the given path points to.
131
+ * @typeParam T - deeply nested type which is indexed by the path
132
+ * @typeParam P - path into the deeply nested type
133
+ * @example
134
+ * ```
135
+ * PathValue<{foo: {bar: string}}, 'foo.bar'> = string
136
+ * PathValue<[number, string], '1'> = string
137
+ * ```
138
+ */
139
+ export type PathValue<T, P extends Path<T> | ArrayPath<T>> = PathValueImpl<T, P>;
140
+
141
+ type PathValueImpl<T, P extends string> = T extends any
142
+ ? P extends `${infer K}.${infer R}`
143
+ ? K extends keyof T
144
+ ? undefined extends T[K]
145
+ ? PathValueImpl<T[K], R> | undefined
146
+ : PathValueImpl<T[K], R>
147
+ : K extends `${ArrayKey}`
148
+ ? T extends ReadonlyArray<infer V>
149
+ ? PathValueImpl<V, R>
150
+ : never
151
+ : never
152
+ : P extends keyof T
153
+ ? T[P]
154
+ : P extends `${ArrayKey}`
155
+ ? T extends ReadonlyArray<infer V>
156
+ ? V
157
+ : T extends undefined
158
+ ? undefined
159
+ : never
160
+ : never
161
+ : never;
162
+
163
+ /**
164
+ * See {@link PathValue}
165
+ */
166
+ export type FieldPathValue<
167
+ TFieldValues extends FieldValues,
168
+ TFieldPath extends FieldPath<TFieldValues>,
169
+ > = PathValue<TFieldValues, TFieldPath>;
170
+
171
+ /**
172
+ * See {@link PathValue}
173
+ */
174
+ export type FieldArrayPathValue<
175
+ TFieldValues extends FieldValues,
176
+ TFieldArrayPath extends FieldArrayPath<TFieldValues>,
177
+ > = PathValue<TFieldValues, TFieldArrayPath>;
178
+
179
+ /**
180
+ * Type to evaluate the type which the given paths point to.
181
+ * @typeParam TFieldValues - field values which are indexed by the paths
182
+ * @typeParam TPath - paths into the deeply nested field values
183
+ * @example
184
+ * ```
185
+ * FieldPathValues<{foo: {bar: string}}, ['foo', 'foo.bar']>
186
+ * = [{bar: string}, string]
187
+ * ```
188
+ */
189
+ export type FieldPathValues<
190
+ TFieldValues extends FieldValues,
191
+ TPath extends FieldPath<TFieldValues>[] | readonly FieldPath<TFieldValues>[],
192
+ > = {} & {
193
+ [K in keyof TPath]: FieldPathValue<TFieldValues, TPath[K] & FieldPath<TFieldValues>>;
194
+ };
195
+
196
+ /**
197
+ * Type which eagerly collects all paths through a fieldType that matches a give type
198
+ * @typeParam TFieldValues - field values which are indexed by the paths
199
+ * @typeParam TValue - the value you want to match into each type
200
+ * @example
201
+ * ```typescript
202
+ * FieldPathByValue<{foo: {bar: number}, baz: number, bar: string}, number>
203
+ * = 'foo.bar' | 'baz'
204
+ * ```
205
+ */
206
+ export type FieldPathByValue<TFieldValues extends FieldValues, TValue> = {
207
+ [Key in FieldPath<TFieldValues>]: FieldPathValue<TFieldValues, Key> extends TValue ? Key : never;
208
+ }[FieldPath<TFieldValues>];
209
+
210
+ /**
211
+ * Type which eagerly collects all array paths through a fieldType that matches a give type
212
+ * @typeParam TFieldValues - field values which are indexed by the paths
213
+ * @typeParam TValue - the value you want to match into each type
214
+ * @example
215
+ * ```typescript
216
+ * FieldArrayPathByValue<{foo: {bar: number}[], baz: number, bar: string}, {bar: number}[]>
217
+ * = 'foo'
218
+ * ```
219
+ */
220
+ export type FieldArrayPathByValue<TFieldValues extends FieldValues, TValue> = {
221
+ [Key in FieldArrayPath<TFieldValues>]: FieldArrayPathValue<TFieldValues, Key> extends TValue
222
+ ? Key
223
+ : never;
224
+ }[FieldArrayPath<TFieldValues>];
@@ -0,0 +1,16 @@
1
+ // Vendored from react-hook-form@7.81.0 src/types/path/index.ts (octane port).
2
+ /** Re-export public API */
3
+
4
+ export type { PathString } from './common';
5
+ export type {
6
+ ArrayPath,
7
+ FieldArrayPath,
8
+ FieldArrayPathByValue,
9
+ FieldArrayPathValue,
10
+ FieldPath,
11
+ FieldPathByValue,
12
+ FieldPathValue,
13
+ FieldPathValues,
14
+ Path,
15
+ PathValue,
16
+ } from './eager';
@@ -0,0 +1,38 @@
1
+ // Vendored from react-hook-form@7.81.0 src/types/resolvers.ts (octane port).
2
+ import type { FieldErrors } from './errors';
3
+ import type { Field, FieldName, FieldValues, InternalFieldName } from './fields';
4
+ import type { CriteriaMode } from './form';
5
+
6
+ export type ResolverSuccess<TTransformedValues> = {
7
+ values: TTransformedValues;
8
+ errors: Record<string, never>;
9
+ };
10
+
11
+ export type ResolverError<TFieldValues extends FieldValues = FieldValues> = {
12
+ values: Record<string, never>;
13
+ errors: FieldErrors<TFieldValues>;
14
+ };
15
+
16
+ export type ResolverResult<
17
+ TFieldValues extends FieldValues = FieldValues,
18
+ TTransformedValues = TFieldValues,
19
+ > = ResolverSuccess<TTransformedValues> | ResolverError<TFieldValues>;
20
+
21
+ export interface ResolverOptions<TFieldValues extends FieldValues> {
22
+ criteriaMode?: CriteriaMode;
23
+ fields: Record<InternalFieldName, Field['_f']>;
24
+ names?: FieldName<TFieldValues>[];
25
+ shouldUseNativeValidation: boolean | undefined;
26
+ }
27
+
28
+ export type Resolver<
29
+ TFieldValues extends FieldValues = FieldValues,
30
+ TContext = any,
31
+ TTransformedValues = TFieldValues,
32
+ > = (
33
+ values: TFieldValues,
34
+ context: TContext | undefined,
35
+ options: ResolverOptions<TFieldValues>,
36
+ ) =>
37
+ | Promise<ResolverResult<TFieldValues, TTransformedValues>>
38
+ | ResolverResult<TFieldValues, TTransformedValues>;