mobx-formkit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/AGENTS.md +761 -0
  2. package/LICENSE +21 -0
  3. package/README.md +214 -0
  4. package/lib/ArrayMap.d.ts +50 -0
  5. package/lib/ArrayMap.d.ts.map +1 -0
  6. package/lib/ArrayMap.js +151 -0
  7. package/lib/ArrayMap.js.map +1 -0
  8. package/lib/Base.d.ts +237 -0
  9. package/lib/Base.d.ts.map +1 -0
  10. package/lib/Base.js +872 -0
  11. package/lib/Base.js.map +1 -0
  12. package/lib/Bindings.d.ts +28 -0
  13. package/lib/Bindings.d.ts.map +1 -0
  14. package/lib/Bindings.js +73 -0
  15. package/lib/Bindings.js.map +1 -0
  16. package/lib/Field.d.ts +195 -0
  17. package/lib/Field.d.ts.map +1 -0
  18. package/lib/Field.js +734 -0
  19. package/lib/Field.js.map +1 -0
  20. package/lib/Form.d.ts +75 -0
  21. package/lib/Form.d.ts.map +1 -0
  22. package/lib/Form.js +199 -0
  23. package/lib/Form.js.map +1 -0
  24. package/lib/Options.d.ts +9 -0
  25. package/lib/Options.d.ts.map +1 -0
  26. package/lib/Options.js +85 -0
  27. package/lib/Options.js.map +1 -0
  28. package/lib/State.d.ts +43 -0
  29. package/lib/State.d.ts.map +1 -0
  30. package/lib/State.js +147 -0
  31. package/lib/State.js.map +1 -0
  32. package/lib/Validator.d.ts +25 -0
  33. package/lib/Validator.d.ts.map +1 -0
  34. package/lib/Validator.js +143 -0
  35. package/lib/Validator.js.map +1 -0
  36. package/lib/compat.d.ts +29 -0
  37. package/lib/compat.d.ts.map +1 -0
  38. package/lib/compat.js +117 -0
  39. package/lib/compat.js.map +1 -0
  40. package/lib/composer.d.ts +26 -0
  41. package/lib/composer.d.ts.map +1 -0
  42. package/lib/composer.js +65 -0
  43. package/lib/composer.js.map +1 -0
  44. package/lib/esm/ArrayMap.js +146 -0
  45. package/lib/esm/ArrayMap.js.map +1 -0
  46. package/lib/esm/Base.js +868 -0
  47. package/lib/esm/Base.js.map +1 -0
  48. package/lib/esm/Bindings.js +69 -0
  49. package/lib/esm/Bindings.js.map +1 -0
  50. package/lib/esm/Field.js +730 -0
  51. package/lib/esm/Field.js.map +1 -0
  52. package/lib/esm/Form.js +195 -0
  53. package/lib/esm/Form.js.map +1 -0
  54. package/lib/esm/Options.js +81 -0
  55. package/lib/esm/Options.js.map +1 -0
  56. package/lib/esm/State.js +143 -0
  57. package/lib/esm/State.js.map +1 -0
  58. package/lib/esm/Validator.js +139 -0
  59. package/lib/esm/Validator.js.map +1 -0
  60. package/lib/esm/compat.js +96 -0
  61. package/lib/esm/compat.js.map +1 -0
  62. package/lib/esm/composer.js +63 -0
  63. package/lib/esm/composer.js.map +1 -0
  64. package/lib/esm/index.js +8 -0
  65. package/lib/esm/index.js.map +1 -0
  66. package/lib/esm/models/FieldProps.js +116 -0
  67. package/lib/esm/models/FieldProps.js.map +1 -0
  68. package/lib/esm/models/OptionsModel.js +51 -0
  69. package/lib/esm/models/OptionsModel.js.map +1 -0
  70. package/lib/esm/models/StateInterface.js +9 -0
  71. package/lib/esm/models/StateInterface.js.map +1 -0
  72. package/lib/esm/models/ValidatorInterface.js +8 -0
  73. package/lib/esm/models/ValidatorInterface.js.map +1 -0
  74. package/lib/esm/parser.js +235 -0
  75. package/lib/esm/parser.js.map +1 -0
  76. package/lib/esm/props.js +118 -0
  77. package/lib/esm/props.js.map +1 -0
  78. package/lib/esm/utils.js +156 -0
  79. package/lib/esm/utils.js.map +1 -0
  80. package/lib/esm/validators/AJV.js +105 -0
  81. package/lib/esm/validators/AJV.js.map +1 -0
  82. package/lib/esm/validators/DVR.js +104 -0
  83. package/lib/esm/validators/DVR.js.map +1 -0
  84. package/lib/esm/validators/JOI.js +54 -0
  85. package/lib/esm/validators/JOI.js.map +1 -0
  86. package/lib/esm/validators/VALIBOT.js +50 -0
  87. package/lib/esm/validators/VALIBOT.js.map +1 -0
  88. package/lib/esm/validators/VINEJS.js +60 -0
  89. package/lib/esm/validators/VINEJS.js.map +1 -0
  90. package/lib/esm/validators/VJF.js +88 -0
  91. package/lib/esm/validators/VJF.js.map +1 -0
  92. package/lib/esm/validators/YUP.js +69 -0
  93. package/lib/esm/validators/YUP.js.map +1 -0
  94. package/lib/esm/validators/ZOD.js +43 -0
  95. package/lib/esm/validators/ZOD.js.map +1 -0
  96. package/lib/index.d.ts +7 -0
  97. package/lib/index.d.ts.map +1 -0
  98. package/lib/index.js +15 -0
  99. package/lib/index.js.map +1 -0
  100. package/lib/models/BaseInterface.d.ts +40 -0
  101. package/lib/models/BaseInterface.d.ts.map +1 -0
  102. package/lib/models/BindingsInterface.d.ts +18 -0
  103. package/lib/models/BindingsInterface.d.ts.map +1 -0
  104. package/lib/models/FieldInterface.d.ts +88 -0
  105. package/lib/models/FieldInterface.d.ts.map +1 -0
  106. package/lib/models/FieldProps.d.ts +111 -0
  107. package/lib/models/FieldProps.d.ts.map +1 -0
  108. package/lib/models/FieldProps.js +116 -0
  109. package/lib/models/FieldProps.js.map +1 -0
  110. package/lib/models/FormInterface.d.ts +184 -0
  111. package/lib/models/FormInterface.d.ts.map +1 -0
  112. package/lib/models/OptionsInterface.d.ts +8 -0
  113. package/lib/models/OptionsInterface.d.ts.map +1 -0
  114. package/lib/models/OptionsModel.d.ts +99 -0
  115. package/lib/models/OptionsModel.d.ts.map +1 -0
  116. package/lib/models/OptionsModel.js +51 -0
  117. package/lib/models/OptionsModel.js.map +1 -0
  118. package/lib/models/SharedActionsInterface.d.ts +27 -0
  119. package/lib/models/SharedActionsInterface.d.ts.map +1 -0
  120. package/lib/models/SharedEventsInterface.d.ts +8 -0
  121. package/lib/models/SharedEventsInterface.d.ts.map +1 -0
  122. package/lib/models/SharedHelpersInterface.d.ts +26 -0
  123. package/lib/models/SharedHelpersInterface.d.ts.map +1 -0
  124. package/lib/models/SharedInitializerInterface.d.ts +6 -0
  125. package/lib/models/SharedInitializerInterface.d.ts.map +1 -0
  126. package/lib/models/SharedUtilsInterface.d.ts +10 -0
  127. package/lib/models/SharedUtilsInterface.d.ts.map +1 -0
  128. package/lib/models/StateInterface.d.ts +37 -0
  129. package/lib/models/StateInterface.d.ts.map +1 -0
  130. package/lib/models/StateInterface.js +9 -0
  131. package/lib/models/StateInterface.js.map +1 -0
  132. package/lib/models/ValidatorInterface.d.ts +74 -0
  133. package/lib/models/ValidatorInterface.d.ts.map +1 -0
  134. package/lib/models/ValidatorInterface.js +8 -0
  135. package/lib/models/ValidatorInterface.js.map +1 -0
  136. package/lib/parser.d.ts +13 -0
  137. package/lib/parser.d.ts.map +1 -0
  138. package/lib/parser.js +247 -0
  139. package/lib/parser.js.map +1 -0
  140. package/lib/props.d.ts +14 -0
  141. package/lib/props.d.ts.map +1 -0
  142. package/lib/props.js +120 -0
  143. package/lib/props.js.map +1 -0
  144. package/lib/utils.d.ts +34 -0
  145. package/lib/utils.d.ts.map +1 -0
  146. package/lib/utils.js +182 -0
  147. package/lib/utils.js.map +1 -0
  148. package/lib/validators/AJV.d.ts +4 -0
  149. package/lib/validators/AJV.d.ts.map +1 -0
  150. package/lib/validators/AJV.js +109 -0
  151. package/lib/validators/AJV.js.map +1 -0
  152. package/lib/validators/DVR.d.ts +4 -0
  153. package/lib/validators/DVR.d.ts.map +1 -0
  154. package/lib/validators/DVR.js +108 -0
  155. package/lib/validators/DVR.js.map +1 -0
  156. package/lib/validators/JOI.d.ts +4 -0
  157. package/lib/validators/JOI.d.ts.map +1 -0
  158. package/lib/validators/JOI.js +58 -0
  159. package/lib/validators/JOI.js.map +1 -0
  160. package/lib/validators/VALIBOT.d.ts +4 -0
  161. package/lib/validators/VALIBOT.d.ts.map +1 -0
  162. package/lib/validators/VALIBOT.js +73 -0
  163. package/lib/validators/VALIBOT.js.map +1 -0
  164. package/lib/validators/VINEJS.d.ts +4 -0
  165. package/lib/validators/VINEJS.d.ts.map +1 -0
  166. package/lib/validators/VINEJS.js +64 -0
  167. package/lib/validators/VINEJS.js.map +1 -0
  168. package/lib/validators/VJF.d.ts +4 -0
  169. package/lib/validators/VJF.d.ts.map +1 -0
  170. package/lib/validators/VJF.js +92 -0
  171. package/lib/validators/VJF.js.map +1 -0
  172. package/lib/validators/YUP.d.ts +4 -0
  173. package/lib/validators/YUP.d.ts.map +1 -0
  174. package/lib/validators/YUP.js +73 -0
  175. package/lib/validators/YUP.js.map +1 -0
  176. package/lib/validators/ZOD.d.ts +4 -0
  177. package/lib/validators/ZOD.d.ts.map +1 -0
  178. package/lib/validators/ZOD.js +47 -0
  179. package/lib/validators/ZOD.js.map +1 -0
  180. package/package.json +183 -0
  181. package/src/ArrayMap.ts +165 -0
  182. package/src/Base.ts +1229 -0
  183. package/src/Bindings.ts +85 -0
  184. package/src/Field.ts +1034 -0
  185. package/src/Form.ts +264 -0
  186. package/src/Options.ts +91 -0
  187. package/src/State.ts +183 -0
  188. package/src/Validator.ts +177 -0
  189. package/src/compat.ts +104 -0
  190. package/src/composer.ts +82 -0
  191. package/src/index.ts +7 -0
  192. package/src/models/BaseInterface.ts +45 -0
  193. package/src/models/BindingsInterface.ts +25 -0
  194. package/src/models/FieldInterface.ts +97 -0
  195. package/src/models/FieldProps.ts +116 -0
  196. package/src/models/FormInterface.ts +203 -0
  197. package/src/models/OptionsInterface.ts +9 -0
  198. package/src/models/OptionsModel.ts +100 -0
  199. package/src/models/SharedActionsInterface.ts +30 -0
  200. package/src/models/SharedEventsInterface.ts +8 -0
  201. package/src/models/SharedHelpersInterface.ts +12 -0
  202. package/src/models/SharedInitializerInterface.ts +6 -0
  203. package/src/models/SharedUtilsInterface.ts +10 -0
  204. package/src/models/StateInterface.ts +44 -0
  205. package/src/models/ValidatorInterface.ts +88 -0
  206. package/src/parser.ts +327 -0
  207. package/src/props.ts +127 -0
  208. package/src/utils.ts +218 -0
  209. package/src/validators/AJV.ts +140 -0
  210. package/src/validators/DVR.ts +145 -0
  211. package/src/validators/JOI.ts +79 -0
  212. package/src/validators/VALIBOT.ts +70 -0
  213. package/src/validators/VINEJS.ts +83 -0
  214. package/src/validators/VJF.ts +123 -0
  215. package/src/validators/YUP.ts +90 -0
  216. package/src/validators/ZOD.ts +66 -0
  217. package/umd/MobxFormkit.umd.js +3093 -0
  218. package/umd/MobxFormkit.umd.js.map +1 -0
  219. package/umd/MobxFormkit.umd.min.js +2 -0
  220. package/umd/MobxFormkit.umd.min.js.map +1 -0
  221. package/umd/MobxFormkitComposer.umd.js +183 -0
  222. package/umd/MobxFormkitComposer.umd.js.map +1 -0
  223. package/umd/MobxFormkitComposer.umd.min.js +2 -0
  224. package/umd/MobxFormkitComposer.umd.min.js.map +1 -0
  225. package/umd/MobxFormkitValidatorAJV.umd.js +113 -0
  226. package/umd/MobxFormkitValidatorAJV.umd.js.map +1 -0
  227. package/umd/MobxFormkitValidatorAJV.umd.min.js +2 -0
  228. package/umd/MobxFormkitValidatorAJV.umd.min.js.map +1 -0
  229. package/umd/MobxFormkitValidatorDVR.umd.js +110 -0
  230. package/umd/MobxFormkitValidatorDVR.umd.js.map +1 -0
  231. package/umd/MobxFormkitValidatorDVR.umd.min.js +2 -0
  232. package/umd/MobxFormkitValidatorDVR.umd.min.js.map +1 -0
  233. package/umd/MobxFormkitValidatorJOI.umd.js +62 -0
  234. package/umd/MobxFormkitValidatorJOI.umd.js.map +1 -0
  235. package/umd/MobxFormkitValidatorJOI.umd.min.js +2 -0
  236. package/umd/MobxFormkitValidatorJOI.umd.min.js.map +1 -0
  237. package/umd/MobxFormkitValidatorVALIBOT.umd.js +8032 -0
  238. package/umd/MobxFormkitValidatorVALIBOT.umd.js.map +1 -0
  239. package/umd/MobxFormkitValidatorVALIBOT.umd.min.js +2 -0
  240. package/umd/MobxFormkitValidatorVALIBOT.umd.min.js.map +1 -0
  241. package/umd/MobxFormkitValidatorVINEJS.umd.js +11737 -0
  242. package/umd/MobxFormkitValidatorVINEJS.umd.js.map +1 -0
  243. package/umd/MobxFormkitValidatorVINEJS.umd.min.js +2 -0
  244. package/umd/MobxFormkitValidatorVINEJS.umd.min.js.map +1 -0
  245. package/umd/MobxFormkitValidatorVJF.umd.js +96 -0
  246. package/umd/MobxFormkitValidatorVJF.umd.js.map +1 -0
  247. package/umd/MobxFormkitValidatorVJF.umd.min.js +2 -0
  248. package/umd/MobxFormkitValidatorVJF.umd.min.js.map +1 -0
  249. package/umd/MobxFormkitValidatorYUP.umd.js +77 -0
  250. package/umd/MobxFormkitValidatorYUP.umd.js.map +1 -0
  251. package/umd/MobxFormkitValidatorYUP.umd.min.js +2 -0
  252. package/umd/MobxFormkitValidatorYUP.umd.min.js.map +1 -0
  253. package/umd/MobxFormkitValidatorZOD.umd.js +49 -0
  254. package/umd/MobxFormkitValidatorZOD.umd.js.map +1 -0
  255. package/umd/MobxFormkitValidatorZOD.umd.min.js +2 -0
  256. package/umd/MobxFormkitValidatorZOD.umd.min.js.map +1 -0
