@schema-engine/forms 0.0.0-stage → 0.5.1

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 (614) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -2
  3. package/dist/actions/index.d.ts +3 -0
  4. package/dist/actions/index.js +50 -0
  5. package/dist/actions/index.js.map +7 -0
  6. package/dist/actions/plugins/auto-save.plugin.d.ts +39 -0
  7. package/dist/actions/plugins/auto-save.plugin.js +10 -0
  8. package/dist/actions/plugins/auto-save.plugin.js.map +7 -0
  9. package/dist/actions/plugins/form-reset.plugin.d.ts +23 -0
  10. package/dist/actions/plugins/form-reset.plugin.js +9 -0
  11. package/dist/actions/plugins/form-reset.plugin.js.map +7 -0
  12. package/dist/actions/plugins/form-submit.plugin.d.ts +31 -0
  13. package/dist/actions/plugins/form-submit.plugin.js +9 -0
  14. package/dist/actions/plugins/form-submit.plugin.js.map +7 -0
  15. package/dist/actions/plugins/form-submit.registration.d.ts +22 -0
  16. package/dist/actions/plugins/form-submit.registration.js +8 -0
  17. package/dist/actions/plugins/form-submit.registration.js.map +7 -0
  18. package/dist/actions/plugins/gtm.plugin.d.ts +30 -0
  19. package/dist/actions/plugins/gtm.plugin.js +8 -0
  20. package/dist/actions/plugins/gtm.plugin.js.map +7 -0
  21. package/dist/actions/plugins/index.d.ts +16 -0
  22. package/dist/actions/plugins/index.js +45 -0
  23. package/dist/actions/plugins/index.js.map +7 -0
  24. package/dist/actions/plugins/notification.plugin.d.ts +32 -0
  25. package/dist/actions/plugins/notification.plugin.js +9 -0
  26. package/dist/actions/plugins/notification.plugin.js.map +7 -0
  27. package/dist/actions/plugins/notification.registration.d.ts +22 -0
  28. package/dist/actions/plugins/notification.registration.js +8 -0
  29. package/dist/actions/plugins/notification.registration.js.map +7 -0
  30. package/dist/actions/plugins/registrations.d.ts +17 -0
  31. package/dist/actions/plugins/registrations.js +14 -0
  32. package/dist/actions/plugins/registrations.js.map +7 -0
  33. package/dist/actions/triggers.d.ts +42 -0
  34. package/dist/actions/triggers.js +6 -0
  35. package/dist/actions/triggers.js.map +7 -0
  36. package/dist/actions/types.d.ts +16 -0
  37. package/dist/actions/types.js +1 -0
  38. package/dist/actions/types.js.map +7 -0
  39. package/dist/agent-tool/index.d.ts +54 -0
  40. package/dist/agent-tool/index.js +128 -0
  41. package/dist/agent-tool/index.js.map +7 -0
  42. package/dist/browser/action-registrations.d.ts +46 -0
  43. package/dist/browser/action-registrations.js +19 -0
  44. package/dist/browser/action-registrations.js.map +7 -0
  45. package/dist/browser/index.d.ts +4 -0
  46. package/dist/browser/index.js +36 -0
  47. package/dist/browser/index.js.map +7 -0
  48. package/dist/browser/tools/auto-save-storage.d.ts +20 -0
  49. package/dist/browser/tools/auto-save-storage.js +12 -0
  50. package/dist/browser/tools/auto-save-storage.js.map +7 -0
  51. package/dist/browser/tools/gdpr-consent.d.ts +32 -0
  52. package/dist/browser/tools/gdpr-consent.js +11 -0
  53. package/dist/browser/tools/gdpr-consent.js.map +7 -0
  54. package/dist/browser/tools/gtm-tracker.d.ts +7 -0
  55. package/dist/browser/tools/gtm-tracker.js +7 -0
  56. package/dist/browser/tools/gtm-tracker.js.map +7 -0
  57. package/dist/builder/form-authoring.d.ts +159 -0
  58. package/dist/builder/form-authoring.js +1 -0
  59. package/dist/builder/form-authoring.js.map +7 -0
  60. package/dist/builder/form-values.d.ts +158 -0
  61. package/dist/builder/form-values.js +0 -0
  62. package/dist/builder/form-values.js.map +7 -0
  63. package/dist/builder/index.d.ts +5 -0
  64. package/dist/builder/index.js +2 -0
  65. package/dist/builder/index.js.map +7 -0
  66. package/dist/builder/types.utils.d.ts +137 -0
  67. package/dist/builder/types.utils.js +1 -0
  68. package/dist/builder/types.utils.js.map +7 -0
  69. package/dist/chunk-237BHPDH.js +88 -0
  70. package/dist/chunk-237BHPDH.js.map +7 -0
  71. package/dist/chunk-25PLQDKN.js +45 -0
  72. package/dist/chunk-25PLQDKN.js.map +7 -0
  73. package/dist/chunk-2XWISGGM.js +8 -0
  74. package/dist/chunk-2XWISGGM.js.map +7 -0
  75. package/dist/chunk-2YKQAVI2.js +8 -0
  76. package/dist/chunk-2YKQAVI2.js.map +7 -0
  77. package/dist/chunk-3B7CJOF6.js +18 -0
  78. package/dist/chunk-3B7CJOF6.js.map +7 -0
  79. package/dist/chunk-3YLRJVVN.js +17 -0
  80. package/dist/chunk-3YLRJVVN.js.map +7 -0
  81. package/dist/chunk-4CUGHU36.js +58 -0
  82. package/dist/chunk-4CUGHU36.js.map +7 -0
  83. package/dist/chunk-4FELFUCD.js +0 -0
  84. package/dist/chunk-4FELFUCD.js.map +7 -0
  85. package/dist/chunk-4XPLSYTK.js +82 -0
  86. package/dist/chunk-4XPLSYTK.js.map +7 -0
  87. package/dist/chunk-5Y6U3MR3.js +54 -0
  88. package/dist/chunk-5Y6U3MR3.js.map +7 -0
  89. package/dist/chunk-6BAJHAGL.js +29 -0
  90. package/dist/chunk-6BAJHAGL.js.map +7 -0
  91. package/dist/chunk-6EYX257T.js +23 -0
  92. package/dist/chunk-6EYX257T.js.map +7 -0
  93. package/dist/chunk-6FUGK5HQ.js +26 -0
  94. package/dist/chunk-6FUGK5HQ.js.map +7 -0
  95. package/dist/chunk-6IZYNHZ3.js +118 -0
  96. package/dist/chunk-6IZYNHZ3.js.map +7 -0
  97. package/dist/chunk-6NV2JDGI.js +8 -0
  98. package/dist/chunk-6NV2JDGI.js.map +7 -0
  99. package/dist/chunk-6R2MBQPL.js +19 -0
  100. package/dist/chunk-6R2MBQPL.js.map +7 -0
  101. package/dist/chunk-7EQJUI5N.js +40 -0
  102. package/dist/chunk-7EQJUI5N.js.map +7 -0
  103. package/dist/chunk-A5VLMQAK.js +2780 -0
  104. package/dist/chunk-A5VLMQAK.js.map +7 -0
  105. package/dist/chunk-AUZ45XPE.js +27 -0
  106. package/dist/chunk-AUZ45XPE.js.map +7 -0
  107. package/dist/chunk-BIHEIJG3.js +0 -0
  108. package/dist/chunk-BIHEIJG3.js.map +7 -0
  109. package/dist/chunk-BKNWW4PK.js +0 -0
  110. package/dist/chunk-BKNWW4PK.js.map +7 -0
  111. package/dist/chunk-BLFLCSG3.js +71 -0
  112. package/dist/chunk-BLFLCSG3.js.map +7 -0
  113. package/dist/chunk-BYYGAGNH.js +0 -0
  114. package/dist/chunk-BYYGAGNH.js.map +7 -0
  115. package/dist/chunk-CANRZY5A.js +87 -0
  116. package/dist/chunk-CANRZY5A.js.map +7 -0
  117. package/dist/chunk-CAZPWBQ4.js +18 -0
  118. package/dist/chunk-CAZPWBQ4.js.map +7 -0
  119. package/dist/chunk-CMPOBBRV.js +10 -0
  120. package/dist/chunk-CMPOBBRV.js.map +7 -0
  121. package/dist/chunk-CRQWKC67.js +118 -0
  122. package/dist/chunk-CRQWKC67.js.map +7 -0
  123. package/dist/chunk-CRVXBWSY.js +70 -0
  124. package/dist/chunk-CRVXBWSY.js.map +7 -0
  125. package/dist/chunk-D6ZHV6AT.js +0 -0
  126. package/dist/chunk-D6ZHV6AT.js.map +7 -0
  127. package/dist/chunk-DP7SYLLI.js +206 -0
  128. package/dist/chunk-DP7SYLLI.js.map +7 -0
  129. package/dist/chunk-DVDKPY2A.js +12 -0
  130. package/dist/chunk-DVDKPY2A.js.map +7 -0
  131. package/dist/chunk-E4VONSLO.js +15 -0
  132. package/dist/chunk-E4VONSLO.js.map +7 -0
  133. package/dist/chunk-EEYGPPLU.js +185 -0
  134. package/dist/chunk-EEYGPPLU.js.map +7 -0
  135. package/dist/chunk-EI5TMJDC.js +56 -0
  136. package/dist/chunk-EI5TMJDC.js.map +7 -0
  137. package/dist/chunk-EQ5AIF3T.js +117 -0
  138. package/dist/chunk-EQ5AIF3T.js.map +7 -0
  139. package/dist/chunk-EZZHO6DQ.js +26 -0
  140. package/dist/chunk-EZZHO6DQ.js.map +7 -0
  141. package/dist/chunk-FUHLRG3M.js +130 -0
  142. package/dist/chunk-FUHLRG3M.js.map +7 -0
  143. package/dist/chunk-FVOL3ZOJ.js +101 -0
  144. package/dist/chunk-FVOL3ZOJ.js.map +7 -0
  145. package/dist/chunk-G33EUV7N.js +288 -0
  146. package/dist/chunk-G33EUV7N.js.map +7 -0
  147. package/dist/chunk-GH34XFCC.js +56 -0
  148. package/dist/chunk-GH34XFCC.js.map +7 -0
  149. package/dist/chunk-GKWRJ6H4.js +27 -0
  150. package/dist/chunk-GKWRJ6H4.js.map +7 -0
  151. package/dist/chunk-GQHMU3AR.js +907 -0
  152. package/dist/chunk-GQHMU3AR.js.map +7 -0
  153. package/dist/chunk-GRZU6F7Q.js +80 -0
  154. package/dist/chunk-GRZU6F7Q.js.map +7 -0
  155. package/dist/chunk-GT2LARKT.js +107 -0
  156. package/dist/chunk-GT2LARKT.js.map +7 -0
  157. package/dist/chunk-H4ABGAAG.js +60 -0
  158. package/dist/chunk-H4ABGAAG.js.map +7 -0
  159. package/dist/chunk-H63UHX42.js +93 -0
  160. package/dist/chunk-H63UHX42.js.map +7 -0
  161. package/dist/chunk-HIS4MGIR.js +8 -0
  162. package/dist/chunk-HIS4MGIR.js.map +7 -0
  163. package/dist/chunk-HPPFSWIV.js +213 -0
  164. package/dist/chunk-HPPFSWIV.js.map +7 -0
  165. package/dist/chunk-J2FC5K4B.js +87 -0
  166. package/dist/chunk-J2FC5K4B.js.map +7 -0
  167. package/dist/chunk-J5BSFDVK.js +123 -0
  168. package/dist/chunk-J5BSFDVK.js.map +7 -0
  169. package/dist/chunk-J7GSNWXT.js +151 -0
  170. package/dist/chunk-J7GSNWXT.js.map +7 -0
  171. package/dist/chunk-JA5ET6MC.js +550 -0
  172. package/dist/chunk-JA5ET6MC.js.map +7 -0
  173. package/dist/chunk-K22S7IZR.js +12 -0
  174. package/dist/chunk-K22S7IZR.js.map +7 -0
  175. package/dist/chunk-K5TMXCMY.js +139 -0
  176. package/dist/chunk-K5TMXCMY.js.map +7 -0
  177. package/dist/chunk-KE53GN4Q.js +246 -0
  178. package/dist/chunk-KE53GN4Q.js.map +7 -0
  179. package/dist/chunk-KFQEDSYI.js +24 -0
  180. package/dist/chunk-KFQEDSYI.js.map +7 -0
  181. package/dist/chunk-KH6QWB5Y.js +51 -0
  182. package/dist/chunk-KH6QWB5Y.js.map +7 -0
  183. package/dist/chunk-KLATKWUA.js +116 -0
  184. package/dist/chunk-KLATKWUA.js.map +7 -0
  185. package/dist/chunk-KSKWSUOB.js +17 -0
  186. package/dist/chunk-KSKWSUOB.js.map +7 -0
  187. package/dist/chunk-LFWEJXWE.js +68 -0
  188. package/dist/chunk-LFWEJXWE.js.map +7 -0
  189. package/dist/chunk-LMWJAD3B.js +24 -0
  190. package/dist/chunk-LMWJAD3B.js.map +7 -0
  191. package/dist/chunk-MD23VADH.js +92 -0
  192. package/dist/chunk-MD23VADH.js.map +7 -0
  193. package/dist/chunk-MKMJT3XR.js +0 -0
  194. package/dist/chunk-MKMJT3XR.js.map +7 -0
  195. package/dist/chunk-NJTLBSTR.js +82 -0
  196. package/dist/chunk-NJTLBSTR.js.map +7 -0
  197. package/dist/chunk-NJXKMF67.js +182 -0
  198. package/dist/chunk-NJXKMF67.js.map +7 -0
  199. package/dist/chunk-OBQBIOGN.js +16 -0
  200. package/dist/chunk-OBQBIOGN.js.map +7 -0
  201. package/dist/chunk-OLYHNFKC.js +82 -0
  202. package/dist/chunk-OLYHNFKC.js.map +7 -0
  203. package/dist/chunk-OR2L7EKO.js +33 -0
  204. package/dist/chunk-OR2L7EKO.js.map +7 -0
  205. package/dist/chunk-OUF57UZ7.js +28 -0
  206. package/dist/chunk-OUF57UZ7.js.map +7 -0
  207. package/dist/chunk-PBMM2N77.js +56 -0
  208. package/dist/chunk-PBMM2N77.js.map +7 -0
  209. package/dist/chunk-PCL2KNHC.js +6 -0
  210. package/dist/chunk-PCL2KNHC.js.map +7 -0
  211. package/dist/chunk-PH3LWLYN.js +49 -0
  212. package/dist/chunk-PH3LWLYN.js.map +7 -0
  213. package/dist/chunk-PHNMDWSK.js +9 -0
  214. package/dist/chunk-PHNMDWSK.js.map +7 -0
  215. package/dist/chunk-PN5EH6DM.js +36 -0
  216. package/dist/chunk-PN5EH6DM.js.map +7 -0
  217. package/dist/chunk-POCAINUE.js +42 -0
  218. package/dist/chunk-POCAINUE.js.map +7 -0
  219. package/dist/chunk-Q5LEK7CI.js +64 -0
  220. package/dist/chunk-Q5LEK7CI.js.map +7 -0
  221. package/dist/chunk-QH7VW7IY.js +0 -0
  222. package/dist/chunk-QH7VW7IY.js.map +7 -0
  223. package/dist/chunk-QLGM2AZW.js +173 -0
  224. package/dist/chunk-QLGM2AZW.js.map +7 -0
  225. package/dist/chunk-QT3EYUL5.js +85 -0
  226. package/dist/chunk-QT3EYUL5.js.map +7 -0
  227. package/dist/chunk-QYHOFLRX.js +43 -0
  228. package/dist/chunk-QYHOFLRX.js.map +7 -0
  229. package/dist/chunk-R3MBVWVI.js +127 -0
  230. package/dist/chunk-R3MBVWVI.js.map +7 -0
  231. package/dist/chunk-R6V3LH7F.js +67 -0
  232. package/dist/chunk-R6V3LH7F.js.map +7 -0
  233. package/dist/chunk-RB22U6HI.js +64 -0
  234. package/dist/chunk-RB22U6HI.js.map +7 -0
  235. package/dist/chunk-RQRQYYTB.js +34 -0
  236. package/dist/chunk-RQRQYYTB.js.map +7 -0
  237. package/dist/chunk-S5D2QE4G.js +53 -0
  238. package/dist/chunk-S5D2QE4G.js.map +7 -0
  239. package/dist/chunk-S7I76ISM.js +245 -0
  240. package/dist/chunk-S7I76ISM.js.map +7 -0
  241. package/dist/chunk-TBDZOQ2C.js +66 -0
  242. package/dist/chunk-TBDZOQ2C.js.map +7 -0
  243. package/dist/chunk-TSKELYW7.js +55 -0
  244. package/dist/chunk-TSKELYW7.js.map +7 -0
  245. package/dist/chunk-U3YHTQHR.js +215 -0
  246. package/dist/chunk-U3YHTQHR.js.map +7 -0
  247. package/dist/chunk-UGL3KNOT.js +42 -0
  248. package/dist/chunk-UGL3KNOT.js.map +7 -0
  249. package/dist/chunk-UN3DARLG.js +35 -0
  250. package/dist/chunk-UN3DARLG.js.map +7 -0
  251. package/dist/chunk-V5H7L5PJ.js +587 -0
  252. package/dist/chunk-V5H7L5PJ.js.map +7 -0
  253. package/dist/chunk-VGGERKV2.js +7 -0
  254. package/dist/chunk-VGGERKV2.js.map +7 -0
  255. package/dist/chunk-VVFXYY65.js +0 -0
  256. package/dist/chunk-VVFXYY65.js.map +7 -0
  257. package/dist/chunk-WLDCYUN6.js +161 -0
  258. package/dist/chunk-WLDCYUN6.js.map +7 -0
  259. package/dist/chunk-WMDTQS36.js +0 -0
  260. package/dist/chunk-WMDTQS36.js.map +7 -0
  261. package/dist/chunk-WUTGZYP4.js +290 -0
  262. package/dist/chunk-WUTGZYP4.js.map +7 -0
  263. package/dist/chunk-XKN3WWK3.js +0 -0
  264. package/dist/chunk-XKN3WWK3.js.map +7 -0
  265. package/dist/chunk-XQ5VJ4QO.js +62 -0
  266. package/dist/chunk-XQ5VJ4QO.js.map +7 -0
  267. package/dist/chunk-XQVKX47O.js +174 -0
  268. package/dist/chunk-XQVKX47O.js.map +7 -0
  269. package/dist/chunk-Y5CNB57V.js +22 -0
  270. package/dist/chunk-Y5CNB57V.js.map +7 -0
  271. package/dist/chunk-YDGH7DTL.js +18 -0
  272. package/dist/chunk-YDGH7DTL.js.map +7 -0
  273. package/dist/chunk-YIFUBPJ3.js +0 -0
  274. package/dist/chunk-YIFUBPJ3.js.map +7 -0
  275. package/dist/chunk-YUUDXRBW.js +212 -0
  276. package/dist/chunk-YUUDXRBW.js.map +7 -0
  277. package/dist/chunk-Z6DQLJS2.js +23 -0
  278. package/dist/chunk-Z6DQLJS2.js.map +7 -0
  279. package/dist/chunk-ZDEZ2FOC.js +81 -0
  280. package/dist/chunk-ZDEZ2FOC.js.map +7 -0
  281. package/dist/chunk-ZUYSGCEH.js +65 -0
  282. package/dist/chunk-ZUYSGCEH.js.map +7 -0
  283. package/dist/chunk-ZYP3MIH3.js +115 -0
  284. package/dist/chunk-ZYP3MIH3.js.map +7 -0
  285. package/dist/constraints/constraint-evaluator.d.ts +57 -0
  286. package/dist/constraints/constraint-evaluator.js +17 -0
  287. package/dist/constraints/constraint-evaluator.js.map +7 -0
  288. package/dist/constraints/index.d.ts +1 -0
  289. package/dist/constraints/index.js +17 -0
  290. package/dist/constraints/index.js.map +7 -0
  291. package/dist/csrf/index.d.ts +43 -0
  292. package/dist/csrf/index.js +43 -0
  293. package/dist/csrf/index.js.map +7 -0
  294. package/dist/engine/build-form-engine.d.ts +27 -0
  295. package/dist/engine/build-form-engine.js +58 -0
  296. package/dist/engine/build-form-engine.js.map +7 -0
  297. package/dist/engine/component-type.d.ts +19 -0
  298. package/dist/engine/component-type.js +6 -0
  299. package/dist/engine/component-type.js.map +7 -0
  300. package/dist/engine/create-form-engine.d.ts +100 -0
  301. package/dist/engine/create-form-engine.js +59 -0
  302. package/dist/engine/create-form-engine.js.map +7 -0
  303. package/dist/engine/form-action-context.d.ts +70 -0
  304. package/dist/engine/form-action-context.js +10 -0
  305. package/dist/engine/form-action-context.js.map +7 -0
  306. package/dist/engine/form-engine.d.ts +236 -0
  307. package/dist/engine/form-engine.js +61 -0
  308. package/dist/engine/form-engine.js.map +7 -0
  309. package/dist/engine/index.d.ts +12 -0
  310. package/dist/engine/index.js +124 -0
  311. package/dist/engine/index.js.map +7 -0
  312. package/dist/engine/schema-artifacts.d.ts +87 -0
  313. package/dist/engine/schema-artifacts.js +14 -0
  314. package/dist/engine/schema-artifacts.js.map +7 -0
  315. package/dist/engine/schema-metadata-catalog.d.ts +41 -0
  316. package/dist/engine/schema-metadata-catalog.js +6 -0
  317. package/dist/engine/schema-metadata-catalog.js.map +7 -0
  318. package/dist/engine/step-machine.d.ts +59 -0
  319. package/dist/engine/step-machine.js +32 -0
  320. package/dist/engine/step-machine.js.map +7 -0
  321. package/dist/engine/submission-schema.d.ts +53 -0
  322. package/dist/engine/submission-schema.js +22 -0
  323. package/dist/engine/submission-schema.js.map +7 -0
  324. package/dist/engine/value-validation.d.ts +237 -0
  325. package/dist/engine/value-validation.js +39 -0
  326. package/dist/engine/value-validation.js.map +7 -0
  327. package/dist/index.d.ts +76 -0
  328. package/dist/index.js +283 -0
  329. package/dist/index.js.map +7 -0
  330. package/dist/lib/authoring-catalog.d.ts +5 -0
  331. package/dist/lib/authoring-catalog.js +11 -0
  332. package/dist/lib/authoring-catalog.js.map +7 -0
  333. package/dist/lib/container-kind-of.d.ts +5 -0
  334. package/dist/lib/container-kind-of.js +6 -0
  335. package/dist/lib/container-kind-of.js.map +7 -0
  336. package/dist/lib/debounce.d.ts +19 -0
  337. package/dist/lib/debounce.js +6 -0
  338. package/dist/lib/debounce.js.map +7 -0
  339. package/dist/lib/deep-readonly.d.ts +12 -0
  340. package/dist/lib/deep-readonly.js +0 -0
  341. package/dist/lib/deep-readonly.js.map +7 -0
  342. package/dist/lib/element-children.d.ts +35 -0
  343. package/dist/lib/element-children.js +16 -0
  344. package/dist/lib/element-children.js.map +7 -0
  345. package/dist/lib/exported-action-envelope.d.ts +11 -0
  346. package/dist/lib/exported-action-envelope.js +7 -0
  347. package/dist/lib/exported-action-envelope.js.map +7 -0
  348. package/dist/lib/exported-rules.d.ts +14 -0
  349. package/dist/lib/exported-rules.js +6 -0
  350. package/dist/lib/exported-rules.js.map +7 -0
  351. package/dist/lib/field-ref.d.ts +13 -0
  352. package/dist/lib/field-ref.js +6 -0
  353. package/dist/lib/field-ref.js.map +7 -0
  354. package/dist/lib/field-scope.d.ts +121 -0
  355. package/dist/lib/field-scope.js +41 -0
  356. package/dist/lib/field-scope.js.map +7 -0
  357. package/dist/lib/form-fetch.d.ts +36 -0
  358. package/dist/lib/form-fetch.js +10 -0
  359. package/dist/lib/form-fetch.js.map +7 -0
  360. package/dist/lib/form-view.d.ts +33 -0
  361. package/dist/lib/form-view.js +12 -0
  362. package/dist/lib/form-view.js.map +7 -0
  363. package/dist/lib/frozen-copy.d.ts +9 -0
  364. package/dist/lib/frozen-copy.js +7 -0
  365. package/dist/lib/frozen-copy.js.map +7 -0
  366. package/dist/lib/is-plain-object.d.ts +2 -0
  367. package/dist/lib/is-plain-object.js +6 -0
  368. package/dist/lib/is-plain-object.js.map +7 -0
  369. package/dist/lib/logger.d.ts +2 -0
  370. package/dist/lib/logger.js +6 -0
  371. package/dist/lib/logger.js.map +7 -0
  372. package/dist/lib/normalized-form-config.d.ts +106 -0
  373. package/dist/lib/normalized-form-config.js +19 -0
  374. package/dist/lib/normalized-form-config.js.map +7 -0
  375. package/dist/lib/omit-paths.d.ts +11 -0
  376. package/dist/lib/omit-paths.js +7 -0
  377. package/dist/lib/omit-paths.js.map +7 -0
  378. package/dist/lib/path-values.d.ts +47 -0
  379. package/dist/lib/path-values.js +22 -0
  380. package/dist/lib/path-values.js.map +7 -0
  381. package/dist/lib/renderer-data-attributes.d.ts +10 -0
  382. package/dist/lib/renderer-data-attributes.js +6 -0
  383. package/dist/lib/renderer-data-attributes.js.map +7 -0
  384. package/dist/lib/reserved-path-keys.d.ts +10 -0
  385. package/dist/lib/reserved-path-keys.js +10 -0
  386. package/dist/lib/reserved-path-keys.js.map +7 -0
  387. package/dist/lib/resolve-appearance.d.ts +48 -0
  388. package/dist/lib/resolve-appearance.js +10 -0
  389. package/dist/lib/resolve-appearance.js.map +7 -0
  390. package/dist/lib/standard-json-schema.d.ts +27 -0
  391. package/dist/lib/standard-json-schema.js +8 -0
  392. package/dist/lib/standard-json-schema.js.map +7 -0
  393. package/dist/lib/standard-schema.d.ts +19 -0
  394. package/dist/lib/standard-schema.js +8 -0
  395. package/dist/lib/standard-schema.js.map +7 -0
  396. package/dist/lib/submission-shape.d.ts +60 -0
  397. package/dist/lib/submission-shape.js +14 -0
  398. package/dist/lib/submission-shape.js.map +7 -0
  399. package/dist/lib/validate-form/grid-checks.d.ts +57 -0
  400. package/dist/lib/validate-form/grid-checks.js +16 -0
  401. package/dist/lib/validate-form/grid-checks.js.map +7 -0
  402. package/dist/lib/validate-form/issues.d.ts +43 -0
  403. package/dist/lib/validate-form/issues.js +13 -0
  404. package/dist/lib/validate-form/issues.js.map +7 -0
  405. package/dist/lib/validate-form/options.d.ts +22 -0
  406. package/dist/lib/validate-form/options.js +0 -0
  407. package/dist/lib/validate-form/options.js.map +7 -0
  408. package/dist/lib/validate-form/parse-elements.d.ts +23 -0
  409. package/dist/lib/validate-form/parse-elements.js +21 -0
  410. package/dist/lib/validate-form/parse-elements.js.map +7 -0
  411. package/dist/lib/validate-form/parse-owner-actions.d.ts +14 -0
  412. package/dist/lib/validate-form/parse-owner-actions.js +10 -0
  413. package/dist/lib/validate-form/parse-owner-actions.js.map +7 -0
  414. package/dist/lib/validate-form/parse-view.d.ts +11 -0
  415. package/dist/lib/validate-form/parse-view.js +11 -0
  416. package/dist/lib/validate-form/parse-view.js.map +7 -0
  417. package/dist/lib/validate-form/schema-issues.d.ts +16 -0
  418. package/dist/lib/validate-form/schema-issues.js +8 -0
  419. package/dist/lib/validate-form/schema-issues.js.map +7 -0
  420. package/dist/lib/validate-form/step-checks.d.ts +18 -0
  421. package/dist/lib/validate-form/step-checks.js +15 -0
  422. package/dist/lib/validate-form/step-checks.js.map +7 -0
  423. package/dist/lib/validate-form/validation-error.d.ts +26 -0
  424. package/dist/lib/validate-form/validation-error.js +8 -0
  425. package/dist/lib/validate-form/validation-error.js.map +7 -0
  426. package/dist/lib/validate-form.d.ts +34 -0
  427. package/dist/lib/validate-form.js +62 -0
  428. package/dist/lib/validate-form.js.map +7 -0
  429. package/dist/lib/zod-json-schema.d.ts +33 -0
  430. package/dist/lib/zod-json-schema.js +10 -0
  431. package/dist/lib/zod-json-schema.js.map +7 -0
  432. package/dist/recipes/built-in-specs.d.ts +1832 -0
  433. package/dist/recipes/built-in-specs.js +28 -0
  434. package/dist/recipes/built-in-specs.js.map +7 -0
  435. package/dist/recipes/built-in.d.ts +2 -0
  436. package/dist/recipes/built-in.js +49 -0
  437. package/dist/recipes/built-in.js.map +7 -0
  438. package/dist/recipes/define-form-recipe.d.ts +10 -0
  439. package/dist/recipes/define-form-recipe.js +6 -0
  440. package/dist/recipes/define-form-recipe.js.map +7 -0
  441. package/dist/recipes/define-recipe-spec.d.ts +13 -0
  442. package/dist/recipes/define-recipe-spec.js +8 -0
  443. package/dist/recipes/define-recipe-spec.js.map +7 -0
  444. package/dist/recipes/index.d.ts +6 -0
  445. package/dist/recipes/index.js +43 -0
  446. package/dist/recipes/index.js.map +7 -0
  447. package/dist/recipes/protocol.d.ts +24 -0
  448. package/dist/recipes/protocol.js +11 -0
  449. package/dist/recipes/protocol.js.map +7 -0
  450. package/dist/recipes/slot-schemas.d.ts +128 -0
  451. package/dist/recipes/slot-schemas.js +24 -0
  452. package/dist/recipes/slot-schemas.js.map +7 -0
  453. package/dist/recipes/types.d.ts +55 -0
  454. package/dist/recipes/types.js +1 -0
  455. package/dist/recipes/types.js.map +7 -0
  456. package/dist/recipes/view-model.d.ts +93 -0
  457. package/dist/recipes/view-model.js +29 -0
  458. package/dist/recipes/view-model.js.map +7 -0
  459. package/dist/registries/index.d.ts +8 -0
  460. package/dist/registries/index.js +0 -0
  461. package/dist/registries/index.js.map +7 -0
  462. package/dist/runtime/create-form-runtime.d.ts +43 -0
  463. package/dist/runtime/create-form-runtime.js +41 -0
  464. package/dist/runtime/create-form-runtime.js.map +7 -0
  465. package/dist/runtime/index.d.ts +3 -0
  466. package/dist/runtime/index.js +44 -0
  467. package/dist/runtime/index.js.map +7 -0
  468. package/dist/runtime/submission-lifecycle.d.ts +65 -0
  469. package/dist/runtime/submission-lifecycle.js +6 -0
  470. package/dist/runtime/submission-lifecycle.js.map +7 -0
  471. package/dist/runtime/types.d.ts +172 -0
  472. package/dist/runtime/types.js +6 -0
  473. package/dist/runtime/types.js.map +7 -0
  474. package/dist/schemas/base-field.d.ts +450 -0
  475. package/dist/schemas/base-field.js +36 -0
  476. package/dist/schemas/base-field.js.map +7 -0
  477. package/dist/schemas/field-config.d.ts +36 -0
  478. package/dist/schemas/field-config.js +1 -0
  479. package/dist/schemas/field-config.js.map +7 -0
  480. package/dist/schemas/form-config-shape.d.ts +763 -0
  481. package/dist/schemas/form-config-shape.js +35 -0
  482. package/dist/schemas/form-config-shape.js.map +7 -0
  483. package/dist/schemas/form-config.d.ts +1288 -0
  484. package/dist/schemas/form-config.js +41 -0
  485. package/dist/schemas/form-config.js.map +7 -0
  486. package/dist/schemas/html-attributes.d.ts +12 -0
  487. package/dist/schemas/html-attributes.js +12 -0
  488. package/dist/schemas/html-attributes.js.map +7 -0
  489. package/dist/schemas/index.d.ts +5 -0
  490. package/dist/schemas/index.js +165 -0
  491. package/dist/schemas/index.js.map +7 -0
  492. package/dist/schemas/validation/async/cache.d.ts +39 -0
  493. package/dist/schemas/validation/async/cache.js +6 -0
  494. package/dist/schemas/validation/async/cache.js.map +7 -0
  495. package/dist/schemas/validation/async/handler.d.ts +21 -0
  496. package/dist/schemas/validation/async/handler.js +11 -0
  497. package/dist/schemas/validation/async/handler.js.map +7 -0
  498. package/dist/schemas/validation/async/index.d.ts +8 -0
  499. package/dist/schemas/validation/async/index.js +14 -0
  500. package/dist/schemas/validation/async/index.js.map +7 -0
  501. package/dist/schemas/validation/defaults.d.ts +6 -0
  502. package/dist/schemas/validation/defaults.js +12 -0
  503. package/dist/schemas/validation/defaults.js.map +7 -0
  504. package/dist/schemas/validation/dependencies.d.ts +52 -0
  505. package/dist/schemas/validation/dependencies.js +17 -0
  506. package/dist/schemas/validation/dependencies.js.map +7 -0
  507. package/dist/schemas/validation/empty.d.ts +1 -0
  508. package/dist/schemas/validation/empty.js +6 -0
  509. package/dist/schemas/validation/empty.js.map +7 -0
  510. package/dist/schemas/validation/errors.d.ts +44 -0
  511. package/dist/schemas/validation/errors.js +12 -0
  512. package/dist/schemas/validation/errors.js.map +7 -0
  513. package/dist/schemas/validation/index.d.ts +9 -0
  514. package/dist/schemas/validation/index.js +130 -0
  515. package/dist/schemas/validation/index.js.map +7 -0
  516. package/dist/schemas/validation/presets.d.ts +66 -0
  517. package/dist/schemas/validation/presets.js +6 -0
  518. package/dist/schemas/validation/presets.js.map +7 -0
  519. package/dist/schemas/validation/relations.d.ts +12 -0
  520. package/dist/schemas/validation/relations.js +8 -0
  521. package/dist/schemas/validation/relations.js.map +7 -0
  522. package/dist/schemas/validation/types.d.ts +817 -0
  523. package/dist/schemas/validation/types.js +64 -0
  524. package/dist/schemas/validation/types.js.map +7 -0
  525. package/dist/schemas/validation/utils.d.ts +76 -0
  526. package/dist/schemas/validation/utils.js +24 -0
  527. package/dist/schemas/validation/utils.js.map +7 -0
  528. package/dist/schemas/validation/validation-registry.d.ts +124 -0
  529. package/dist/schemas/validation/validation-registry.js +27 -0
  530. package/dist/schemas/validation/validation-registry.js.map +7 -0
  531. package/dist/server-errors/from-json-api-errors.d.ts +13 -0
  532. package/dist/server-errors/from-json-api-errors.js +10 -0
  533. package/dist/server-errors/from-json-api-errors.js.map +7 -0
  534. package/dist/server-errors/from-problem-details.d.ts +15 -0
  535. package/dist/server-errors/from-problem-details.js +10 -0
  536. package/dist/server-errors/from-problem-details.js.map +7 -0
  537. package/dist/server-errors/index.d.ts +8 -0
  538. package/dist/server-errors/index.js +17 -0
  539. package/dist/server-errors/index.js.map +7 -0
  540. package/dist/server-errors/shared.d.ts +34 -0
  541. package/dist/server-errors/shared.js +23 -0
  542. package/dist/server-errors/shared.js.map +7 -0
  543. package/dist/state-container/create-form-state-container.d.ts +17 -0
  544. package/dist/state-container/create-form-state-container.js +31 -0
  545. package/dist/state-container/create-form-state-container.js.map +7 -0
  546. package/dist/state-container/dirty.d.ts +11 -0
  547. package/dist/state-container/dirty.js +9 -0
  548. package/dist/state-container/dirty.js.map +7 -0
  549. package/dist/state-container/field-array.d.ts +41 -0
  550. package/dist/state-container/field-array.js +8 -0
  551. package/dist/state-container/field-array.js.map +7 -0
  552. package/dist/state-container/hydration.d.ts +21 -0
  553. package/dist/state-container/hydration.js +11 -0
  554. package/dist/state-container/hydration.js.map +7 -0
  555. package/dist/state-container/index.d.ts +16 -0
  556. package/dist/state-container/index.js +51 -0
  557. package/dist/state-container/index.js.map +7 -0
  558. package/dist/state-container/issue-text.d.ts +11 -0
  559. package/dist/state-container/issue-text.js +6 -0
  560. package/dist/state-container/issue-text.js.map +7 -0
  561. package/dist/state-container/row-ids.d.ts +34 -0
  562. package/dist/state-container/row-ids.js +19 -0
  563. package/dist/state-container/row-ids.js.map +7 -0
  564. package/dist/state-container/selectors.d.ts +33 -0
  565. package/dist/state-container/selectors.js +22 -0
  566. package/dist/state-container/selectors.js.map +7 -0
  567. package/dist/state-container/stale-results.d.ts +28 -0
  568. package/dist/state-container/stale-results.js +9 -0
  569. package/dist/state-container/stale-results.js.map +7 -0
  570. package/dist/state-container/state-tree.d.ts +56 -0
  571. package/dist/state-container/state-tree.js +33 -0
  572. package/dist/state-container/state-tree.js.map +7 -0
  573. package/dist/state-container/submission.d.ts +45 -0
  574. package/dist/state-container/submission.js +26 -0
  575. package/dist/state-container/submission.js.map +7 -0
  576. package/dist/state-container/types.d.ts +404 -0
  577. package/dist/state-container/types.js +6 -0
  578. package/dist/state-container/types.js.map +7 -0
  579. package/dist/state-container/value-tree.d.ts +56 -0
  580. package/dist/state-container/value-tree.js +34 -0
  581. package/dist/state-container/value-tree.js.map +7 -0
  582. package/dist/testing/assert.d.ts +92 -0
  583. package/dist/testing/assert.js +42 -0
  584. package/dist/testing/assert.js.map +7 -0
  585. package/dist/testing/form-state-container-conformance.d.ts +63 -0
  586. package/dist/testing/form-state-container-conformance.js +44 -0
  587. package/dist/testing/form-state-container-conformance.js.map +7 -0
  588. package/dist/testing/index.d.ts +8 -0
  589. package/dist/testing/index.js +36 -0
  590. package/dist/testing/index.js.map +7 -0
  591. package/dist/types/actions.d.ts +75 -0
  592. package/dist/types/actions.js +1 -0
  593. package/dist/types/actions.js.map +7 -0
  594. package/dist/types/categories.d.ts +21 -0
  595. package/dist/types/categories.js +8 -0
  596. package/dist/types/categories.js.map +7 -0
  597. package/dist/types/define-element.d.ts +73 -0
  598. package/dist/types/define-element.js +8 -0
  599. package/dist/types/define-element.js.map +7 -0
  600. package/dist/types/index.d.ts +10 -0
  601. package/dist/types/index.js +17 -0
  602. package/dist/types/index.js.map +7 -0
  603. package/dist/types/metadata.d.ts +14 -0
  604. package/dist/types/metadata.js +1 -0
  605. package/dist/types/metadata.js.map +7 -0
  606. package/dist/types/registration.d.ts +142 -0
  607. package/dist/types/registration.js +1 -0
  608. package/dist/types/registration.js.map +7 -0
  609. package/package.json +113 -3
  610. package/skill/SKILL.md +79 -0
  611. package/skill/reference/forms-actions.md +51 -0
  612. package/skill/reference/forms-submission.md +44 -0
  613. package/skill/reference/forms-validation.md +91 -0
  614. package/skill/reference/forms.md +180 -0
