@digigov/form 2.0.0-e7d30530 → 2.0.0-eaf330f5

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 (268) hide show
  1. package/Field/FieldBase/index.js +4 -3
  2. package/Field/FieldBase.js.map +2 -2
  3. package/Field/FieldBaseContainer/index.js +19 -37
  4. package/Field/FieldBaseContainer.d.ts +1 -1
  5. package/Field/FieldBaseContainer.js.map +2 -2
  6. package/Field/FieldConditional/index.js +1 -1
  7. package/Field/FieldConditional.js.map +1 -1
  8. package/Field/index.js +1 -2
  9. package/Field/index.js.map +2 -2
  10. package/Field/types.d.ts +2 -2
  11. package/Field/utils/index.js +3 -4
  12. package/Field/utils/index.js.map +2 -2
  13. package/FieldArray/FormDialog/index.js +7 -2
  14. package/FieldArray/FormDialog.d.ts +3 -2
  15. package/FieldArray/FormDialog.js.map +2 -2
  16. package/FieldArray/__stories__/Default.d.ts +1 -1
  17. package/FieldArray/__stories__/WithExactLength.d.ts +1 -1
  18. package/FieldArray/__stories__/WithModal.d.ts +1 -1
  19. package/FieldArray/index.d.ts +1 -1
  20. package/FieldArray/index.js +88 -95
  21. package/FieldArray/index.js.map +3 -3
  22. package/FormBuilder/FormBuilder.stories.d.ts +0 -2
  23. package/FormBuilder/__stories__/AutoErrorGrouping.d.ts +1 -1
  24. package/FormBuilder/__stories__/Default.d.ts +1 -1
  25. package/FormBuilder/__stories__/ErrorGrouping.d.ts +1 -1
  26. package/FormBuilder/index.d.ts +1 -1
  27. package/FormBuilder/index.js +7 -3
  28. package/FormBuilder/index.js.map +2 -2
  29. package/FormContext/index.js +2 -6
  30. package/FormContext.d.ts +0 -1
  31. package/FormContext.js.map +2 -2
  32. package/MultiplicityField/MultiplicityField.stories.d.ts +0 -2
  33. package/MultiplicityField/__stories__/Default.d.ts +1 -1
  34. package/MultiplicityField/__stories__/PreviewDisplay.d.ts +1 -1
  35. package/MultiplicityField/__stories__/WithExactLength.d.ts +1 -1
  36. package/MultiplicityField/__stories__/WithMaxLength.d.ts +1 -1
  37. package/MultiplicityField/__stories__/WithMinAndMaxLength.d.ts +1 -1
  38. package/MultiplicityField/__stories__/WithMinLength.d.ts +1 -1
  39. package/MultiplicityField/add-objects/index.js +1 -1
  40. package/MultiplicityField/add-objects.js.map +2 -2
  41. package/MultiplicityField/index.js +2 -4
  42. package/MultiplicityField/index.js.map +2 -2
  43. package/MultiplicityField/types.d.ts +0 -1
  44. package/Questions/Questions.stories.d.ts +0 -2
  45. package/Questions/__stories__/Default.d.ts +1 -1
  46. package/cjs/Field/FieldBase/index.js +4 -3
  47. package/cjs/Field/FieldBase.js.map +2 -2
  48. package/cjs/Field/FieldBaseContainer/index.js +18 -36
  49. package/cjs/Field/FieldBaseContainer.js.map +3 -3
  50. package/cjs/Field/FieldConditional/index.js +1 -1
  51. package/cjs/Field/FieldConditional.js.map +1 -1
  52. package/cjs/Field/index.js +1 -2
  53. package/cjs/Field/index.js.map +2 -2
  54. package/cjs/Field/types.js.map +1 -1
  55. package/cjs/Field/utils/index.js +3 -4
  56. package/cjs/Field/utils/index.js.map +2 -2
  57. package/cjs/FieldArray/FormDialog/index.js +7 -2
  58. package/cjs/FieldArray/FormDialog.js.map +2 -2
  59. package/cjs/FieldArray/index.js +83 -93
  60. package/cjs/FieldArray/index.js.map +3 -3
  61. package/cjs/FormBuilder/index.js +7 -3
  62. package/cjs/FormBuilder/index.js.map +2 -2
  63. package/cjs/FormContext/index.js +2 -7
  64. package/cjs/FormContext.js.map +2 -2
  65. package/cjs/MultiplicityField/add-objects/index.js +1 -1
  66. package/cjs/MultiplicityField/add-objects.js.map +2 -2
  67. package/cjs/MultiplicityField/index.js +2 -4
  68. package/cjs/MultiplicityField/index.js.map +2 -2
  69. package/cjs/MultiplicityField/types.js.map +1 -1
  70. package/cjs/inputs/Checkboxes/index.js +1 -2
  71. package/cjs/inputs/Checkboxes/index.js.map +2 -2
  72. package/cjs/inputs/DateInput/index.js +12 -27
  73. package/cjs/inputs/DateInput/index.js.map +2 -2
  74. package/cjs/inputs/DateTimeInput/index.js +34 -55
  75. package/cjs/inputs/DateTimeInput/index.js.map +2 -2
  76. package/cjs/inputs/FileInput/index.js +26 -45
  77. package/cjs/inputs/FileInput/index.js.map +2 -2
  78. package/cjs/inputs/ImageInput/index.js +10 -41
  79. package/cjs/inputs/ImageInput/index.js.map +2 -2
  80. package/cjs/inputs/Input/inputsInputScenarios/index.js +439 -0
  81. package/cjs/inputs/Input/inputsInputScenarios.js.map +7 -0
  82. package/cjs/lazy/index.js +6 -6
  83. package/cjs/lazy.js.map +2 -2
  84. package/cjs/registry/index.js +10 -8
  85. package/cjs/registry.js.map +2 -2
  86. package/cjs/validators/index.js +10 -8
  87. package/cjs/validators/index.js.map +2 -2
  88. package/cjs/validators/utils/date/index.js +3 -21
  89. package/cjs/validators/utils/date.js.map +2 -2
  90. package/cjs/validators/utils/datetime/index.js +24 -113
  91. package/cjs/validators/utils/datetime.js.map +2 -2
  92. package/index.js +1 -1
  93. package/inputs/AutoCompleteInput/AutoComplete.stories.d.ts +0 -2
  94. package/inputs/AutoCompleteInput/__stories__/Default.d.ts +1 -1
  95. package/inputs/AutoCompleteInput/__stories__/Multiple.d.ts +1 -1
  96. package/inputs/Checkboxes/Checkboxes.stories.d.ts +0 -2
  97. package/inputs/Checkboxes/__stories__/Conditional.d.ts +1 -1
  98. package/inputs/Checkboxes/__stories__/Default.d.ts +1 -1
  99. package/inputs/Checkboxes/__stories__/WithDivider.d.ts +1 -1
  100. package/inputs/Checkboxes/index.js +1 -2
  101. package/inputs/Checkboxes/index.js.map +2 -2
  102. package/inputs/DateInput/DateInput.stories.d.ts +0 -6
  103. package/inputs/DateInput/__stories__/Default.d.ts +1 -1
  104. package/inputs/DateInput/index.js +13 -28
  105. package/inputs/DateInput/index.js.map +2 -2
  106. package/inputs/DateTimeInput/DateTimeInput.stories.d.ts +3 -10
  107. package/inputs/DateTimeInput/__stories__/Default.d.ts +1 -1
  108. package/inputs/DateTimeInput/index.js +35 -56
  109. package/inputs/DateTimeInput/index.js.map +2 -2
  110. package/inputs/FileInput/FileInput.stories.d.ts +0 -2
  111. package/inputs/FileInput/__stories__/Default.d.ts +1 -1
  112. package/inputs/FileInput/index.d.ts +1 -5
  113. package/inputs/FileInput/index.js +27 -46
  114. package/inputs/FileInput/index.js.map +2 -2
  115. package/inputs/ImageInput/ImageInput.stories.d.ts +1 -4
  116. package/inputs/ImageInput/__stories__/Default.d.ts +1 -1
  117. package/inputs/ImageInput/__stories__/MaxSize.d.ts +1 -1
  118. package/inputs/ImageInput/__stories__/WithInvalidImageDimension.d.ts +1 -1
  119. package/inputs/ImageInput/index.d.ts +1 -9
  120. package/inputs/ImageInput/index.js +11 -42
  121. package/inputs/ImageInput/index.js.map +2 -2
  122. package/inputs/Input/Input.stories.d.ts +0 -3
  123. package/inputs/Input/__stories__/AFM.d.ts +1 -1
  124. package/inputs/Input/__stories__/Boolean.d.ts +1 -1
  125. package/inputs/Input/__stories__/IBAN.d.ts +1 -1
  126. package/inputs/Input/__stories__/Integer.d.ts +1 -1
  127. package/inputs/Input/__stories__/LandlineNumber.d.ts +1 -1
  128. package/inputs/Input/__stories__/MobilePhone.d.ts +1 -1
  129. package/inputs/Input/__stories__/PhoneNumber.d.ts +1 -1
  130. package/inputs/Input/__stories__/PostalCode.d.ts +1 -1
  131. package/inputs/Input/__stories__/String.d.ts +1 -1
  132. package/inputs/Input/__stories__/StringWithTrimValidation.d.ts +1 -1
  133. package/inputs/Input/__stories__/TextWithLimit.d.ts +1 -1
  134. package/inputs/Input/inputsInputScenarios/index.js +406 -0
  135. package/inputs/{ImageInput/__stories__/logo.d → Input/inputsInputScenarios}/package.json +1 -1
  136. package/inputs/Input/inputsInputScenarios.d.ts +57 -0
  137. package/inputs/Input/inputsInputScenarios.js.map +7 -0
  138. package/inputs/Label/Label.stories.d.ts +0 -2
  139. package/inputs/Label/__stories__/Default.d.ts +1 -1
  140. package/inputs/OtpInput/OtpInput.stories.d.ts +0 -2
  141. package/inputs/OtpInput/__stories__/Default.d.ts +1 -1
  142. package/inputs/Radio/Radio.stories.d.ts +0 -2
  143. package/inputs/Radio/__stories__/Conditional.d.ts +1 -1
  144. package/inputs/Radio/__stories__/Default.d.ts +1 -1
  145. package/inputs/Radio/__stories__/WithDivider.d.ts +1 -1
  146. package/inputs/Select/Select.stories.d.ts +0 -2
  147. package/inputs/Select/__stories__/Default.d.ts +1 -1
  148. package/lazy/index.js +6 -6
  149. package/package.json +8 -8
  150. package/registry/index.js +10 -8
  151. package/src/Field/FieldBase.tsx +18 -14
  152. package/src/Field/FieldBaseContainer.tsx +51 -68
  153. package/src/Field/FieldConditional.tsx +1 -1
  154. package/src/Field/index.tsx +1 -2
  155. package/src/Field/types.tsx +2 -2
  156. package/src/Field/utils/index.ts +1 -2
  157. package/src/FieldArray/FormDialog.tsx +12 -3
  158. package/src/FieldArray/__stories__/Default.tsx +1 -1
  159. package/src/FieldArray/__stories__/WithExactLength.tsx +1 -1
  160. package/src/FieldArray/__stories__/WithModal.tsx +1 -1
  161. package/src/FieldArray/index.tsx +103 -111
  162. package/src/FormBuilder/FormBuilder.stories.js +0 -3
  163. package/src/FormBuilder/__stories__/AutoErrorGrouping.tsx +1 -1
  164. package/src/FormBuilder/__stories__/Default.tsx +1 -1
  165. package/src/FormBuilder/__stories__/ErrorGrouping.tsx +1 -1
  166. package/src/FormBuilder/index.tsx +7 -3
  167. package/src/FormBuilder/scenarios.test.tsx +1 -1
  168. package/src/FormContext.tsx +1 -5
  169. package/src/MultiplicityField/MultiplicityField.stories.js +0 -3
  170. package/src/MultiplicityField/__stories__/Default.tsx +1 -1
  171. package/src/MultiplicityField/__stories__/PreviewDisplay.tsx +1 -1
  172. package/src/MultiplicityField/__stories__/WithExactLength.tsx +1 -1
  173. package/src/MultiplicityField/__stories__/WithMaxLength.tsx +1 -1
  174. package/src/MultiplicityField/__stories__/WithMinAndMaxLength.tsx +1 -1
  175. package/src/MultiplicityField/__stories__/WithMinLength.tsx +1 -1
  176. package/src/MultiplicityField/add-objects.tsx +1 -1
  177. package/src/MultiplicityField/index.tsx +1 -3
  178. package/src/MultiplicityField/types.ts +0 -1
  179. package/src/Questions/Questions.stories.js +0 -3
  180. package/src/Questions/__stories__/Default.tsx +1 -1
  181. package/src/inputs/AutoCompleteInput/AutoComplete.stories.js +0 -3
  182. package/src/inputs/AutoCompleteInput/__stories__/Default.tsx +1 -1
  183. package/src/inputs/AutoCompleteInput/__stories__/Multiple.tsx +1 -1
  184. package/src/inputs/Checkboxes/Checkboxes.stories.js +0 -3
  185. package/src/inputs/Checkboxes/__stories__/Conditional.tsx +1 -1
  186. package/src/inputs/Checkboxes/__stories__/Default.tsx +1 -1
  187. package/src/inputs/Checkboxes/__stories__/WithDivider.tsx +1 -1
  188. package/src/inputs/Checkboxes/index.tsx +5 -4
  189. package/src/inputs/DateInput/DateInput.stories.js +0 -7
  190. package/src/inputs/DateInput/__stories__/Default.tsx +1 -1
  191. package/src/inputs/DateInput/index.test.tsx +0 -16
  192. package/src/inputs/DateInput/index.tsx +13 -31
  193. package/src/inputs/DateTimeInput/DateTimeInput.stories.js +6 -12
  194. package/src/inputs/DateTimeInput/__stories__/Default.tsx +1 -1
  195. package/src/inputs/DateTimeInput/index.test.tsx +0 -16
  196. package/src/inputs/DateTimeInput/index.tsx +35 -59
  197. package/src/inputs/FileInput/FileInput.stories.js +0 -3
  198. package/src/inputs/FileInput/__stories__/Default.tsx +1 -1
  199. package/src/inputs/FileInput/index.tsx +39 -72
  200. package/src/inputs/ImageInput/ImageInput.stories.js +1 -5
  201. package/src/inputs/ImageInput/__stories__/Default.tsx +1 -1
  202. package/src/inputs/ImageInput/__stories__/MaxSize.tsx +2 -3
  203. package/src/inputs/ImageInput/__stories__/WithInvalidImageDimension.tsx +1 -1
  204. package/src/inputs/ImageInput/index.test.tsx +0 -4
  205. package/src/inputs/ImageInput/index.tsx +48 -100
  206. package/src/inputs/Input/Input.stories.js +0 -4
  207. package/src/inputs/Input/__stories__/AFM.tsx +1 -1
  208. package/src/inputs/Input/__stories__/Boolean.tsx +1 -1
  209. package/src/inputs/Input/__stories__/IBAN.tsx +1 -1
  210. package/src/inputs/Input/__stories__/Integer.tsx +1 -1
  211. package/src/inputs/Input/__stories__/LandlineNumber.tsx +1 -1
  212. package/src/inputs/Input/__stories__/MobilePhone.tsx +1 -1
  213. package/src/inputs/Input/__stories__/PhoneNumber.tsx +1 -1
  214. package/src/inputs/Input/__stories__/PostalCode.tsx +1 -1
  215. package/src/inputs/Input/__stories__/String.tsx +1 -1
  216. package/src/inputs/Input/__stories__/StringWithTrimValidation.tsx +1 -1
  217. package/src/inputs/Input/__stories__/TextWithLimit.tsx +1 -1
  218. package/src/inputs/Input/index.test.tsx +0 -8
  219. package/src/inputs/Input/inputsInputScenarios.ts +404 -0
  220. package/src/inputs/Label/Label.stories.js +0 -3
  221. package/src/inputs/Label/__stories__/Default.tsx +1 -1
  222. package/src/inputs/OtpInput/OtpInput.stories.js +0 -3
  223. package/src/inputs/OtpInput/__stories__/Default.tsx +1 -1
  224. package/src/inputs/Radio/Radio.stories.js +0 -3
  225. package/src/inputs/Radio/__stories__/Conditional.tsx +1 -1
  226. package/src/inputs/Radio/__stories__/Default.tsx +1 -1
  227. package/src/inputs/Radio/__stories__/WithDivider.tsx +1 -1
  228. package/src/inputs/Select/Select.stories.js +0 -3
  229. package/src/inputs/Select/__stories__/Default.tsx +1 -1
  230. package/src/lazy.js +6 -6
  231. package/src/registry.js +10 -8
  232. package/src/validators/index.ts +13 -13
  233. package/src/validators/utils/date.ts +4 -23
  234. package/src/validators/utils/datetime.ts +29 -116
  235. package/validators/index.js +10 -8
  236. package/validators/index.js.map +2 -2
  237. package/validators/utils/date/index.js +3 -21
  238. package/validators/utils/date.d.ts +1 -1
  239. package/validators/utils/date.js.map +2 -2
  240. package/validators/utils/datetime/index.js +24 -113
  241. package/validators/utils/datetime.d.ts +1 -1
  242. package/validators/utils/datetime.js.map +2 -2
  243. package/cjs/inputs/ImageInput/__stories__/logo.d/index.js +0 -1
  244. package/cjs/inputs/ImageInput/__stories__/logo.d.js.map +0 -7
  245. package/inputs/DateInput/__stories__/WithDefaultValue.d.ts +0 -3
  246. package/inputs/DateInput/__stories__/WithInitialValue.d.ts +0 -3
  247. package/inputs/DateInput/__stories__/WithWrongDefaultValue.d.ts +0 -3
  248. package/inputs/DateInput/__stories__/WithWrongInitialValue.d.ts +0 -3
  249. package/inputs/DateTimeInput/__stories__/WithDefaultValue.d.ts +0 -3
  250. package/inputs/DateTimeInput/__stories__/WithInitialValue.d.ts +0 -3
  251. package/inputs/DateTimeInput/__stories__/WithWrongDefaultValue.d.ts +0 -3
  252. package/inputs/DateTimeInput/__stories__/WithWrongInitialValue.d.ts +0 -3
  253. package/inputs/ImageInput/__stories__/WithInitialValues.d.ts +0 -3
  254. package/inputs/ImageInput/__stories__/logo.d/index.js +0 -1
  255. package/inputs/ImageInput/__stories__/logo.d.js.map +0 -7
  256. package/inputs/Input/__stories__/Email.d.ts +0 -3
  257. package/src/inputs/DateInput/__stories__/WithDefaultValue.tsx +0 -26
  258. package/src/inputs/DateInput/__stories__/WithInitialValue.tsx +0 -28
  259. package/src/inputs/DateInput/__stories__/WithWrongDefaultValue.tsx +0 -26
  260. package/src/inputs/DateInput/__stories__/WithWrongInitialValue.tsx +0 -28
  261. package/src/inputs/DateTimeInput/__stories__/WithDefaultValue.tsx +0 -26
  262. package/src/inputs/DateTimeInput/__stories__/WithInitialValue.tsx +0 -28
  263. package/src/inputs/DateTimeInput/__stories__/WithWrongDefaultValue.tsx +0 -26
  264. package/src/inputs/DateTimeInput/__stories__/WithWrongInitialValue.tsx +0 -28
  265. package/src/inputs/ImageInput/__stories__/WithInitialValues.tsx +0 -45
  266. package/src/inputs/ImageInput/__stories__/logo.d.ts +0 -4
  267. package/src/inputs/ImageInput/__stories__/logo.png +0 -0
  268. package/src/inputs/Input/__stories__/Email.tsx +0 -23
