@jcoreio/zod-forms 1.2.1 → 2.0.0-beta.2

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 (257) hide show
  1. package/FieldPath.d.ts +2 -2
  2. package/FieldPath.d.ts.map +1 -1
  3. package/FieldPath.js.map +1 -1
  4. package/FieldPath.mjs.map +1 -1
  5. package/FormAction.d.ts +2 -2
  6. package/FormAction.d.ts.map +1 -1
  7. package/FormAction.js.map +1 -1
  8. package/FormAction.mjs.map +1 -1
  9. package/FormContext.d.ts +7 -6
  10. package/FormContext.d.ts.map +1 -1
  11. package/FormContext.js.map +1 -1
  12. package/FormContext.mjs.map +1 -1
  13. package/FormState.d.ts +10 -8
  14. package/FormState.d.ts.map +1 -1
  15. package/FormState.js.map +1 -1
  16. package/FormState.mjs.map +1 -1
  17. package/README.md +2 -2
  18. package/actions/arrayActions.d.ts +40 -39
  19. package/actions/arrayActions.d.ts.map +1 -1
  20. package/actions/arrayActions.js +37 -37
  21. package/actions/arrayActions.js.map +1 -1
  22. package/actions/arrayActions.mjs +29 -29
  23. package/actions/arrayActions.mjs.map +1 -1
  24. package/actions/initialize.d.ts +5 -4
  25. package/actions/initialize.d.ts.map +1 -1
  26. package/actions/initialize.js.map +1 -1
  27. package/actions/initialize.mjs.map +1 -1
  28. package/actions/setParsedValue.d.ts +12 -0
  29. package/actions/setParsedValue.d.ts.map +1 -0
  30. package/{reducers/setRawValue.js → actions/setParsedValue.js} +9 -26
  31. package/actions/setParsedValue.js.map +1 -0
  32. package/actions/setParsedValue.mjs +9 -0
  33. package/actions/setParsedValue.mjs.map +1 -0
  34. package/actions/setSubmitStatus.d.ts +3 -3
  35. package/actions/setSubmitStatus.d.ts.map +1 -1
  36. package/actions/setSubmitStatus.js.map +1 -1
  37. package/actions/setSubmitStatus.mjs.map +1 -1
  38. package/actions/setValue.d.ts +3 -5
  39. package/actions/setValue.d.ts.map +1 -1
  40. package/actions/setValue.js +3 -7
  41. package/actions/setValue.js.map +1 -1
  42. package/actions/setValue.mjs +2 -3
  43. package/actions/setValue.mjs.map +1 -1
  44. package/createFormMiddleware.d.ts.map +1 -1
  45. package/createFormMiddleware.js +7 -5
  46. package/createFormMiddleware.js.map +1 -1
  47. package/createFormMiddleware.mjs +7 -5
  48. package/createFormMiddleware.mjs.map +1 -1
  49. package/createFormProvider.d.ts.map +1 -1
  50. package/createFormProvider.js +10 -10
  51. package/createFormProvider.js.map +1 -1
  52. package/createFormProvider.mjs +7 -7
  53. package/createFormProvider.mjs.map +1 -1
  54. package/createFormReducer.js +17 -17
  55. package/createFormReducer.js.map +1 -1
  56. package/createFormReducer.mjs +17 -17
  57. package/createFormReducer.mjs.map +1 -1
  58. package/createSelectFormStatus.d.ts +1 -1
  59. package/createSelectFormStatus.d.ts.map +1 -1
  60. package/createSelectFormStatus.js +6 -6
  61. package/createSelectFormStatus.js.map +1 -1
  62. package/createSelectFormStatus.mjs +1 -1
  63. package/createSelectFormStatus.mjs.map +1 -1
  64. package/createSelectFormValues.d.ts +19 -19
  65. package/createSelectFormValues.d.ts.map +1 -1
  66. package/createSelectFormValues.js +5 -5
  67. package/createSelectFormValues.js.map +1 -1
  68. package/createSelectFormValues.mjs +3 -3
  69. package/createSelectFormValues.mjs.map +1 -1
  70. package/createZodForm.d.ts +4 -4
  71. package/index.d.ts +1 -1
  72. package/index.d.ts.map +1 -1
  73. package/index.js.map +1 -1
  74. package/index.mjs.map +1 -1
  75. package/initFormState.js +1 -1
  76. package/initFormState.js.map +1 -1
  77. package/initFormState.mjs +1 -1
  78. package/initFormState.mjs.map +1 -1
  79. package/package.json +4 -1
  80. package/reducers/addHandlers.d.ts +6 -6
  81. package/reducers/arrayInsert.d.ts.map +1 -1
  82. package/reducers/arrayInsert.js +10 -4
  83. package/reducers/arrayInsert.js.map +1 -1
  84. package/reducers/arrayInsert.mjs +6 -4
  85. package/reducers/arrayInsert.mjs.map +1 -1
  86. package/reducers/arrayInsertParsed.d.ts +7 -0
  87. package/reducers/arrayInsertParsed.d.ts.map +1 -0
  88. package/reducers/arrayInsertParsed.js +16 -0
  89. package/reducers/arrayInsertParsed.js.map +1 -0
  90. package/reducers/arrayInsertParsed.mjs +12 -0
  91. package/reducers/arrayInsertParsed.mjs.map +1 -0
  92. package/reducers/arrayPush.d.ts.map +1 -1
  93. package/reducers/arrayPush.js +6 -4
  94. package/reducers/arrayPush.js.map +1 -1
  95. package/reducers/arrayPush.mjs +2 -4
  96. package/reducers/arrayPush.mjs.map +1 -1
  97. package/reducers/arrayPushParsed.d.ts +7 -0
  98. package/reducers/{arrayUnshiftRaw.d.ts.map → arrayPushParsed.d.ts.map} +1 -1
  99. package/reducers/arrayPushParsed.js +15 -0
  100. package/reducers/arrayPushParsed.js.map +1 -0
  101. package/reducers/arrayPushParsed.mjs +11 -0
  102. package/reducers/arrayPushParsed.mjs.map +1 -0
  103. package/reducers/arraySplice.d.ts.map +1 -1
  104. package/reducers/arraySplice.js +12 -6
  105. package/reducers/arraySplice.js.map +1 -1
  106. package/reducers/arraySplice.mjs +9 -5
  107. package/reducers/arraySplice.mjs.map +1 -1
  108. package/reducers/arraySpliceParsed.d.ts +7 -0
  109. package/reducers/arraySpliceParsed.d.ts.map +1 -0
  110. package/reducers/arraySpliceParsed.js +20 -0
  111. package/reducers/arraySpliceParsed.js.map +1 -0
  112. package/reducers/arraySpliceParsed.mjs +14 -0
  113. package/reducers/arraySpliceParsed.mjs.map +1 -0
  114. package/reducers/arrayUnshift.d.ts.map +1 -1
  115. package/reducers/arrayUnshift.js +6 -4
  116. package/reducers/arrayUnshift.js.map +1 -1
  117. package/reducers/arrayUnshift.mjs +2 -4
  118. package/reducers/arrayUnshift.mjs.map +1 -1
  119. package/reducers/arrayUnshiftParsed.d.ts +7 -0
  120. package/reducers/arrayUnshiftParsed.d.ts.map +1 -0
  121. package/reducers/arrayUnshiftParsed.js +15 -0
  122. package/reducers/arrayUnshiftParsed.js.map +1 -0
  123. package/reducers/arrayUnshiftParsed.mjs +11 -0
  124. package/reducers/arrayUnshiftParsed.mjs.map +1 -0
  125. package/reducers/initialize.d.ts +12 -12
  126. package/reducers/initialize.d.ts.map +1 -1
  127. package/reducers/initialize.js +9 -9
  128. package/reducers/initialize.js.map +1 -1
  129. package/reducers/initialize.mjs +8 -8
  130. package/reducers/initialize.mjs.map +1 -1
  131. package/reducers/removeHandlers.d.ts +6 -6
  132. package/reducers/setParsedValue.d.ts +47 -0
  133. package/reducers/setParsedValue.d.ts.map +1 -0
  134. package/reducers/setParsedValue.js +41 -0
  135. package/reducers/setParsedValue.js.map +1 -0
  136. package/reducers/setParsedValue.mjs +32 -0
  137. package/reducers/setParsedValue.mjs.map +1 -0
  138. package/reducers/setSubmitStatus.d.ts +6 -6
  139. package/reducers/setValue.d.ts +9 -10
  140. package/reducers/setValue.d.ts.map +1 -1
  141. package/reducers/setValue.js +9 -15
  142. package/reducers/setValue.js.map +1 -1
  143. package/reducers/setValue.mjs +9 -15
  144. package/reducers/setValue.mjs.map +1 -1
  145. package/reducers/submitSucceeded.d.ts +6 -6
  146. package/reducers/submitSucceeded.js +2 -2
  147. package/reducers/submitSucceeded.js.map +1 -1
  148. package/reducers/submitSucceeded.mjs +2 -2
  149. package/reducers/submitSucceeded.mjs.map +1 -1
  150. package/reducers/util/updateRawArray.d.ts +2 -2
  151. package/reducers/util/updateRawArray.d.ts.map +1 -1
  152. package/reducers/util/updateRawArray.js +3 -3
  153. package/reducers/util/updateRawArray.js.map +1 -1
  154. package/reducers/util/updateRawArray.mjs +3 -3
  155. package/reducers/util/updateRawArray.mjs.map +1 -1
  156. package/src/FieldPath.ts +3 -5
  157. package/src/FormAction.ts +2 -2
  158. package/src/FormContext.ts +7 -6
  159. package/src/FormState.ts +12 -8
  160. package/src/actions/arrayActions.ts +66 -58
  161. package/src/actions/initialize.ts +3 -2
  162. package/src/actions/setParsedValue.ts +21 -0
  163. package/src/actions/setSubmitStatus.ts +1 -1
  164. package/src/actions/setValue.ts +2 -5
  165. package/src/createFormMiddleware.ts +5 -3
  166. package/src/createFormProvider.tsx +12 -9
  167. package/src/createFormReducer.ts +17 -17
  168. package/src/createSelectFormStatus.ts +5 -5
  169. package/src/createSelectFormValues.ts +2 -2
  170. package/src/index.ts +1 -1
  171. package/src/initFormState.ts +1 -1
  172. package/src/reducers/arrayInsert.ts +9 -6
  173. package/src/reducers/arrayInsertParsed.ts +19 -0
  174. package/src/reducers/arrayPush.ts +5 -7
  175. package/src/reducers/arrayPushParsed.ts +23 -0
  176. package/src/reducers/arraySplice.ts +18 -5
  177. package/src/reducers/arraySpliceParsed.ts +17 -0
  178. package/src/reducers/arrayUnshift.ts +6 -4
  179. package/src/reducers/arrayUnshiftParsed.ts +16 -0
  180. package/src/reducers/initialize.ts +10 -8
  181. package/src/reducers/setParsedValue.ts +61 -0
  182. package/src/reducers/setValue.ts +12 -21
  183. package/src/reducers/submitSucceeded.ts +1 -1
  184. package/src/reducers/util/updateRawArray.ts +5 -5
  185. package/src/useArrayField.ts +13 -12
  186. package/src/useField.ts +26 -24
  187. package/src/useHtmlField.ts +42 -42
  188. package/src/useInitialize.ts +1 -1
  189. package/src/util/maybeParse.ts +2 -2
  190. package/useArrayField.d.ts +4 -4
  191. package/useArrayField.d.ts.map +1 -1
  192. package/useArrayField.js +8 -13
  193. package/useArrayField.js.map +1 -1
  194. package/useArrayField.mjs +3 -7
  195. package/useArrayField.mjs.map +1 -1
  196. package/useField.d.ts +7 -6
  197. package/useField.d.ts.map +1 -1
  198. package/useField.js +24 -24
  199. package/useField.js.map +1 -1
  200. package/useField.mjs +18 -18
  201. package/useField.mjs.map +1 -1
  202. package/useFormValues.d.ts +4 -4
  203. package/useHtmlField.js +40 -40
  204. package/useHtmlField.js.map +1 -1
  205. package/useHtmlField.mjs +36 -36
  206. package/useHtmlField.mjs.map +1 -1
  207. package/useInitialize.d.ts.map +1 -1
  208. package/useInitialize.js +1 -1
  209. package/useInitialize.js.map +1 -1
  210. package/useInitialize.mjs +1 -1
  211. package/useInitialize.mjs.map +1 -1
  212. package/util/maybeParse.d.ts +1 -1
  213. package/util/maybeParse.d.ts.map +1 -1
  214. package/util/maybeParse.js +2 -2
  215. package/util/maybeParse.js.map +1 -1
  216. package/util/maybeParse.mjs +2 -2
  217. package/util/maybeParse.mjs.map +1 -1
  218. package/actions/setRawValue.d.ts +0 -9
  219. package/actions/setRawValue.d.ts.map +0 -1
  220. package/actions/setRawValue.js +0 -14
  221. package/actions/setRawValue.js.map +0 -1
  222. package/actions/setRawValue.mjs +0 -8
  223. package/actions/setRawValue.mjs.map +0 -1
  224. package/reducers/arrayInsertRaw.d.ts +0 -7
  225. package/reducers/arrayInsertRaw.d.ts.map +0 -1
  226. package/reducers/arrayInsertRaw.js +0 -22
  227. package/reducers/arrayInsertRaw.js.map +0 -1
  228. package/reducers/arrayInsertRaw.mjs +0 -14
  229. package/reducers/arrayInsertRaw.mjs.map +0 -1
  230. package/reducers/arrayPushRaw.d.ts +0 -7
  231. package/reducers/arrayPushRaw.d.ts.map +0 -1
  232. package/reducers/arrayPushRaw.js +0 -17
  233. package/reducers/arrayPushRaw.js.map +0 -1
  234. package/reducers/arrayPushRaw.mjs +0 -9
  235. package/reducers/arrayPushRaw.mjs.map +0 -1
  236. package/reducers/arraySpliceRaw.d.ts +0 -7
  237. package/reducers/arraySpliceRaw.d.ts.map +0 -1
  238. package/reducers/arraySpliceRaw.js +0 -26
  239. package/reducers/arraySpliceRaw.js.map +0 -1
  240. package/reducers/arraySpliceRaw.mjs +0 -18
  241. package/reducers/arraySpliceRaw.mjs.map +0 -1
  242. package/reducers/arrayUnshiftRaw.d.ts +0 -7
  243. package/reducers/arrayUnshiftRaw.js +0 -17
  244. package/reducers/arrayUnshiftRaw.js.map +0 -1
  245. package/reducers/arrayUnshiftRaw.mjs +0 -9
  246. package/reducers/arrayUnshiftRaw.mjs.map +0 -1
  247. package/reducers/setRawValue.d.ts +0 -27
  248. package/reducers/setRawValue.d.ts.map +0 -1
  249. package/reducers/setRawValue.js.map +0 -1
  250. package/reducers/setRawValue.mjs +0 -26
  251. package/reducers/setRawValue.mjs.map +0 -1
  252. package/src/actions/setRawValue.ts +0 -17
  253. package/src/reducers/arrayInsertRaw.ts +0 -22
  254. package/src/reducers/arrayPushRaw.ts +0 -18
  255. package/src/reducers/arraySpliceRaw.ts +0 -30
  256. package/src/reducers/arrayUnshiftRaw.ts +0 -18
  257. package/src/reducers/setRawValue.ts +0 -40