@@ -0,0 +1,2780 @@
1
+ import {
2
+ selectFieldState,
3
+ selectValue
4
+ } from "./chunk-CRVXBWSY.js";
5
+ import {
6
+ toFieldArrayValue
7
+ } from "./chunk-KFQEDSYI.js";
8
+ import {
9
+ createDefaultValidationRegistry
10
+ } from "./chunk-6IZYNHZ3.js";
11
+ import {
12
+ logger
13
+ } from "./chunk-VGGERKV2.js";
14
+ import {
15
+ markNormalizedElementConfig
16
+ } from "./chunk-CRQWKC67.js";
17
+ import {
18
+ FORM_ROOT
19
+ } from "./chunk-PCL2KNHC.js";
20
+ import {
21
+ toHydration
22
+ } from "./chunk-RB22U6HI.js";
23
+ import {
24
+ assertCallCount,
25
+ assertDefined,
26
+ assertDoesNotThrow,
27
+ assertEqual,
28
+ assertLength,
29
+ assertMatches,
30
+ assertNoProperty,
31
+ assertNotIncludes,
32
+ assertNotMatches,
33
+ assertNotSame,
34
+ assertSame,
35
+ assertThrows,
36
+ assertUndefined,
37
+ createSpy,
38
+ spyOn,
39
+ stubGlobal,
40
+ waitFor
41
+ } from "./chunk-U3YHTQHR.js";
42
+
43
+ // src/testing/form-state-container-conformance.ts
44
+ import * as z from "zod/mini";
45
+ function createGate() {
46
+ const waiting = /* @__PURE__ */ new Map();
47
+ const held = /* @__PURE__ */ new Set();
48
+ const settled = /* @__PURE__ */ new Map();
49
+ return {
50
+ hold: (...values) => values.forEach((value) => held.add(value)),
51
+ check: (value) => settled.has(String(value)) ? Promise.resolve(settled.get(String(value)) === true) : !held.has(String(value)) ? Promise.resolve(true) : new Promise((resolve) => {
52
+ const key = String(value);
53
+ waiting.set(key, [...waiting.get(key) ?? [], resolve]);
54
+ }),
55
+ pending: (value) => waiting.get(value)?.length ?? 0,
56
+ settle(value, valid) {
57
+ settled.set(value, valid);
58
+ for (const resolve of waiting.get(value) ?? []) resolve(valid);
59
+ waiting.delete(value);
60
+ }
61
+ };
62
+ }
63
+ var element = (config, containerKind) => markNormalizedElementConfig(config, containerKind);
64
+ var defaultModes = { validationMode: "onSubmit", reValidateMode: "onChange" };
65
+ function containerOptions(setup) {
66
+ const { registry, elements, defaults, snapshot, payload } = setup;
67
+ return {
68
+ config: {
69
+ steps: [{ elements }],
70
+ validationMode: setup.validationMode ?? defaultModes.validationMode,
71
+ reValidateMode: setup.reValidateMode ?? defaultModes.reValidateMode,
72
+ ...payload === void 0 ? {} : { payload }
73
+ },
74
+ registry,
75
+ ...defaults === void 0 ? {} : { defaults },
76
+ ...snapshot === void 0 ? {} : { snapshot }
77
+ };
78
+ }
79
+ function fixture(overrides = {}) {
80
+ const gate = createGate();
81
+ const registry = createDefaultValidationRegistry([
82
+ {
83
+ $type: "gate",
84
+ schema: z.object({ $type: z.literal("gate") }),
85
+ metadata: { name: "Gate" },
86
+ category: "generic",
87
+ handler: () => z.any().check(z.refine((value) => gate.check(value), "Value is taken"))
88
+ }
89
+ ]);
90
+ return {
91
+ gate,
92
+ options: containerOptions({
93
+ registry,
94
+ elements: [
95
+ element({ $type: "input", name: "name", rules: [{ $type: "required", message: "Name is required" }] }),
96
+ element({
97
+ $type: "input",
98
+ name: "nickname",
99
+ rules: [{ $type: "required", message: "Nickname is required" }]
100
+ }),
101
+ element({ $type: "input", name: "slug", rules: [{ $type: "gate" }] }),
102
+ element({
103
+ $type: "input",
104
+ name: "code",
105
+ rules: [
106
+ { $type: "minLength", value: 5, message: "Too short" },
107
+ { $type: "pattern", value: "^\\d+$", message: "Digits only" }
108
+ ]
109
+ }),
110
+ element(
111
+ {
112
+ $type: "fieldset",
113
+ name: "address",
114
+ elements: [
115
+ { $type: "input", name: "street" },
116
+ { $type: "input", name: "country" }
117
+ ]
118
+ },
119
+ "object"
120
+ ),
121
+ element(
122
+ {
123
+ $type: "arrayElements",
124
+ name: "contacts",
125
+ elements: [
126
+ {
127
+ $type: "input",
128
+ name: "email",
129
+ rules: [{ $type: "required", message: "Email is required" }]
130
+ },
131
+ { $type: "input", name: "label" }
132
+ ]
133
+ },
134
+ "array"
135
+ ),
136
+ element(
137
+ {
138
+ $type: "arrayElements",
139
+ name: "groups",
140
+ elements: [
141
+ { $type: "input", name: "title" },
142
+ {
143
+ $type: "arrayElements",
144
+ name: "members",
145
+ elements: [
146
+ { $type: "input", name: "code", rules: [{ $type: "gate" }] },
147
+ { $type: "input", name: "label" }
148
+ ]
149
+ }
150
+ ]
151
+ },
152
+ "array"
153
+ ),
154
+ element(
155
+ {
156
+ $type: "arrayElements",
157
+ name: "tags",
158
+ elements: [{ $type: "input", name: "tag" }]
159
+ },
160
+ "array"
161
+ ),
162
+ element(
163
+ {
164
+ $type: "arrayElements",
165
+ name: "invites",
166
+ rules: [{ $type: "minItems", value: 2, message: "Add two invites" }],
167
+ elements: [
168
+ { $type: "input", name: "code", rules: [{ $type: "gate" }] },
169
+ { $type: "input", name: "label" }
170
+ ]
171
+ },
172
+ "array"
173
+ )
174
+ ],
175
+ defaults: {
176
+ name: "Ada",
177
+ nickname: "ada",
178
+ slug: "free",
179
+ code: "12345",
180
+ address: { street: "1 Main", country: "IT" },
181
+ contacts: [{ email: "a@example.com" }, { email: "b@example.com" }, { email: "c@example.com" }],
182
+ invites: [{ code: "one" }, { code: "two" }]
183
+ },
184
+ ...overrides
185
+ })
186
+ };
187
+ }
188
+ var containerKinds = { arrayElements: "array", fieldset: "object" };
189
+ function custom(configs, defaults, overrides = {}) {
190
+ const registry = createDefaultValidationRegistry([
191
+ {
192
+ $type: "boom",
193
+ schema: z.object({ $type: z.literal("boom") }),
194
+ metadata: { name: "Boom" },
195
+ category: "generic",
196
+ handler: () => z.any().check(
197
+ z.refine(() => {
198
+ throw new Error("exploded");
199
+ })
200
+ )
201
+ }
202
+ ]);
203
+ return containerOptions({
204
+ registry,
205
+ elements: configs.map((config) => element(config, containerKinds[String(config.$type)])),
206
+ defaults,
207
+ ...overrides
208
+ });
209
+ }
210
+ var stepsForm = (defaults) => {
211
+ const { config, ...rest } = custom([], defaults);
212
+ const input = (name, extra = {}) => element({ $type: "input", name, ...extra });
213
+ return {
214
+ ...rest,
215
+ config: {
216
+ ...config,
217
+ steps: [
218
+ { id: "contact", elements: [input("hasCompany")] },
219
+ {
220
+ id: "company",
221
+ constraints: [{ field: "hasCompany", operator: "equals", value: true, $type: "show" }],
222
+ elements: [input("wantsInvoice"), input("company", { rules: [{ $type: "required" }] })]
223
+ },
224
+ {
225
+ id: "payment",
226
+ elements: [
227
+ input("vat", {
228
+ rules: [{ $type: "required" }],
229
+ constraints: [{ field: "wantsInvoice", operator: "equals", value: "yes", $type: "show" }]
230
+ })
231
+ ]
232
+ }
233
+ ]
234
+ }
235
+ };
236
+ };
237
+ var businessForm = (defaults = { kind: "person", company: "" }) => custom(
238
+ [
239
+ { $type: "input", name: "kind" },
240
+ {
241
+ $type: "input",
242
+ name: "company",
243
+ rules: [{ $type: "required", id: "company-required", message: "Company is required" }],
244
+ constraints: [{ field: "kind", $type: "show", operator: "equals", value: "business" }]
245
+ }
246
+ ],
247
+ defaults
248
+ );
249
+ var passwordForm = () => custom(
250
+ [
251
+ { $type: "input", name: "password" },
252
+ { $type: "input", name: "confirm", rules: [{ $type: "relate", fields: ["password"], assert: "allEqual" }] }
253
+ ],
254
+ { password: "a", confirm: "a" }
255
+ );
256
+ var isValidating = (store) => Object.keys(store.getSnapshot().validating).length > 0;
257
+ function watchValidation(store) {
258
+ const watch = { started: false };
259
+ store.subscribe(() => {
260
+ if (isValidating(store)) watch.started = true;
261
+ });
262
+ return watch;
263
+ }
264
+ var marked = (marks) => Object.keys(marks).toSorted();
265
+ var serverIssue = (message) => ({ type: "custom", message });
266
+ var fieldState = (store, path) => selectFieldState(store.getSnapshot(), path);
267
+ function passedOf(result) {
268
+ if (result.outcome !== "passed") throw new Error(`The submission did not pass: ${result.outcome}.`);
269
+ return result;
270
+ }
271
+ var passedSubmission = async (store) => passedOf(await store.submit());
272
+ var serverAnswer = async (store, errors) => (await passedSubmission(store)).setServerErrors(errors);
273
+ var settle = () => new Promise((resolve) => setTimeout(resolve, 5));
274
+ var errorsOf = (store) => store.getSnapshot().errors;
275
+ var contacts = (store) => store.getSnapshot().values.contacts.map((row) => row.email);
276
+ var ids = (store) => store.getSnapshot().rowIds.contacts ?? [];
277
+ var cases = [
278
+ {
279
+ rule: "snapshot stability",
280
+ name: "returns the same snapshot until something changes, and notifies once per change",
281
+ run(createStore) {
282
+ const store = createStore(fixture().options);
283
+ const first = store.getSnapshot();
284
+ assertSame(store.getSnapshot(), first);
285
+ const listener = createSpy();
286
+ const unsubscribe = store.subscribe(listener);
287
+ store.setValue("name", "Grace");
288
+ assertCallCount(listener, 1);
289
+ const second = store.getSnapshot();
290
+ assertNotSame(second, first);
291
+ assertSame(store.getSnapshot(), second);
292
+ store.setValue("name", "Grace");
293
+ assertCallCount(listener, 1);
294
+ assertSame(store.getSnapshot(), second);
295
+ unsubscribe();
296
+ store.setValue("name", "Hedy");
297
+ assertCallCount(listener, 1);
298
+ }
299
+ },
300
+ {
301
+ rule: "reset generation",
302
+ name: "the snapshot counts resets in generation: it starts at the server snapshot, goes up by one on every reset and on nothing else",
303
+ async run(createStore) {
304
+ const store = createStore(fixture().options);
305
+ const start = store.getSnapshot().generation;
306
+ assertSame(store.getServerSnapshot().generation, start);
307
+ store.setValue("name", "Grace");
308
+ store.blur("name");
309
+ await store.validate();
310
+ await store.submit();
311
+ assertSame(store.getSnapshot().generation, start);
312
+ store.reset();
313
+ assertSame(store.getSnapshot().generation, start + 1);
314
+ store.reset({ name: "Hedy" });
315
+ assertSame(store.getSnapshot().generation, start + 2);
316
+ assertSame(store.getServerSnapshot().generation, start);
317
+ }
318
+ },
319
+ {
320
+ rule: "reset generation",
321
+ name: "a reset of a pristine form still notifies, since the generation changed",
322
+ run(createStore) {
323
+ const store = createStore(fixture().options);
324
+ const listener = createSpy();
325
+ store.subscribe(listener);
326
+ const before = store.getSnapshot();
327
+ store.reset();
328
+ assertCallCount(listener, 1);
329
+ assertNotSame(store.getSnapshot(), before);
330
+ assertEqual(store.getSnapshot().values, before.values);
331
+ }
332
+ },
333
+ {
334
+ rule: "snapshot stability",
335
+ name: "keeps one server snapshot for hydration, whatever happens to the store",
336
+ run(createStore) {
337
+ const store = createStore(fixture().options);
338
+ const server = store.getServerSnapshot();
339
+ assertSame(store.getServerSnapshot(), server);
340
+ assertSame(server.values.name, "Ada");
341
+ store.setValue("name", "Grace");
342
+ store.blur("name");
343
+ assertSame(store.getServerSnapshot(), server);
344
+ assertSame(server.values.name, "Ada");
345
+ assertSame(store.getSnapshot().values.name, "Grace");
346
+ }
347
+ },
348
+ {
349
+ rule: "immutability",
350
+ name: "freezes snapshots and does not alias the values it was given",
351
+ async run(createStore) {
352
+ const defaults = {
353
+ name: "Ada",
354
+ nickname: "ada",
355
+ slug: "free",
356
+ code: "12345",
357
+ address: { street: "1 Main", country: "IT" },
358
+ contacts: [{ email: "a@example.com" }],
359
+ invites: [{ code: "one" }, { code: "two" }]
360
+ };
361
+ const store = createStore(fixture({ defaults }).options);
362
+ const snapshot = store.getSnapshot();
363
+ assertThrows(() => {
364
+ snapshot.values.name = "Eve";
365
+ }, TypeError);
366
+ assertThrows(() => {
367
+ snapshot.values.address.street = "Eve";
368
+ }, TypeError);
369
+ assertThrows(() => {
370
+ snapshot.errors.name = serverIssue("x");
371
+ }, TypeError);
372
+ assertThrows(() => {
373
+ snapshot.touched.name = true;
374
+ }, TypeError);
375
+ assertThrows(() => {
376
+ snapshot.dirty.name = true;
377
+ }, TypeError);
378
+ assertThrows(() => {
379
+ snapshot.rowIds.contacts = [];
380
+ }, TypeError);
381
+ assertThrows(() => {
382
+ snapshot.rowIds.contacts.push("x");
383
+ }, TypeError);
384
+ defaults.address.street = "changed after create";
385
+ assertSame(store.getSnapshot().values.address.street, "1 Main");
386
+ const branch = { street: "2 Side", country: "FR" };
387
+ store.setValue("address", branch);
388
+ branch.street = "changed after setValue";
389
+ assertSame(store.getSnapshot().values.address.street, "2 Side");
390
+ const given = { name: serverIssue("Taken") };
391
+ await serverAnswer(store, given);
392
+ given.name.message = "changed after the answer";
393
+ assertSame(errorsOf(store).name?.message, "Taken");
394
+ assertSame(Object.getPrototypeOf(store.getSnapshot().values), Object.prototype);
395
+ }
396
+ },
397
+ {
398
+ rule: "branch reference preservation",
399
+ name: "keeps the reference of every branch a command did not change",
400
+ run(createStore) {
401
+ const store = createStore(fixture().options);
402
+ const before = store.getSnapshot();
403
+ store.setValue("name", "Grace");
404
+ const after = store.getSnapshot();
405
+ assertSame(after.values.address, before.values.address);
406
+ assertSame(after.values.contacts, before.values.contacts);
407
+ assertSame(after.errors, before.errors);
408
+ assertSame(after.touched, before.touched);
409
+ assertSame(after.rowIds, before.rowIds);
410
+ store.setValue("contacts.1.email", "new@example.com");
411
+ const rows = store.getSnapshot().values.contacts;
412
+ const earlier = after.values.contacts;
413
+ assertNotSame(rows, earlier);
414
+ assertSame(rows[0], earlier[0]);
415
+ assertNotSame(rows[1], earlier[1]);
416
+ assertSame(rows[2], earlier[2]);
417
+ const valuesBeforeBlur = store.getSnapshot().values;
418
+ store.blur("name");
419
+ assertSame(store.getSnapshot().values, valuesBeforeBlur);
420
+ assertSame(store.getSnapshot().values.contacts, rows);
421
+ }
422
+ },
423
+ {
424
+ rule: "field state per exact path",
425
+ name: "keeps errors, touched and dirty as flat maps by exact path, stable while unrelated state changes",
426
+ async run(createStore) {
427
+ const store = createStore(fixture().options);
428
+ await serverAnswer(store, { "contacts.1.email": serverIssue("one"), nickname: serverIssue("nick") });
429
+ store.blur("contacts.1.email");
430
+ store.setValue("address.street", "2 Side");
431
+ const { errors, touched, dirty } = store.getSnapshot();
432
+ assertEqual(Object.keys(errors).toSorted(), ["contacts.1.email", "nickname"]);
433
+ assertMatches(errors.nickname, { message: "nick", source: "server" });
434
+ assertEqual(touched, { "contacts.1.email": true });
435
+ assertEqual(dirty, { "address.street": true });
436
+ assertEqual(Object.keys(store.getSnapshot()).toSorted(), [
437
+ "dirty",
438
+ "errors",
439
+ "generation",
440
+ "isValid",
441
+ "participation",
442
+ "rowIds",
443
+ "touched",
444
+ "validating",
445
+ "values",
446
+ "warnings"
447
+ ]);
448
+ store.setValue("name", "Grace");
449
+ const after = store.getSnapshot();
450
+ assertSame(after.errors, errors);
451
+ assertSame(after.touched, touched);
452
+ assertNotSame(after.dirty, dirty);
453
+ }
454
+ },
455
+ {
456
+ rule: "field state per exact path",
457
+ name: "an error is never inherited: a container and a row inside it can both have one",
458
+ async run(createStore) {
459
+ const store = createStore(fixture().options);
460
+ await serverAnswer(store, { contacts: serverIssue("too few"), "contacts.0.email": serverIssue("row") });
461
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["contacts", "contacts.0.email"]);
462
+ assertSame(fieldState(store, "contacts").error?.message, "too few");
463
+ assertSame(fieldState(store, "contacts.0.email").error?.message, "row");
464
+ assertUndefined(fieldState(store, "contacts.1.email").error);
465
+ assertSame(store.getSnapshot().isValid, false);
466
+ }
467
+ },
468
+ {
469
+ rule: "field state per exact path",
470
+ name: "an error about the form is its own entry and is no field error",
471
+ async run(createStore) {
472
+ const store = createStore(fixture().options);
473
+ await serverAnswer(store, { [FORM_ROOT]: serverIssue("Try again later") });
474
+ assertEqual(Object.keys(errorsOf(store)), [FORM_ROOT]);
475
+ assertMatches(errorsOf(store)[FORM_ROOT], { message: "Try again later" });
476
+ assertUndefined(fieldState(store, "name").error);
477
+ assertSame(fieldState(store, "contacts.0.email").invalid, false);
478
+ assertSame(store.getSnapshot().isValid, false);
479
+ assertEqual(await store.validate(["name"]), { outcome: "passed" });
480
+ assertEqual(await store.validate(), { outcome: "invalid" });
481
+ store.setValue("name", "Grace");
482
+ assertEqual(errorsOf(store), {});
483
+ assertSame(store.getSnapshot().isValid, true);
484
+ }
485
+ },
486
+ {
487
+ rule: "field state per exact path",
488
+ name: "editing a path drops the errors a host set there, below it and around it, and keeps the others",
489
+ async run(createStore) {
490
+ const store = createStore(fixture().options);
491
+ await serverAnswer(store, {
492
+ [FORM_ROOT]: serverIssue("form"),
493
+ contacts: serverIssue("array"),
494
+ "contacts.0": serverIssue("row"),
495
+ "contacts.0.email": serverIssue("field"),
496
+ "contacts.1.email": serverIssue("sibling"),
497
+ nickname: serverIssue("other")
498
+ });
499
+ store.setValue("contacts.0.email", "new@example.com");
500
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["contacts.1.email", "nickname"]);
501
+ }
502
+ },
503
+ {
504
+ rule: "field state per exact path",
505
+ name: "touched marks cover the fields below them, and dirty marks do not cover but report below",
506
+ run(createStore) {
507
+ const store = createStore(fixture().options);
508
+ store.blur("address");
509
+ assertSame(fieldState(store, "address.street").isTouched, true);
510
+ assertSame(fieldState(store, "address").isTouched, true);
511
+ assertSame(fieldState(store, "name").isTouched, false);
512
+ store.setValue("address.street", "2 Side");
513
+ assertSame(fieldState(store, "address").isDirty, true);
514
+ assertSame(fieldState(store, "address.country").isDirty, false);
515
+ }
516
+ },
517
+ {
518
+ rule: "field state in the shape of the values",
519
+ name: "selectFieldState reports error, touched and dirty for a path",
520
+ async run(createStore) {
521
+ const store = createStore(fixture().options);
522
+ assertEqual(fieldState(store, "name"), {
523
+ error: void 0,
524
+ warning: void 0,
525
+ invalid: false,
526
+ isTouched: false,
527
+ isDirty: false,
528
+ isValidating: false,
529
+ hidden: false,
530
+ disabled: false,
531
+ required: false
532
+ });
533
+ store.setValue("name", "Grace");
534
+ store.blur("name");
535
+ await serverAnswer(store, { name: serverIssue("no") });
536
+ assertMatches(fieldState(store, "name"), {
537
+ error: { message: "no" },
538
+ invalid: true,
539
+ isTouched: true,
540
+ isDirty: true
541
+ });
542
+ store.setValue("address.street", "2 Side");
543
+ assertMatches(fieldState(store, "address"), { isDirty: true, isTouched: false });
544
+ }
545
+ },
546
+ {
547
+ rule: "snapshot stability",
548
+ name: "selectValue and selectFieldState read a snapshot by exact path and keep their reference while it holds",
549
+ async run(createStore) {
550
+ const store = createStore(fixture().options);
551
+ const before = store.getSnapshot();
552
+ assertSame(selectValue(before, "address"), before.values.address);
553
+ assertSame(selectValue(before, "contacts.1.email"), "b@example.com");
554
+ await serverAnswer(store, { contacts: serverIssue("array") });
555
+ store.setValue("name", "Grace");
556
+ const after = store.getSnapshot();
557
+ assertSame(selectValue(after, "address"), selectValue(before, "address"));
558
+ assertSame(selectFieldState(after, "contacts").error?.message, "array");
559
+ assertUndefined(selectFieldState(after, "contacts.0.email").error);
560
+ }
561
+ },
562
+ {
563
+ rule: "snapshot stability",
564
+ name: "selectFieldState gives the same object for a snapshot and path, and a new one once its state changed",
565
+ run(createStore) {
566
+ const store = createStore(fixture().options);
567
+ const before = store.getSnapshot();
568
+ assertSame(selectFieldState(before, "name"), selectFieldState(before, "name"));
569
+ assertSame(fieldState(store, "name"), selectFieldState(before, "name"));
570
+ store.blur("name");
571
+ const after = store.getSnapshot();
572
+ assertNotSame(selectFieldState(after, "name"), selectFieldState(before, "name"));
573
+ assertSame(selectFieldState(after, "name").isTouched, true);
574
+ assertSame(selectFieldState(before, "name").isTouched, false);
575
+ }
576
+ },
577
+ {
578
+ rule: "opaque values",
579
+ name: "stores and compares opaque values, such as File arrays, by reference",
580
+ run(createStore) {
581
+ const store = createStore(fixture().options);
582
+ const file = new File(["x"], "a.txt");
583
+ const files = [file];
584
+ store.setValue("files", files);
585
+ assertSame(store.getSnapshot().values.files, files);
586
+ assertSame(store.getValue("files"), files);
587
+ assertSame(Object.isFrozen(files), false);
588
+ const listener = createSpy();
589
+ store.subscribe(listener);
590
+ store.setValue("files", files);
591
+ assertCallCount(listener, 0);
592
+ store.setValue("files", [file]);
593
+ assertCallCount(listener, 1);
594
+ assertNotSame(store.getSnapshot().values.files, files);
595
+ assertSame(store.getSnapshot().values.files[0], file);
596
+ assertSame(store.getSnapshot().dirty.files, true);
597
+ store.setValue("profile", { avatar: [file] });
598
+ assertSame(store.getSnapshot().values.profile.avatar[0], file);
599
+ }
600
+ },
601
+ {
602
+ rule: "branch replacement",
603
+ name: "setValue replaces the whole branch rather than merging into it",
604
+ run(createStore) {
605
+ const store = createStore(fixture().options);
606
+ store.setValue("address", { street: "9 Elm" });
607
+ assertEqual(store.getSnapshot().values.address, { street: "9 Elm" });
608
+ assertUndefined(store.getValue("address.country"));
609
+ store.setValue("contacts", [{ email: "only@example.com" }]);
610
+ assertEqual(contacts(store), ["only@example.com"]);
611
+ assertLength(store.getSnapshot().rowIds.contacts, 1);
612
+ }
613
+ },
614
+ {
615
+ rule: "branch replacement",
616
+ name: "a replacement with the same content changes nothing and keeps references",
617
+ run(createStore) {
618
+ const store = createStore(fixture().options);
619
+ const before = store.getSnapshot();
620
+ const listener = createSpy();
621
+ store.subscribe(listener);
622
+ store.setValue("address", { street: "1 Main", country: "IT" });
623
+ assertCallCount(listener, 0);
624
+ assertSame(store.getSnapshot(), before);
625
+ store.setValue("address", { street: "1 Main", country: "FR" });
626
+ const after = store.getSnapshot();
627
+ assertSame(after.values.contacts, before.values.contacts);
628
+ }
629
+ },
630
+ {
631
+ rule: "snapshot stability",
632
+ name: "notifies once per command even when the command starts a validation",
633
+ run(createStore) {
634
+ const change = createStore(fixture({ validationMode: "onChange" }).options);
635
+ const onChange = createSpy();
636
+ change.subscribe(onChange);
637
+ change.setValue("name", "");
638
+ assertCallCount(onChange, 1);
639
+ assertEqual(change.getSnapshot().validating, { name: true });
640
+ change.fieldArray("contacts").swap(0, 1);
641
+ assertCallCount(onChange, 2);
642
+ const blur = createStore(fixture({ validationMode: "onBlur" }).options);
643
+ const onBlur = createSpy();
644
+ blur.subscribe(onBlur);
645
+ blur.blur("name");
646
+ assertCallCount(onBlur, 1);
647
+ assertEqual(blur.getSnapshot().validating, { name: true });
648
+ }
649
+ },
650
+ {
651
+ rule: "validation mode timing",
652
+ name: "an empty list of paths validates nothing, and undefined validates the form",
653
+ async run(createStore) {
654
+ const store = createStore(fixture({ defaults: { name: "", nickname: "" } }).options);
655
+ const watch = watchValidation(store);
656
+ assertEqual(await store.validate([]), { outcome: "passed" });
657
+ assertSame(watch.started, false);
658
+ assertSame(await store.checkValidity(), false);
659
+ }
660
+ },
661
+ {
662
+ rule: "validation mode timing",
663
+ name: "a row operation validates the array own rules, and the rows inside it are not validated",
664
+ async run(createStore) {
665
+ const { options, gate } = fixture({ validationMode: "onChange", defaults: { invites: [] } });
666
+ const store = createStore(options);
667
+ gate.hold("x");
668
+ store.fieldArray("invites").append({ code: "x", label: "first" });
669
+ await waitFor(() => assertSame(errorsOf(store).invites?.message, "Add two invites"));
670
+ assertSame(gate.pending("x"), 0);
671
+ assertUndefined(errorsOf(store)["invites.0.code"]);
672
+ assertSame(isValidating(store), false);
673
+ }
674
+ },
675
+ {
676
+ rule: "validation mode timing",
677
+ name: "with onSubmit nothing validates until validate is called",
678
+ async run(createStore) {
679
+ const store = createStore(fixture({ validationMode: "onSubmit" }).options);
680
+ const watch = watchValidation(store);
681
+ store.setValue("name", "");
682
+ store.blur("name");
683
+ await settle();
684
+ assertSame(watch.started, false);
685
+ assertEqual(errorsOf(store), {});
686
+ assertEqual(await store.validate(), { outcome: "invalid" });
687
+ assertSame(errorsOf(store).name?.message, "Name is required");
688
+ }
689
+ },
690
+ {
691
+ rule: "validation mode timing",
692
+ name: "with onChange a change validates and a blur alone does not",
693
+ async run(createStore) {
694
+ const store = createStore(fixture({ validationMode: "onChange", defaults: { name: "" } }).options);
695
+ const watch = watchValidation(store);
696
+ store.blur("name");
697
+ await settle();
698
+ assertSame(watch.started, false);
699
+ assertEqual(errorsOf(store), {});
700
+ store.setValue("name", "Ada");
701
+ store.setValue("name", "");
702
+ await waitFor(() => assertSame(errorsOf(store).name?.message, "Name is required"));
703
+ }
704
+ },
705
+ {
706
+ rule: "validation mode timing",
707
+ name: "with onBlur a blur validates and a change alone does not",
708
+ async run(createStore) {
709
+ const store = createStore(fixture({ validationMode: "onBlur" }).options);
710
+ const watch = watchValidation(store);
711
+ store.setValue("name", "");
712
+ await settle();
713
+ assertSame(watch.started, false);
714
+ assertEqual(errorsOf(store), {});
715
+ store.blur("name");
716
+ await waitFor(() => assertSame(errorsOf(store).name?.message, "Name is required"));
717
+ }
718
+ },
719
+ {
720
+ rule: "validation mode timing",
721
+ name: "with all both a change and a blur validate",
722
+ async run(createStore) {
723
+ const onChange = createStore(fixture({ validationMode: "all" }).options);
724
+ onChange.setValue("name", "");
725
+ await waitFor(() => assertDefined(errorsOf(onChange).name));
726
+ onChange.setValue("name", "Ada");
727
+ await waitFor(() => assertUndefined(errorsOf(onChange).name));
728
+ const onBlur = createStore(fixture({ validationMode: "all", defaults: { name: "" } }).options);
729
+ assertEqual(errorsOf(onBlur), {});
730
+ onBlur.blur("name");
731
+ await waitFor(() => assertSame(errorsOf(onBlur).name?.message, "Name is required"));
732
+ }
733
+ },
734
+ {
735
+ rule: "validation mode timing",
736
+ name: "validates only the path that changed",
737
+ async run(createStore) {
738
+ const store = createStore(
739
+ fixture({ validationMode: "onChange", defaults: { name: "", nickname: "ada" } }).options
740
+ );
741
+ store.setValue("nickname", "");
742
+ await waitFor(() => assertSame(errorsOf(store).nickname?.message, "Nickname is required"));
743
+ assertUndefined(errorsOf(store).name);
744
+ }
745
+ },
746
+ {
747
+ rule: "re-validation after a failed check",
748
+ name: "after a failed validate the paths re-validate on change under reValidateMode onChange",
749
+ async run(createStore) {
750
+ const store = createStore(fixture({ validationMode: "onSubmit", reValidateMode: "onChange" }).options);
751
+ store.setValue("name", "");
752
+ assertEqual(await store.validate(), { outcome: "invalid" });
753
+ store.setValue("name", "Ada");
754
+ await waitFor(() => assertUndefined(errorsOf(store).name));
755
+ store.setValue("name", "");
756
+ await waitFor(() => assertSame(errorsOf(store).name?.message, "Name is required"));
757
+ }
758
+ },
759
+ {
760
+ rule: "re-validation after a failed check",
761
+ name: "reValidateMode onBlur re-validates on blur only, and onSubmit not at all",
762
+ async run(createStore) {
763
+ const onBlur = createStore(fixture({ validationMode: "onSubmit", reValidateMode: "onBlur" }).options);
764
+ onBlur.setValue("name", "");
765
+ await onBlur.validate();
766
+ const changeWatch = watchValidation(onBlur);
767
+ onBlur.setValue("name", "Ada");
768
+ await settle();
769
+ assertSame(changeWatch.started, false);
770
+ assertDefined(errorsOf(onBlur).name);
771
+ onBlur.blur("name");
772
+ await waitFor(() => assertUndefined(errorsOf(onBlur).name));
773
+ const onSubmit = createStore(fixture({ validationMode: "onSubmit", reValidateMode: "onSubmit" }).options);
774
+ onSubmit.setValue("name", "");
775
+ await onSubmit.validate();
776
+ const submitWatch = watchValidation(onSubmit);
777
+ onSubmit.setValue("name", "Ada");
778
+ onSubmit.blur("name");
779
+ await settle();
780
+ assertSame(submitWatch.started, false);
781
+ assertDefined(errorsOf(onSubmit).name);
782
+ }
783
+ },
784
+ {
785
+ rule: "re-validation after a failed check",
786
+ name: "a failed check of some paths does not turn on re-validation for the others",
787
+ async run(createStore) {
788
+ const store = createStore(fixture({ validationMode: "onSubmit", reValidateMode: "onChange" }).options);
789
+ store.setValue("name", "");
790
+ assertEqual(await store.validate(["name"]), { outcome: "invalid" });
791
+ const watch = watchValidation(store);
792
+ store.setValue("nickname", "");
793
+ await settle();
794
+ assertSame(watch.started, false);
795
+ assertUndefined(errorsOf(store).nickname);
796
+ store.setValue("name", "Ada");
797
+ await waitFor(() => assertUndefined(errorsOf(store).name));
798
+ }
799
+ },
800
+ {
801
+ rule: "re-validation after a failed check",
802
+ name: "a passing validate starts re-validation of the paths it checked, and no others, and reset ends it",
803
+ async run(createStore) {
804
+ const store = createStore(fixture({ validationMode: "onSubmit", reValidateMode: "onChange" }).options);
805
+ assertEqual(await store.validate(["nickname"]), { outcome: "passed" });
806
+ const other = watchValidation(store);
807
+ store.setValue("name", "");
808
+ await settle();
809
+ assertSame(other.started, false);
810
+ assertEqual(errorsOf(store), {});
811
+ store.setValue("name", "Ada");
812
+ assertEqual(await store.validate(), { outcome: "passed" });
813
+ store.setValue("name", "");
814
+ await waitFor(() => assertSame(errorsOf(store).name?.type, "required"));
815
+ assertEqual(await store.validate(), { outcome: "invalid" });
816
+ store.reset();
817
+ const afterReset = watchValidation(store);
818
+ store.setValue("name", "");
819
+ await settle();
820
+ assertSame(afterReset.started, false);
821
+ assertEqual(errorsOf(store), {});
822
+ }
823
+ },
824
+ {
825
+ rule: "silent validity check",
826
+ name: "checkValidity reports validity and changes nothing, not even for subscribers",
827
+ async run(createStore) {
828
+ const store = createStore(fixture().options);
829
+ store.setValue("name", "");
830
+ const before = store.getSnapshot();
831
+ const listener = createSpy();
832
+ store.subscribe(listener);
833
+ assertSame(await store.checkValidity(), false);
834
+ assertSame(store.getSnapshot(), before);
835
+ assertCallCount(listener, 0);
836
+ assertEqual(errorsOf(store), {});
837
+ }
838
+ },
839
+ {
840
+ rule: "touched and dirty",
841
+ name: "touched is set by a blur, not by a change, and cleared by reset",
842
+ run(createStore) {
843
+ const store = createStore(fixture().options);
844
+ store.setValue("name", "Grace");
845
+ assertEqual(store.getSnapshot().touched, {});
846
+ store.blur("name");
847
+ assertEqual(store.getSnapshot().touched, { name: true });
848
+ const touched = store.getSnapshot().touched;
849
+ store.blur("name");
850
+ assertSame(store.getSnapshot().touched, touched);
851
+ store.reset();
852
+ assertEqual(store.getSnapshot().touched, {});
853
+ }
854
+ },
855
+ {
856
+ rule: "touched and dirty",
857
+ name: "dirty follows the difference from the defaults, and appending a row does not make the rows before it dirty",
858
+ run(createStore) {
859
+ const store = createStore(fixture().options);
860
+ assertEqual(store.getSnapshot().dirty, {});
861
+ store.setValue("address.street", "2 Side");
862
+ assertEqual(marked(store.getSnapshot().dirty), ["address.street"]);
863
+ store.setValue("address.street", "1 Main");
864
+ assertEqual(store.getSnapshot().dirty, {});
865
+ store.setValue("contacts.1.email", "x@example.com");
866
+ assertEqual(marked(store.getSnapshot().dirty), ["contacts.1.email"]);
867
+ store.setValue("contacts.1.email", "b@example.com");
868
+ store.fieldArray("contacts").append({ email: "x@example.com" });
869
+ assertEqual(marked(store.getSnapshot().dirty), ["contacts", "contacts.3.email"]);
870
+ store.setValue("name", "Grace");
871
+ store.reset();
872
+ assertEqual(store.getSnapshot().dirty, {});
873
+ }
874
+ },
875
+ {
876
+ rule: "touched and dirty",
877
+ name: "reset without values restores the defaults and clears every source of state",
878
+ async run(createStore) {
879
+ const store = createStore(fixture({ validationMode: "onSubmit" }).options);
880
+ await serverAnswer(store, { nickname: serverIssue("no") });
881
+ store.setValue("name", "");
882
+ store.blur("name");
883
+ assertEqual(await store.validate(), { outcome: "invalid" });
884
+ store.reset();
885
+ const snapshot = store.getSnapshot();
886
+ assertSame(snapshot.values.name, "Ada");
887
+ assertEqual(snapshot.errors, {});
888
+ assertEqual(snapshot.touched, {});
889
+ assertEqual(snapshot.dirty, {});
890
+ }
891
+ },
892
+ {
893
+ rule: "touched and dirty",
894
+ name: "reset with values makes them the new defaults",
895
+ run(createStore) {
896
+ const store = createStore(fixture().options);
897
+ store.setValue("name", "Grace");
898
+ store.reset({ name: "Hedy" });
899
+ assertEqual(store.getSnapshot().values, { name: "Hedy" });
900
+ assertEqual(store.getSnapshot().dirty, {});
901
+ store.setValue("name", "Ada");
902
+ assertEqual(store.getSnapshot().dirty, { name: true });
903
+ store.reset();
904
+ assertSame(store.getValue("name"), "Hedy");
905
+ }
906
+ },
907
+ {
908
+ rule: "array reindexing",
909
+ name: "swap moves rows with their ids, errors and touched state in one update",
910
+ async run(createStore) {
911
+ const store = createStore(fixture().options);
912
+ await serverAnswer(store, {
913
+ "contacts.0.email": serverIssue("zero"),
914
+ "contacts.1.email": serverIssue("one")
915
+ });
916
+ store.blur("contacts.1.email");
917
+ const [first, second, third] = ids(store);
918
+ const listener = createSpy(() => {
919
+ const snapshot = store.getSnapshot();
920
+ assertEqual(snapshot.rowIds.contacts, [second, first, third]);
921
+ assertEqual(contacts(store), ["b@example.com", "a@example.com", "c@example.com"]);
922
+ assertSame(snapshot.errors["contacts.0.email"]?.message, "one");
923
+ assertSame(snapshot.errors["contacts.1.email"]?.message, "zero");
924
+ assertEqual(marked(snapshot.touched), ["contacts.0.email"]);
925
+ });
926
+ store.subscribe(listener);
927
+ store.fieldArray("contacts").swap(0, 1);
928
+ assertCallCount(listener, 1);
929
+ }
930
+ },
931
+ {
932
+ rule: "array reindexing",
933
+ name: "move, prepend, insert and remove shift state to the rows it belongs to",
934
+ async run(createStore) {
935
+ const store = createStore(fixture().options);
936
+ const array = store.fieldArray("contacts");
937
+ const [first, second, third] = ids(store);
938
+ await serverAnswer(store, { "contacts.2.email": serverIssue("two") });
939
+ store.blur("contacts.2.email");
940
+ array.move(2, 0);
941
+ assertEqual(ids(store), [third, first, second]);
942
+ assertSame(errorsOf(store)["contacts.0.email"]?.message, "two");
943
+ assertEqual(marked(store.getSnapshot().touched), ["contacts.0.email"]);
944
+ array.prepend({ email: "new@example.com" });
945
+ assertEqual(ids(store).slice(1), [third, first, second]);
946
+ assertEqual(Object.keys(errorsOf(store)), ["contacts.1.email"]);
947
+ array.insert(1, { email: "mid@example.com" });
948
+ assertEqual(Object.keys(errorsOf(store)), ["contacts.2.email"]);
949
+ assertEqual(ids(store).slice(2), [third, first, second]);
950
+ array.remove(0);
951
+ assertEqual(Object.keys(errorsOf(store)), ["contacts.1.email"]);
952
+ assertEqual(marked(store.getSnapshot().touched), ["contacts.1.email"]);
953
+ array.remove(1);
954
+ assertEqual(errorsOf(store), {});
955
+ assertEqual(store.getSnapshot().touched, {});
956
+ assertEqual(contacts(store), ["mid@example.com", "a@example.com", "b@example.com"]);
957
+ assertEqual(
958
+ toFieldArrayValue(ids(store), array).fields.map(({ id }) => id),
959
+ ids(store)
960
+ );
961
+ }
962
+ },
963
+ {
964
+ rule: "array reindexing",
965
+ name: "append keeps existing rows and ids, and an array value appends several rows",
966
+ run(createStore) {
967
+ const store = createStore(fixture().options);
968
+ const before = ids(store);
969
+ store.fieldArray("contacts").append([{ email: "x@example.com" }, { email: "y@example.com" }]);
970
+ assertEqual(ids(store).slice(0, 3), before);
971
+ assertLength(ids(store), 5);
972
+ assertSame(new Set(ids(store)).size, 5);
973
+ assertEqual(contacts(store).slice(3), ["x@example.com", "y@example.com"]);
974
+ }
975
+ },
976
+ {
977
+ rule: "array reindexing",
978
+ name: "update and replace give the rows new ids and drop their state",
979
+ async run(createStore) {
980
+ const store = createStore(fixture().options);
981
+ const array = store.fieldArray("contacts");
982
+ const before = ids(store);
983
+ await serverAnswer(store, {
984
+ "contacts.0.email": serverIssue("zero"),
985
+ "contacts.1.email": serverIssue("one")
986
+ });
987
+ store.blur("contacts.0.email");
988
+ array.update(1, { email: "u@example.com" });
989
+ assertSame(ids(store)[0], before[0]);
990
+ assertNotSame(ids(store)[1], before[1]);
991
+ assertSame(ids(store)[2], before[2]);
992
+ assertEqual(Object.keys(errorsOf(store)), ["contacts.0.email"]);
993
+ assertEqual(marked(store.getSnapshot().touched), ["contacts.0.email"]);
994
+ array.replace([{ email: "r@example.com" }]);
995
+ assertLength(ids(store), 1);
996
+ assertNotIncludes(before, ids(store)[0]);
997
+ assertEqual(errorsOf(store), {});
998
+ assertEqual(store.getSnapshot().touched, {});
999
+ }
1000
+ },
1001
+ {
1002
+ rule: "array reindexing",
1003
+ name: "an operation naming a row that does not exist does nothing, and remove removes each row once",
1004
+ run(createStore) {
1005
+ const store = createStore(fixture().options);
1006
+ const array = store.fieldArray("contacts");
1007
+ const before = store.getSnapshot();
1008
+ const listener = createSpy();
1009
+ store.subscribe(listener);
1010
+ array.swap(0, 5);
1011
+ array.move(-1, 0);
1012
+ array.move(0, 3);
1013
+ array.update(3, { email: "z@example.com" });
1014
+ array.insert(4, { email: "z@example.com" });
1015
+ array.insert(1.5, { email: "z@example.com" });
1016
+ array.remove(7);
1017
+ array.remove([]);
1018
+ assertCallCount(listener, 0);
1019
+ assertSame(store.getSnapshot(), before);
1020
+ array.insert(3, { email: "end@example.com" });
1021
+ assertLength(contacts(store), 4);
1022
+ array.remove([0, 0, 9]);
1023
+ assertEqual(contacts(store), ["b@example.com", "c@example.com", "end@example.com"]);
1024
+ array.remove();
1025
+ assertEqual(contacts(store), []);
1026
+ assertEqual(store.getSnapshot().rowIds.contacts, []);
1027
+ }
1028
+ },
1029
+ {
1030
+ rule: "array reindexing",
1031
+ name: "nested and scalar arrays carry their rows state, and shrinking drops it",
1032
+ async run(createStore) {
1033
+ const store = createStore(
1034
+ fixture({
1035
+ defaults: {
1036
+ name: "Ada",
1037
+ nickname: "ada",
1038
+ slug: "free",
1039
+ code: "12345",
1040
+ contacts: [{ email: "a@example.com" }],
1041
+ invites: [{ code: "one" }, { code: "two" }],
1042
+ groups: [
1043
+ { title: "a", members: [{ label: "x" }, { label: "y" }] },
1044
+ { title: "b", members: [{ label: "z" }] }
1045
+ ],
1046
+ tags: ["p", "q", "r"]
1047
+ }
1048
+ }).options
1049
+ );
1050
+ await serverAnswer(store, {
1051
+ "groups.0.members.1.label": serverIssue("inner"),
1052
+ "tags.1": serverIssue("scalar")
1053
+ });
1054
+ store.blur("groups.0.members.1.label");
1055
+ store.blur("tags.2");
1056
+ store.fieldArray("groups").swap(0, 1);
1057
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["groups.1.members.1.label", "tags.1"]);
1058
+ assertEqual(marked(store.getSnapshot().touched).toSorted(), ["groups.1.members.1.label", "tags.2"]);
1059
+ store.fieldArray("groups.1.members").remove(0);
1060
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["groups.1.members.0.label", "tags.1"]);
1061
+ store.fieldArray("tags").remove(0);
1062
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["groups.1.members.0.label", "tags.0"]);
1063
+ assertEqual(marked(store.getSnapshot().touched).toSorted(), ["groups.1.members.0.label", "tags.1"]);
1064
+ store.setValue("groups", [{ title: "only", members: [] }]);
1065
+ assertEqual(Object.keys(errorsOf(store)), ["tags.0"]);
1066
+ assertEqual(marked(store.getSnapshot().touched), ["tags.1"]);
1067
+ assertEqual(store.getSnapshot().rowIds["groups.0.members"], []);
1068
+ }
1069
+ },
1070
+ {
1071
+ rule: "array reindexing",
1072
+ name: "hydrates from the serialized snapshot of another container: its values and ids after row operations, and fresh ids for new rows",
1073
+ async run(createStore) {
1074
+ const server = createStore(fixture().options);
1075
+ server.fieldArray("contacts").remove(0);
1076
+ server.fieldArray("contacts").prepend({ email: "first@example.com" });
1077
+ server.fieldArray("invites").swap(0, 1);
1078
+ server.setValue("name", "Typed");
1079
+ const hydration = JSON.parse(JSON.stringify(toHydration(server.getSnapshot())));
1080
+ const client = createStore(fixture({ snapshot: hydration }).options);
1081
+ const was = server.getSnapshot();
1082
+ assertEqual(client.getSnapshot().values, was.values);
1083
+ assertEqual(client.getSnapshot().rowIds, was.rowIds);
1084
+ assertEqual(client.getSnapshot().dirty, was.dirty);
1085
+ assertEqual(client.getServerSnapshot().rowIds, was.rowIds);
1086
+ client.fieldArray("contacts").append({ email: "new@example.com" });
1087
+ client.fieldArray("contacts").remove(0);
1088
+ const adopted = Object.values(was.rowIds).flat();
1089
+ const minted = ids(client).filter((id) => !adopted.includes(id));
1090
+ assertLength(minted, 1);
1091
+ assertEqual(ids(client).slice(0, 2), was.rowIds.contacts.slice(1));
1092
+ assertSame(new Set(ids(client)).size, ids(client).length);
1093
+ }
1094
+ },
1095
+ {
1096
+ rule: "array reindexing",
1097
+ name: "a branch written over rows drops the state of rows that no longer exist",
1098
+ async run(createStore) {
1099
+ const store = createStore(fixture().options);
1100
+ await serverAnswer(store, { "contacts.2.email": serverIssue("two") });
1101
+ store.blur("contacts.2.email");
1102
+ store.setValue("contacts", [{ email: "a@example.com" }]);
1103
+ assertEqual(errorsOf(store), {});
1104
+ assertEqual(store.getSnapshot().touched, {});
1105
+ const checked = createStore(fixture().options);
1106
+ checked.setValue("contacts.2.email", "");
1107
+ assertEqual(await checked.validate(["contacts.2.email"]), { outcome: "invalid" });
1108
+ checked.setValue("contacts", [{ email: "a@example.com" }]);
1109
+ checked.fieldArray("contacts").append({ email: "fresh@example.com" });
1110
+ checked.fieldArray("contacts").append({ email: "" });
1111
+ assertEqual(errorsOf(checked), {});
1112
+ const watch = watchValidation(checked);
1113
+ checked.setValue("contacts.2.email", "changed@example.com");
1114
+ await settle();
1115
+ assertSame(watch.started, false);
1116
+ }
1117
+ },
1118
+ {
1119
+ rule: "array reindexing",
1120
+ name: "keeps row ids by position when a branch is replaced with the same number of rows",
1121
+ run(createStore) {
1122
+ const store = createStore(fixture().options);
1123
+ const before = ids(store);
1124
+ store.setValue("contacts", [{ email: "1" }, { email: "2" }, { email: "3" }]);
1125
+ assertEqual(ids(store), before);
1126
+ store.setValue("contacts", [{ email: "1" }]);
1127
+ assertEqual(ids(store), before.slice(0, 1));
1128
+ }
1129
+ },
1130
+ {
1131
+ rule: "error sources",
1132
+ name: "lists the server, then each failed rule in rule order, then the native fact, and shows the first",
1133
+ async run(createStore) {
1134
+ const { options, gate } = fixture({
1135
+ elements: [
1136
+ element({
1137
+ $type: "input",
1138
+ name: "slug",
1139
+ rules: [
1140
+ { $type: "gate", id: "first" },
1141
+ { $type: "gate", id: "second" }
1142
+ ]
1143
+ })
1144
+ ],
1145
+ defaults: { slug: "free" }
1146
+ });
1147
+ const store = createStore(options);
1148
+ const answer = await passedSubmission(store);
1149
+ answer.setServerErrors({ slug: { message: "Taken" } });
1150
+ gate.settle("free", false);
1151
+ store.reportNative({ slug: { type: "patternMismatch", params: { pattern: "[0-9]+" } } });
1152
+ assertEqual(await store.validate(["slug"]), { outcome: "invalid" });
1153
+ const shown = errorsOf(store).slug;
1154
+ assertEqual(shown.all, [
1155
+ { type: "server", message: "Taken", source: "server" },
1156
+ { type: "gate", source: "validation", ruleId: "first" },
1157
+ { type: "gate", source: "validation", ruleId: "second" },
1158
+ { type: "patternMismatch", source: "native", params: { pattern: "[0-9]+" } }
1159
+ ]);
1160
+ const { all, ...fields } = shown;
1161
+ assertEqual(fields, all[0]);
1162
+ answer.setServerErrors({});
1163
+ const withoutServer = errorsOf(store).slug;
1164
+ assertSame(withoutServer.type, "gate");
1165
+ assertLength(withoutServer.all, 3);
1166
+ }
1167
+ },
1168
+ {
1169
+ rule: "error sources",
1170
+ name: "native validity is an input to validation: it shows with the timing of validation, and notifies nobody alone",
1171
+ async run(createStore) {
1172
+ const store = createStore(fixture({ validationMode: "onSubmit" }).options);
1173
+ store.reportNative({ name: { type: "valueMissing", message: "Fill in" } });
1174
+ assertEqual(await store.validate(["name"]), { outcome: "invalid" });
1175
+ assertMatches(errorsOf(store).name, { source: "native", type: "valueMissing", message: "Fill in" });
1176
+ store.reportNative({ name: null });
1177
+ await waitFor(() => assertEqual(errorsOf(store), {}));
1178
+ assertEqual(await store.validate(["name"]), { outcome: "passed" });
1179
+ const quiet = createStore(fixture({ validationMode: "onSubmit" }).options);
1180
+ quiet.blur("name");
1181
+ quiet.reportNative({ name: { type: "valueMissing" } });
1182
+ assertEqual(errorsOf(quiet), {});
1183
+ const onChange = createStore(fixture({ validationMode: "onChange" }).options);
1184
+ onChange.setValue("nickname", "nick");
1185
+ onChange.reportNative({ nickname: { type: "valueMissing", message: "Fill in this field" } });
1186
+ await waitFor(() => assertSame(errorsOf(onChange).nickname?.source, "native"));
1187
+ assertSame(errorsOf(onChange).nickname.message, "Fill in this field");
1188
+ }
1189
+ },
1190
+ {
1191
+ rule: "error sources",
1192
+ name: "validate and checkValidity count native facts and server errors alike, and checkValidity writes nothing",
1193
+ async run(createStore) {
1194
+ const store = createStore(fixture().options);
1195
+ store.reportNative({ name: { type: "valueMissing" } });
1196
+ const before = store.getSnapshot();
1197
+ assertSame(await store.checkValidity(), false);
1198
+ assertSame(store.getSnapshot(), before);
1199
+ assertEqual(await store.validate(), { outcome: "invalid" });
1200
+ store.reportNative({ name: null });
1201
+ assertSame(await store.checkValidity(), true);
1202
+ assertEqual(await store.validate(), { outcome: "passed" });
1203
+ await serverAnswer(store, { name: { message: "Taken" } });
1204
+ assertSame(await store.checkValidity(), false);
1205
+ assertEqual(await store.validate(), { outcome: "invalid" });
1206
+ }
1207
+ },
1208
+ {
1209
+ rule: "error sources",
1210
+ name: "a cleared server source lets validate pass, and the form-level error alone blocks it",
1211
+ async run(createStore) {
1212
+ const store = createStore(fixture().options);
1213
+ const answer = await passedSubmission(store);
1214
+ answer.setServerErrors({ [FORM_ROOT]: { message: "Service unavailable" } });
1215
+ assertEqual(await store.validate(), { outcome: "invalid" });
1216
+ assertEqual(await store.validate(["name"]), { outcome: "passed" });
1217
+ answer.setServerErrors({});
1218
+ assertEqual(await store.validate(), { outcome: "passed" });
1219
+ assertEqual(errorsOf(store), {});
1220
+ }
1221
+ },
1222
+ {
1223
+ rule: "error sources",
1224
+ name: "a source may give a type or a message or neither, and the error still counts",
1225
+ async run(createStore) {
1226
+ const store = createStore(fixture().options);
1227
+ await serverAnswer(store, {
1228
+ name: { message: "Taken" },
1229
+ nickname: {},
1230
+ "address.street": { type: "taken" }
1231
+ });
1232
+ const { errors } = store.getSnapshot();
1233
+ assertMatches(errors.name, { type: "server", message: "Taken", source: "server" });
1234
+ assertEqual(errors.nickname, {
1235
+ type: "server",
1236
+ source: "server",
1237
+ all: [{ type: "server", source: "server" }]
1238
+ });
1239
+ assertMatches(errors["address.street"], { type: "taken", source: "server" });
1240
+ assertUndefined(errors["address.street"].message);
1241
+ assertEqual(await store.validate(["nickname"]), { outcome: "invalid" });
1242
+ }
1243
+ },
1244
+ {
1245
+ rule: "error sources",
1246
+ name: "identical errors change nothing and keep their objects, and a changed one replaces only its own",
1247
+ async run(createStore) {
1248
+ const store = createStore(fixture().options);
1249
+ const answer = await passedSubmission(store);
1250
+ answer.setServerErrors({ name: { message: "Taken" }, nickname: { message: "Taken" } });
1251
+ const before = store.getSnapshot();
1252
+ const listener = createSpy();
1253
+ store.subscribe(listener);
1254
+ answer.setServerErrors({ name: { message: "Taken" }, nickname: { message: "Taken" } });
1255
+ assertCallCount(listener, 0);
1256
+ assertSame(store.getSnapshot(), before);
1257
+ answer.setServerErrors({ name: { message: "Taken" }, nickname: { message: "Other" } });
1258
+ const after = store.getSnapshot();
1259
+ assertSame(after.errors.name, before.errors.name);
1260
+ assertNotSame(after.errors.nickname, before.errors.nickname);
1261
+ assertSame(selectFieldState(after, "name"), selectFieldState(before, "name"));
1262
+ }
1263
+ },
1264
+ {
1265
+ rule: "error sources",
1266
+ name: "a validation that finds the same thing again keeps its error objects, and reset clears native facts",
1267
+ async run(createStore) {
1268
+ const store = createStore(fixture({ defaults: { name: "" } }).options);
1269
+ await store.validate(["name"]);
1270
+ const first = store.getSnapshot();
1271
+ await store.validate(["name"]);
1272
+ assertSame(store.getSnapshot().errors.name, first.errors.name);
1273
+ assertSame(selectFieldState(store.getSnapshot(), "name"), selectFieldState(first, "name"));
1274
+ store.blur("nickname");
1275
+ store.reportNative({ nickname: { type: "valueMissing" } });
1276
+ store.reset();
1277
+ store.setValue("nickname", "");
1278
+ assertEqual(await store.validate(), { outcome: "invalid" });
1279
+ assertEqual(
1280
+ errorsOf(store).nickname.all.map(({ source }) => source),
1281
+ ["validation"]
1282
+ );
1283
+ }
1284
+ },
1285
+ {
1286
+ rule: "array reindexing",
1287
+ name: "toFieldArrayValue gives the same object while the rows are the same, and the commands are one object per path",
1288
+ run(createStore) {
1289
+ const store = createStore(fixture().options);
1290
+ const commands = store.fieldArray("contacts");
1291
+ assertSame(store.fieldArray("contacts"), commands);
1292
+ const rows = store.getSnapshot().rowIds.contacts;
1293
+ assertSame(toFieldArrayValue(rows, commands), toFieldArrayValue(rows, commands));
1294
+ store.setValue("name", "Grace");
1295
+ assertSame(
1296
+ toFieldArrayValue(store.getSnapshot().rowIds.contacts, commands),
1297
+ toFieldArrayValue(rows, commands)
1298
+ );
1299
+ commands.append({ email: "x@example.com" });
1300
+ assertNotSame(
1301
+ toFieldArrayValue(store.getSnapshot().rowIds.contacts, commands),
1302
+ toFieldArrayValue(rows, commands)
1303
+ );
1304
+ }
1305
+ },
1306
+ {
1307
+ rule: "array reindexing",
1308
+ name: "a hydration whose row ids do not match its own values throws instead of starting from it",
1309
+ run(createStore) {
1310
+ const good = toHydration(createStore(fixture().options).getSnapshot());
1311
+ const start = (hydration) => () => createStore(fixture({ snapshot: hydration }).options);
1312
+ assertThrows(start({ ...good, rowIds: { ...good.rowIds, contacts: ["a", "b"] } }));
1313
+ assertThrows(start({ ...good, rowIds: { ...good.rowIds, invites: ["k", "k"] } }));
1314
+ assertThrows(start({ ...good, rowIds: { ...good.rowIds, name: ["x"] } }));
1315
+ assertThrows(start({ ...good, rowIds: { ...good.rowIds, contacts: ["a", "b", 7] } }));
1316
+ assertThrows(start({ ...good, rowIds: { contacts: good.rowIds.contacts } }));
1317
+ assertThrows(start({ ...good, rowIds: { ...good.rowIds, invites: good.rowIds.contacts } }));
1318
+ assertThrows(start({ values: good.values }));
1319
+ assertThrows(start("nothing"));
1320
+ assertThrows(start(null));
1321
+ assertDoesNotThrow(start(good));
1322
+ }
1323
+ },
1324
+ {
1325
+ rule: "array reindexing",
1326
+ name: "containers built from the same defaults mint the same row ids",
1327
+ run(createStore) {
1328
+ assertEqual(
1329
+ createStore(fixture().options).getSnapshot().rowIds,
1330
+ createStore(fixture().options).getSnapshot().rowIds
1331
+ );
1332
+ }
1333
+ },
1334
+ {
1335
+ rule: "error sources",
1336
+ name: "refuses a reserved path and a row that does not exist, and writes nothing",
1337
+ async run(createStore) {
1338
+ const store = createStore(fixture().options);
1339
+ const answer = await passedSubmission(store);
1340
+ const values = store.getSnapshot().values;
1341
+ assertThrows(() => answer.setServerErrors({ __secret: { message: "x" } }), /reserved segment '__secret'/);
1342
+ assertThrows(() => answer.setServerErrors({ "contacts.__proto__.email": { message: "x" } }), /reserved/);
1343
+ assertThrows(() => store.blur(FORM_ROOT), /reserved/);
1344
+ assertThrows(() => store.setValue("__x", 1), /reserved/);
1345
+ assertThrows(() => store.reportNative({ __x: { type: "valueMissing" } }), /reserved/);
1346
+ assertThrows(
1347
+ () => answer.setServerErrors({ "contacts.4.email": { message: "bounced" } }),
1348
+ /'contacts.4.email' names row '4' of 'contacts', which has 3 rows/
1349
+ );
1350
+ assertThrows(() => answer.setServerErrors({ "contacts.x": { message: "x" } }), /names row 'x'/);
1351
+ assertThrows(() => answer.setServerErrors({ "contacts.-1.email": { message: "x" } }), /names row '-1'/);
1352
+ assertThrows(() => answer.setServerErrors({ "contacts.1e-7.email": { message: "x" } }), /names row '1e-7'/);
1353
+ assertThrows(() => answer.setServerErrors({ "contacts.01.email": { message: "x" } }), /names row '01'/);
1354
+ assertSame(store.getSnapshot().values, values);
1355
+ assertEqual(store.getSnapshot().errors, {});
1356
+ assertDoesNotThrow(
1357
+ () => answer.setServerErrors({ "nothing.here": { message: "fine" }, [FORM_ROOT]: { message: "form" } })
1358
+ );
1359
+ assertEqual(Object.keys(errorsOf(store)), [FORM_ROOT]);
1360
+ }
1361
+ },
1362
+ {
1363
+ rule: "stale async results",
1364
+ name: "applies an async result that is still current",
1365
+ async run(createStore) {
1366
+ const { options, gate } = fixture();
1367
+ const store = createStore(options);
1368
+ gate.hold("taken");
1369
+ store.setValue("slug", "taken");
1370
+ const run = store.validate(["slug"]);
1371
+ await waitFor(() => assertSame(gate.pending("taken"), 1));
1372
+ assertSame(isValidating(store), true);
1373
+ gate.settle("taken", false);
1374
+ assertEqual(await run, { outcome: "invalid" });
1375
+ assertSame(errorsOf(store).slug?.type, "gate");
1376
+ assertSame(isValidating(store), false);
1377
+ }
1378
+ },
1379
+ {
1380
+ rule: "stale async results",
1381
+ name: "drops a result that arrives after a reset",
1382
+ async run(createStore) {
1383
+ const { options, gate } = fixture({ validationMode: "onChange", defaults: { slug: "taken" } });
1384
+ const store = createStore(options);
1385
+ gate.hold("taken");
1386
+ store.setValue("slug", "other");
1387
+ store.setValue("slug", "taken");
1388
+ await waitFor(() => assertSame(gate.pending("taken"), 1));
1389
+ store.reset();
1390
+ assertSame(store.getValue("slug"), "taken");
1391
+ gate.settle("taken", false);
1392
+ await waitFor(() => assertSame(isValidating(store), false));
1393
+ await settle();
1394
+ assertEqual(errorsOf(store), {});
1395
+ }
1396
+ },
1397
+ {
1398
+ rule: "stale async results",
1399
+ name: "applies a late result to the row it was for after the rows moved",
1400
+ async run(createStore) {
1401
+ const { options, gate } = fixture({
1402
+ validationMode: "onBlur",
1403
+ defaults: {
1404
+ invites: [
1405
+ { code: "one", label: "first" },
1406
+ { code: "one", label: "second" }
1407
+ ]
1408
+ }
1409
+ });
1410
+ const store = createStore(options);
1411
+ gate.hold("one");
1412
+ store.blur("invites.0.code");
1413
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1414
+ store.fieldArray("invites").swap(0, 1);
1415
+ gate.settle("one", false);
1416
+ await waitFor(() => assertSame(isValidating(store), false));
1417
+ await settle();
1418
+ assertSame(store.getValue("invites.1.label"), "first");
1419
+ assertEqual(Object.keys(errorsOf(store)), ["invites.1.code"]);
1420
+ assertEqual(marked(store.getSnapshot().touched), ["invites.1.code"]);
1421
+ }
1422
+ },
1423
+ {
1424
+ rule: "stale async results",
1425
+ name: "drops a late result for a row that was removed",
1426
+ async run(createStore) {
1427
+ const { options, gate } = fixture({
1428
+ validationMode: "onBlur",
1429
+ defaults: {
1430
+ invites: [
1431
+ { code: "one", label: "first" },
1432
+ { code: "one", label: "second" }
1433
+ ]
1434
+ }
1435
+ });
1436
+ const store = createStore(options);
1437
+ gate.hold("one");
1438
+ store.blur("invites.0.code");
1439
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1440
+ store.fieldArray("invites").remove(0);
1441
+ gate.settle("one", false);
1442
+ await waitFor(() => assertSame(isValidating(store), false));
1443
+ await settle();
1444
+ assertEqual(errorsOf(store), {});
1445
+ }
1446
+ },
1447
+ {
1448
+ rule: "stale async results",
1449
+ name: "a validate that a reset overtook is dropped and not run again against the defaults",
1450
+ async run(createStore) {
1451
+ const { options, gate } = fixture({ defaults: { name: "Ada", slug: "taken" } });
1452
+ const store = createStore(options);
1453
+ gate.hold("taken");
1454
+ const run = store.validate(["slug"]);
1455
+ await waitFor(() => assertSame(gate.pending("taken"), 1));
1456
+ store.setValue("slug", "other");
1457
+ store.reset();
1458
+ const watch = watchValidation(store);
1459
+ gate.settle("taken", false);
1460
+ assertEqual(await run, { outcome: "overtaken" });
1461
+ assertSame(watch.started, false);
1462
+ assertSame(gate.pending("taken"), 0);
1463
+ assertEqual(errorsOf(store), {});
1464
+ assertSame(isValidating(store), false);
1465
+ }
1466
+ },
1467
+ {
1468
+ rule: "stale async results",
1469
+ name: "drops a late result when its rows were replaced",
1470
+ async run(createStore) {
1471
+ const { options, gate } = fixture({
1472
+ validationMode: "onBlur",
1473
+ defaults: { invites: [{ code: "one", label: "first" }] }
1474
+ });
1475
+ const store = createStore(options);
1476
+ gate.hold("one");
1477
+ store.blur("invites.0.code");
1478
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1479
+ store.fieldArray("invites").replace([{ code: "one", label: "second" }]);
1480
+ gate.settle("one", false);
1481
+ await waitFor(() => assertSame(isValidating(store), false));
1482
+ await settle();
1483
+ assertEqual(errorsOf(store), {});
1484
+ }
1485
+ },
1486
+ {
1487
+ rule: "stale async results",
1488
+ name: "follows a nested row when the rows around it move",
1489
+ async run(createStore) {
1490
+ const { options, gate } = fixture({
1491
+ validationMode: "onBlur",
1492
+ defaults: {
1493
+ groups: [
1494
+ { title: "a", members: [{ code: "x" }, { code: "one" }] },
1495
+ { title: "b", members: [{ code: "y" }] }
1496
+ ]
1497
+ }
1498
+ });
1499
+ const store = createStore(options);
1500
+ gate.hold("one");
1501
+ store.blur("groups.0.members.1.code");
1502
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1503
+ store.fieldArray("groups").swap(0, 1);
1504
+ store.fieldArray("groups.1.members").remove(0);
1505
+ gate.settle("one", false);
1506
+ await waitFor(() => assertSame(isValidating(store), false));
1507
+ await settle();
1508
+ assertEqual(Object.keys(errorsOf(store)), ["groups.1.members.0.code"]);
1509
+ }
1510
+ },
1511
+ {
1512
+ rule: "stale async results",
1513
+ name: "an explicit validate that lost part of its result ends with the verdict of the current state",
1514
+ async run(createStore) {
1515
+ const { options, gate } = fixture();
1516
+ const store = createStore(options);
1517
+ gate.hold("one", "two");
1518
+ const run = store.validate(["invites"]);
1519
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1520
+ store.fieldArray("invites").swap(0, 1);
1521
+ gate.settle("one", false);
1522
+ gate.settle("two", true);
1523
+ assertEqual(await run, { outcome: "invalid" });
1524
+ assertEqual(Object.keys(errorsOf(store)), ["invites.1.code"]);
1525
+ assertSame(isValidating(store), false);
1526
+ }
1527
+ },
1528
+ {
1529
+ rule: "stale async results",
1530
+ name: "drops a result for a path whose value was written while it was running, and ends with the verdict of the current state",
1531
+ async run(createStore) {
1532
+ const { options, gate } = fixture();
1533
+ const store = createStore(options);
1534
+ gate.hold("taken");
1535
+ store.setValue("slug", "taken");
1536
+ const run = store.validate(["slug"]);
1537
+ await waitFor(() => assertSame(gate.pending("taken"), 1));
1538
+ store.setValue("slug", "free");
1539
+ gate.settle("taken", false);
1540
+ assertEqual(await run, { outcome: "passed" });
1541
+ assertUndefined(errorsOf(store).slug);
1542
+ }
1543
+ },
1544
+ {
1545
+ rule: "freshness",
1546
+ name: "a second edit during a validation retry is overtaken, not the validity of a value it never checked",
1547
+ async run(createStore) {
1548
+ const { options, gate } = fixture();
1549
+ const store = createStore(options);
1550
+ gate.settle("taken", false);
1551
+ gate.hold("a", "b");
1552
+ store.setValue("slug", "a");
1553
+ const run = store.validate(["slug"]);
1554
+ await waitFor(() => assertSame(gate.pending("a"), 1));
1555
+ store.setValue("slug", "b");
1556
+ gate.settle("a", true);
1557
+ await waitFor(() => assertSame(gate.pending("b"), 1));
1558
+ store.setValue("slug", "taken");
1559
+ gate.settle("b", true);
1560
+ assertEqual(await run, { outcome: "overtaken" });
1561
+ assertEqual(await store.validate(["slug"]), { outcome: "invalid" });
1562
+ }
1563
+ },
1564
+ {
1565
+ rule: "freshness",
1566
+ name: "a validate and a submit that one reset overtook end the same way: overtaken is no verdict, and the next check starts clean",
1567
+ async run(createStore) {
1568
+ const { options, gate } = fixture({ defaults: { name: "Ada", nickname: "Ace", slug: "taken" } });
1569
+ const store = createStore(options);
1570
+ gate.hold("taken");
1571
+ const validated = store.validate(["slug"]);
1572
+ const submitted = store.submit();
1573
+ await waitFor(() => assertSame(gate.pending("taken"), 2));
1574
+ store.reset();
1575
+ gate.settle("taken", false);
1576
+ assertEqual(await validated, { outcome: "overtaken" });
1577
+ assertEqual(await submitted, { outcome: "overtaken" });
1578
+ assertEqual(errorsOf(store), {});
1579
+ assertEqual(await store.validate(["slug"]), { outcome: "invalid" });
1580
+ }
1581
+ },
1582
+ {
1583
+ rule: "freshness",
1584
+ name: "a retry checks the rows it was asked about after they moved, not the ones now at those indexes",
1585
+ async run(createStore) {
1586
+ const { options, gate } = fixture();
1587
+ const store = createStore(options);
1588
+ gate.hold("one");
1589
+ const run = store.validate(["invites.0.code"]);
1590
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1591
+ store.fieldArray("invites").swap(0, 1);
1592
+ gate.settle("one", false);
1593
+ assertEqual(await run, { outcome: "invalid" });
1594
+ assertEqual(Object.keys(errorsOf(store)), ["invites.1.code"]);
1595
+ }
1596
+ },
1597
+ {
1598
+ rule: "freshness",
1599
+ name: "a check whose rows were removed, or that a reset overtook, is overtaken and writes nothing",
1600
+ async run(createStore) {
1601
+ const { options, gate } = fixture();
1602
+ const store = createStore(options);
1603
+ gate.hold("two");
1604
+ const removed = store.validate(["invites.1.code"]);
1605
+ await waitFor(() => assertSame(gate.pending("two"), 1));
1606
+ store.fieldArray("invites").remove(1);
1607
+ gate.settle("two", false);
1608
+ assertEqual(await removed, { outcome: "overtaken" });
1609
+ assertEqual(errorsOf(store), {});
1610
+ const other = createStore(fixture().options);
1611
+ const held = fixture();
1612
+ held.gate.hold("one");
1613
+ const reset = createStore(held.options);
1614
+ const overtaken = reset.validate(["invites.0.code"]);
1615
+ await waitFor(() => assertSame(held.gate.pending("one"), 1));
1616
+ reset.reset();
1617
+ held.gate.settle("one", false);
1618
+ assertEqual(await overtaken, { outcome: "overtaken" });
1619
+ assertEqual(errorsOf(reset), {});
1620
+ assertEqual(await other.validate(), { outcome: "passed" });
1621
+ }
1622
+ },
1623
+ {
1624
+ rule: "freshness",
1625
+ name: "checkValidity answers for the values, facts and errors current when it was called, and never mixes in later ones",
1626
+ async run(createStore) {
1627
+ const { options, gate } = fixture();
1628
+ const store = createStore(options);
1629
+ store.setValue("invites.0.code", "pending");
1630
+ const answer = await passedSubmission(store);
1631
+ gate.hold("pending");
1632
+ const first = store.checkValidity();
1633
+ await waitFor(() => assertSame(gate.pending("pending"), 1));
1634
+ answer.setServerErrors({ "invites.0.code": { type: "late" } });
1635
+ gate.settle("pending", true);
1636
+ assertSame(await first, true);
1637
+ assertSame(await store.checkValidity(), false);
1638
+ store.setValue("invites.0.code", "other");
1639
+ assertSame(await store.checkValidity(), true);
1640
+ store.reportNative({ "invites.0.code": { type: "valueMissing" } });
1641
+ assertSame(await store.checkValidity(), false);
1642
+ }
1643
+ },
1644
+ {
1645
+ rule: "submission-bound validation",
1646
+ name: "an edit during validation followed by a passing retry submits the edited values, and the server answer for them applies",
1647
+ async run(createStore) {
1648
+ const store = createStore(fixture().options);
1649
+ const run = store.submit();
1650
+ store.setValue("nickname", "ada2");
1651
+ const submitted = passedOf(await run);
1652
+ assertSame(submitted.values.nickname, "ada2");
1653
+ assertEqual(submitted.values, store.getSnapshot().values);
1654
+ assertSame(Object.isFrozen(submitted.values), true);
1655
+ submitted.setServerErrors({ nickname: { type: "taken" } });
1656
+ assertSame(errorsOf(store).nickname?.type, "taken");
1657
+ }
1658
+ },
1659
+ {
1660
+ rule: "submission-bound validation",
1661
+ name: "a submission that fails is invalid and has no values, and an edit after a pass drops only the answers for what it touched",
1662
+ async run(createStore) {
1663
+ const store = createStore(fixture().options);
1664
+ store.setValue("name", "");
1665
+ assertEqual(await store.submit(), { outcome: "invalid" });
1666
+ store.setValue("name", "Grace");
1667
+ const submitted = await passedSubmission(store);
1668
+ assertSame(submitted.values.name, "Grace");
1669
+ store.setValue("nickname", "changed");
1670
+ submitted.setServerErrors({ name: { type: "taken" }, nickname: { type: "taken" } });
1671
+ assertEqual(Object.keys(errorsOf(store)), ["name"]);
1672
+ }
1673
+ },
1674
+ {
1675
+ rule: "submission-bound validation",
1676
+ name: "a submission a later one superseded ends overtaken, and the values of a passed one are plain frozen data",
1677
+ async run(createStore) {
1678
+ const store = createStore(fixture().options);
1679
+ const older = store.submit();
1680
+ const newer = store.submit();
1681
+ store.setValue("name", "Grace");
1682
+ assertEqual(await older, { outcome: "overtaken" });
1683
+ const submitted = passedOf(await newer);
1684
+ assertSame(submitted.values.name, "Grace");
1685
+ assertSame(Object.isFrozen(submitted.values), true);
1686
+ assertSame(submitted.values, submitted.values);
1687
+ }
1688
+ },
1689
+ {
1690
+ rule: "pending per field",
1691
+ name: "lists the paths a check is running for, moves with a row, and drops a path a later check took over",
1692
+ async run(createStore) {
1693
+ const { options, gate } = fixture({ validationMode: "onBlur" });
1694
+ const store = createStore(options);
1695
+ gate.hold("one");
1696
+ store.blur("invites.0.code");
1697
+ await waitFor(() => assertSame(gate.pending("one"), 1));
1698
+ assertEqual(store.getSnapshot().validating, { "invites.0.code": true });
1699
+ assertSame(isValidating(store), true);
1700
+ assertSame(fieldState(store, "invites.0.code").isValidating, true);
1701
+ assertSame(fieldState(store, "invites.1.code").isValidating, false);
1702
+ store.fieldArray("invites").swap(0, 1);
1703
+ assertEqual(store.getSnapshot().validating, { "invites.1.code": true });
1704
+ gate.settle("one", true);
1705
+ await waitFor(() => assertSame(isValidating(store), false));
1706
+ await settle();
1707
+ assertEqual(store.getSnapshot().validating, {});
1708
+ const racing = fixture();
1709
+ racing.gate.hold("a");
1710
+ const second = createStore(racing.options);
1711
+ second.setValue("slug", "a");
1712
+ const older = second.validate(["slug"]);
1713
+ await waitFor(() => assertSame(racing.gate.pending("a"), 1));
1714
+ second.setValue("slug", "b");
1715
+ assertEqual(await second.validate(["slug"]), { outcome: "passed" });
1716
+ assertEqual(second.getSnapshot().validating, {});
1717
+ racing.gate.settle("a", true);
1718
+ assertEqual(await older, { outcome: "passed" });
1719
+ }
1720
+ },
1721
+ {
1722
+ rule: "pending per field",
1723
+ name: "a superseded async check leaves no issue and no pending mark: pending is state, never an issue",
1724
+ async run(createStore) {
1725
+ const restoreFetch = stubGlobal(
1726
+ "fetch",
1727
+ createSpy(async () => new Response(JSON.stringify({ valid: true })))
1728
+ );
1729
+ try {
1730
+ const store = createStore(
1731
+ custom(
1732
+ [
1733
+ {
1734
+ $type: "input",
1735
+ name: "handle",
1736
+ rules: [{ $type: "async", url: "/check", debounce: 30, disableCache: true }]
1737
+ }
1738
+ ],
1739
+ { handle: "" },
1740
+ { validationMode: "onChange" }
1741
+ )
1742
+ );
1743
+ store.setValue("handle", "a");
1744
+ store.setValue("handle", "ab");
1745
+ await waitFor(() => assertSame(isValidating(store), false), { timeout: 2e3 });
1746
+ assertEqual(errorsOf(store), {});
1747
+ assertEqual(store.getSnapshot().warnings, {});
1748
+ assertEqual(store.getSnapshot().validating, {});
1749
+ assertNotMatches(JSON.stringify(store.getSnapshot()), /pending/i);
1750
+ assertEqual(await store.validate(), { outcome: "passed" });
1751
+ } finally {
1752
+ restoreFetch();
1753
+ }
1754
+ }
1755
+ },
1756
+ {
1757
+ rule: "server errors tied to a submission",
1758
+ name: "a response for values that were edited since the submission began is dropped",
1759
+ async run(createStore) {
1760
+ const store = createStore(fixture().options);
1761
+ const old = await passedSubmission(store);
1762
+ old.setServerErrors({ name: { type: "old" } });
1763
+ assertSame(errorsOf(store).name?.type, "old");
1764
+ const submission = await passedSubmission(store);
1765
+ assertEqual(errorsOf(store), {});
1766
+ store.setValue("name", "Grace");
1767
+ submission.setServerErrors({ name: { type: "taken" }, nickname: { type: "blocked" } });
1768
+ assertEqual(Object.keys(errorsOf(store)), ["nickname"]);
1769
+ store.setValue("nickname", "other");
1770
+ assertEqual(errorsOf(store), {});
1771
+ const formWide = await passedSubmission(store);
1772
+ store.setValue("slug", "changed");
1773
+ formWide.setServerErrors({ [FORM_ROOT]: { type: "invalid_credentials" } });
1774
+ assertEqual(errorsOf(store), {});
1775
+ }
1776
+ },
1777
+ {
1778
+ rule: "server errors tied to a submission",
1779
+ name: "an error for a submitted row lands on that row wherever it is now, and is dropped when the row is gone",
1780
+ async run(createStore) {
1781
+ const store = createStore(fixture().options);
1782
+ const submission = await passedSubmission(store);
1783
+ store.fieldArray("contacts").swap(0, 1);
1784
+ submission.setServerErrors({
1785
+ "contacts.0.email": { type: "bounced" },
1786
+ "contacts.2.email": { type: "blocked" }
1787
+ });
1788
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["contacts.1.email", "contacts.2.email"]);
1789
+ store.fieldArray("contacts").remove(2);
1790
+ const again = await passedSubmission(store);
1791
+ store.fieldArray("contacts").remove(0);
1792
+ again.setServerErrors({ "contacts.0.email": { type: "bounced" } });
1793
+ assertEqual(errorsOf(store), {});
1794
+ }
1795
+ },
1796
+ {
1797
+ rule: "server errors tied to a submission",
1798
+ name: "a cross-field error is dropped when a value its rules read changed, and cleared when one changes after it was applied",
1799
+ async run(createStore) {
1800
+ const store = createStore(passwordForm());
1801
+ const stale = await passedSubmission(store);
1802
+ store.setValue("password", "b");
1803
+ stale.setServerErrors({ confirm: { type: "mismatch" } });
1804
+ assertEqual(errorsOf(store), {});
1805
+ store.setValue("password", "a");
1806
+ const fresh = await passedSubmission(store);
1807
+ fresh.setServerErrors({ confirm: { type: "mismatch" } });
1808
+ assertSame(errorsOf(store).confirm?.type, "mismatch");
1809
+ store.setValue("password", "b");
1810
+ assertEqual(errorsOf(store), {});
1811
+ }
1812
+ },
1813
+ {
1814
+ rule: "server errors tied to a submission",
1815
+ name: "an answer to a submission from before a reset, or to a superseded one, is ignored",
1816
+ async run(createStore) {
1817
+ const store = createStore(fixture().options);
1818
+ const beforeReset = await passedSubmission(store);
1819
+ store.reset();
1820
+ beforeReset.setServerErrors({ name: { type: "taken" } });
1821
+ assertEqual(errorsOf(store), {});
1822
+ const first = await passedSubmission(store);
1823
+ const second = await passedSubmission(store);
1824
+ first.setServerErrors({ name: { type: "taken" } });
1825
+ assertEqual(errorsOf(store), {});
1826
+ second.setServerErrors({ name: { type: "taken" } });
1827
+ assertSame(errorsOf(store).name?.type, "taken");
1828
+ assertEqual(second.values, store.getSnapshot().values);
1829
+ }
1830
+ },
1831
+ {
1832
+ rule: "server errors tied to a submission",
1833
+ name: "a path takes one issue or several, each with a code, params and an optional message",
1834
+ async run(createStore) {
1835
+ const store = createStore(fixture().options);
1836
+ const submission = await passedSubmission(store);
1837
+ submission.setServerErrors({
1838
+ name: [
1839
+ { type: "taken", message: "Already registered" },
1840
+ { type: "blocked_domain", params: { domain: "example.com" } }
1841
+ ],
1842
+ nickname: []
1843
+ });
1844
+ assertEqual(Object.keys(errorsOf(store)), ["name"]);
1845
+ assertEqual(errorsOf(store).name.all, [
1846
+ { type: "taken", source: "server", message: "Already registered" },
1847
+ { type: "blocked_domain", source: "server", params: { domain: "example.com" } }
1848
+ ]);
1849
+ }
1850
+ },
1851
+ {
1852
+ rule: "native facts",
1853
+ name: "a fact describes the value it came with: a later write makes it stale, and a programmatic write never reuses it",
1854
+ async run(createStore) {
1855
+ const store = createStore(fixture({ validationMode: "onChange" }).options);
1856
+ store.setValue("nickname", "bad");
1857
+ store.reportNative({ nickname: { type: "typeMismatch" } });
1858
+ await waitFor(() => assertSame(errorsOf(store).nickname?.source, "native"));
1859
+ assertSame(await store.checkValidity(), false);
1860
+ store.setValue("nickname", "good");
1861
+ await waitFor(() => assertUndefined(errorsOf(store).nickname));
1862
+ assertSame(await store.checkValidity(), true);
1863
+ store.setValue("nickname", "bad");
1864
+ await settle();
1865
+ assertUndefined(errorsOf(store).nickname);
1866
+ assertEqual(await store.validate(["nickname"]), { outcome: "passed" });
1867
+ }
1868
+ },
1869
+ {
1870
+ rule: "native facts",
1871
+ name: "reporting the fact already held, or writing the value already held, changes nothing",
1872
+ async run(createStore) {
1873
+ const store = createStore(fixture({ validationMode: "onChange" }).options);
1874
+ store.setValue("nickname", "nick");
1875
+ store.reportNative({ nickname: { type: "badInput" } });
1876
+ await waitFor(() => assertSame(errorsOf(store).nickname?.source, "native"));
1877
+ await waitFor(() => assertSame(isValidating(store), false));
1878
+ await settle();
1879
+ const watch = watchValidation(store);
1880
+ const before = store.getSnapshot();
1881
+ store.reportNative({ nickname: { type: "badInput" } });
1882
+ store.setValue("nickname", "nick");
1883
+ await settle();
1884
+ assertSame(watch.started, false);
1885
+ assertSame(store.getSnapshot(), before);
1886
+ assertSame(await store.checkValidity(), false);
1887
+ }
1888
+ },
1889
+ {
1890
+ rule: "native facts",
1891
+ name: "reportNative binds facts to the values held now, validates only paths already checked, and is no user change",
1892
+ async run(createStore) {
1893
+ const store = createStore(fixture({ validationMode: "onChange" }).options);
1894
+ const listener = createSpy();
1895
+ store.subscribe(listener);
1896
+ store.reportNative({ nickname: { type: "valueMissing" } });
1897
+ await settle();
1898
+ assertEqual(errorsOf(store), {});
1899
+ assertEqual(store.getSnapshot().dirty, {});
1900
+ assertEqual(store.getSnapshot().touched, {});
1901
+ assertCallCount(listener, 0);
1902
+ assertSame(await store.checkValidity(), false);
1903
+ assertEqual(await store.validate(["name"]), { outcome: "passed" });
1904
+ store.reportNative({ name: { type: "typeMismatch", params: { type: "email" } }, nickname: null });
1905
+ await waitFor(() => assertSame(errorsOf(store).name?.type, "typeMismatch"));
1906
+ assertSame(await store.checkValidity(), false);
1907
+ store.reportNative({ name: null });
1908
+ await waitFor(() => assertUndefined(errorsOf(store).name));
1909
+ assertSame(await store.checkValidity(), true);
1910
+ assertEqual(store.getSnapshot().dirty, {});
1911
+ }
1912
+ },
1913
+ {
1914
+ rule: "native facts",
1915
+ name: "a reset form that hears from its controls again shows no error, and a path the container does not hold is ignored",
1916
+ async run(createStore) {
1917
+ const store = createStore(fixture({ validationMode: "onChange" }).options);
1918
+ store.setValue("nickname", "x");
1919
+ store.reportNative({ nickname: { type: "typeMismatch" } });
1920
+ await waitFor(() => assertSame(errorsOf(store).nickname?.source, "native"));
1921
+ store.reset();
1922
+ store.reportNative({
1923
+ nickname: { type: "valueMissing" },
1924
+ "contacts.9.email": { type: "valueMissing" },
1925
+ "nothing.here": { type: "valueMissing" }
1926
+ });
1927
+ await settle();
1928
+ assertEqual(errorsOf(store), {});
1929
+ assertSame(store.getSnapshot().isValid, true);
1930
+ store.fieldArray("contacts").remove(2);
1931
+ assertDoesNotThrow(() => store.reportNative({ "contacts.2.email": { type: "valueMissing" } }));
1932
+ assertSame(await store.checkValidity(), false);
1933
+ }
1934
+ },
1935
+ {
1936
+ rule: "native facts",
1937
+ name: "a fact moves with its row, and a removed row never returns as a native error",
1938
+ async run(createStore) {
1939
+ const store = createStore(fixture().options);
1940
+ store.blur("contacts.1.label");
1941
+ store.reportNative({ "contacts.1.label": { type: "typeMismatch" } });
1942
+ store.fieldArray("contacts").swap(1, 2);
1943
+ assertEqual(await store.validate(), { outcome: "invalid" });
1944
+ assertEqual(Object.keys(errorsOf(store)), ["contacts.2.label"]);
1945
+ store.fieldArray("contacts").remove(2);
1946
+ assertEqual(await store.validate(), { outcome: "passed" });
1947
+ assertEqual(errorsOf(store), {});
1948
+ }
1949
+ },
1950
+ {
1951
+ rule: "native facts",
1952
+ name: "a fact stays held until its control withdraws it: every held fact counts for validate and checkValidity",
1953
+ async run(createStore) {
1954
+ const store = createStore(fixture().options);
1955
+ store.reportNative({ name: { type: "valueMissing" }, nickname: { type: "valueMissing" } });
1956
+ assertSame(await store.checkValidity(), false);
1957
+ store.reportNative({ nickname: null });
1958
+ assertSame(await store.checkValidity(), false);
1959
+ assertEqual(await store.validate(), { outcome: "invalid" });
1960
+ assertEqual(Object.keys(errorsOf(store)), ["name"]);
1961
+ store.reportNative({ name: null });
1962
+ assertSame(await store.checkValidity(), true);
1963
+ assertEqual(await store.validate(), { outcome: "passed" });
1964
+ assertEqual(errorsOf(store), {});
1965
+ }
1966
+ },
1967
+ {
1968
+ rule: "native facts",
1969
+ name: "one call reports several controls in one commit, and ignores the rows that do not exist",
1970
+ async run(createStore) {
1971
+ const store = createStore(fixture().options);
1972
+ const listener = createSpy();
1973
+ store.subscribe(listener);
1974
+ store.reportNative({
1975
+ name: { type: "valueMissing" },
1976
+ nickname: { type: "valueMissing" },
1977
+ "contacts.9.email": { type: "valueMissing" }
1978
+ });
1979
+ assertCallCount(listener, 0);
1980
+ assertEqual(await store.validate(), { outcome: "invalid" });
1981
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["name", "nickname"]);
1982
+ store.reportNative({ name: null, nickname: null });
1983
+ await waitFor(() => assertEqual(errorsOf(store), {}));
1984
+ assertEqual(await store.validate(), { outcome: "passed" });
1985
+ }
1986
+ },
1987
+ {
1988
+ rule: "native facts",
1989
+ name: "facts for rows that do not exist are ignored, and facts and params are copied so a caller cannot change a snapshot",
1990
+ async run(createStore) {
1991
+ const store = createStore(fixture().options);
1992
+ store.fieldArray("contacts").remove([0, 1, 2]);
1993
+ assertThrows(() => store.blur("contacts.0.label"), /names row/);
1994
+ store.setValue("contacts", null);
1995
+ store.reportNative({
1996
+ "contacts.0.label": { type: "valueMissing" },
1997
+ "contacts.1.label": { type: "valueMissing" }
1998
+ });
1999
+ await store.validate();
2000
+ assertEqual(
2001
+ Object.keys(errorsOf(store)).filter((path) => path.endsWith(".label")),
2002
+ []
2003
+ );
2004
+ store.setValue("contacts", []);
2005
+ const allowed = ["first"];
2006
+ const answer = await passedSubmission(store);
2007
+ answer.setServerErrors({ name: { type: "invalid", params: { allowed } } });
2008
+ const fact = { type: "rangeUnderflow", params: { list: ["a"] } };
2009
+ store.reportNative({ nickname: fact });
2010
+ await store.validate(["nickname"]);
2011
+ allowed.push("second");
2012
+ fact.params.list.push("b");
2013
+ assertEqual(errorsOf(store).name.params, { allowed: ["first"] });
2014
+ assertEqual(errorsOf(store).nickname.params, { list: ["a"] });
2015
+ assertSame(Object.isFrozen(errorsOf(store).name.params.allowed), true);
2016
+ }
2017
+ },
2018
+ {
2019
+ rule: "pending per field",
2020
+ name: "a branch write that removes rows takes their pending marks with them",
2021
+ async run(createStore) {
2022
+ const { options, gate } = fixture();
2023
+ const store = createStore(options);
2024
+ gate.hold("two");
2025
+ const run = store.validate(["invites.1.code"]);
2026
+ await waitFor(() => assertSame(gate.pending("two"), 1));
2027
+ assertEqual(store.getSnapshot().validating, { "invites.1.code": true });
2028
+ store.setValue("invites", []);
2029
+ assertEqual(store.getSnapshot().validating, {});
2030
+ gate.settle("two", true);
2031
+ assertEqual(await run, { outcome: "overtaken" });
2032
+ assertEqual(store.getSnapshot().validating, {});
2033
+ assertSame(isValidating(store), false);
2034
+ }
2035
+ },
2036
+ {
2037
+ rule: "participation",
2038
+ name: "a field a constraint hides takes no part: it does not block, count or show, however it was recorded",
2039
+ async run(createStore) {
2040
+ const store = createStore(businessForm());
2041
+ assertEqual(await store.validate(), { outcome: "passed" });
2042
+ assertSame(await store.checkValidity(), true);
2043
+ assertEqual(errorsOf(store), {});
2044
+ store.setValue("kind", "business");
2045
+ assertEqual(await store.validate(), { outcome: "invalid" });
2046
+ assertMatches(errorsOf(store).company, { type: "required", ruleId: "company-required" });
2047
+ store.setValue("kind", "person");
2048
+ assertEqual(errorsOf(store), {});
2049
+ assertSame(store.getSnapshot().isValid, true);
2050
+ assertEqual(await store.validate(), { outcome: "passed" });
2051
+ await serverAnswer(store, { company: { type: "taken" } });
2052
+ store.blur("company");
2053
+ store.reportNative({ company: { type: "valueMissing" } });
2054
+ assertSame(store.getSnapshot().isValid, true);
2055
+ assertEqual(await store.validate(), { outcome: "passed" });
2056
+ assertSame(await store.checkValidity(), true);
2057
+ }
2058
+ },
2059
+ {
2060
+ rule: "participation",
2061
+ name: "a step its constraints hide is skipped: its fields take no part, its values are kept and the submitted values leave them out",
2062
+ async run(createStore) {
2063
+ const store = createStore(stepsForm({ hasCompany: false, wantsInvoice: "no", company: "", vat: "" }));
2064
+ assertEqual(await store.validate(), { outcome: "passed" });
2065
+ assertSame(await store.checkValidity(), true);
2066
+ assertEqual(errorsOf(store), {});
2067
+ const submitted = await store.submit();
2068
+ assertSame(submitted.outcome, "passed");
2069
+ if (submitted.outcome === "passed") assertEqual(submitted.values, { hasCompany: false });
2070
+ assertMatches(store.getSnapshot().values, { wantsInvoice: "no", company: "" });
2071
+ store.setValue("hasCompany", true);
2072
+ assertEqual(await store.validate(), { outcome: "invalid" });
2073
+ assertEqual(Object.keys(errorsOf(store)), ["company"]);
2074
+ }
2075
+ },
2076
+ {
2077
+ rule: "participation",
2078
+ name: "a value that takes no part is absent for the constraints after it, in order: a skipped step cannot keep a later field shown",
2079
+ async run(createStore) {
2080
+ const store = createStore(stepsForm({ hasCompany: false, wantsInvoice: "yes", company: "", vat: "" }));
2081
+ assertEqual(await store.validate(), { outcome: "passed" });
2082
+ assertSame(await store.checkValidity(), true);
2083
+ store.setValue("hasCompany", true);
2084
+ store.setValue("company", "Acme");
2085
+ assertEqual(await store.validate(), { outcome: "invalid" });
2086
+ assertEqual(Object.keys(errorsOf(store)), ["vat"]);
2087
+ store.setValue("hasCompany", false);
2088
+ assertEqual(errorsOf(store), {});
2089
+ assertEqual(await store.validate(), { outcome: "passed" });
2090
+ }
2091
+ },
2092
+ {
2093
+ rule: "participation",
2094
+ name: "a path that takes part again is re-validated when it was checked before, and waits for its timing otherwise",
2095
+ async run(createStore) {
2096
+ const checked = createStore(businessForm({ kind: "business", company: "" }));
2097
+ assertEqual(await checked.validate(), { outcome: "invalid" });
2098
+ checked.setValue("kind", "person");
2099
+ assertEqual(errorsOf(checked), {});
2100
+ checked.setValue("kind", "business");
2101
+ await waitFor(() => assertSame(errorsOf(checked).company?.type, "required"));
2102
+ const untouched = createStore(businessForm());
2103
+ untouched.setValue("kind", "business");
2104
+ await settle();
2105
+ assertEqual(errorsOf(untouched), {});
2106
+ assertSame(untouched.getSnapshot().isValid, true);
2107
+ }
2108
+ },
2109
+ {
2110
+ rule: "participation",
2111
+ name: "disabled fields, authored or by a constraint, and everything inside an excluded container, are excluded; an inactive step is not",
2112
+ async run(createStore) {
2113
+ const store = createStore(
2114
+ custom(
2115
+ [
2116
+ { $type: "input", name: "locked", disabled: true, rules: [{ $type: "required" }] },
2117
+ { $type: "input", name: "mode" },
2118
+ {
2119
+ $type: "input",
2120
+ name: "frozen",
2121
+ rules: [{ $type: "required" }],
2122
+ constraints: [{ field: "mode", $type: "enable", operator: "equals", value: "edit" }]
2123
+ },
2124
+ {
2125
+ $type: "fieldset",
2126
+ name: "extra",
2127
+ constraints: [{ field: "mode", $type: "show", operator: "equals", value: "edit" }],
2128
+ elements: [{ $type: "input", name: "note", rules: [{ $type: "required" }] }]
2129
+ },
2130
+ { $type: "input", name: "later", rules: [{ $type: "required" }] }
2131
+ ],
2132
+ { locked: "", mode: "view", frozen: "", extra: { note: "" }, later: "" }
2133
+ )
2134
+ );
2135
+ assertEqual(await store.validate(), { outcome: "invalid" });
2136
+ assertEqual(Object.keys(errorsOf(store)), ["later"]);
2137
+ store.setValue("later", "x");
2138
+ assertEqual(await store.validate(), { outcome: "passed" });
2139
+ store.setValue("mode", "edit");
2140
+ assertEqual(await store.validate(), { outcome: "invalid" });
2141
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), ["extra.note", "frozen"]);
2142
+ }
2143
+ },
2144
+ {
2145
+ rule: "issue identity",
2146
+ name: "every rule reports its type, id and params, two rules of one type stay apart, and a rule reports once",
2147
+ async run(createStore) {
2148
+ const store = createStore(
2149
+ custom(
2150
+ [
2151
+ {
2152
+ $type: "input",
2153
+ name: "tag",
2154
+ rules: [
2155
+ { $type: "required", id: "tag-required" },
2156
+ { $type: "pattern", id: "tag-letters", value: "^[a-z]+$", message: "Letters only" },
2157
+ { $type: "pattern", id: "tag-short", value: "^.{0,3}$", message: "Three at most" },
2158
+ { $type: "minLength", value: 8 }
2159
+ ]
2160
+ }
2161
+ ],
2162
+ { tag: "AB1234" }
2163
+ )
2164
+ );
2165
+ assertEqual(await store.validate(), { outcome: "invalid" });
2166
+ const { all } = errorsOf(store).tag;
2167
+ assertEqual(
2168
+ all.map(({ type, ruleId }) => [type, ruleId]),
2169
+ [
2170
+ ["pattern", "tag-letters"],
2171
+ ["pattern", "tag-short"],
2172
+ ["minLength", void 0]
2173
+ ]
2174
+ );
2175
+ assertEqual(all[0], {
2176
+ type: "pattern",
2177
+ source: "validation",
2178
+ ruleId: "tag-letters",
2179
+ params: { pattern: "^[a-z]+$" },
2180
+ message: "Letters only"
2181
+ });
2182
+ assertEqual(all[2].params, { minLength: 8 });
2183
+ assertUndefined(all[2].message);
2184
+ assertSame(Object.isFrozen(all[2].params), true);
2185
+ }
2186
+ },
2187
+ {
2188
+ rule: "issue identity",
2189
+ name: "rules are listed in authored order whatever order their checks finish in, and unchanged issues keep their objects",
2190
+ async run(createStore) {
2191
+ const { options, gate } = fixture({
2192
+ elements: [
2193
+ element({
2194
+ $type: "input",
2195
+ name: "slug",
2196
+ rules: [{ $type: "gate" }, { $type: "minLength", value: 5, message: "Too short" }]
2197
+ })
2198
+ ],
2199
+ defaults: { slug: "ab" }
2200
+ });
2201
+ const store = createStore(options);
2202
+ gate.hold("ab");
2203
+ const run = store.validate();
2204
+ await waitFor(() => assertSame(gate.pending("ab"), 1));
2205
+ gate.settle("ab", false);
2206
+ assertEqual(await run, { outcome: "invalid" });
2207
+ const first = errorsOf(store).slug;
2208
+ assertEqual(
2209
+ first.all.map(({ type }) => type),
2210
+ ["gate", "minLength"]
2211
+ );
2212
+ await store.validate();
2213
+ assertSame(errorsOf(store).slug, first);
2214
+ }
2215
+ },
2216
+ {
2217
+ rule: "validator failures",
2218
+ name: "a rule that throws is no field error: validate and checkValidity reject naming it, an automatic check logs it, the others still report",
2219
+ async run(createStore) {
2220
+ const options = custom(
2221
+ [
2222
+ {
2223
+ $type: "input",
2224
+ name: "secret",
2225
+ rules: [
2226
+ { $type: "required", id: "secret-required" },
2227
+ { $type: "boom", id: "secret-boom" }
2228
+ ]
2229
+ }
2230
+ ],
2231
+ { secret: "" },
2232
+ { validationMode: "onChange" }
2233
+ );
2234
+ const store = createStore(options);
2235
+ const rejected = await store.validate().then(
2236
+ () => void 0,
2237
+ (error) => error
2238
+ );
2239
+ assertMatches(rejected, {
2240
+ name: "FormValidationFailure",
2241
+ path: "secret",
2242
+ ruleType: "boom",
2243
+ ruleId: "secret-boom"
2244
+ });
2245
+ const check = await store.checkValidity().then(
2246
+ () => void 0,
2247
+ (error) => error
2248
+ );
2249
+ assertMatches(check, { path: "secret", ruleType: "boom" });
2250
+ assertSame(isValidating(store), false);
2251
+ const logged = spyOn(logger, "error");
2252
+ try {
2253
+ const automatic = createStore(options);
2254
+ automatic.setValue("secret", "x");
2255
+ automatic.setValue("secret", "");
2256
+ await waitFor(() => assertDefined(errorsOf(automatic).secret));
2257
+ const report = logged.spy.calls.find(
2258
+ ([message]) => typeof message === "string" && message.includes("'secret'")
2259
+ );
2260
+ assertMatches(report?.[1], { error: { path: "secret", ruleType: "boom", ruleId: "secret-boom" } });
2261
+ assertEqual(errorsOf(automatic).secret?.all, [
2262
+ { type: "required", source: "validation", ruleId: "secret-required" }
2263
+ ]);
2264
+ assertSame(automatic.getSnapshot().isValid, false);
2265
+ } finally {
2266
+ logged.restore();
2267
+ }
2268
+ }
2269
+ },
2270
+ {
2271
+ rule: "warnings",
2272
+ name: "a rule with severity warning reports advice that never blocks, with the shape and timing of an error",
2273
+ async run(createStore) {
2274
+ const store = createStore(
2275
+ custom(
2276
+ [
2277
+ {
2278
+ $type: "input",
2279
+ name: "password",
2280
+ rules: [
2281
+ {
2282
+ $type: "minLength",
2283
+ value: 8,
2284
+ severity: "warning",
2285
+ id: "password-strength",
2286
+ message: "Weak"
2287
+ },
2288
+ { $type: "minLength", value: 3, message: "Too short" }
2289
+ ]
2290
+ }
2291
+ ],
2292
+ { password: "abcd" },
2293
+ { validationMode: "onChange" }
2294
+ )
2295
+ );
2296
+ store.setValue("password", "abcde");
2297
+ await waitFor(() => assertDefined(store.getSnapshot().warnings.password));
2298
+ assertEqual(store.getSnapshot().warnings.password, {
2299
+ type: "minLength",
2300
+ source: "validation",
2301
+ ruleId: "password-strength",
2302
+ params: { minLength: 8 },
2303
+ message: "Weak",
2304
+ all: [
2305
+ {
2306
+ type: "minLength",
2307
+ source: "validation",
2308
+ ruleId: "password-strength",
2309
+ params: { minLength: 8 },
2310
+ message: "Weak"
2311
+ }
2312
+ ]
2313
+ });
2314
+ assertEqual(errorsOf(store), {});
2315
+ assertSame(store.getSnapshot().isValid, true);
2316
+ assertEqual(await store.validate(), { outcome: "passed" });
2317
+ assertSame(await store.checkValidity(), true);
2318
+ const field = fieldState(store, "password");
2319
+ assertMatches(field, { invalid: false, error: void 0 });
2320
+ assertSame(fieldState(store, "password").warning, field.warning);
2321
+ store.setValue("password", "abcdefgh");
2322
+ await waitFor(() => assertEqual(store.getSnapshot().warnings, {}));
2323
+ store.setValue("password", "a");
2324
+ await waitFor(() => assertSame(errorsOf(store).password?.message, "Too short"));
2325
+ assertSame(store.getSnapshot().warnings.password?.message, "Weak");
2326
+ assertEqual(await store.validate(), { outcome: "invalid" });
2327
+ }
2328
+ },
2329
+ {
2330
+ rule: "warnings",
2331
+ name: "warnings of a path that takes no part are not shown",
2332
+ async run(createStore) {
2333
+ const store = createStore(
2334
+ custom(
2335
+ [
2336
+ { $type: "input", name: "kind" },
2337
+ {
2338
+ $type: "input",
2339
+ name: "company",
2340
+ rules: [{ $type: "required", severity: "warning" }],
2341
+ constraints: [{ field: "kind", $type: "show", operator: "equals", value: "business" }]
2342
+ }
2343
+ ],
2344
+ { kind: "business", company: "" }
2345
+ )
2346
+ );
2347
+ assertEqual(await store.validate(), { outcome: "passed" });
2348
+ assertSame(store.getSnapshot().warnings.company?.type, "required");
2349
+ store.setValue("kind", "person");
2350
+ assertEqual(store.getSnapshot().warnings, {});
2351
+ }
2352
+ },
2353
+ {
2354
+ rule: "issue identity",
2355
+ name: "an async rule reports the endpoint text on its own channel, never library text, and an outage is a failure to run and is not cached",
2356
+ async run(createStore) {
2357
+ let reply = () => new Response(JSON.stringify({ valid: false }));
2358
+ const fetchSpy = createSpy(async () => reply());
2359
+ const restoreFetch = stubGlobal("fetch", fetchSpy);
2360
+ try {
2361
+ const options = custom(
2362
+ [
2363
+ {
2364
+ $type: "input",
2365
+ name: "handle",
2366
+ rules: [
2367
+ {
2368
+ $type: "async",
2369
+ id: "handle-free",
2370
+ url: "/check",
2371
+ headers: { Authorization: "secret" }
2372
+ }
2373
+ ]
2374
+ }
2375
+ ],
2376
+ { handle: "ada" }
2377
+ );
2378
+ const store = createStore(options);
2379
+ assertEqual(await store.validate(), { outcome: "invalid" });
2380
+ assertEqual(errorsOf(store).handle.all, [
2381
+ { type: "async", source: "validation", ruleId: "handle-free" }
2382
+ ]);
2383
+ reply = () => new Response(JSON.stringify({ valid: false, message: "Handle is taken" }));
2384
+ store.setValue("handle", "ada2");
2385
+ assertEqual(await store.validate(), { outcome: "invalid" });
2386
+ assertSame(errorsOf(store).handle.message, "Handle is taken");
2387
+ assertNotMatches(JSON.stringify(store.getSnapshot()), /Invalid input|secret|\/check/);
2388
+ reply = () => new Response("no", { status: 503 });
2389
+ store.setValue("handle", "ada3");
2390
+ const down = await store.validate().then(
2391
+ () => void 0,
2392
+ (error) => error
2393
+ );
2394
+ assertMatches(down, { name: "FormValidationFailure", path: "handle", ruleType: "async" });
2395
+ assertSame(down.cause.name, "ValidationUnavailable");
2396
+ const calls = fetchSpy.calls.length;
2397
+ reply = () => new Response(JSON.stringify({ valid: true }));
2398
+ assertEqual(await store.validate(), { outcome: "passed" });
2399
+ assertSame(fetchSpy.calls.length, calls + 1);
2400
+ fetchSpy.implement(async () => {
2401
+ throw new Error("offline");
2402
+ });
2403
+ const offline = await createStore({ ...options, defaults: { handle: "zed" } }).validate().then(
2404
+ () => void 0,
2405
+ (error) => error
2406
+ );
2407
+ assertSame(offline.cause.name, "ValidationUnavailable");
2408
+ } finally {
2409
+ restoreFetch();
2410
+ }
2411
+ }
2412
+ },
2413
+ {
2414
+ rule: "submission-bound validation",
2415
+ name: "a submission ends passed, invalid, or overtaken",
2416
+ async run(createStore) {
2417
+ const store = createStore(fixture().options);
2418
+ assertSame((await store.submit()).outcome, "passed");
2419
+ store.setValue("name", "");
2420
+ assertEqual(await store.submit(), { outcome: "invalid" });
2421
+ const edit = fixture();
2422
+ const edited = createStore(edit.options);
2423
+ edit.gate.settle("taken", false);
2424
+ edit.gate.hold("a", "b");
2425
+ edited.setValue("slug", "a");
2426
+ const outrun = edited.submit();
2427
+ await waitFor(() => assertSame(edit.gate.pending("a"), 1));
2428
+ edited.setValue("slug", "b");
2429
+ edit.gate.settle("a", true);
2430
+ await waitFor(() => assertSame(edit.gate.pending("b"), 1));
2431
+ edited.setValue("slug", "taken");
2432
+ edit.gate.settle("b", true);
2433
+ assertEqual(await outrun, { outcome: "overtaken" });
2434
+ const hold = fixture();
2435
+ const reset = createStore(hold.options);
2436
+ hold.gate.hold("free");
2437
+ const cleared = reset.submit();
2438
+ await waitFor(() => assertSame(hold.gate.pending("free"), 1));
2439
+ reset.reset();
2440
+ hold.gate.settle("free", true);
2441
+ assertEqual(await cleared, { outcome: "overtaken" });
2442
+ }
2443
+ },
2444
+ {
2445
+ rule: "paths of writes",
2446
+ name: "a reserved path rejects the whole reportNative call before any write, and every reserved segment is named",
2447
+ async run(createStore) {
2448
+ const store = createStore(fixture().options);
2449
+ const before = store.getSnapshot();
2450
+ assertThrows(
2451
+ () => store.reportNative({ name: { type: "valueMissing" }, __x: { type: "valueMissing" } }),
2452
+ /reserved/
2453
+ );
2454
+ assertThrows(() => store.reportNative({ "a.constructor": null }), /'constructor'/);
2455
+ assertSame(await store.checkValidity(), true);
2456
+ assertSame(store.getSnapshot(), before);
2457
+ assertThrows(() => store.setValue("prototype.x", 1), /__.*constructor.*prototype/s);
2458
+ }
2459
+ },
2460
+ {
2461
+ rule: "native facts",
2462
+ name: "a fact reported while the check of a write runs is not lost: the rule error and the native one both appear",
2463
+ async run(createStore) {
2464
+ const store = createStore(fixture({ validationMode: "onChange" }).options);
2465
+ store.setValue("nickname", "");
2466
+ assertSame(store.getSnapshot().validating.nickname, true);
2467
+ store.reportNative({ nickname: { type: "valueMissing" } });
2468
+ await waitFor(
2469
+ () => assertEqual(
2470
+ errorsOf(store).nickname?.all.map(({ source }) => source),
2471
+ ["validation", "native"]
2472
+ )
2473
+ );
2474
+ assertSame(isValidating(store), false);
2475
+ const touched = createStore(fixture({ validationMode: "onTouched" }).options);
2476
+ touched.blur("name");
2477
+ touched.setValue("name", "");
2478
+ touched.reportNative({ name: { type: "valueMissing" } });
2479
+ await waitFor(
2480
+ () => assertEqual(
2481
+ errorsOf(touched).name?.all.map(({ source }) => source),
2482
+ ["validation", "native"]
2483
+ )
2484
+ );
2485
+ }
2486
+ },
2487
+ {
2488
+ rule: "submission-bound validation",
2489
+ name: "a later submission overtakes the earlier one, which has no verdict",
2490
+ async run(createStore) {
2491
+ const store = createStore(fixture().options);
2492
+ const first = store.submit();
2493
+ const second = store.submit();
2494
+ assertEqual(await first, { outcome: "overtaken" });
2495
+ assertSame((await second).outcome, "passed");
2496
+ }
2497
+ },
2498
+ {
2499
+ rule: "validator failures",
2500
+ name: "a failure says why a rule did not run: the service was unavailable, or the rule threw",
2501
+ async run(createStore) {
2502
+ const restoreFetch = stubGlobal(
2503
+ "fetch",
2504
+ createSpy(async () => new Response("no", { status: 500 }))
2505
+ );
2506
+ try {
2507
+ const unavailable = createStore(
2508
+ custom(
2509
+ [
2510
+ {
2511
+ $type: "input",
2512
+ name: "handle",
2513
+ rules: [{ $type: "async", url: "/check", disableCache: true }]
2514
+ }
2515
+ ],
2516
+ { handle: "a" }
2517
+ )
2518
+ );
2519
+ const down = await unavailable.validate().then(
2520
+ () => void 0,
2521
+ (error) => error
2522
+ );
2523
+ assertMatches(down, { path: "handle", ruleType: "async", reason: "unavailable" });
2524
+ const threw = createStore(
2525
+ custom([{ $type: "input", name: "secret", rules: [{ $type: "boom" }] }], { secret: "" })
2526
+ );
2527
+ const exploded = await threw.validate().then(
2528
+ () => void 0,
2529
+ (error) => error
2530
+ );
2531
+ assertMatches(exploded, { path: "secret", ruleType: "boom", reason: "error" });
2532
+ } finally {
2533
+ restoreFetch();
2534
+ }
2535
+ }
2536
+ },
2537
+ {
2538
+ rule: "submission-bound validation",
2539
+ name: "the values of a submission leave out the paths that take no part, and carry the payload, which is never rendered or validated",
2540
+ async run(createStore) {
2541
+ const options = custom(
2542
+ [
2543
+ { $type: "input", name: "kind" },
2544
+ {
2545
+ $type: "input",
2546
+ name: "company",
2547
+ rules: [{ $type: "required" }],
2548
+ constraints: [{ field: "kind", $type: "show", operator: "equals", value: "business" }]
2549
+ },
2550
+ { $type: "input", name: "locked", disabled: true },
2551
+ {
2552
+ $type: "arrayElements",
2553
+ name: "rows",
2554
+ elements: [
2555
+ { $type: "input", name: "role" },
2556
+ {
2557
+ $type: "input",
2558
+ name: "fax",
2559
+ constraints: [{ field: "role", $type: "hide", operator: "equals", value: "office" }]
2560
+ }
2561
+ ]
2562
+ }
2563
+ ],
2564
+ {
2565
+ kind: "person",
2566
+ company: "Acme",
2567
+ locked: "x",
2568
+ rows: [
2569
+ { role: "office", fax: "1" },
2570
+ { role: "home", fax: "2" }
2571
+ ]
2572
+ },
2573
+ { payload: { tenant: "t1", context: { source: "email" } } }
2574
+ );
2575
+ const store = createStore(options);
2576
+ const submitted = await passedSubmission(store);
2577
+ assertEqual(submitted.values, {
2578
+ kind: "person",
2579
+ rows: [{ role: "office" }, { role: "home", fax: "2" }],
2580
+ tenant: "t1",
2581
+ context: { source: "email" }
2582
+ });
2583
+ assertSame(Object.isFrozen(submitted.values), true);
2584
+ assertNoProperty(store.getSnapshot().values, "tenant");
2585
+ assertEqual(store.getSnapshot().dirty, {});
2586
+ store.setValue("kind", "business");
2587
+ const next = await passedSubmission(store);
2588
+ assertMatches(next.values, { company: "Acme", tenant: "t1" });
2589
+ assertNoProperty(next.values, "locked");
2590
+ }
2591
+ },
2592
+ {
2593
+ rule: "touched and dirty",
2594
+ name: "blur marks the path touched in one notification and validates by mode; written first, the value ends as one command would leave it",
2595
+ async run(createStore) {
2596
+ const quiet = createStore(fixture().options);
2597
+ const listener = createSpy();
2598
+ quiet.subscribe(listener);
2599
+ quiet.blur("name");
2600
+ assertCallCount(listener, 1);
2601
+ assertEqual(quiet.getSnapshot().touched, { name: true });
2602
+ assertSame(quiet.getValue("name"), "Ada");
2603
+ assertEqual(quiet.getSnapshot().dirty, {});
2604
+ const store = createStore(fixture({ validationMode: "onBlur" }).options);
2605
+ store.setValue("name", "");
2606
+ store.blur("name");
2607
+ await waitFor(() => assertSame(errorsOf(store).name?.type, "required"));
2608
+ assertSame(store.getSnapshot().dirty.name, true);
2609
+ assertSame(store.getSnapshot().touched.name, true);
2610
+ }
2611
+ },
2612
+ {
2613
+ rule: "paths of writes",
2614
+ name: "setValue and blur refuse a row the array does not have, and a reserved segment, instead of leaving holes",
2615
+ run(createStore) {
2616
+ const store = createStore(fixture().options);
2617
+ const before = store.getSnapshot();
2618
+ assertThrows(
2619
+ () => store.setValue("contacts.3.email", "x"),
2620
+ /'contacts.3.email' names row '3' of 'contacts'/
2621
+ );
2622
+ assertThrows(() => store.setValue("contacts.-1.email", "x"), /names row '-1'/);
2623
+ assertThrows(() => store.blur("contacts.5.email"), /names row '5'/);
2624
+ assertThrows(() => store.setValue("name.3.x", "x"), /names row '3' of 'name'/);
2625
+ assertThrows(() => store.setValue("name.03.x", "x"), /names row '03' of 'name'/);
2626
+ assertThrows(() => store.setValue("__secret", 1), /reserved segment/);
2627
+ assertSame(store.getSnapshot(), before);
2628
+ store.fieldArray("contacts").append({ email: "d@example.com" });
2629
+ store.setValue("contacts.3.email", "e@example.com");
2630
+ assertLength(store.getSnapshot().rowIds.contacts, 4);
2631
+ assertSame(
2632
+ store.getSnapshot().rowIds.contacts.every((id) => typeof id === "string"),
2633
+ true
2634
+ );
2635
+ }
2636
+ },
2637
+ {
2638
+ rule: "validation mode timing",
2639
+ name: "onTouched validates on the first blur and on every change after it",
2640
+ async run(createStore) {
2641
+ const store = createStore(fixture({ validationMode: "onTouched", defaults: { name: "" } }).options);
2642
+ const watch = watchValidation(store);
2643
+ store.setValue("name", "");
2644
+ store.setValue("name", "x");
2645
+ store.setValue("name", "");
2646
+ await settle();
2647
+ assertSame(watch.started, false);
2648
+ store.blur("name");
2649
+ await waitFor(() => assertSame(errorsOf(store).name?.type, "required"));
2650
+ store.setValue("name", "Ada");
2651
+ await waitFor(() => assertUndefined(errorsOf(store).name));
2652
+ store.setValue("name", "");
2653
+ await waitFor(() => assertSame(errorsOf(store).name?.type, "required"));
2654
+ }
2655
+ },
2656
+ {
2657
+ rule: "issue identity",
2658
+ name: "a registration declares the params of its rule: native names, and nothing of the configuration of async or relate rules",
2659
+ async run(createStore) {
2660
+ const store = createStore(
2661
+ custom(
2662
+ [
2663
+ {
2664
+ $type: "input",
2665
+ name: "tag",
2666
+ rules: [
2667
+ { $type: "maxLength", value: 2 },
2668
+ {
2669
+ $type: "relate",
2670
+ fields: ["other"],
2671
+ assert: "allEqual",
2672
+ includeSelf: true,
2673
+ id: "same"
2674
+ }
2675
+ ]
2676
+ },
2677
+ { $type: "input", name: "other" },
2678
+ {
2679
+ $type: "input",
2680
+ name: "count",
2681
+ rules: [
2682
+ { $type: "min", value: 5 },
2683
+ { $type: "max", value: 1 }
2684
+ ]
2685
+ },
2686
+ {
2687
+ $type: "arrayElements",
2688
+ name: "rows",
2689
+ rules: [{ $type: "minItems", value: 2 }],
2690
+ elements: [{ $type: "input", name: "x" }]
2691
+ }
2692
+ ],
2693
+ { tag: "abc", other: "z", count: 3, rows: [{ x: "" }] }
2694
+ )
2695
+ );
2696
+ assertEqual(await store.validate(), { outcome: "invalid" });
2697
+ const errors = errorsOf(store);
2698
+ assertEqual(errors.tag.all, [
2699
+ { type: "maxLength", source: "validation", params: { maxLength: 2 } },
2700
+ { type: "relate", source: "validation", ruleId: "same", params: { assert: "allEqual" } }
2701
+ ]);
2702
+ assertEqual(
2703
+ errors.count.all.map(({ params }) => params),
2704
+ [{ min: 5 }, { max: 1 }]
2705
+ );
2706
+ assertEqual(errors.rows.params, { minItems: 2 });
2707
+ assertNotMatches(JSON.stringify(errors), /Too (small|big)|Invalid input/);
2708
+ }
2709
+ },
2710
+ {
2711
+ rule: "native facts",
2712
+ name: "a fact for a path no field declares is ignored: it does not block validate, checkValidity or a submission",
2713
+ async run(createStore) {
2714
+ const store = createStore(fixture().options);
2715
+ const before = store.getSnapshot();
2716
+ store.reportNative({
2717
+ ghost: { type: "valueMissing" },
2718
+ "name.x": { type: "valueMissing" },
2719
+ "address.nowhere": { type: "valueMissing" },
2720
+ "contacts.0.nothing": { type: "valueMissing" }
2721
+ });
2722
+ assertEqual(await store.validate(), { outcome: "passed" });
2723
+ assertSame(await store.checkValidity(), true);
2724
+ assertEqual(errorsOf(store), {});
2725
+ assertSame(store.getSnapshot().isValid, true);
2726
+ assertSame((await store.submit()).outcome, "passed");
2727
+ assertEqual(before.errors, {});
2728
+ }
2729
+ },
2730
+ {
2731
+ rule: "server errors tied to a submission",
2732
+ name: "an error for a path no field declares shows at the form, with its type, params and message",
2733
+ async run(createStore) {
2734
+ const store = createStore(fixture().options);
2735
+ const answer = await passedSubmission(store);
2736
+ answer.setServerErrors({
2737
+ ghost: { type: "unknown_field", params: { field: "ghost" }, message: "No such field" },
2738
+ "address.nowhere": [{ type: "also_unknown" }],
2739
+ [FORM_ROOT]: { message: "Try later" },
2740
+ name: { type: "taken" },
2741
+ "contacts.1": { type: "row_error" }
2742
+ });
2743
+ assertEqual(Object.keys(errorsOf(store)).toSorted(), [FORM_ROOT, "contacts.1", "name"]);
2744
+ assertEqual(errorsOf(store)[FORM_ROOT].all, [
2745
+ { type: "server", source: "server", message: "Try later" },
2746
+ {
2747
+ type: "unknown_field",
2748
+ source: "server",
2749
+ params: { field: "ghost" },
2750
+ message: "No such field"
2751
+ },
2752
+ { type: "also_unknown", source: "server" }
2753
+ ]);
2754
+ const alone = await passedSubmission(store);
2755
+ alone.setServerErrors({ ghost: { type: "unknown_field" } });
2756
+ assertEqual(Object.keys(errorsOf(store)), [FORM_ROOT]);
2757
+ assertSame(errorsOf(store)[FORM_ROOT].type, "unknown_field");
2758
+ }
2759
+ }
2760
+ ];
2761
+ var formStateContainerCases = cases;
2762
+ function formStateContainerConformance(createStore, runner) {
2763
+ runner.describe("form state container conformance", () => {
2764
+ for (const rule of new Set(cases.map((entry) => entry.rule))) {
2765
+ runner.describe(rule, () => {
2766
+ for (const entry of cases.filter((candidate) => candidate.rule === rule)) {
2767
+ runner.test(entry.name, () => entry.run(createStore));
2768
+ }
2769
+ });
2770
+ }
2771
+ });
2772
+ }
2773
+
2774
+ export {
2775
+ defaultModes,
2776
+ containerOptions,
2777
+ watchValidation,
2778
+ formStateContainerCases,
2779
+ formStateContainerConformance
2780
+ };