@@ -12,12 +12,9 @@ const getDate = (v: string) => {
12
12
  return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
13
13
  };
14
14
 
15
- const getDaysInMonth = (month: number, year: number): number => {
16
- return dayjs(`${year}-${month}-01`).daysInMonth(); // e.g. 29 for Feb 2024
17
- };
18
-
19
15
  // Function to check partial date completion
20
16
  const checkPartialDate = (value: string) => {
17
+ console.log("value.split('/')", value.split('/'));
21
18
  const [day, month, year] = value.split('/');
22
19
  if (day && !month && !year) {
23
20
  return 'form.error.date.invalid_month_year';
@@ -36,25 +33,9 @@ const checkPartialDate = (value: string) => {
36
33
  }
37
34
  if (!day && month && year) {
38
35
  return 'form.error.date.invalid_day';
36
+ } else {
37
+ return 'form.error.date.invalid';
39
38
  }
40
- if (day && month) {
41
- const dayNum = parseInt(day);
42
- const monthNum = parseInt(month);
43
- const yearNum = parseInt(year);
44
- const maxDay = getDaysInMonth(monthNum, yearNum);
45
- if (dayNum < 1 || dayNum > maxDay) {
46
- return {
47
- key: 'form.error.date.invalid_day_range',
48
- context: {
49
- maxDay: maxDay,
50
- },
51
- };
52
- }
53
- if (monthNum < 1 || monthNum > 12) {
54
- return 'form.error.datetime.invalid_month_range';
55
- }
56
- }
57
- return 'form.error.date.invalid';
58
39
  };
59
40
 
60
41
  export const LATER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {
@@ -115,7 +96,7 @@ export const EARLIER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {
115
96
 
116
97
  export const DATE_VALIDATOR = {
117
98
  name: 'date-validator',
118
- message: (v): string | object => {
99
+ message: (v): string => {
119
100
  return checkPartialDate(v.value);
120
101
  },
121
102
  test: function (value: string) {
@@ -14,141 +14,54 @@ const getDateTime = (v: string) => {
14
14
  : dayjs(v, VALID_DATETIME_FORMATS, true);
15
15
  };
16
16
 
17
- const getDaysInMonth = (month: number, year: number): number => {
18
- return dayjs(`${year}-${month}-01`).daysInMonth(); // e.g. 29 for Feb 2024
19
- };
20
-
17
+ // Function to check partial datetime completion
21
18
  const checkPartialDateTime = (value: string) => {
22
19
  const [datePart, timePart] = value.split(' ');
23
- const [day, month, year] = datePart?.split('/') ?? [];
24
- const [hours, minutes] = timePart?.split(':') ?? [];
20
+ const [day, month, year] = datePart.split('/');
21
+ const [hours, minutes] = timePart ? timePart.split(':') : [null, null];
25
22
 
26
- const hasDay = Boolean(day);
27
- const hasMonth = Boolean(month);
28
- const hasYear = Boolean(year);
29
- const hasHours = Boolean(hours);
30
- const hasMinutes = Boolean(minutes);
31
-
32
- if (hasDay && !hasMonth && !hasYear && !hasHours && !hasMinutes) {
33
- return 'form.error.datetime.invalid_month_year_hours_minutes';
34
- }
35
- if (!hasDay && hasMonth && !hasYear && !hasHours && !hasMinutes) {
36
- return 'form.error.datetime.invalid_day_year_hours_minutes';
37
- }
38
- if (!hasDay && !hasMonth && hasYear && !hasHours && !hasMinutes) {
39
- return 'form.error.datetime.invalid_day_month_hours_minutes';
40
- }
41
- if (hasDay && hasMonth && !hasYear && !hasHours && !hasMinutes) {
42
- return 'form.error.datetime.invalid_year_hours_minutes';
43
- }
44
- if (hasDay && !hasMonth && hasYear && !hasHours && !hasMinutes) {
45
- return 'form.error.datetime.invalid_month_hours_minutes';
46
- }
47
- if (!hasDay && hasMonth && hasYear && !hasHours && !hasMinutes) {
48
- return 'form.error.datetime.invalid_day_hours_minutes';
49
- }
50
- if (hasDay && hasMonth && hasYear && !hasHours && !hasMinutes) {
51
- return 'form.error.datetime.invalid__hours_minutes';
52
- }
53
- if (hasDay && hasMonth && hasYear && hasHours && !hasMinutes) {
54
- return 'form.error.datetime.invalid_minutes';
55
- }
56
- if (hasDay && hasMonth && hasYear && !hasHours && hasMinutes) {
57
- return 'form.error.datetime.invalid_hours';
58
- }
59
- if (hasDay && !hasMonth && !hasYear && hasHours && !hasMinutes) {
60
- return 'form.error.datetime.invalid_month_year_minutes';
61
- }
62
- if (hasDay && !hasMonth && !hasYear && !hasHours && hasMinutes) {
63
- return 'form.error.datetime.invalid_month_year_hours';
64
- }
65
- if (hasDay && !hasMonth && !hasYear && hasHours && hasMinutes) {
23
+ // Check date part first
24
+ if (day && !month && !year) {
66
25
  return 'form.error.datetime.invalid_month_year';
67
26
  }
68
- if (!hasDay && hasMonth && !hasYear && hasHours && !hasMinutes) {
69
- return 'form.error.datetime.invalid_day_year_minutes';
70
- }
71
- if (!hasDay && hasMonth && !hasYear && !hasHours && hasMinutes) {
72
- return 'form.error.datetime.invalid_day_year_hours';
73
- }
74
- if (!hasDay && hasMonth && !hasYear && hasHours && hasMinutes) {
27
+ if (!day && month && !year) {
75
28
  return 'form.error.datetime.invalid_day_year';
76
29
  }
77
- if (!hasDay && !hasMonth && hasYear && hasHours && !hasMinutes) {
78
- return 'form.error.datetime.invalid_day_month_minutes';
79
- }
80
- if (!hasDay && !hasMonth && hasYear && !hasHours && hasMinutes) {
81
- return 'form.error.datetime.invalid_day_month_hours';
82
- }
83
- if (!hasDay && !hasMonth && hasYear && hasHours && hasMinutes) {
30
+ if (!day && !month && year) {
84
31
  return 'form.error.datetime.invalid_day_month';
85
32
  }
86
- if (!hasDay && !hasMonth && !hasYear && hasHours && hasMinutes) {
87
- return 'form.error.datetime.invalid_day_month_year';
88
- }
89
- if (!hasDay && !hasMonth && !hasYear && !hasHours && hasMinutes) {
90
- return 'form.error.datetime.invalid_day_month_year_hours';
91
- }
92
- if (hasDay && hasMonth && !hasYear && hasHours && !hasMinutes) {
93
- return 'form.error.datetime.invalid_year_minutes';
94
- }
95
- if (hasDay && hasMonth && !hasYear && !hasHours && hasMinutes) {
96
- return 'form.error.datetime.invalid_year_hours';
97
- }
98
- if (hasDay && hasMonth && !hasYear && hasHours && hasMinutes) {
33
+ if (day && month && !year) {
99
34
  return 'form.error.datetime.invalid_year';
100
35
  }
101
- if (hasDay && !hasMonth && hasYear && hasHours && !hasMinutes) {
102
- return 'form.error.datetime.invalid_month_minutes';
103
- }
104
- if (hasDay && !hasMonth && hasYear && !hasHours && hasMinutes) {
105
- return 'form.error.datetime.invalid_month_hours';
106
- }
107
- if (hasDay && !hasMonth && hasYear && hasHours && hasMinutes) {
36
+ if (day && !month && year) {
108
37
  return 'form.error.datetime.invalid_month';
109
38
  }
110
- if (!hasDay && hasMonth && hasYear && hasHours && !hasMinutes) {
111
- return 'form.error.datetime.invalid_day_minutes';
112
- }
113
- if (!hasDay && hasMonth && hasYear && !hasHours && hasMinutes) {
114
- return 'form.error.datetime.invalid_day_hours';
115
- }
116
- if (!hasDay && hasMonth && hasYear && hasHours && hasMinutes) {
39
+ if (!day && month && year) {
117
40
  return 'form.error.datetime.invalid_day';
118
41
  }
119
- if (!hasDay && !hasMonth && !hasYear && hasHours && !hasMinutes) {
120
- return 'form.error.datetime.invalid_day_month_year_minutes';
121
- }
122
- if (hasDay && hasMonth && hasYear && !hasHours && !hasMinutes) {
123
- return 'form.error.datetime.invalid_hours_minutes';
124
- }
125
- if (day && month) {
126
- const dayNum = parseInt(day);
127
- const monthNum = parseInt(month);
128
- const yearNum = parseInt(year);
129
- const maxDay = getDaysInMonth(monthNum, yearNum);
130
- if (dayNum < 1 || dayNum > maxDay) {
131
- return {
132
- key: 'form.error.datetime.invalid_day_range',
133
- context: {
134
- maxDay: maxDay,
135
- },
136
- };
42
+ // If date is complete, check time part
43
+ if (day && month && year) {
44
+ if (hours && !minutes) {
45
+ return 'form.error.datetime.invalid_minutes';
137
46
  }
138
- if (monthNum < 1 || monthNum > 12) {
139
- return 'form.error.datetime.invalid_month_range';
47
+ if (!hours && minutes) {
48
+ return 'form.error.datetime.invalid_hours';
140
49
  }
141
- }
142
- if (hours && minutes) {
143
- const hoursNum = parseInt(hours);
144
- const minutesNum = parseInt(minutes);
145
- if (hoursNum < 0 || hoursNum > 23) {
146
- return 'form.error.datetime.invalid_hours_range';
50
+ if (!hours && !minutes) {
51
+ return 'form.error.datetime.invalid';
147
52
  }
148
- if (minutesNum < 0 || minutesNum > 59) {
149
- return 'form.error.datetime.invalid_minutes_range';
53
+ if (hours && minutes) {
54
+ const hoursNum = parseInt(hours);
55
+ const minutesNum = parseInt(minutes);
56
+ if (hoursNum < 0 || hoursNum > 23) {
57
+ return 'form.error.datetime.invalid_hours_range';
58
+ }
59
+ if (minutesNum < 0 || minutesNum > 59) {
60
+ return 'form.error.datetime.invalid_minutes_range';
61
+ }
150
62
  }
151
63
  }
64
+
152
65
  return 'form.error.datetime.invalid';
153
66
  };
154
67
 
@@ -206,7 +119,7 @@ export const EARLIER_THAN_DATETIME_VALIDATOR = (
206
119
 
207
120
  export const DATETIME_VALIDATOR = {
208
121
  name: 'datetime-validator',
209
- message: (v): string | object => {
122
+ message: (v): string => {
210
123
  return checkPartialDateTime(v.value);
211
124
  },
212
125
  test: function (value: string) {
@@ -25,12 +25,7 @@ import { NUMBER_VALIDATOR } from "@digigov/form/validators/utils/number";
25
25
  const getYUPTypeMap = (validatorRegistry) => {
26
26
  const yupTypeMap = {
27
27
  file: (field) => {
28
- return yup.mixed().transform((value) => {
29
- if (!value || value.length === 0) {
30
- return null;
31
- }
32
- return value;
33
- }).nullable().test(FILE_MAX_SIZE_VALIDATOR(field));
28
+ return yup.mixed().nullable().test(FILE_MAX_SIZE_VALIDATOR(field));
34
29
  },
35
30
  string: (field) => {
36
31
  if (field.trim) {
@@ -110,7 +105,14 @@ const getYUPTypeMap = (validatorRegistry) => {
110
105
  postal_code: (field) => field.trim ? yup.string().trim().test(POSTALCODE_VALIDATOR(field)) : yup.string().test(POSTALCODE_VALIDATOR(field)),
111
106
  mobile_phone: (field) => field.trim ? yup.string().trim().test(MOBILE_PHONE_VALIDATOR) : yup.string().test(MOBILE_PHONE_VALIDATOR),
112
107
  phone_number: (field) => field.trim ? yup.string().trim().test(PHONE_NUMBER_VALIDATOR(field)) : yup.string().test(PHONE_NUMBER_VALIDATOR(field)),
113
- "choice:multiple": () => yup.array().of(yup.string()).nullable(),
108
+ "choice:multiple": (field) => {
109
+ if (field.required) {
110
+ return yup.array().of(yup.string()).min(1, {
111
+ key: "form.error.required"
112
+ });
113
+ }
114
+ return yup.array().of(yup.string()).nullable();
115
+ },
114
116
  "choice:single": () => yup.string().nullable(),
115
117
  date: (field) => {
116
118
  let schema = yup.string().nullable();
@@ -141,7 +143,7 @@ function useValidationSchema(fields, validatorRegistry) {
141
143
  return useMemo(() => {
142
144
  const yupTypeMap = getYUPTypeMap(validatorRegistry);
143
145
  return getYupObjectShape(fields, yupTypeMap, validatorRegistry);
144
- }, [fields, validatorRegistry]);
146
+ }, []);
145
147
  }
146
148
  export {
147
149
  getYUPTypeMap,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/validators/index.ts"],
4
- "sourcesContent": ["import { MutableRefObject, useMemo } from 'react';\nimport * as yup from 'yup';\nimport { AnyObjectSchema, StringSchema } from 'yup';\nimport { RequiredArraySchema } from 'yup/lib/array';\nimport Lazy from 'yup/lib/Lazy';\nimport { MixedSchema } from 'yup/lib/mixed';\nimport { TypeOfShape, AssertsShape } from 'yup/lib/object';\nimport { AnyObject } from 'yup/lib/types';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\nimport {\n getYupObjectShape,\n AFM_VALIDATOR,\n AMKA_VALIDATOR,\n FILE_MAX_SIZE_VALIDATOR,\n IBAN_VALIDATOR,\n OTP_VALIDATOR,\n MOBILE_PHONE_VALIDATOR,\n PHONE_NUMBER_VALIDATOR,\n TEXT_LIMIT_VALIDATOR,\n POSTALCODE_VALIDATOR,\n UUID4_VALIDATOR,\n IMAGE_DIMENSION_VALIDATOR,\n DATE_VALIDATOR,\n LATER_THAN_VALIDATOR,\n EARLIER_THAN_VALIDATOR,\n DATETIME_VALIDATOR,\n LATER_THAN_DATETIME_VALIDATOR,\n EARLIER_THAN_DATETIME_VALIDATOR,\n} from '@digigov/form/validators/utils';\nimport { INT_VALIDATOR } from '@digigov/form/validators/utils/int';\nimport { NUMBER_VALIDATOR } from '@digigov/form/validators/utils/number';\n\nexport const getYUPTypeMap = (\n validatorRegistry?: Record<string, any>\n): Record<string, any> => {\n const yupTypeMap = {\n file: (field): MixedSchema => {\n return yup\n .mixed()\n .transform((value) => {\n if (!value || value.length === 0) {\n return null;\n }\n return value;\n })\n .nullable()\n .test(FILE_MAX_SIZE_VALIDATOR(field));\n },\n string: (field): yup.StringSchema => {\n if (field.trim) {\n return yup.string().trim();\n } else {\n return yup.string();\n }\n },\n boolean: (): yup.BooleanSchema<\n boolean | null | undefined,\n AnyObject,\n boolean | null | undefined\n > => {\n return yup\n .boolean()\n .nullable()\n .transform((value) => {\n if (value === '' || typeof value === 'string') return null;\n if (typeof value === 'boolean') return value;\n if (value === 0 || value === 1) return Boolean(value);\n return value;\n });\n },\n object: (\n field\n ):\n | yup.ObjectSchema<any, AnyObject, TypeOfShape<any>, AssertsShape<any>>\n | Lazy<any, unknown> => {\n return getYupObjectShape(field.extra.fields, yupTypeMap);\n },\n array: (\n field\n ):\n | yup.ArraySchema<any, AnyObject, any[] | undefined>\n | RequiredArraySchema<any, AnyObject, any[] | undefined> => {\n if (typeof field.extra.of === 'object') {\n // else if the field is an object then it is a scalar type\n // eg. number, string etc.\n if (field.required) {\n field.extra.min = 1;\n }\n let arrayValidator = yup\n .array()\n .nullable()\n .of(yupTypeMap[field.extra.of.type](field.extra.of));\n if (field.extra.length)\n arrayValidator = arrayValidator.length(field.extra.length, {\n key: 'form.error.array.length',\n context: {\n length: field.extra.length,\n },\n });\n if (field.extra.min)\n arrayValidator = arrayValidator.min(field.extra.min, {\n key: 'form.error.array.min',\n context: {\n min: field.extra.min,\n },\n });\n if (field.extra.max)\n arrayValidator = arrayValidator.max(field.extra.max, {\n key: 'form.error.array.max',\n context: {\n max: field.extra.max,\n },\n });\n return arrayValidator;\n } else if (typeof field.extra.of === 'string') {\n return yup.array().of(yupTypeMap[field.extra.of]());\n } else {\n // in any other case it will be string\n return yup.array().of(yup.string());\n }\n },\n number: (): MixedSchema =>\n yup\n .mixed()\n .transform((_, val) => (val !== '' ? Number(val) : undefined))\n .test(NUMBER_VALIDATOR()),\n int: (): MixedSchema =>\n yup\n .mixed()\n .transform((_, val) => (val !== '' ? Number(val) : undefined))\n .test(NUMBER_VALIDATOR())\n .test(INT_VALIDATOR()),\n email: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().email('form.error.email')\n : yup.string().email('form.error.email'),\n afm: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(AFM_VALIDATOR)\n : yup.string().test(AFM_VALIDATOR),\n amka: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(AMKA_VALIDATOR)\n : yup.string().test(AMKA_VALIDATOR),\n uuid4: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(UUID4_VALIDATOR)\n : yup.string().test(UUID4_VALIDATOR),\n iban: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(IBAN_VALIDATOR(field))\n : yup.string().trim().test(IBAN_VALIDATOR(field)),\n otp: (field): yup.StringSchema =>\n yup.string().trim().test(OTP_VALIDATOR(field)),\n text: (field): yup.StringSchema => {\n if (field?.extra?.limit?.max || !!field?.extra?.limit?.min) {\n return field.trim\n ? yup.string().trim().test(TEXT_LIMIT_VALIDATOR(field))\n : yup.string().test(TEXT_LIMIT_VALIDATOR(field));\n } else {\n return field.trim ? yup.string().trim() : yup.string();\n }\n },\n image: (field): MixedSchema => {\n return yup\n .mixed()\n .transform((value) => {\n if (!value.length || value.length === 0) {\n return null;\n }\n return value;\n })\n .nullable()\n .test(FILE_MAX_SIZE_VALIDATOR(field))\n .test(IMAGE_DIMENSION_VALIDATOR(field));\n },\n postal_code: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(POSTALCODE_VALIDATOR(field))\n : yup.string().test(POSTALCODE_VALIDATOR(field)),\n mobile_phone: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(MOBILE_PHONE_VALIDATOR)\n : yup.string().test(MOBILE_PHONE_VALIDATOR),\n phone_number: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(PHONE_NUMBER_VALIDATOR(field))\n : yup.string().test(PHONE_NUMBER_VALIDATOR(field)),\n 'choice:multiple': (): yup.ArraySchema<\n yup.StringSchema<string | undefined, AnyObject, string | undefined>,\n AnyObject,\n (string | undefined)[] | null | undefined,\n (string | undefined)[] | null | undefined\n > => yup.array().of(yup.string()).nullable(),\n 'choice:single': (): StringSchema<\n string | null | undefined,\n AnyObject,\n string | null | undefined\n > => yup.string().nullable(),\n date: (\n field\n ): yup.StringSchema<\n string | null | undefined,\n AnyObject,\n string | null | undefined\n > => {\n let schema = yup.string().nullable();\n if (field?.extra?.min) {\n schema = schema.test(LATER_THAN_VALIDATOR(field));\n }\n if (field?.extra?.max) {\n schema = schema.test(EARLIER_THAN_VALIDATOR(field));\n } else {\n schema = schema.test(DATE_VALIDATOR);\n }\n return schema;\n },\n datetime: (field): yup.StringSchema => {\n if (field?.extra?.min) {\n return yup.string().test(LATER_THAN_DATETIME_VALIDATOR(field));\n }\n if (field?.extra?.max) {\n return yup.string().test(EARLIER_THAN_DATETIME_VALIDATOR(field));\n }\n return yup.string().test(DATETIME_VALIDATOR);\n },\n ...validatorRegistry,\n };\n return yupTypeMap;\n};\n\nexport interface MutableRefObjectProps {}\n// Create a yup validation schema from given fields input\nexport function useValidationSchema(\n fields: FieldSpec[] | MutableRefObject<MutableRefObjectProps>,\n validatorRegistry?: Record<string, ValidatorSchema[]>\n): Lazy<any, unknown> | AnyObjectSchema | void {\n return useMemo(() => {\n const yupTypeMap = getYUPTypeMap(validatorRegistry);\n return getYupObjectShape(fields, yupTypeMap, validatorRegistry);\n }, [fields, validatorRegistry]);\n}\n"],
5
- "mappings": "AAAA,SAA2B,eAAe;AAC1C,YAAY,SAAS;AASrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA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AK,sBAAsB;AAAA,IAC9C,cAAc,CAAC,UACb,MAAM,OACF,IAAI,OAAO,EAAE,KAAK,EAAE,KAAK,uBAAuB,KAAK,CAAC,IACtD,IAAI,OAAO,EAAE,KAAK,uBAAuB,KAAK,CAAC;AAAA,IACrD,mBAAmB,MAKd,IAAI,MAAM,EAAE,GAAG,IAAI,OAAO,CAAC,EAAE,SAAS;AAAA,IAC3C,iBAAiB,MAIZ,IAAI,OAAO,EAAE,SAAS;AAAA,IAC3B,MAAM,CACJ,UAKG;AACH,UAAI,SAAS,IAAI,OAAO,EAAE,SAAS;AACnC,UAAI,OAAO,OAAO,KAAK;AACrB,iBAAS,OAAO,KAAK,qBAAqB,KAAK,CAAC;AAAA,MAClD;AACA,UAAI,OAAO,OAAO,KAAK;AACrB,iBAAS,OAAO,KAAK,uBAAuB,KAAK,CAAC;AAAA,MACpD,OAAO;AACL,iBAAS,OAAO,KAAK,cAAc;AAAA,MACrC;AACA,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,UAA4B;AACrC,UAAI,OAAO,OAAO,KAAK;AACrB,eAAO,IAAI,OAAO,EAAE,KAAK,8BAA8B,KAAK,CAAC;AAAA,MAC/D;AACA,UAAI,OAAO,OAAO,KAAK;AACrB,eAAO,IAAI,OAAO,EAAE,KAAK,gCAAgC,KAAK,CAAC;AAAA,MACjE;AACA,aAAO,IAAI,OAAO,EAAE,KAAK,kBAAkB;AAAA,IAC7C;AAAA,IACA,GAAG;AAAA,EACL;AACA,SAAO;AACT;AAIO,SAAS,oBACd,QACA,mBAC6C;AAC7C,SAAO,QAAQ,MAAM;AACnB,UAAM,aAAa,cAAc,iBAAiB;AAClD,WAAO,kBAAkB,QAAQ,YAAY,iBAAiB;AAAA,EAChE,GAAG,CAAC,QAAQ,iBAAiB,CAAC;AAChC;",
4
+ "sourcesContent": ["import { MutableRefObject, useMemo } from 'react';\nimport * as yup from 'yup';\nimport { AnyObjectSchema, StringSchema } from 'yup';\nimport { RequiredArraySchema } from 'yup/lib/array';\nimport Lazy from 'yup/lib/Lazy';\nimport { MixedSchema } from 'yup/lib/mixed';\nimport { TypeOfShape, AssertsShape } from 'yup/lib/object';\nimport { AnyObject } from 'yup/lib/types';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\nimport {\n getYupObjectShape,\n AFM_VALIDATOR,\n AMKA_VALIDATOR,\n FILE_MAX_SIZE_VALIDATOR,\n IBAN_VALIDATOR,\n OTP_VALIDATOR,\n MOBILE_PHONE_VALIDATOR,\n PHONE_NUMBER_VALIDATOR,\n TEXT_LIMIT_VALIDATOR,\n POSTALCODE_VALIDATOR,\n UUID4_VALIDATOR,\n IMAGE_DIMENSION_VALIDATOR,\n DATE_VALIDATOR,\n LATER_THAN_VALIDATOR,\n EARLIER_THAN_VALIDATOR,\n DATETIME_VALIDATOR,\n LATER_THAN_DATETIME_VALIDATOR,\n EARLIER_THAN_DATETIME_VALIDATOR,\n} from '@digigov/form/validators/utils';\nimport { INT_VALIDATOR } from '@digigov/form/validators/utils/int';\nimport { NUMBER_VALIDATOR } from '@digigov/form/validators/utils/number';\n\nexport const getYUPTypeMap = (\n validatorRegistry?: Record<string, any>\n): Record<string, any> => {\n const yupTypeMap = {\n file: (field): MixedSchema => {\n return yup.mixed().nullable().test(FILE_MAX_SIZE_VALIDATOR(field));\n },\n string: (field): yup.StringSchema => {\n if (field.trim) {\n return yup.string().trim();\n } else {\n return yup.string();\n }\n },\n boolean: (): yup.BooleanSchema<\n boolean | null | undefined,\n AnyObject,\n boolean | null | undefined\n > => {\n return yup\n .boolean()\n .nullable()\n .transform((value) => {\n if (value === '' || typeof value === 'string') return null;\n if (typeof value === 'boolean') return value;\n if (value === 0 || value === 1) return Boolean(value);\n return value;\n });\n },\n object: (\n field\n ):\n | yup.ObjectSchema<any, AnyObject, TypeOfShape<any>, AssertsShape<any>>\n | Lazy<any, unknown> => {\n return getYupObjectShape(field.extra.fields, yupTypeMap);\n },\n array: (\n field\n ):\n | yup.ArraySchema<any, AnyObject, any[] | undefined>\n | RequiredArraySchema<any, AnyObject, any[] | undefined> => {\n if (typeof field.extra.of === 'object') {\n // else if the field is an object then it is a scalar type\n // eg. number, string etc.\n if (field.required) {\n field.extra.min = 1;\n }\n let arrayValidator = yup\n .array()\n .nullable()\n .of(yupTypeMap[field.extra.of.type](field.extra.of));\n if (field.extra.length)\n arrayValidator = arrayValidator.length(field.extra.length, {\n key: 'form.error.array.length',\n context: {\n length: field.extra.length,\n },\n });\n if (field.extra.min)\n arrayValidator = arrayValidator.min(field.extra.min, {\n key: 'form.error.array.min',\n context: {\n min: field.extra.min,\n },\n });\n if (field.extra.max)\n arrayValidator = arrayValidator.max(field.extra.max, {\n key: 'form.error.array.max',\n context: {\n max: field.extra.max,\n },\n });\n return arrayValidator;\n } else if (typeof field.extra.of === 'string') {\n return yup.array().of(yupTypeMap[field.extra.of]());\n } else {\n // in any other case it will be string\n return yup.array().of(yup.string());\n }\n },\n number: (): MixedSchema =>\n yup\n .mixed()\n .transform((_, val) => (val !== '' ? Number(val) : undefined))\n .test(NUMBER_VALIDATOR()),\n int: (): MixedSchema =>\n yup\n .mixed()\n .transform((_, val) => (val !== '' ? Number(val) : undefined))\n .test(NUMBER_VALIDATOR())\n .test(INT_VALIDATOR()),\n email: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().email('form.error.email')\n : yup.string().email('form.error.email'),\n afm: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(AFM_VALIDATOR)\n : yup.string().test(AFM_VALIDATOR),\n amka: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(AMKA_VALIDATOR)\n : yup.string().test(AMKA_VALIDATOR),\n uuid4: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(UUID4_VALIDATOR)\n : yup.string().test(UUID4_VALIDATOR),\n iban: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(IBAN_VALIDATOR(field))\n : yup.string().trim().test(IBAN_VALIDATOR(field)),\n otp: (field): yup.StringSchema =>\n yup.string().trim().test(OTP_VALIDATOR(field)),\n text: (field): yup.StringSchema => {\n if (field?.extra?.limit?.max || !!field?.extra?.limit?.min) {\n return field.trim\n ? yup.string().trim().test(TEXT_LIMIT_VALIDATOR(field))\n : yup.string().test(TEXT_LIMIT_VALIDATOR(field));\n } else {\n return field.trim ? yup.string().trim() : yup.string();\n }\n },\n image: (field): MixedSchema => {\n return yup\n .mixed()\n .transform((value) => {\n if (!value.length || value.length === 0) {\n return null;\n }\n return value;\n })\n .nullable()\n .test(FILE_MAX_SIZE_VALIDATOR(field))\n .test(IMAGE_DIMENSION_VALIDATOR(field));\n },\n postal_code: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(POSTALCODE_VALIDATOR(field))\n : yup.string().test(POSTALCODE_VALIDATOR(field)),\n mobile_phone: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(MOBILE_PHONE_VALIDATOR)\n : yup.string().test(MOBILE_PHONE_VALIDATOR),\n phone_number: (field): yup.StringSchema =>\n field.trim\n ? yup.string().trim().test(PHONE_NUMBER_VALIDATOR(field))\n : yup.string().test(PHONE_NUMBER_VALIDATOR(field)),\n 'choice:multiple': (\n field\n ): yup.ArraySchema<\n yup.StringSchema<string | undefined, AnyObject, string | undefined>,\n AnyObject,\n (string | undefined)[] | null | undefined,\n (string | undefined)[] | null | undefined\n > => {\n if (field.required) {\n return yup.array().of(yup.string()).min(1, {\n key: 'form.error.required',\n });\n }\n return yup.array().of(yup.string()).nullable();\n },\n 'choice:single': (): StringSchema<\n string | null | undefined,\n AnyObject,\n string | null | undefined\n > => yup.string().nullable(),\n date: (\n field\n ): yup.StringSchema<\n string | null | undefined,\n AnyObject,\n string | null | undefined\n > => {\n let schema = yup.string().nullable();\n if (field?.extra?.min) {\n schema = schema.test(LATER_THAN_VALIDATOR(field));\n }\n if (field?.extra?.max) {\n schema = schema.test(EARLIER_THAN_VALIDATOR(field));\n } else {\n schema = schema.test(DATE_VALIDATOR);\n }\n return schema;\n },\n datetime: (field): yup.StringSchema => {\n if (field?.extra?.min) {\n return yup.string().test(LATER_THAN_DATETIME_VALIDATOR(field));\n }\n if (field?.extra?.max) {\n return yup.string().test(EARLIER_THAN_DATETIME_VALIDATOR(field));\n }\n return yup.string().test(DATETIME_VALIDATOR);\n },\n ...validatorRegistry,\n };\n return yupTypeMap;\n};\n\nexport interface MutableRefObjectProps {}\n// Create a yup validation schema from given fields input\nexport function useValidationSchema(\n fields: FieldSpec[] | MutableRefObject<MutableRefObjectProps>,\n validatorRegistry?: Record<string, ValidatorSchema[]>\n): Lazy<any, unknown> | AnyObjectSchema | void {\n return useMemo(() => {\n const yupTypeMap = getYUPTypeMap(validatorRegistry);\n return getYupObjectShape(fields, yupTypeMap, validatorRegistry);\n }, []);\n}\n"],
5
+ "mappings": "AAAA,SAA2B,eAAe;AAC1C,YAAY,SAAS;AASrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AAE1B,MAAM,gBAAgB,CAC3B,sBACwB;AACxB,QAAM,aAAa;AAAA,IACjB,MAAM,CAAC,UAAuB;AAC5B,aAAO,IAAI,MAAM,EAAE,SAAS,EAAE,KAAK,wBAAwB,KAAK,CAAC;AAAA,IACnE;AAAA,IACA,QAAQ,CAAC,UAA4B;AACnC,UAAI,MAAM,MAAM;AACd,eAAO,IAAI,OAAO,EAAE,KAAK;AAAA,MAC3B,OAAO;AACL,eAAO,IAAI,OAAO;AAAA,MACpB;AAAA,IACF;AAAA,IACA,SAAS,MAIJ;AACH,aAAO,IACJ,QAAQ,EACR,SAAS,EACT,UAAU,CAAC,UAAU;AACpB,YAAI,U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AACA,UAAI,OAAO,OAAO,KAAK;AACrB,iBAAS,OAAO,KAAK,uBAAuB,KAAK,CAAC;AAAA,MACpD,OAAO;AACL,iBAAS,OAAO,KAAK,cAAc;AAAA,MACrC;AACA,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,UAA4B;AACrC,UAAI,OAAO,OAAO,KAAK;AACrB,eAAO,IAAI,OAAO,EAAE,KAAK,8BAA8B,KAAK,CAAC;AAAA,MAC/D;AACA,UAAI,OAAO,OAAO,KAAK;AACrB,eAAO,IAAI,OAAO,EAAE,KAAK,gCAAgC,KAAK,CAAC;AAAA,MACjE;AACA,aAAO,IAAI,OAAO,EAAE,KAAK,kBAAkB;AAAA,IAC7C;AAAA,IACA,GAAG;AAAA,EACL;AACA,SAAO;AACT;AAIO,SAAS,oBACd,QACA,mBAC6C;AAC7C,SAAO,QAAQ,MAAM;AACnB,UAAM,aAAa,cAAc,iBAAiB;AAClD,WAAO,kBAAkB,QAAQ,YAAY,iBAAiB;AAAA,EAChE,GAAG,CAAC,CAAC;AACP;",
6
6
  "names": []
7
7
  }
@@ -6,10 +6,8 @@ const DATE_CACHE = {};
6
6
  const getDate = (v) => {
7
7
  return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
8
8
  };
9
- const getDaysInMonth = (month, year) => {
10
- return dayjs(`${year}-${month}-01`).daysInMonth();
11
- };
12
9
  const checkPartialDate = (value) => {
10
+ console.log("value.split('/')", value.split("/"));
13
11
  const [day, month, year] = value.split("/");
14
12
  if (day && !month && !year) {
15
13
  return "form.error.date.invalid_month_year";
@@ -28,25 +26,9 @@ const checkPartialDate = (value) => {
28
26
  }
29
27
  if (!day && month && year) {
30
28
  return "form.error.date.invalid_day";
29
+ } else {
30
+ return "form.error.date.invalid";
31
31
  }
32
- if (day && month) {
33
- const dayNum = parseInt(day);
34
- const monthNum = parseInt(month);
35
- const yearNum = parseInt(year);
36
- const maxDay = getDaysInMonth(monthNum, yearNum);
37
- if (dayNum < 1 || dayNum > maxDay) {
38
- return {
39
- key: "form.error.date.invalid_day_range",
40
- context: {
41
- maxDay
42
- }
43
- };
44
- }
45
- if (monthNum < 1 || monthNum > 12) {
46
- return "form.error.datetime.invalid_month_range";
47
- }
48
- }
49
- return "form.error.date.invalid";
50
32
  };
51
33
  const LATER_THAN_VALIDATOR = (field) => {
52
34
  let minDate;
@@ -4,6 +4,6 @@ export declare const LATER_THAN_VALIDATOR: (field: FieldSpec) => ValidatorSchema
4
4
  export declare const EARLIER_THAN_VALIDATOR: (field: FieldSpec) => ValidatorSchema;
5
5
  export declare const DATE_VALIDATOR: {
6
6
  name: string;
7
- message: (v: any) => string | object;
7
+ message: (v: any) => string;
8
8
  test: (value: string) => any;
9
9
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/validators/utils/date.ts"],
4
- "sourcesContent": ["import dayjs from 'dayjs';\nimport customParseFormat from 'dayjs/plugin/customParseFormat';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\ndayjs.extend(customParseFormat);\n\nconst VALID_DATE_FORMATS = ['DD/MM/YYYY'];\nconst DATE_CACHE = {};\n\nconst getDate = (v: string) => {\n return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);\n};\n\nconst getDaysInMonth = (month: number, year: number): number => {\n return dayjs(`${year}-${month}-01`).daysInMonth(); // e.g. 29 for Feb 2024\n};\n\n// Function to check partial date completion\nconst checkPartialDate = (value: string) => {\n const [day, month, year] = value.split('/');\n if (day && !month && !year) {\n return 'form.error.date.invalid_month_year';\n }\n if (!day && month && !year) {\n return 'form.error.date.invalid_day_year';\n }\n if (!day && !month && year) {\n return 'form.error.date.invalid_day_month';\n }\n if (day && month && !year) {\n return 'form.error.date.invalid_year';\n }\n if (day && !month && year) {\n return 'form.error.date.invalid_month';\n }\n if (!day && month && year) {\n return 'form.error.date.invalid_day';\n }\n if (day && month) {\n const dayNum = parseInt(day);\n const monthNum = parseInt(month);\n const yearNum = parseInt(year);\n const maxDay = getDaysInMonth(monthNum, yearNum);\n if (dayNum < 1 || dayNum > maxDay) {\n return {\n key: 'form.error.date.invalid_day_range',\n context: {\n maxDay: maxDay,\n },\n };\n }\n if (monthNum < 1 || monthNum > 12) {\n return 'form.error.datetime.invalid_month_range';\n }\n }\n return 'form.error.date.invalid';\n};\n\nexport const LATER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {\n let minDate;\n const params = field.extra || {};\n if (params.min === 'now') {\n const today = new Date();\n minDate = new Date(today);\n } else {\n minDate = getDate(params.min).toDate();\n }\n const minPreviousDate = new Date(minDate);\n minPreviousDate.setDate(minDate.getDate() - 1);\n return {\n name: 'later-than',\n message: {\n key: 'form.error.date.later_than',\n context: {\n minDate: minPreviousDate.toLocaleDateString(),\n },\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n const isValid = +date.toDate() > +minDate;\n return isValid;\n },\n };\n};\n\nexport const EARLIER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {\n let maxDate;\n const params = field.extra || {};\n if (params.max === 'now') {\n const today = new Date();\n maxDate = new Date(today);\n } else {\n maxDate = getDate(params.max).toDate();\n }\n const maxNextDate = new Date(maxDate);\n maxNextDate.setDate(maxDate.getDate() + 1);\n return {\n name: 'earlier-than',\n message: {\n key: 'form.error.date.earlier_than',\n context: {\n maxDate: maxNextDate.toLocaleDateString(),\n },\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n const isValid = +date.toDate() < +maxDate;\n return isValid;\n },\n };\n};\n\nexport const DATE_VALIDATOR = {\n name: 'date-validator',\n message: (v): string | object => {\n return checkPartialDate(v.value);\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n return date.isValid();\n },\n};\n"],
5
- "mappings": "AAAA,OAAO,WAAW;AAClB,OAAO,uBAAuB;AAI9B,MAAM,OAAO,iBAAiB;AAE9B,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MAAuB;AAC/B,WAAO,iBAAiB,EAAE,KAAK;AAAA,EACjC;AAAA,EACA,MAAM,SAAU,OAAe;AAC7B,QAAI,CAAC,MAAO,QAAO;AACnB,UAAM,OAAO,QAAQ,KAAK;AAC1B,WAAO,KAAK,QAAQ;AAAA,EACtB;AACF;",
4
+ "sourcesContent": ["import dayjs from 'dayjs';\nimport customParseFormat from 'dayjs/plugin/customParseFormat';\nimport { FieldSpec } from '@digigov/form/types';\nimport { ValidatorSchema } from '@digigov/form/validators/types';\n\ndayjs.extend(customParseFormat);\n\nconst VALID_DATE_FORMATS = ['DD/MM/YYYY'];\nconst DATE_CACHE = {};\n\nconst getDate = (v: string) => {\n return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);\n};\n\n// Function to check partial date completion\nconst checkPartialDate = (value: string) => {\n console.log(\"value.split('/')\", value.split('/'));\n const [day, month, year] = value.split('/');\n if (day && !month && !year) {\n return 'form.error.date.invalid_month_year';\n }\n if (!day && month && !year) {\n return 'form.error.date.invalid_day_year';\n }\n if (!day && !month && year) {\n return 'form.error.date.invalid_day_month';\n }\n if (day && month && !year) {\n return 'form.error.date.invalid_year';\n }\n if (day && !month && year) {\n return 'form.error.date.invalid_month';\n }\n if (!day && month && year) {\n return 'form.error.date.invalid_day';\n } else {\n return 'form.error.date.invalid';\n }\n};\n\nexport const LATER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {\n let minDate;\n const params = field.extra || {};\n if (params.min === 'now') {\n const today = new Date();\n minDate = new Date(today);\n } else {\n minDate = getDate(params.min).toDate();\n }\n const minPreviousDate = new Date(minDate);\n minPreviousDate.setDate(minDate.getDate() - 1);\n return {\n name: 'later-than',\n message: {\n key: 'form.error.date.later_than',\n context: {\n minDate: minPreviousDate.toLocaleDateString(),\n },\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n const isValid = +date.toDate() > +minDate;\n return isValid;\n },\n };\n};\n\nexport const EARLIER_THAN_VALIDATOR = (field: FieldSpec): ValidatorSchema => {\n let maxDate;\n const params = field.extra || {};\n if (params.max === 'now') {\n const today = new Date();\n maxDate = new Date(today);\n } else {\n maxDate = getDate(params.max).toDate();\n }\n const maxNextDate = new Date(maxDate);\n maxNextDate.setDate(maxDate.getDate() + 1);\n return {\n name: 'earlier-than',\n message: {\n key: 'form.error.date.earlier_than',\n context: {\n maxDate: maxNextDate.toLocaleDateString(),\n },\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n const isValid = +date.toDate() < +maxDate;\n return isValid;\n },\n };\n};\n\nexport const DATE_VALIDATOR = {\n name: 'date-validator',\n message: (v): string => {\n return checkPartialDate(v.value);\n },\n test: function (value: string) {\n if (!value) return true;\n const date = getDate(value);\n return date.isValid();\n },\n};\n"],
5
+ "mappings": "AAAA,OAAO,WAAW;AAClB,OAAO,uBAAuB;AAI9B,MAAM,OAAO,iBAAiB;AAE9B,MAAM,qBAAqB,CAAC,YAAY;AACxC,MAAM,aAAa,CAAC;AAEpB,MAAM,UAAU,CAAC,MAAc;AAC7B,SAAO,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI,MAAM,GAAG,oBAAoB,IAAI;AAC1E;AAGA,MAAM,mBAAmB,CAAC,UAAkB;AAC1C,UAAQ,IAAI,oBAAoB,MAAM,MAAM,GAAG,CAAC;AAChD,QAAM,CAAC,KAAK,OAAO,IAAI,IAAI,MAAM,MAAM,GAAG;AAC1C,MAAI,OAAO,CAAC,SAAS,CAAC,MAAM;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,CAAC,OAAO,SAAS,CAAC,MAAM;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,CAAC,OAAO,CAAC,SAAS,MAAM;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,OAAO,SAAS,CAAC,MAAM;AACzB,WAAO;AAAA,EACT;AACA,MAAI,OAAO,CAAC,SAAS,MAAM;AACzB,WAAO;AAAA,EACT;AACA,MAAI,CAAC,OAAO,SAAS,MA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,QAAO;AACnB,YAAM,OAAO,QAAQ,KAAK;AAC1B,YAAM,UAAU,CAAC,KAAK,OAAO,IAAI,CAAC;AAClC,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAEO,MAAM,iBAAiB;AAAA,EAC5B,MAAM;AAAA,EACN,SAAS,CAAC,MAAc;AACtB,WAAO,iBAAiB,EAAE,KAAK;AAAA,EACjC;AAAA,EACA,MAAM,SAAU,OAAe;AAC7B,QAAI,CAAC,MAAO,QAAO;AACnB,UAAM,OAAO,QAAQ,KAAK;AAC1B,WAAO,KAAK,QAAQ;AAAA,EACtB;AACF;",
6
6
  "names": []
7
7
  }
@@ -6,136 +6,47 @@ const DATETIME_CACHE = {};
6
6
  const getDateTime = (v) => {
7
7
  return DATETIME_CACHE[v] ? DATETIME_CACHE[v] : dayjs(v, VALID_DATETIME_FORMATS, true);
8
8
  };
9
- const getDaysInMonth = (month, year) => {
10
- return dayjs(`${year}-${month}-01`).daysInMonth();
11
- };
12
9
  const checkPartialDateTime = (value) => {
13
10
  const [datePart, timePart] = value.split(" ");
14
- const [day, month, year] = datePart?.split("/") ?? [];
15
- const [hours, minutes] = timePart?.split(":") ?? [];
16
- const hasDay = Boolean(day);
17
- const hasMonth = Boolean(month);
18
- const hasYear = Boolean(year);
19
- const hasHours = Boolean(hours);
20
- const hasMinutes = Boolean(minutes);
21
- if (hasDay && !hasMonth && !hasYear && !hasHours && !hasMinutes) {
22
- return "form.error.datetime.invalid_month_year_hours_minutes";
23
- }
24
- if (!hasDay && hasMonth && !hasYear && !hasHours && !hasMinutes) {
25
- return "form.error.datetime.invalid_day_year_hours_minutes";
26
- }
27
- if (!hasDay && !hasMonth && hasYear && !hasHours && !hasMinutes) {
28
- return "form.error.datetime.invalid_day_month_hours_minutes";
29
- }
30
- if (hasDay && hasMonth && !hasYear && !hasHours && !hasMinutes) {
31
- return "form.error.datetime.invalid_year_hours_minutes";
32
- }
33
- if (hasDay && !hasMonth && hasYear && !hasHours && !hasMinutes) {
34
- return "form.error.datetime.invalid_month_hours_minutes";
35
- }
36
- if (!hasDay && hasMonth && hasYear && !hasHours && !hasMinutes) {
37
- return "form.error.datetime.invalid_day_hours_minutes";
38
- }
39
- if (hasDay && hasMonth && hasYear && !hasHours && !hasMinutes) {
40
- return "form.error.datetime.invalid__hours_minutes";
41
- }
42
- if (hasDay && hasMonth && hasYear && hasHours && !hasMinutes) {
43
- return "form.error.datetime.invalid_minutes";
44
- }
45
- if (hasDay && hasMonth && hasYear && !hasHours && hasMinutes) {
46
- return "form.error.datetime.invalid_hours";
47
- }
48
- if (hasDay && !hasMonth && !hasYear && hasHours && !hasMinutes) {
49
- return "form.error.datetime.invalid_month_year_minutes";
50
- }
51
- if (hasDay && !hasMonth && !hasYear && !hasHours && hasMinutes) {
52
- return "form.error.datetime.invalid_month_year_hours";
53
- }
54
- if (hasDay && !hasMonth && !hasYear && hasHours && hasMinutes) {
11
+ const [day, month, year] = datePart.split("/");
12
+ const [hours, minutes] = timePart ? timePart.split(":") : [null, null];
13
+ if (day && !month && !year) {
55
14
  return "form.error.datetime.invalid_month_year";
56
15
  }
57
- if (!hasDay && hasMonth && !hasYear && hasHours && !hasMinutes) {
58
- return "form.error.datetime.invalid_day_year_minutes";
59
- }
60
- if (!hasDay && hasMonth && !hasYear && !hasHours && hasMinutes) {
61
- return "form.error.datetime.invalid_day_year_hours";
62
- }
63
- if (!hasDay && hasMonth && !hasYear && hasHours && hasMinutes) {
16
+ if (!day && month && !year) {
64
17
  return "form.error.datetime.invalid_day_year";
65
18
  }
66
- if (!hasDay && !hasMonth && hasYear && hasHours && !hasMinutes) {
67
- return "form.error.datetime.invalid_day_month_minutes";
68
- }
69
- if (!hasDay && !hasMonth && hasYear && !hasHours && hasMinutes) {
70
- return "form.error.datetime.invalid_day_month_hours";
71
- }
72
- if (!hasDay && !hasMonth && hasYear && hasHours && hasMinutes) {
19
+ if (!day && !month && year) {
73
20
  return "form.error.datetime.invalid_day_month";
74
21
  }
75
- if (!hasDay && !hasMonth && !hasYear && hasHours && hasMinutes) {
76
- return "form.error.datetime.invalid_day_month_year";
77
- }
78
- if (!hasDay && !hasMonth && !hasYear && !hasHours && hasMinutes) {
79
- return "form.error.datetime.invalid_day_month_year_hours";
80
- }
81
- if (hasDay && hasMonth && !hasYear && hasHours && !hasMinutes) {
82
- return "form.error.datetime.invalid_year_minutes";
83
- }
84
- if (hasDay && hasMonth && !hasYear && !hasHours && hasMinutes) {
85
- return "form.error.datetime.invalid_year_hours";
86
- }
87
- if (hasDay && hasMonth && !hasYear && hasHours && hasMinutes) {
22
+ if (day && month && !year) {
88
23
  return "form.error.datetime.invalid_year";
89
24
  }
90
- if (hasDay && !hasMonth && hasYear && hasHours && !hasMinutes) {
91
- return "form.error.datetime.invalid_month_minutes";
92
- }
93
- if (hasDay && !hasMonth && hasYear && !hasHours && hasMinutes) {
94
- return "form.error.datetime.invalid_month_hours";
95
- }
96
- if (hasDay && !hasMonth && hasYear && hasHours && hasMinutes) {
25
+ if (day && !month && year) {
97
26
  return "form.error.datetime.invalid_month";
98
27
  }
99
- if (!hasDay && hasMonth && hasYear && hasHours && !hasMinutes) {
100
- return "form.error.datetime.invalid_day_minutes";
101
- }
102
- if (!hasDay && hasMonth && hasYear && !hasHours && hasMinutes) {
103
- return "form.error.datetime.invalid_day_hours";
104
- }
105
- if (!hasDay && hasMonth && hasYear && hasHours && hasMinutes) {
28
+ if (!day && month && year) {
106
29
  return "form.error.datetime.invalid_day";
107
30
  }
108
- if (!hasDay && !hasMonth && !hasYear && hasHours && !hasMinutes) {
109
- return "form.error.datetime.invalid_day_month_year_minutes";
110
- }
111
- if (hasDay && hasMonth && hasYear && !hasHours && !hasMinutes) {
112
- return "form.error.datetime.invalid_hours_minutes";
113
- }
114
- if (day && month) {
115
- const dayNum = parseInt(day);
116
- const monthNum = parseInt(month);
117
- const yearNum = parseInt(year);
118
- const maxDay = getDaysInMonth(monthNum, yearNum);
119
- if (dayNum < 1 || dayNum > maxDay) {
120
- return {
121
- key: "form.error.datetime.invalid_day_range",
122
- context: {
123
- maxDay
124
- }
125
- };
31
+ if (day && month && year) {
32
+ if (hours && !minutes) {
33
+ return "form.error.datetime.invalid_minutes";
126
34
  }
127
- if (monthNum < 1 || monthNum > 12) {
128
- return "form.error.datetime.invalid_month_range";
35
+ if (!hours && minutes) {
36
+ return "form.error.datetime.invalid_hours";
129
37
  }
130
- }
131
- if (hours && minutes) {
132
- const hoursNum = parseInt(hours);
133
- const minutesNum = parseInt(minutes);
134
- if (hoursNum < 0 || hoursNum > 23) {
135
- return "form.error.datetime.invalid_hours_range";
38
+ if (!hours && !minutes) {
39
+ return "form.error.datetime.invalid";
136
40
  }
137
- if (minutesNum < 0 || minutesNum > 59) {
138
- return "form.error.datetime.invalid_minutes_range";
41
+ if (hours && minutes) {
42
+ const hoursNum = parseInt(hours);
43
+ const minutesNum = parseInt(minutes);
44
+ if (hoursNum < 0 || hoursNum > 23) {
45
+ return "form.error.datetime.invalid_hours_range";
46
+ }
47
+ if (minutesNum < 0 || minutesNum > 59) {
48
+ return "form.error.datetime.invalid_minutes_range";
49
+ }
139
50
  }
140
51
  }
141
52
  return "form.error.datetime.invalid";
@@ -4,6 +4,6 @@ export declare const LATER_THAN_DATETIME_VALIDATOR: (field: FieldSpec) => Valida
4
4
  export declare const EARLIER_THAN_DATETIME_VALIDATOR: (field: FieldSpec) => ValidatorSchema;
5
5
  export declare const DATETIME_VALIDATOR: {
6
6
  name: string;
7
- message: (v: any) => string | object;
7
+ message: (v: any) => string;
8
8
  test: (value: string) => any;
9
9
  };