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,100 @@
1
+ export enum OptionsEnum {
2
+ uniqueId = 'uniqueId',
3
+ fallback = 'fallback',
4
+ fallbackValue = 'fallbackValue',
5
+ defaultGenericError = 'defaultGenericError',
6
+ submitThrowsError = 'submitThrowsError',
7
+ showErrorsOnInit = 'showErrorsOnInit',
8
+ showErrorsOnSubmit = 'showErrorsOnSubmit',
9
+ showErrorsOnBlur = 'showErrorsOnBlur',
10
+ showErrorsOnChange = 'showErrorsOnChange',
11
+ showErrorsOnClear = 'showErrorsOnClear',
12
+ showErrorsOnReset = 'showErrorsOnReset',
13
+ validateOnInit = 'validateOnInit',
14
+ validateOnSubmit = 'validateOnSubmit',
15
+ validateOnBlur = 'validateOnBlur',
16
+ validateOnChange = 'validateOnChange',
17
+ validateOnChangeAfterInitialBlur = 'validateOnChangeAfterInitialBlur',
18
+ validateOnChangeAfterSubmit = 'validateOnChangeAfterSubmit',
19
+ validateDisabledFields = 'validateDisabledFields',
20
+ validateDeletedFields = 'validateDeletedFields',
21
+ validatePristineFields = 'validatePristineFields',
22
+ validateTrimmedValue = 'validateTrimmedValue',
23
+ validateOnClear = 'validateOnClear',
24
+ validateOnReset = 'validateOnReset',
25
+ strictSet = 'strictSet',
26
+ strictUpdate = 'strictUpdate',
27
+ strictDelete = 'strictDelete',
28
+ strictSelect = 'strictSelect',
29
+ softDelete = 'softDelete',
30
+ retrieveOnlyDirtyFieldsValues = 'retrieveOnlyDirtyFieldsValues',
31
+ retrieveOnlyEnabledFieldsValues = 'retrieveOnlyEnabledFieldsValues',
32
+ retrieveOnlyEnabledFieldsErrors = 'retrieveOnlyEnabledFieldsErrors',
33
+ retrieveNullifiedEmptyStrings = 'retrieveNullifiedEmptyStrings',
34
+ removeNullishValuesInArrays = 'removeNullishValuesInArrays',
35
+ preserveDeletedFieldsValues = 'preserveDeletedFieldsValues',
36
+ autoTrimValue = 'autoTrimValue',
37
+ autoParseNumbers = 'autoParseNumbers',
38
+ validationDebounceWait = 'validationDebounceWait',
39
+ validationDebounceOptions = 'validationDebounceOptions',
40
+ stopValidationOnError = 'stopValidationOnError',
41
+ validationPluginsOrder = 'validationPluginsOrder',
42
+ resetValidationBeforeValidate = 'resetValidationBeforeValidate',
43
+ applyInputConverterOnInit = 'applyInputConverterOnInit',
44
+ applyInputConverterOnSet = 'applyInputConverterOnSet',
45
+ applyInputConverterOnUpdate = 'applyInputConverterOnUpdate',
46
+ bubbleUpErrorMessages = 'bubbleUpErrorMessages',
47
+ }
48
+
49
+ export interface OptionsModel {
50
+ [OptionsEnum.uniqueId]?: (field: any) => string | undefined;
51
+ [OptionsEnum.fallback]?: boolean;
52
+ [OptionsEnum.fallbackValue]?: any;
53
+ [OptionsEnum.defaultGenericError]?: null | string;
54
+ [OptionsEnum.submitThrowsError]?: boolean;
55
+ [OptionsEnum.showErrorsOnInit]?: boolean;
56
+ [OptionsEnum.showErrorsOnSubmit]?: boolean;
57
+ [OptionsEnum.showErrorsOnBlur]?: boolean;
58
+ [OptionsEnum.showErrorsOnChange]?: boolean;
59
+ [OptionsEnum.showErrorsOnClear]?: boolean;
60
+ [OptionsEnum.showErrorsOnReset]?: boolean;
61
+ [OptionsEnum.validateOnInit]?: boolean;
62
+ [OptionsEnum.validateOnSubmit]?: boolean;
63
+ [OptionsEnum.validateOnBlur]?: boolean;
64
+ [OptionsEnum.validateOnChange]?: boolean;
65
+ [OptionsEnum.validateOnChangeAfterInitialBlur]?: boolean;
66
+ [OptionsEnum.validateOnChangeAfterSubmit]?: boolean;
67
+ [OptionsEnum.validateOnClear]?: boolean;
68
+ [OptionsEnum.validateOnReset]?: boolean;
69
+ [OptionsEnum.validateDisabledFields]?: boolean;
70
+ [OptionsEnum.validateDeletedFields]?: boolean;
71
+ [OptionsEnum.validatePristineFields]?: boolean;
72
+ [OptionsEnum.validateTrimmedValue]?: boolean;
73
+ [OptionsEnum.strictSet]?: boolean;
74
+ [OptionsEnum.strictUpdate]?: boolean;
75
+ [OptionsEnum.strictDelete]?: boolean;
76
+ [OptionsEnum.strictSelect]?: boolean;
77
+ [OptionsEnum.softDelete]?: boolean;
78
+ [OptionsEnum.retrieveOnlyDirtyFieldsValues]?: boolean;
79
+ [OptionsEnum.retrieveOnlyEnabledFieldsValues]?: boolean;
80
+ [OptionsEnum.retrieveOnlyEnabledFieldsErrors]?: boolean;
81
+ [OptionsEnum.retrieveNullifiedEmptyStrings]?: boolean;
82
+ [OptionsEnum.removeNullishValuesInArrays]?: boolean;
83
+ [OptionsEnum.preserveDeletedFieldsValues]?: boolean;
84
+ [OptionsEnum.autoTrimValue]?: boolean;
85
+ [OptionsEnum.autoParseNumbers]?: boolean;
86
+ [OptionsEnum.validationDebounceWait]?: number;
87
+ [OptionsEnum.validationDebounceOptions]?: {
88
+ leading?: boolean;
89
+ trailing?: boolean;
90
+ };
91
+ [OptionsEnum.stopValidationOnError]?: boolean;
92
+ [OptionsEnum.validationPluginsOrder]?: undefined | string[];
93
+ [OptionsEnum.resetValidationBeforeValidate]?: boolean;
94
+ [OptionsEnum.applyInputConverterOnInit]?: boolean,
95
+ [OptionsEnum.applyInputConverterOnSet]?: boolean,
96
+ [OptionsEnum.applyInputConverterOnUpdate]?: boolean,
97
+ [OptionsEnum.bubbleUpErrorMessages]?: boolean,
98
+ }
99
+
100
+ export default OptionsModel;
@@ -0,0 +1,30 @@
1
+ import { ValidateOptions } from "./ValidatorInterface";
2
+
3
+ export interface SubmitOptions {
4
+ execOnSubmitHook: boolean,
5
+ execValidationHooks: boolean,
6
+ validate: boolean
7
+ };
8
+
9
+ export interface SubmitHooks {
10
+ onSubmit?(instance: any): void
11
+ onSuccess?(instance: any): void
12
+ onError?(instance: any): void
13
+ }
14
+
15
+ export interface SharedActionsInterface {
16
+ validate(opt?: ValidateOptions, obj?: ValidateOptions): Promise<any>;
17
+ submit(hooks?: SubmitHooks, opt?: SubmitOptions): Promise<any>;
18
+ check(prop: string, deep?: boolean): boolean;
19
+ deepCheck(type: string, prop: string, fields: any): any;
20
+ update(fields: any): void;
21
+ deepUpdate(fields: any, path: string, recursion: boolean): void;
22
+ get(prop?: any, strict?: boolean): any;
23
+ deepGet(prop: any, fields: any): any;
24
+ set(prop: any, data?: any): void;
25
+ deepSet(prop: any, data: any, path: string, recursion: boolean): void;
26
+ add(obj?: any): any;
27
+ del(path?: string): any;
28
+ }
29
+
30
+ export default SharedActionsInterface;
@@ -0,0 +1,8 @@
1
+ export interface SharedEventsInterface {
2
+ MOBXEvent(config: any): void;
3
+ dispose(config: any): void;
4
+ disposeAll(): void;
5
+ disposeSingle(config: any): void;
6
+ }
7
+
8
+ export default SharedEventsInterface;
@@ -0,0 +1,12 @@
1
+ export interface SharedHelpersInterface<F extends Record<string, any> = Record<string, any>> {
2
+ $(key: keyof F & (string | number)): any;
3
+ values(): { [K in keyof F]?: F[K] };
4
+ errors(): { [K in keyof F]?: any };
5
+ labels(): { [K in keyof F]?: any };
6
+ placeholders(): { [K in keyof F]?: any };
7
+ defaults(): { [K in keyof F]?: any };
8
+ initials(): { [K in keyof F]?: any };
9
+ types(): { [K in keyof F]?: any };
10
+ }
11
+
12
+ export default SharedHelpersInterface;
@@ -0,0 +1,6 @@
1
+ export interface SharedInitializerInterface {
2
+ initFields(initial: any, update: boolean): void;
3
+ initField(key: string, path: string, data: any, update: boolean): any;
4
+ }
5
+
6
+ export default SharedInitializerInterface;
@@ -0,0 +1,10 @@
1
+ export interface SharedUtilsInferface {
2
+ select(path: string|number, fields?: any, isStrict?: boolean): any;
3
+ container(path?: string | null): any;
4
+ has(path: string): boolean;
5
+ map(cb: any): any;
6
+ each(iteratee: any, fields?: any, depth?: number): any;
7
+ reduce(iteratee: any, acc: any): any;
8
+ }
9
+
10
+ export default SharedUtilsInferface;
@@ -0,0 +1,44 @@
1
+ import {BindingsInterface} from "./BindingsInterface";
2
+ import {FormInterface} from "./FormInterface";
3
+ import {OptionsInterface} from "./OptionsInterface";
4
+
5
+ export enum RuntimeMode {
6
+ mixed = "mixed",
7
+ unified = "unified",
8
+ separated = "separated",
9
+ }
10
+
11
+ export interface StateInterface {
12
+ mode: RuntimeMode;
13
+ strict: boolean;
14
+ form: FormInterface;
15
+ options: OptionsInterface;
16
+ bindings: BindingsInterface;
17
+
18
+ $struct: string[];
19
+ $extra: any;
20
+
21
+ disposers: {
22
+ interceptor: any;
23
+ observer: any;
24
+ };
25
+
26
+ initial: {
27
+ props: any;
28
+ fields: any;
29
+ };
30
+
31
+ current: {
32
+ props: any;
33
+ fields: any;
34
+ };
35
+
36
+ initProps(initial: any): void;
37
+ struct(data?: string[]): string[];
38
+ get(type: any, subtype: any): any;
39
+ set(type: string, subtype: string, state: any): void;
40
+ extra(data?: any): any | null;
41
+ observeOptions(): void;
42
+ }
43
+
44
+ export default StateInterface;
@@ -0,0 +1,88 @@
1
+ import Form from "../Form";
2
+ import Field from "../Field";
3
+ import { FieldInterface } from "./FieldInterface";
4
+ import { FormInterface } from "./FormInterface";
5
+ import { StateInterface } from "./StateInterface";
6
+
7
+ export interface ValidatorConstructor {
8
+ form: FormInterface | null;
9
+ plugins: ValidationPlugins;
10
+ }
11
+
12
+ export interface ValidateOptionsInterface {
13
+ showErrors?: boolean;
14
+ related?: boolean;
15
+ field?: FieldInterface;
16
+ path?: string;
17
+ }
18
+
19
+ export type ValidateOptions = string | ValidateOptionsInterface | Form | Field;
20
+
21
+ export interface ValidatorInterface {
22
+ promises: Promise<any>[];
23
+ form: FormInterface | null;
24
+ drivers: any;
25
+ plugins: ValidationPlugins;
26
+ error: string | null;
27
+ initDrivers(): void;
28
+ validate(opt?: ValidateOptions, obj?: ValidateOptions): Promise<any>;
29
+ validateField(opt: ValidateOptionsInterface): void;
30
+ validateRelatedFields(field: any, showErrors: boolean): void;
31
+ }
32
+
33
+ export type ValidationPackage = any;
34
+
35
+ export type ExtendPlugin<TValidator = ValidationPackage> = (args: {
36
+ validator: TValidator;
37
+ form: FormInterface;
38
+ }) => void;
39
+
40
+ export interface ValidationPluginConfig<TValidator = ValidationPackage> {
41
+ package?: TValidator;
42
+ schema?: any;
43
+ options?: any;
44
+ extend?: ExtendPlugin<TValidator>;
45
+ }
46
+
47
+ export interface ValidationPluginConstructor<TValidator = ValidationPackage> {
48
+ config: ValidationPluginConfig<TValidator>;
49
+ state: StateInterface | null;
50
+ promises: Promise<unknown>[];
51
+ }
52
+
53
+ export interface ValidationPluginInterface<TValidator = ValidationPackage>
54
+ extends ValidationPluginConstructor<TValidator> {
55
+ validator: TValidator;
56
+ schema?: any;
57
+ extend?: ExtendPlugin<TValidator>;
58
+ validate(field: FieldInterface): void;
59
+ class?(constructor: ValidationPluginConstructor<TValidator>): void;
60
+ }
61
+
62
+ export type ValidationPlugin<TValidator = ValidationPackage> = {
63
+ class: any;
64
+ config?: ValidationPluginConfig<TValidator>;
65
+ };
66
+
67
+ export interface ValidationPlugins {
68
+ [key: string]: ValidationPlugin | undefined;
69
+ vjf?: ValidationPlugin;
70
+ dvr?: ValidationPlugin;
71
+ ajv?: ValidationPlugin;
72
+ yup?: ValidationPlugin;
73
+ zod?: ValidationPlugin;
74
+ joi?: ValidationPlugin;
75
+ valibot?: ValidationPlugin;
76
+ vinejs?: ValidationPlugin;
77
+ }
78
+
79
+ export type DriversMap = {
80
+ [key in keyof ValidationPlugins]: ValidationPluginInterface;
81
+ }
82
+
83
+ export enum ValidationHooks {
84
+ onSuccess = 'onSuccess',
85
+ onError = 'onError',
86
+ }
87
+
88
+ export default ValidatorInterface;
package/src/parser.ts ADDED
@@ -0,0 +1,327 @@
1
+ import { each, get, has, isEmpty, isPlainObject, merge, set, transform } from "lodash";
2
+ import { FieldPropsEnum, SeparatedPropsMode } from "./models/FieldProps";
3
+ import {
4
+ $try,
5
+ isArrayOfStrings,
6
+ isArrayOfObjects,
7
+ hasUnifiedProps,
8
+ allowNested,
9
+ pathToStruct,
10
+ isEmptyArray,
11
+ } from "./utils";
12
+
13
+ const defaultValue = ({
14
+ type = undefined,
15
+ value = undefined,
16
+ nullable = undefined,
17
+ isEmptyArray = false,
18
+ fallbackValueOption = "",
19
+ }: any): null | false | 0 | [] | "" => {
20
+ if (Array.isArray(value) || isEmptyArray) return [];
21
+ if (nullable || value instanceof Date || type === "date" || type === "datetime-local") return null;
22
+ if (typeof value === 'number' || type === "number") return 0;
23
+ if (typeof value === 'boolean' || type === "checkbox") return false;
24
+ if (typeof value === 'string' || type === "file") return "";
25
+ return fallbackValueOption;
26
+ };
27
+
28
+ const parsePath = (path: any): string => {
29
+ let $path = String(path ?? '');
30
+ $path = $path.replace(/\[/g, ".");
31
+ $path = $path.replace(/\]/g, "");
32
+ return $path;
33
+ };
34
+
35
+ const parseInput = (
36
+ input: (...args: any[]) => any,
37
+ { fallbackValueOption = "", type, isEmptyArray, separated, unified, fallback }: any
38
+ ) =>
39
+ input(
40
+ $try(
41
+ separated,
42
+ unified,
43
+ fallback,
44
+ defaultValue({
45
+ fallbackValueOption,
46
+ type,
47
+ isEmptyArray,
48
+ })
49
+ )
50
+ );
51
+
52
+ const parseArrayProp = (val: any, prop: string, removeNullishValuesInArrays: boolean): any => {
53
+ const values = Object.values(val);
54
+ const isValProp: boolean = ([
55
+ FieldPropsEnum.value,
56
+ FieldPropsEnum.initial,
57
+ FieldPropsEnum.default,
58
+ ] as string[]).includes(prop)
59
+
60
+ if (removeNullishValuesInArrays && isValProp) {
61
+ return values.filter(v => v !== null && v !== undefined && v !== "");
62
+ }
63
+
64
+ return values;
65
+ };
66
+
67
+ const parseCheckArray = (field: any, value: any, prop: string, removeNullishValuesInArrays: boolean) => {
68
+ if (field.incremental && value !== null && typeof value === 'object' && isEmpty(value)) return [];
69
+ return field.hasIncrementalKeys ? parseArrayProp(value, prop, removeNullishValuesInArrays) : value;
70
+ }
71
+
72
+ const parseCheckOutput = (field: any, prop: string, retrieveNullifiedEmptyStrings = false) => {
73
+ if (prop === FieldPropsEnum.value || prop.startsWith("value.")) {
74
+ const base = field.$output ? field.$output(field[FieldPropsEnum.value]) : field[FieldPropsEnum.value]
75
+ const value = prop.startsWith("value.") ? get(base, prop.substring(6)) : base
76
+ if (typeof value === 'string' && isEmpty(value) && retrieveNullifiedEmptyStrings) return null
77
+ return value;
78
+ }
79
+ return field[prop];
80
+ }
81
+
82
+ const defineFieldsFromStruct = (struct: string[], add: boolean = false) =>
83
+ struct.reduceRight(
84
+ ($, name) => {
85
+ const obj: any = {};
86
+ if (name.endsWith("[]")) {
87
+ const val = add ? [$] : [];
88
+ obj[name.replace(/\[\]$/, "")] = val;
89
+ return obj;
90
+ }
91
+ // no brakets
92
+ const prev = struct[struct.indexOf(name) - 1];
93
+ const stop = !!prev && prev.endsWith("[]") && struct[struct.length - 1] === name;
94
+ if (!add && stop) return obj;
95
+ obj[name] = $;
96
+ return obj;
97
+ },
98
+ {}
99
+ );
100
+
101
+ const handleFieldsArrayOfStrings = ($fields: any, add = false) => {
102
+ let fields = $fields;
103
+ // handle array with field struct (strings)
104
+ if (isArrayOfStrings(fields)) {
105
+ fields = transform(
106
+ fields,
107
+ ($obj, $) => {
108
+ const pathStruct = $.split(".");
109
+ // as array of strings (with empty values)
110
+ if (!pathStruct.length) return Object.assign($obj, { [$]: "" });
111
+ // define flat or nested fields from pathStruct
112
+ return merge($obj, defineFieldsFromStruct(pathStruct, add));
113
+ },
114
+ {}
115
+ );
116
+ }
117
+ return fields;
118
+ };
119
+
120
+ const handleFieldsArrayOfObjects = ($fields: any) => {
121
+ let fields = $fields;
122
+ // handle array of objects (with unified props)
123
+ if (isArrayOfObjects(fields)) {
124
+ fields = transform(
125
+ fields,
126
+ ($obj, field) => {
127
+ if (hasUnifiedProps({ fields: { field } }) && !has(field, FieldPropsEnum.name)) return undefined;
128
+ return Object.assign($obj, { [field.name]: field });
129
+ },
130
+ {}
131
+ );
132
+ }
133
+ return fields;
134
+ };
135
+
136
+ const handleFieldsNested = (fields: any, strictProps: boolean = true): any =>
137
+ transform(
138
+ fields,
139
+ (obj, field, key) => {
140
+ if (allowNested(field, strictProps)) {
141
+ // define nested field
142
+ return Object.assign(obj, {
143
+ [key]: {
144
+ fields: isEmptyArray(field) ? [] : handleFieldsNested(field),
145
+ },
146
+ });
147
+ }
148
+ return Object.assign(obj, { [key]: field });
149
+ },
150
+ {}
151
+ );
152
+
153
+ /* mapNestedValuesToUnifiedValues
154
+
155
+ FROM:
156
+
157
+ {
158
+ street: '123 Fake St.',
159
+ zip: '12345',
160
+ }
161
+
162
+ TO:
163
+
164
+ [{
165
+ name: 'street'
166
+ value: '123 Fake St.',
167
+ }, {
168
+ name: 'zip'
169
+ value: '12345',
170
+ }]
171
+
172
+ */
173
+ const mapNestedValuesToUnifiedValues = (data: Record<string, any>): any =>
174
+ isPlainObject(data)
175
+ ? Object.entries(data).map(([name, value]) => ({ value, name }))
176
+ : undefined;
177
+
178
+ /* reduceValuesToUnifiedFields
179
+
180
+ FROM:
181
+
182
+ {
183
+ name: 'fatty',
184
+ address: {
185
+ street: '123 Fake St.',
186
+ zip: '12345',
187
+ },
188
+ };
189
+
190
+ TO:
191
+
192
+ {
193
+ name: {
194
+ value: 'fatty',
195
+ fields: undefined
196
+ },
197
+ address: {
198
+ value: {
199
+ street: '123 Fake St.',
200
+ zip: '12345'
201
+ },
202
+ fields: [ ... ]
203
+ },
204
+ };
205
+
206
+ */
207
+ const reduceValuesToUnifiedFields = (values: Record<string, any>): Record<string, any> =>
208
+ transform(
209
+ values,
210
+ (obj, value, key) =>
211
+ Object.assign(obj, {
212
+ [key]: {
213
+ value,
214
+ fields: mapNestedValuesToUnifiedValues(value),
215
+ },
216
+ }),
217
+ {}
218
+ );
219
+
220
+ /*
221
+ Fallback Unified Props to Separated Mode
222
+ */
223
+ const handleFieldsPropsFallback = (
224
+ fields: any,
225
+ initial: any,
226
+ fallback: any
227
+ ) => {
228
+ if (!has(initial, SeparatedPropsMode.values)) return fields;
229
+ // if the 'values' object is passed in constructor
230
+ // then update the fields definitions
231
+ let { values } = initial;
232
+ if (hasUnifiedProps({ fields: initial.fields })) {
233
+ values = reduceValuesToUnifiedFields(values);
234
+ }
235
+ return merge(
236
+ fields,
237
+ transform(
238
+ values,
239
+ (result: any, v, k) => {
240
+ if (Array.isArray(fields[k])) result[k] = v;
241
+ if (!(k in fields) && (!isNaN(Number(k)) || fallback)) result[k] = v;
242
+ },
243
+ {}
244
+ )
245
+ );
246
+ };
247
+
248
+ const mergeSchemaDefaults = (fields: any, validator: any) => {
249
+ if (validator) {
250
+ const schema = get(validator.plugins, "ajv.config.schema");
251
+ if (isEmpty(fields) && schema && !!schema.properties) {
252
+ each(schema.properties, (prop, key) => {
253
+ set(fields, key, {
254
+ value: prop.default,
255
+ label: prop.title,
256
+ });
257
+ });
258
+ }
259
+ }
260
+ return fields;
261
+ };
262
+
263
+ const prepareFieldsData = (
264
+ initial: any,
265
+ strictProps: boolean = true,
266
+ fallback: boolean = true,
267
+ ) => {
268
+ let fields = merge(
269
+ handleFieldsArrayOfStrings(initial.fields, false),
270
+ handleFieldsArrayOfStrings(initial.struct, false)
271
+ );
272
+
273
+ fields = handleFieldsArrayOfObjects(fields);
274
+ fields = handleFieldsPropsFallback(fields, initial, fallback);
275
+ fields = handleFieldsNested(fields, strictProps);
276
+
277
+ return fields;
278
+ };
279
+
280
+ const pathToFieldsTree = (
281
+ struct: any,
282
+ path: string,
283
+ n: number = 0,
284
+ add: boolean = false
285
+ ) => {
286
+ const $struct = (Array.isArray(struct) ? struct : Object.values(struct)).filter((item: any) => typeof item === 'string');
287
+ const structPath = pathToStruct(path);
288
+ const structArray = $struct.filter((item) =>
289
+ item.startsWith(structPath)
290
+ );
291
+ const $tree = handleFieldsArrayOfStrings(structArray, add);
292
+ const $structPath = structPath.replace(/\[\]/g, `[${n}]`);
293
+ const fields = handleFieldsNested(get($tree, $structPath));
294
+
295
+ // fix issues #614 & #615
296
+ $struct.length && $struct
297
+ .filter(s => s.startsWith(path + '[]'))
298
+ .map(s => s.substring((path + '[].').length))
299
+ .filter(s => s.endsWith('[]'))
300
+ .map(s => s.substring(0, s.length - 2))
301
+ .forEach(s => {
302
+ const ss = s.split('.')
303
+ let t = fields[0]?.fields
304
+ for (let i = 0; i < ss.length; i++) {
305
+ t = t?.[ss[i]]?.[FieldPropsEnum.fields]
306
+ if (!t) break;
307
+ }
308
+ if (t)
309
+ delete t[0]
310
+ });
311
+
312
+ return fields;
313
+ };
314
+
315
+ export {
316
+ defaultValue,
317
+ parseInput,
318
+ parsePath,
319
+ parseArrayProp,
320
+ parseCheckArray,
321
+ parseCheckOutput,
322
+ mergeSchemaDefaults,
323
+ handleFieldsNested,
324
+ handleFieldsArrayOfStrings,
325
+ prepareFieldsData,
326
+ pathToFieldsTree,
327
+ };