@@ -0,0 +1,70 @@
1
+ import * as v from "valibot";
2
+ import FieldInterface from "../models/FieldInterface";
3
+ import FormInterface from "../models/FormInterface";
4
+ import StateInterface from "../models/StateInterface";
5
+ import {
6
+ ValidationPlugin,
7
+ ValidationPluginConfig,
8
+ ValidationPluginConstructor,
9
+ ValidationPluginInterface,
10
+ } from "../models/ValidatorInterface";
11
+
12
+ class VALIBOT<TValidator = any> implements ValidationPluginInterface {
13
+ promises: Promise<any>[];
14
+ config: ValidationPluginConfig<TValidator>;
15
+ state: StateInterface | null;
16
+ extend?: (args: { validator: TValidator; form: FormInterface }) => void;
17
+ validator: any;
18
+ schema: any;
19
+
20
+ constructor({
21
+ config,
22
+ state = null,
23
+ promises = [],
24
+ }: ValidationPluginConstructor<TValidator>) {
25
+ this.state = state;
26
+ this.promises = promises;
27
+ this.config = config;
28
+ this.extend = config?.extend;
29
+ this.validator = config.package ?? v;
30
+ this.schema = config.schema;
31
+
32
+ this.extendValidator();
33
+ }
34
+
35
+ extendValidator(): void {
36
+ if (typeof this.extend === "function") {
37
+ this.extend({
38
+ validator: this.validator,
39
+ form: this.state!.form,
40
+ });
41
+ }
42
+ }
43
+
44
+ // Convert a Valibot issue.path (array of { key }) into an MRF-style string path
45
+ // e.g. [{ key: 'members' }, { key: 0 }, { key: 'name' }] => 'members.0.name'
46
+ normalizePath(issuePath: any[] = []): string {
47
+ return issuePath.map((seg) => seg?.key).join(".");
48
+ }
49
+
50
+ validate(field: FieldInterface): void {
51
+ const result = v.safeParse(this.schema, field.state.form.flatMapValues);
52
+
53
+ if (result.success) return;
54
+
55
+ const messages = result.issues
56
+ .filter((issue: any) => this.normalizePath(issue.path) === field.path)
57
+ .map((issue: any) => issue.message);
58
+
59
+ if (messages?.length) {
60
+ field.validationErrorStack = messages;
61
+ }
62
+ }
63
+ }
64
+
65
+ export default <TValidator = any>(
66
+ config?: ValidationPluginConfig<TValidator>
67
+ ): ValidationPlugin<TValidator> => ({
68
+ class: VALIBOT<TValidator>,
69
+ config,
70
+ });
@@ -0,0 +1,83 @@
1
+ import vine from "@vinejs/vine";
2
+ import FieldInterface from "../models/FieldInterface";
3
+ import FormInterface from "../models/FormInterface";
4
+ import StateInterface from "../models/StateInterface";
5
+ import {
6
+ ValidationPlugin,
7
+ ValidationPluginConfig,
8
+ ValidationPluginConstructor,
9
+ ValidationPluginInterface,
10
+ } from "../models/ValidatorInterface";
11
+
12
+ class VINEJS<TValidator = any> implements ValidationPluginInterface {
13
+ promises: Promise<any>[] = [];
14
+ config: ValidationPluginConfig<TValidator>;
15
+ state: StateInterface | null;
16
+ extend?: (args: { validator: TValidator; form: FormInterface }) => void;
17
+ validator: any;
18
+ schema: any;
19
+ compiled: any;
20
+
21
+ constructor({
22
+ config,
23
+ state = null,
24
+ promises = [],
25
+ }: ValidationPluginConstructor<TValidator>) {
26
+ this.state = state;
27
+ this.promises = promises;
28
+ this.config = config;
29
+ this.extend = config?.extend;
30
+ this.validator = config.package ?? vine;
31
+ this.schema = config.schema;
32
+ this.compiled = this.validator.compile(this.schema);
33
+
34
+ this.extendValidator();
35
+ }
36
+
37
+ extendValidator(): void {
38
+ if (typeof this.extend === "function") {
39
+ this.extend({
40
+ validator: this.validator,
41
+ form: this.state!.form,
42
+ });
43
+ }
44
+ }
45
+
46
+ // VineJS emits dot-notation paths (e.g. 'members.0.name') which match
47
+ // the MRF field.path convention.
48
+ validate(field: FieldInterface): void {
49
+ const validationPromise = this.compiled
50
+ .validate(field.state.form.flatMapValues)
51
+ .then(() => field.setValidationAsyncData(true))
52
+ .catch((error: any) => this.handleAsyncFail(field, error))
53
+ .then(() => this.executeAsyncValidation(field));
54
+
55
+ this.promises.push(validationPromise);
56
+ }
57
+
58
+ handleAsyncFail(field: FieldInterface, error: any): void {
59
+ const messages = error?.messages ?? [];
60
+ if (!messages.length) return;
61
+
62
+ const fieldError = messages.find(
63
+ (item: any) => item.field === field.path
64
+ );
65
+
66
+ if (fieldError) {
67
+ field.setValidationAsyncData(false, fieldError.message);
68
+ }
69
+ }
70
+
71
+ executeAsyncValidation(field: FieldInterface): void {
72
+ if (field.validationAsyncData.valid === false) {
73
+ field.invalidate(field.validationAsyncData.message ?? undefined, false, true);
74
+ }
75
+ }
76
+ }
77
+
78
+ export default <TValidator = any>(
79
+ config?: ValidationPluginConfig<TValidator>
80
+ ): ValidationPlugin<TValidator> => ({
81
+ class: VINEJS<TValidator>,
82
+ config,
83
+ });
@@ -0,0 +1,123 @@
1
+
2
+ import FieldInterface from "../models/FieldInterface";
3
+ import FormInterface from "../models/FormInterface";
4
+ import StateInterface from "../models/StateInterface";
5
+ import {
6
+ ValidationPlugin,
7
+ ValidationPluginConfig,
8
+ ValidationPluginConstructor,
9
+ ValidationPluginInterface,
10
+ } from "../models/ValidatorInterface";
11
+
12
+ function isPromise(obj: any): obj is Promise<any> {
13
+ return (
14
+ !!obj &&
15
+ typeof obj.then === "function" &&
16
+ (typeof obj === "object" || typeof obj === "function")
17
+ );
18
+ }
19
+
20
+ class VJF<TValidator = any> implements ValidationPluginInterface<TValidator> {
21
+ promises: Promise<any>[];
22
+ config: ValidationPluginConfig<TValidator>;
23
+ state: StateInterface | null;
24
+ extend?: (args: { validator: TValidator; form: FormInterface }) => void;
25
+ validator: TValidator;
26
+
27
+ constructor({
28
+ config,
29
+ state = null,
30
+ promises = [],
31
+ }: ValidationPluginConstructor<TValidator>) {
32
+ this.state = state;
33
+ this.promises = promises;
34
+ this.config = config;
35
+ this.extend = config?.extend;
36
+ this.validator = config?.package!;
37
+ this.extendValidator();
38
+ }
39
+
40
+ extendValidator(): void {
41
+ if (typeof this.extend === "function") {
42
+ this.extend({
43
+ validator: this.validator,
44
+ form: this.state!.form,
45
+ });
46
+ }
47
+ }
48
+
49
+ validate(field: FieldInterface): void {
50
+ if (!field.validators) return;
51
+
52
+ const validators = Array.isArray(field.validators) ? field.validators : [field.validators];
53
+
54
+ validators.forEach((fn) => this.collectData(fn, field));
55
+
56
+ this.executeValidation(field);
57
+ }
58
+
59
+ collectData(fn: (args: { validator: TValidator; form: FormInterface; field: FieldInterface }) => [boolean, string] | Promise<[boolean, string]>, field: FieldInterface): void {
60
+ const result = this.handleFunctionResult(fn, field);
61
+
62
+ if (isPromise(result)) {
63
+ const $p = result
64
+ .then(([valid, message]) => field.setValidationAsyncData(valid, message))
65
+ .then(() => this.executeAsyncValidation(field));
66
+
67
+ this.promises.push($p);
68
+ return;
69
+ }
70
+
71
+ field.validationFunctionsData.unshift({
72
+ valid: result[0],
73
+ message: result[1],
74
+ });
75
+ }
76
+
77
+ executeValidation(field: FieldInterface): void {
78
+ field.validationFunctionsData.forEach(({ valid, message }) => {
79
+ if (valid === false) {
80
+ field.invalidate(message, false);
81
+ }
82
+ });
83
+ }
84
+
85
+ executeAsyncValidation(field: FieldInterface): void {
86
+ if (field.validationAsyncData.valid === false) {
87
+ field.invalidate(field.validationAsyncData.message ?? undefined, false, true);
88
+ }
89
+ }
90
+
91
+ handleFunctionResult(fn: (args: { validator: TValidator; form: FormInterface; field: FieldInterface }) => [boolean, string] | Promise<[boolean, string]>, field: FieldInterface): [boolean, string] | Promise<[boolean, string]> {
92
+ const result = fn({
93
+ validator: this.validator,
94
+ form: this.state!.form,
95
+ field,
96
+ });
97
+
98
+ if (Array.isArray(result)) {
99
+ return [result[0] || false, result[1] || "Error"];
100
+ }
101
+
102
+ if (typeof result === 'boolean') {
103
+ return [result, "Error"];
104
+ }
105
+
106
+ if (typeof result === 'string') {
107
+ return [false, result];
108
+ }
109
+
110
+ if (isPromise(result)) {
111
+ return result;
112
+ }
113
+
114
+ return [false, "Error"];
115
+ }
116
+ }
117
+
118
+ export default <TValidator = any>(
119
+ config?: ValidationPluginConfig<TValidator>
120
+ ): ValidationPlugin<TValidator> => ({
121
+ class: VJF<TValidator>,
122
+ config,
123
+ });
@@ -0,0 +1,90 @@
1
+ import {
2
+ ValidationPlugin,
3
+ ValidationPluginConfig,
4
+ ValidationPluginConstructor,
5
+ ValidationPluginInterface,
6
+ } from "../models/ValidatorInterface";
7
+ import FormInterface from "../models/FormInterface";
8
+ import StateInterface from "../models/StateInterface";
9
+
10
+ class YUP<TValidator = any> implements ValidationPluginInterface {
11
+ promises: Promise<any>[] = [];
12
+ config: ValidationPluginConfig<TValidator>;
13
+ state: StateInterface | null;
14
+ extend?: (args: { validator: TValidator; form: FormInterface }) => void;
15
+ validator: TValidator;
16
+ schema: any;
17
+
18
+ constructor({
19
+ config,
20
+ state = null,
21
+ promises = [],
22
+ }: ValidationPluginConstructor) {
23
+ this.state = state;
24
+ this.promises = promises;
25
+ this.config = config;
26
+ this.extend = config?.extend;
27
+ this.validator = config.package;
28
+ this.schema = config.schema(this.validator);
29
+ this.extendValidator();
30
+ }
31
+
32
+ // Metodo per estendere il validatore
33
+ extendValidator(): void {
34
+ if (typeof this.extend === 'function') {
35
+ this.extend({
36
+ validator: this.validator,
37
+ form: this.state!.form,
38
+ });
39
+ }
40
+ }
41
+
42
+ // Metodo di validazione principale
43
+ validate(field: any): void {
44
+ const fieldValidationPromise = this.createValidationPromise(field);
45
+ this.promises.push(fieldValidationPromise);
46
+ }
47
+
48
+ // Creazione della promise per la validazione
49
+ private createValidationPromise(field: any): Promise<any> {
50
+ return new Promise((resolve) => {
51
+ this.schema
52
+ .validateAt(field.path, this.state!.form.values(), { strict: true })
53
+ .then(() => this.handleAsyncPasses(field, resolve))
54
+ .catch((error: any) => this.handleAsyncFails(field, resolve, error));
55
+ });
56
+ }
57
+ // Gestione dei successi della validazione asincrona
58
+ private handleAsyncPasses(field: any, resolve: (value?: undefined) => void): void {
59
+ field.setValidationAsyncData(true);
60
+ resolve();
61
+ }
62
+
63
+ // Gestione dei fallimenti della validazione asincrona
64
+ private handleAsyncFails(field: any, resolve: (value?: undefined) => void, error: any): void {
65
+ // Yup a volte restituisce errori senza path (es. array vuoti)
66
+ const isSameField = error.path === field.path || error.path === undefined;
67
+
68
+ if (isSameField) {
69
+ const message = error.message?.replace(error.path ?? field.path, field.label);
70
+ field.setValidationAsyncData(false, message);
71
+ this.executeAsyncValidation(field);
72
+ }
73
+
74
+ resolve(undefined);
75
+ }
76
+
77
+ // Esecuzione della validazione asincrona
78
+ private executeAsyncValidation(field: any): void {
79
+ if (field.validationAsyncData.valid === false) {
80
+ field.invalidate(field.validationAsyncData.message, false, true);
81
+ }
82
+ }
83
+ }
84
+
85
+ export default <TValidator = any>(
86
+ config?: ValidationPluginConfig<TValidator>
87
+ ): ValidationPlugin<TValidator> => ({
88
+ class: YUP<TValidator>,
89
+ config,
90
+ });
@@ -0,0 +1,66 @@
1
+ import { get } from "lodash";
2
+ import FieldInterface from "../models/FieldInterface";
3
+ import FormInterface from "../models/FormInterface";
4
+ import StateInterface from "../models/StateInterface";
5
+ import { ZodSchema } from "zod";
6
+ import {
7
+ ValidationPlugin,
8
+ ValidationPluginConfig,
9
+ ValidationPluginConstructor,
10
+ ValidationPluginInterface,
11
+ } from "../models/ValidatorInterface";
12
+
13
+ class ZOD<TValidator = any> implements ValidationPluginInterface {
14
+ promises: Promise<any>[];
15
+ config: ValidationPluginConfig;
16
+ state: StateInterface | null;
17
+ extend?: (args: { validator: TValidator; form: FormInterface }) => void;
18
+ validator: any;
19
+ schema: ZodSchema<any>;
20
+
21
+ constructor({
22
+ config,
23
+ state = null,
24
+ promises = [],
25
+ }: ValidationPluginConstructor) {
26
+ this.state = state;
27
+ this.promises = promises;
28
+ this.config = config;
29
+ this.extend = config?.extend;
30
+ this.validator = config.package;
31
+ this.schema = config.schema as ZodSchema<any>;
32
+
33
+ this.extendValidator();
34
+ }
35
+
36
+ extendValidator(): void {
37
+ if (typeof this.extend === "function") {
38
+ this.extend({
39
+ validator: this.validator,
40
+ form: this.state!.form,
41
+ });
42
+ }
43
+ }
44
+
45
+ validate(field: FieldInterface): void {
46
+ const result = this.schema.safeParse(field.state.form.flatMapValues);
47
+
48
+ if (result.success) return;
49
+
50
+ const fieldErrors = get(
51
+ (result as any).error.format(),
52
+ field.path ?? ""
53
+ )?._errors;
54
+
55
+ if (fieldErrors?.length) {
56
+ field.validationErrorStack = fieldErrors;
57
+ }
58
+ }
59
+ }
60
+
61
+ export default <TValidator = any>(
62
+ config?: ValidationPluginConfig<TValidator>
63
+ ): ValidationPlugin<TValidator> => ({
64
+ class: ZOD<TValidator>,
65
+ config,
66
+ });