package/src/FormState.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import z from 'zod'
2
+ import { DeepPartial } from './util/DeepPartial'
2
3
 
3
4
  export type FieldMeta = {
4
5
  touched: boolean
@@ -6,8 +7,11 @@ export type FieldMeta = {
6
7
  }
7
8
 
8
9
  export type SubmitHandler<T extends z.ZodTypeAny> = (
9
- values: z.output<T>,
10
- options: { initialValues: z.output<T> }
10
+ parsedValues: z.output<T>,
11
+ options: {
12
+ initialValues?: DeepPartial<z.input<T>>
13
+ initialParsedValues?: z.output<T>
14
+ }
11
15
  ) => void | Promise<void>
12
16
 
13
17
  export type SubmitSuccededHandler = () => void
@@ -18,12 +22,12 @@ export type FormState<T extends z.ZodTypeAny> = {
18
22
  mounted: boolean
19
23
  initialized: boolean
20
24
  fieldMeta: Record<string, FieldMeta>
21
- rawValues?: unknown
22
- values?: z.output<T>
23
- submittedValues?: z.output<T>
24
- rawSubmittedValues?: z.input<T>
25
- rawInitialValues?: unknown
26
- initialValues?: z.output<T>
25
+ values?: DeepPartial<z.input<T>>
26
+ parsedValues?: z.output<T>
27
+ submittedParsedValues?: z.output<T>
28
+ submittedValues?: z.input<T>
29
+ initialValues?: DeepPartial<z.input<T>>
30
+ initialParsedValues?: z.output<T>
27
31
  validationError?: any
28
32
  submitPromise?: Promise<void>
29
33
  onSubmit: Set<SubmitHandler<T>>
@@ -1,40 +1,41 @@
1
- import { FieldPathForValue } from '../FieldPath'
1
+ import { FieldPathForParsedValue } from '../FieldPath'
2
2
  import z from 'zod'
3
+ import { DeepPartial } from '../util/DeepPartial'
3
4
 
4
- export type ArrayFieldPath<V = any, R = any> = FieldPathForValue<
5
+ export type ArrayFieldPath<V = any, R = any> = FieldPathForParsedValue<
5
6
  V[] | null | undefined,
6
7
  R[] | null | undefined
7
8
  >
8
9
 
9
- type ValueFor<Field extends ArrayFieldPath> = NonNullable<
10
+ type ParsedValueFor<Field extends ArrayFieldPath> = NonNullable<
10
11
  z.output<Field['schema']>
11
12
  >[number]
12
13
 
13
- type RawValueFor<Field extends ArrayFieldPath> = NonNullable<
14
- z.input<Field['schema']>
15
- >[number]
14
+ type ValueFor<Field extends ArrayFieldPath> = DeepPartial<
15
+ NonNullable<z.input<Field['schema']>>[number]
16
+ >
16
17
 
17
- export type ArrayInsertAction<Field extends ArrayFieldPath = ArrayFieldPath> =
18
- ReturnType<typeof arrayInsert<Field>>
18
+ export type ArrayInsertParsedAction<
19
+ Field extends ArrayFieldPath = ArrayFieldPath
20
+ > = ReturnType<typeof arrayInsertParsed<Field>>
19
21
 
20
- export function arrayInsert<Field extends ArrayFieldPath>(
22
+ export function arrayInsertParsed<Field extends ArrayFieldPath>(
21
23
  field: Field,
22
24
  index: number,
23
- value: ValueFor<Field>
25
+ parsedValue: ParsedValueFor<Field>
24
26
  ) {
25
- return { type: 'arrayInsert', field, index, value } as const
27
+ return { type: 'arrayInsertParsed', field, index, parsedValue } as const
26
28
  }
27
29
 
28
- export type ArrayInsertRawAction<
29
- Field extends ArrayFieldPath = ArrayFieldPath
30
- > = ReturnType<typeof arrayInsertRaw<Field>>
30
+ export type ArrayInsertAction<Field extends ArrayFieldPath = ArrayFieldPath> =
31
+ ReturnType<typeof arrayInsert<Field>>
31
32
 
32
- export function arrayInsertRaw<Field extends ArrayFieldPath>(
33
+ export function arrayInsert<Field extends ArrayFieldPath>(
33
34
  field: Field,
34
35
  index: number,
35
- rawValue: RawValueFor<Field>
36
+ value: ValueFor<Field>
36
37
  ) {
37
- return { type: 'arrayInsertRaw', field, index, rawValue } as const
38
+ return { type: 'arrayInsert', field, index, value } as const
38
39
  }
39
40
 
40
41
  export type ArrayMoveAction = ReturnType<typeof arrayMove>
@@ -49,24 +50,25 @@ export function arrayPop(field: ArrayFieldPath) {
49
50
  return { type: 'arrayPop', field } as const
50
51
  }
51
52
 
52
- export type ArrayPushAction<Field extends ArrayFieldPath = ArrayFieldPath> =
53
- ReturnType<typeof arrayPush<Field>>
53
+ export type ArrayPushParsedAction<
54
+ Field extends ArrayFieldPath = ArrayFieldPath
55
+ > = ReturnType<typeof arrayPushParsed<Field>>
54
56
 
55
- export function arrayPush<Field extends ArrayFieldPath>(
57
+ export function arrayPushParsed<Field extends ArrayFieldPath>(
56
58
  field: Field,
57
- value: ValueFor<Field>
59
+ parsedValue: ParsedValueFor<Field>
58
60
  ) {
59
- return { type: 'arrayPush', field, value } as const
61
+ return { type: 'arrayPushParsed', field, parsedValue } as const
60
62
  }
61
63
 
62
- export type ArrayPushRawAction<Field extends ArrayFieldPath = ArrayFieldPath> =
63
- ReturnType<typeof arrayPushRaw<Field>>
64
+ export type ArrayPushAction<Field extends ArrayFieldPath = ArrayFieldPath> =
65
+ ReturnType<typeof arrayPush<Field>>
64
66
 
65
- export function arrayPushRaw<Field extends ArrayFieldPath>(
67
+ export function arrayPush<Field extends ArrayFieldPath>(
66
68
  field: Field,
67
- rawValue: RawValueFor<Field>
69
+ value: ValueFor<Field>
68
70
  ) {
69
- return { type: 'arrayPushRaw', field, rawValue } as const
71
+ return { type: 'arrayPush', field, value } as const
70
72
  }
71
73
 
72
74
  export type ArrayRemoveAction = ReturnType<typeof arrayRemove>
@@ -87,34 +89,40 @@ export function arrayShift(field: ArrayFieldPath) {
87
89
  return { type: 'arrayShift', field } as const
88
90
  }
89
91
 
90
- export type ArraySpliceAction<Field extends ArrayFieldPath = ArrayFieldPath> =
91
- ReturnType<typeof arraySplice<Field>>
92
+ export type ArraySpliceParsedAction<
93
+ Field extends ArrayFieldPath = ArrayFieldPath
94
+ > = ReturnType<typeof arraySpliceParsed<Field>>
92
95
 
93
- export function arraySplice<Field extends ArrayFieldPath>(
96
+ export function arraySpliceParsed<Field extends ArrayFieldPath>(
94
97
  field: Field,
95
98
  index: number,
96
99
  deleteCount: number,
97
- ...values: ValueFor<Field>[]
100
+ ...parsedValues: ParsedValueFor<Field>[]
98
101
  ) {
99
- return { type: 'arraySplice', field, index, deleteCount, values } as const
102
+ return {
103
+ type: 'arraySpliceParsed',
104
+ field,
105
+ index,
106
+ deleteCount,
107
+ parsedValues,
108
+ } as const
100
109
  }
101
110
 
102
- export type ArraySpliceRawAction<
103
- Field extends ArrayFieldPath = ArrayFieldPath
104
- > = ReturnType<typeof arraySpliceRaw<Field>>
111
+ export type ArraySpliceAction<Field extends ArrayFieldPath = ArrayFieldPath> =
112
+ ReturnType<typeof arraySplice<Field>>
105
113
 
106
- export function arraySpliceRaw<Field extends ArrayFieldPath>(
114
+ export function arraySplice<Field extends ArrayFieldPath>(
107
115
  field: Field,
108
116
  index: number,
109
117
  deleteCount: number,
110
- ...rawValues: RawValueFor<Field>[]
118
+ ...values: ValueFor<Field>[]
111
119
  ) {
112
120
  return {
113
- type: 'arraySpliceRaw',
121
+ type: 'arraySplice',
114
122
  field,
115
123
  index,
116
124
  deleteCount,
117
- rawValues,
125
+ values,
118
126
  } as const
119
127
  }
120
128
 
@@ -128,56 +136,56 @@ export function arraySwap(
128
136
  return { type: 'arraySwap', field, indexA, indexB } as const
129
137
  }
130
138
 
131
- export type ArrayUnshiftAction<Field extends ArrayFieldPath = ArrayFieldPath> =
132
- ReturnType<typeof arrayUnshift<Field>>
139
+ export type ArrayUnshiftParsedAction<
140
+ Field extends ArrayFieldPath = ArrayFieldPath
141
+ > = ReturnType<typeof arrayUnshiftParsed<Field>>
133
142
 
134
- export function arrayUnshift<Field extends ArrayFieldPath>(
143
+ export function arrayUnshiftParsed<Field extends ArrayFieldPath>(
135
144
  field: Field,
136
- value: ValueFor<Field>
145
+ parsedValue: ParsedValueFor<Field>
137
146
  ) {
138
- return { type: 'arrayUnshift', field, value } as const
147
+ return { type: 'arrayUnshiftParsed', field, parsedValue } as const
139
148
  }
140
149
 
141
- export type ArrayUnshiftRawAction<
142
- Field extends ArrayFieldPath = ArrayFieldPath
143
- > = ReturnType<typeof arrayUnshiftRaw<Field>>
150
+ export type ArrayUnshiftAction<Field extends ArrayFieldPath = ArrayFieldPath> =
151
+ ReturnType<typeof arrayUnshift<Field>>
144
152
 
145
- export function arrayUnshiftRaw<Field extends ArrayFieldPath>(
153
+ export function arrayUnshift<Field extends ArrayFieldPath>(
146
154
  field: Field,
147
- rawValue: RawValueFor<Field>
155
+ value: ValueFor<Field>
148
156
  ) {
149
- return { type: 'arrayUnshiftRaw', field, rawValue } as const
157
+ return { type: 'arrayUnshift', field, value } as const
150
158
  }
151
159
 
152
160
  export type ArrayAction<Field extends ArrayFieldPath = ArrayFieldPath> =
161
+ | ArrayInsertParsedAction<Field>
153
162
  | ArrayInsertAction<Field>
154
- | ArrayInsertRawAction<Field>
155
163
  | ArrayMoveAction
156
164
  | ArrayPopAction
165
+ | ArrayPushParsedAction<Field>
157
166
  | ArrayPushAction<Field>
158
- | ArrayPushRawAction<Field>
159
167
  | ArrayRemoveAction
160
168
  | ArrayRemoveAllAction
161
169
  | ArrayShiftAction
170
+ | ArraySpliceParsedAction<Field>
162
171
  | ArraySpliceAction<Field>
163
- | ArraySpliceRawAction<Field>
164
172
  | ArraySwapAction
173
+ | ArrayUnshiftParsedAction<Field>
165
174
  | ArrayUnshiftAction<Field>
166
- | ArrayUnshiftRawAction<Field>
167
175
 
168
176
  export const arrayActions = {
177
+ insertParsed: arrayInsertParsed,
169
178
  insert: arrayInsert,
170
- insertRaw: arrayInsertRaw,
171
179
  move: arrayMove,
172
180
  pop: arrayPop,
181
+ pushParsed: arrayPushParsed,
173
182
  push: arrayPush,
174
- pushRaw: arrayPushRaw,
175
183
  remove: arrayRemove,
176
184
  removeAll: arrayRemoveAll,
177
185
  shift: arrayShift,
186
+ spliceParsed: arraySpliceParsed,
178
187
  splice: arraySplice,
179
- spliceRaw: arraySpliceRaw,
180
188
  swap: arraySwap,
189
+ unshiftParsed: arrayUnshiftParsed,
181
190
  unshift: arrayUnshift,
182
- unshiftRaw: arrayUnshiftRaw,
183
191
  }
@@ -1,12 +1,13 @@
1
1
  import z from 'zod'
2
+ import { DeepPartial } from '../util/DeepPartial'
2
3
 
3
4
  export type InitializeAction<T extends z.ZodTypeAny> = ReturnType<
4
5
  typeof initialize<T>
5
6
  >
6
7
 
7
8
  export function initialize<T extends z.ZodTypeAny>(props: {
8
- rawValues?: z.input<T>
9
- values?: z.output<T>
9
+ values?: DeepPartial<z.input<T>>
10
+ parsedValues?: z.output<T>
10
11
  keepSubmitSucceeded?: boolean
11
12
  }) {
12
13
  return {
@@ -0,0 +1,21 @@
1
+ import z from 'zod'
2
+ import { FieldPath } from '../FieldPath'
3
+
4
+ export type SetParsedValueAction<Field extends FieldPath> = ReturnType<
5
+ typeof setParsedValue<Field>
6
+ >
7
+
8
+ export function setParsedValue<Field extends FieldPath>(
9
+ field: Field,
10
+ parsedValue: z.output<Field['schema']>,
11
+ options?: {
12
+ normalize?: boolean
13
+ }
14
+ ) {
15
+ return {
16
+ type: 'setParsedValue',
17
+ field,
18
+ parsedValue,
19
+ ...options,
20
+ } as const
21
+ }
@@ -14,8 +14,8 @@ export function setSubmitStatus<T extends z.ZodTypeAny>(
14
14
  | 'submitPromise'
15
15
  | 'submitSucceeded'
16
16
  | 'submitFailed'
17
+ | 'submittedParsedValues'
17
18
  | 'submittedValues'
18
- | 'rawSubmittedValues'
19
19
  >
20
20
  >
21
21
  ) {
@@ -1,5 +1,6 @@
1
1
  import z from 'zod'
2
2
  import { FieldPath } from '../FieldPath'
3
+ import { DeepPartial } from '../util/DeepPartial'
3
4
 
4
5
  export type SetValueAction<Field extends FieldPath> = ReturnType<
5
6
  typeof setValue<Field>
@@ -7,15 +8,11 @@ export type SetValueAction<Field extends FieldPath> = ReturnType<
7
8
 
8
9
  export function setValue<Field extends FieldPath>(
9
10
  field: Field,
10
- value: z.output<Field['schema']>,
11
- options?: {
12
- normalize?: boolean
13
- }
11
+ value: DeepPartial<z.input<Field['schema']>>
14
12
  ) {
15
13
  return {
16
14
  type: 'setValue',
17
15
  field,
18
16
  value,
19
- ...options,
20
17
  } as const
21
18
  }
@@ -22,19 +22,21 @@ export function createFormMiddleware<T extends z.ZodTypeAny>(): Middleware<
22
22
  onSubmit,
23
23
  onSubmitSucceeded,
24
24
  onSubmitFailed,
25
+ parsedValues,
25
26
  values,
26
- rawValues,
27
27
  initialValues,
28
+ initialParsedValues,
28
29
  } = nextState
29
30
  const submitPromise = (async () => {
30
31
  if (nextState.validationError) throw nextState.validationError
31
- for (const fn of onSubmit) await fn(values, { initialValues })
32
+ for (const fn of onSubmit)
33
+ await fn(parsedValues, { initialValues, initialParsedValues })
32
34
  })()
33
35
  store.dispatch(
34
36
  setSubmitStatus({
35
37
  submitting: true,
38
+ submittedParsedValues: parsedValues,
36
39
  submittedValues: values,
37
- rawSubmittedValues: rawValues,
38
40
  submitError: undefined,
39
41
  submitSucceeded: false,
40
42
  submitFailed: false,
@@ -7,8 +7,8 @@ import { FormAction } from './FormAction'
7
7
  import { FormState } from './FormState'
8
8
  import { Provider } from 'react-redux'
9
9
  import { initialize } from './actions/initialize'
10
- import { setRawValue } from './actions/setRawValue'
11
10
  import { setValue } from './actions/setValue'
11
+ import { setParsedValue } from './actions/setParsedValue'
12
12
  import { FormContext, FormContextProps } from './FormContext'
13
13
  import { FormStateContext } from './FormStateContext'
14
14
  import { createFormMiddleware } from './createFormMiddleware'
@@ -41,14 +41,17 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
41
41
  const store = storeRef.current
42
42
  const { dispatch } = store
43
43
 
44
+ const getParsedValues = React.useCallback(
45
+ () => store.getState().parsedValues,
46
+ []
47
+ )
44
48
  const getValues = React.useCallback(() => store.getState().values, [])
45
- const getRawValues = React.useCallback(() => store.getState().rawValues, [])
46
- const getInitialValues = React.useCallback(
47
- () => store.getState().initialValues,
49
+ const getInitialParsedValues = React.useCallback(
50
+ () => store.getState().initialParsedValues,
48
51
  []
49
52
  )
50
- const getRawInitialValues = React.useCallback(
51
- () => store.getState().rawInitialValues,
53
+ const getInitialValues = React.useCallback(
54
+ () => store.getState().initialValues,
52
55
  []
53
56
  )
54
57
  const getStatus = React.useCallback(
@@ -65,10 +68,10 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
65
68
  const formContext = React.useMemo(
66
69
  (): FormContextProps<T> => ({
67
70
  ...props,
71
+ getParsedValues,
68
72
  getValues,
69
- getRawValues,
73
+ getInitialParsedValues,
70
74
  getInitialValues,
71
- getRawInitialValues,
72
75
  getStatus,
73
76
  ...bindActionCreators(
74
77
  {
@@ -78,8 +81,8 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
78
81
  submit,
79
82
  setSubmitStatus: setSubmitStatus<T>,
80
83
  setMeta: setMeta as any,
81
- setRawValue: setRawValue as any,
82
84
  setValue: setValue as any,
85
+ setParsedValue: setParsedValue as any,
83
86
  },
84
87
  dispatch
85
88
  ),
@@ -5,8 +5,8 @@ import { FormState } from './FormState'
5
5
  import { initFormState } from './initFormState'
6
6
  import { addHandlersReducer } from './reducers/addHandlers'
7
7
  import { removeHandlersReducer } from './reducers/removeHandlers'
8
+ import { createSetParsedValueReducer } from './reducers/setParsedValue'
8
9
  import { createSetValueReducer } from './reducers/setValue'
9
- import { createSetRawValueReducer } from './reducers/setRawValue'
10
10
  import { setMetaReducer } from './reducers/setMeta'
11
11
  import { submitSucceededReducer } from './reducers/submitSucceeded'
12
12
  import { setSubmitStatusReducer } from './reducers/setSubmitStatus'
@@ -17,22 +17,22 @@ import { arrayRemoveReducer } from './reducers/arrayRemove'
17
17
  import { arrayRemoveAllReducer } from './reducers/arrayRemoveAll'
18
18
  import { arrayShiftReducer } from './reducers/arrayShift'
19
19
  import { arraySwapReducer } from './reducers/arraySwap'
20
- import { arrayPushRawReducer } from './reducers/arrayPushRaw'
21
- import { arrayInsertRawReducer } from './reducers/arrayInsertRaw'
22
- import { arraySpliceRawReducer } from './reducers/arraySpliceRaw'
23
- import { arrayUnshiftRawReducer } from './reducers/arrayUnshiftRaw'
24
20
  import { arrayPushReducer } from './reducers/arrayPush'
25
- import { arrayUnshiftReducer } from './reducers/arrayUnshift'
26
21
  import { arrayInsertReducer } from './reducers/arrayInsert'
27
22
  import { arraySpliceReducer } from './reducers/arraySplice'
23
+ import { arrayUnshiftReducer } from './reducers/arrayUnshift'
24
+ import { arrayPushParsedReducer } from './reducers/arrayPushParsed'
25
+ import { arrayUnshiftParsedReducer } from './reducers/arrayUnshiftParsed'
26
+ import { arrayInsertParsedReducer } from './reducers/arrayInsertParsed'
27
+ import { arraySpliceParsedReducer } from './reducers/arraySpliceParsed'
28
28
 
29
29
  export function createFormReducer<T extends z.ZodTypeAny>(options: {
30
30
  schema: T
31
31
  inverseSchema: z.ZodType<z.input<T>, any, z.output<T>>
32
32
  }): Reducer<FormState<T>, FormAction<T>> {
33
33
  const initializeReducer = createInitializeReducer(options)
34
+ const setParsedValueReducer = createSetParsedValueReducer(options)
34
35
  const setValueReducer = createSetValueReducer(options)
35
- const setRawValueReducer = createSetRawValueReducer(options)
36
36
  const formReducer = (
37
37
  state: FormState<T> = initFormState(),
38
38
  action: FormAction<T>
@@ -50,40 +50,40 @@ export function createFormReducer<T extends z.ZodTypeAny>(options: {
50
50
  return setSubmitStatusReducer(state, action)
51
51
  case 'submitSucceeded':
52
52
  return submitSucceededReducer(state)
53
+ case 'setParsedValue':
54
+ return setParsedValueReducer(state, action)
53
55
  case 'setValue':
54
56
  return setValueReducer(state, action)
55
- case 'setRawValue':
56
- return setRawValueReducer(state, action)
57
57
  case 'setMeta':
58
58
  return setMetaReducer(state, action)
59
+ case 'arrayInsertParsed':
60
+ return arrayInsertParsedReducer(formReducer, state, action)
59
61
  case 'arrayInsert':
60
62
  return arrayInsertReducer(formReducer, state, action)
61
- case 'arrayInsertRaw':
62
- return arrayInsertRawReducer(formReducer, state, action)
63
63
  case 'arrayMove':
64
64
  return arrayMoveReducer(formReducer, state, action)
65
65
  case 'arrayPop':
66
66
  return arrayPopReducer(formReducer, state, action)
67
+ case 'arrayPushParsed':
68
+ return arrayPushParsedReducer(formReducer, state, action)
67
69
  case 'arrayPush':
68
70
  return arrayPushReducer(formReducer, state, action)
69
- case 'arrayPushRaw':
70
- return arrayPushRawReducer(formReducer, state, action)
71
71
  case 'arrayRemove':
72
72
  return arrayRemoveReducer(formReducer, state, action)
73
73
  case 'arrayRemoveAll':
74
74
  return arrayRemoveAllReducer(formReducer, state, action)
75
75
  case 'arrayShift':
76
76
  return arrayShiftReducer(formReducer, state, action)
77
+ case 'arraySpliceParsed':
78
+ return arraySpliceParsedReducer(formReducer, state, action)
77
79
  case 'arraySplice':
78
80
  return arraySpliceReducer(formReducer, state, action)
79
- case 'arraySpliceRaw':
80
- return arraySpliceRawReducer(formReducer, state, action)
81
81
  case 'arraySwap':
82
82
  return arraySwapReducer(formReducer, state, action)
83
+ case 'arrayUnshiftParsed':
84
+ return arrayUnshiftParsedReducer(formReducer, state, action)
83
85
  case 'arrayUnshift':
84
86
  return arrayUnshiftReducer(formReducer, state, action)
85
- case 'arrayUnshiftRaw':
86
- return arrayUnshiftRawReducer(formReducer, state, action)
87
87
  }
88
88
  return state
89
89
  }
@@ -22,20 +22,20 @@ export function createSelectFormStatus() {
22
22
  [
23
23
  createSelector(
24
24
  [
25
- (state: FormState<any>) => state.values,
26
- (state: FormState<any>) => state.initialValues,
25
+ (state: FormState<any>) => state.parsedValues,
26
+ (state: FormState<any>) => state.initialParsedValues,
27
27
  ],
28
28
  isEqual
29
29
  ),
30
30
  createSelector(
31
31
  [
32
- (state: FormState<any>) => state.rawValues,
33
- (state: FormState<any>) => state.rawInitialValues,
32
+ (state: FormState<any>) => state.values,
33
+ (state: FormState<any>) => state.initialValues,
34
34
  ],
35
35
  isEqual
36
36
  ),
37
37
  ],
38
- (pristine, rawPristine) => pristine && rawPristine
38
+ (parsedPristine, pristine) => parsedPristine && pristine
39
39
  )
40
40
 
41
41
  return createStructuredSelector({
@@ -8,9 +8,9 @@ export type SelectFormValues<T extends z.ZodTypeAny> = ReturnType<
8
8
 
9
9
  export function createSelectFormValues<T extends z.ZodTypeAny>() {
10
10
  return createStructuredSelector({
11
+ parsedValues: (state: FormState<T>) => state.parsedValues,
11
12
  values: (state: FormState<T>) => state.values,
12
- rawValues: (state: FormState<T>) => state.rawValues,
13
+ initialParsedValues: (state: FormState<T>) => state.initialParsedValues,
13
14
  initialValues: (state: FormState<T>) => state.initialValues,
14
- rawInitialValues: (state: FormState<T>) => state.rawInitialValues,
15
15
  })
16
16
  }
package/src/index.ts CHANGED
@@ -7,8 +7,8 @@ export {
7
7
  } from './FormState'
8
8
  export {
9
9
  FieldPath,
10
+ type FieldPathForParsedValue,
10
11
  type FieldPathForValue,
11
- type FieldPathForRawValue,
12
12
  } from './FieldPath'
13
13
  export { useField, type UseFieldProps, type TypedUseField } from './useField'
14
14
  export {
@@ -6,8 +6,8 @@ export function initFormState<T extends z.ZodTypeAny>(): FormState<T> {
6
6
  mounted: true,
7
7
  initialized: false,
8
8
  fieldMeta: {},
9
- rawInitialValues: undefined,
10
9
  initialValues: undefined,
10
+ initialParsedValues: undefined,
11
11
  submitting: false,
12
12
  submitFailed: false,
13
13
  submitSucceeded: false,
@@ -1,9 +1,9 @@
1
1
  import z from 'zod'
2
2
  import { FormState } from '../FormState'
3
- import { arrayInsertRaw, ArrayInsertAction } from '../actions/arrayActions'
3
+ import { ArrayInsertAction } from '../actions/arrayActions'
4
4
  import { Reducer } from 'redux'
5
5
  import { FormAction } from '../FormAction'
6
- import { getInverseArrayElementSchema } from './util/getInverseArrayElementSchema'
6
+ import { updateRawArray } from './util/updateRawArray'
7
7
 
8
8
  export function arrayInsertReducer<T extends z.ZodTypeAny>(
9
9
  reducer: Reducer<FormState<T>, FormAction<T>>,
@@ -11,9 +11,12 @@ export function arrayInsertReducer<T extends z.ZodTypeAny>(
11
11
  action: ArrayInsertAction
12
12
  ) {
13
13
  const { field, index, value } = action
14
- const inverseSchema = getInverseArrayElementSchema(field.schema)
15
- return reducer(
16
- state,
17
- arrayInsertRaw(field, index, inverseSchema.parse(value))
14
+ return updateRawArray(reducer, state, field, (array) =>
15
+ insert(array ?? [], index, value)
18
16
  )
19
17
  }
18
+
19
+ function insert<T>(array: T[], index: number, parsedValue: T) {
20
+ if (index < 0 || index > array.length) throw new Error(`index out of range`)
21
+ return [...array.slice(0, index), parsedValue, ...array.slice(index)]
22
+ }
@@ -0,0 +1,19 @@
1
+ import z from 'zod'
2
+ import { FormState } from '../FormState'
3
+ import { arrayInsert, ArrayInsertParsedAction } from '../actions/arrayActions'
4
+ import { Reducer } from 'redux'
5
+ import { FormAction } from '../FormAction'
6
+ import { getInverseArrayElementSchema } from './util/getInverseArrayElementSchema'
7
+
8
+ export function arrayInsertParsedReducer<T extends z.ZodTypeAny>(
9
+ reducer: Reducer<FormState<T>, FormAction<T>>,
10
+ state: FormState<T>,
11
+ action: ArrayInsertParsedAction
12
+ ) {
13
+ const { field, index, parsedValue } = action
14
+ const inverseSchema = getInverseArrayElementSchema(field.schema)
15
+ return reducer(
16
+ state,
17
+ arrayInsert(field, index, inverseSchema.parse(parsedValue))
18
+ )
19
+ }
@@ -3,8 +3,7 @@ import { FormState } from '../FormState'
3
3
  import { ArrayPushAction } from '../actions/arrayActions'
4
4
  import { Reducer } from 'redux'
5
5
  import { FormAction } from '../FormAction'
6
- import { setValue } from '../actions/setValue'
7
- import { get } from '../util/get'
6
+ import { updateRawArray } from './util/updateRawArray'
8
7
 
9
8
  export function arrayPushReducer<T extends z.ZodTypeAny>(
10
9
  reducer: Reducer<FormState<T>, FormAction<T>>,
@@ -12,9 +11,8 @@ export function arrayPushReducer<T extends z.ZodTypeAny>(
12
11
  action: ArrayPushAction
13
12
  ) {
14
13
  const { field, value } = action
15
- const array = get(state.rawValues, field.path)
16
- return reducer(
17
- state,
18
- setValue(field.get([Array.isArray(array) ? array.length : 0]), value)
19
- )
14
+ return updateRawArray(reducer, state, field, (array) => [
15
+ ...(array ?? []),
16
+ value,
17
+ ])
20
18
  }