hof 22.8.4 → 22.9.0-beta

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 (326) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +30 -0
  3. package/config/hof-defaults.js +6 -1
  4. package/frontend/govuk-template/govuk_template_generated.html +102 -0
  5. package/frontend/template-partials/views/layout.html +2 -8
  6. package/frontend/themes/gov-uk/styles/_cookie-banner.scss +0 -3
  7. package/hof-22.9.0.tgz +0 -0
  8. package/lib/settings.js +1 -0
  9. package/package/.eslintignore +1 -0
  10. package/package/CHANGELOG.md +232 -0
  11. package/package/LICENSE +21 -0
  12. package/package/README.md +1887 -0
  13. package/package/bin/hof-build +10 -0
  14. package/package/bin/hof-transpiler +12 -0
  15. package/package/build/helpers/importer.js +29 -0
  16. package/package/build/helpers/local.js +35 -0
  17. package/package/build/helpers/resolver/import.js +32 -0
  18. package/package/build/helpers/resolver/nearest-package-root.js +33 -0
  19. package/package/build/helpers/resolver/package.js +29 -0
  20. package/package/build/helpers/resolver.js +16 -0
  21. package/package/build/index.js +49 -0
  22. package/package/build/lib/env.js +36 -0
  23. package/package/build/lib/mkdir.js +9 -0
  24. package/package/build/lib/run.js +17 -0
  25. package/package/build/lib/spawn.js +18 -0
  26. package/package/build/tasks/browserify/compress.js +15 -0
  27. package/package/build/tasks/browserify/index.js +48 -0
  28. package/package/build/tasks/build/index.js +6 -0
  29. package/package/build/tasks/images/index.js +27 -0
  30. package/package/build/tasks/index.js +8 -0
  31. package/package/build/tasks/sass/index.js +67 -0
  32. package/package/build/tasks/translate/index.js +20 -0
  33. package/package/build/tasks/watch/index.js +161 -0
  34. package/package/components/address-lookup/default-model.js +76 -0
  35. package/package/components/address-lookup/defaults.js +25 -0
  36. package/package/components/address-lookup/index.js +251 -0
  37. package/package/components/address-lookup/templates/address-lookup.html +14 -0
  38. package/package/components/address-lookup/templates/address.html +22 -0
  39. package/package/components/address-lookup/templates/postcode.html +9 -0
  40. package/package/components/clear-session/Readme.md +46 -0
  41. package/package/components/clear-session/index.js +26 -0
  42. package/package/components/combine-and-loop-fields/Readme.md +42 -0
  43. package/package/components/combine-and-loop-fields/index.js +156 -0
  44. package/package/components/date/fields.js +16 -0
  45. package/package/components/date/index.js +172 -0
  46. package/package/components/date/templates/date.html +20 -0
  47. package/package/components/emailer/assets/images/govuk_logotype_email.png +0 -0
  48. package/package/components/emailer/assets/images/ho_crest_27px.png +0 -0
  49. package/package/components/emailer/assets/images/spacer.gif +0 -0
  50. package/package/components/emailer/email-service.js +51 -0
  51. package/package/components/emailer/emailer.js +53 -0
  52. package/package/components/emailer/index.js +74 -0
  53. package/package/components/emailer/transports/debug.js +74 -0
  54. package/package/components/emailer/transports/index.js +8 -0
  55. package/package/components/emailer/transports/ses.js +36 -0
  56. package/package/components/emailer/transports/smtp.js +26 -0
  57. package/package/components/emailer/transports/stub.js +5 -0
  58. package/package/components/emailer/views/layout.html +63 -0
  59. package/package/components/homeoffice-countries/index.js +22 -0
  60. package/package/components/index.js +13 -0
  61. package/package/components/notify/index.js +62 -0
  62. package/package/components/notify/notify.js +51 -0
  63. package/package/components/session-timeout-warning/index.js +67 -0
  64. package/package/components/summary/index.js +237 -0
  65. package/package/config/builder-defaults.js +45 -0
  66. package/package/config/component-defaults.js +13 -0
  67. package/package/config/hof-defaults.js +65 -0
  68. package/package/config/rate-limits.js +20 -0
  69. package/package/config/sanitisation-rules.js +32 -0
  70. package/package/controller/base-controller.js +296 -0
  71. package/package/controller/behaviour-hooks.js +51 -0
  72. package/package/controller/behaviour-session.js +64 -0
  73. package/package/controller/controller.js +258 -0
  74. package/package/controller/deprecate-error.js +10 -0
  75. package/package/controller/formatting/formatters.js +70 -0
  76. package/package/controller/formatting/index.js +32 -0
  77. package/package/controller/index.js +17 -0
  78. package/package/controller/validation/email.js +30 -0
  79. package/package/controller/validation/index.js +101 -0
  80. package/package/controller/validation/validators.js +181 -0
  81. package/package/controller/validation-error.js +14 -0
  82. package/package/frontend/govuk-template/build/config.js +24 -0
  83. package/package/frontend/govuk-template/build/govuk_template.html +102 -0
  84. package/package/frontend/govuk-template/build/index.js +23 -0
  85. package/package/frontend/govuk-template/govuk_template_generated.html +102 -0
  86. package/package/frontend/govuk-template/index.js +29 -0
  87. package/package/frontend/index.js +9 -0
  88. package/package/frontend/template-mixins/mixins/helpers.js +103 -0
  89. package/package/frontend/template-mixins/mixins/index.js +37 -0
  90. package/package/frontend/template-mixins/mixins/render.js +12 -0
  91. package/package/frontend/template-mixins/mixins/template-mixins.js +520 -0
  92. package/package/frontend/template-mixins/partials/forms/checkbox-group.html +47 -0
  93. package/package/frontend/template-mixins/partials/forms/checkbox.html +16 -0
  94. package/package/frontend/template-mixins/partials/forms/input-submit.html +1 -0
  95. package/package/frontend/template-mixins/partials/forms/input-text-date.html +37 -0
  96. package/package/frontend/template-mixins/partials/forms/input-text-group.html +45 -0
  97. package/package/frontend/template-mixins/partials/forms/option-group.html +43 -0
  98. package/package/frontend/template-mixins/partials/forms/select.html +17 -0
  99. package/package/frontend/template-mixins/partials/forms/textarea-group.html +37 -0
  100. package/package/frontend/template-mixins/partials/mixins/panel.html +4 -0
  101. package/package/frontend/template-partials/index.js +9 -0
  102. package/package/frontend/template-partials/translations/index.js +26 -0
  103. package/package/frontend/template-partials/translations/src/cy/base.json +4 -0
  104. package/package/frontend/template-partials/translations/src/cy/buttons.json +6 -0
  105. package/package/frontend/template-partials/translations/src/cy/cookies.json +104 -0
  106. package/package/frontend/template-partials/translations/src/cy/errorlist.json +6 -0
  107. package/package/frontend/template-partials/translations/src/cy/errors.json +18 -0
  108. package/package/frontend/template-partials/translations/src/cy/terms.json +28 -0
  109. package/package/frontend/template-partials/translations/src/en/accessibility.json +43 -0
  110. package/package/frontend/template-partials/translations/src/en/base.json +5 -0
  111. package/package/frontend/template-partials/translations/src/en/buttons.json +10 -0
  112. package/package/frontend/template-partials/translations/src/en/cookies.json +116 -0
  113. package/package/frontend/template-partials/translations/src/en/errorlist.json +7 -0
  114. package/package/frontend/template-partials/translations/src/en/errors.json +35 -0
  115. package/package/frontend/template-partials/translations/src/en/exit.json +5 -0
  116. package/package/frontend/template-partials/translations/src/en/fields.json +5 -0
  117. package/package/frontend/template-partials/translations/src/en/journey.json +6 -0
  118. package/package/frontend/template-partials/translations/src/en/save-and-exit.json +4 -0
  119. package/package/frontend/template-partials/translations/src/en/terms.json +28 -0
  120. package/package/frontend/template-partials/views/404.html +9 -0
  121. package/package/frontend/template-partials/views/accessibility.html +55 -0
  122. package/package/frontend/template-partials/views/confirm.html +8 -0
  123. package/package/frontend/template-partials/views/confirmation.html +19 -0
  124. package/package/frontend/template-partials/views/content/en/what-happens-next.md +0 -0
  125. package/package/frontend/template-partials/views/cookie-error.html +1 -0
  126. package/package/frontend/template-partials/views/cookies.html +84 -0
  127. package/package/frontend/template-partials/views/email/data-row.html +4 -0
  128. package/package/frontend/template-partials/views/email/formatted.html +12 -0
  129. package/package/frontend/template-partials/views/email/layout.html +63 -0
  130. package/package/frontend/template-partials/views/email/raw.html +11 -0
  131. package/package/frontend/template-partials/views/email/section-row.html +3 -0
  132. package/package/frontend/template-partials/views/error.html +20 -0
  133. package/package/frontend/template-partials/views/exit.html +9 -0
  134. package/package/frontend/template-partials/views/feedback-submitted.html +11 -0
  135. package/package/frontend/template-partials/views/layout.html +55 -0
  136. package/package/frontend/template-partials/views/partials/analytics-table.html +25 -0
  137. package/package/frontend/template-partials/views/partials/back.html +5 -0
  138. package/package/frontend/template-partials/views/partials/bullet-list.html +7 -0
  139. package/package/frontend/template-partials/views/partials/confirmation-alert.html +6 -0
  140. package/package/frontend/template-partials/views/partials/continue.html +5 -0
  141. package/package/frontend/template-partials/views/partials/cookie-banner.html +29 -0
  142. package/package/frontend/template-partials/views/partials/cookie-notification.html +4 -0
  143. package/package/frontend/template-partials/views/partials/cookie-settings-button.html +1 -0
  144. package/package/frontend/template-partials/views/partials/cookie-settings-radio.html +8 -0
  145. package/package/frontend/template-partials/views/partials/details-summary.html +8 -0
  146. package/package/frontend/template-partials/views/partials/external-link.html +1 -0
  147. package/package/frontend/template-partials/views/partials/form.html +10 -0
  148. package/package/frontend/template-partials/views/partials/gatag.html +60 -0
  149. package/package/frontend/template-partials/views/partials/head.html +31 -0
  150. package/package/frontend/template-partials/views/partials/heading.html +11 -0
  151. package/package/frontend/template-partials/views/partials/items-table.html +32 -0
  152. package/package/frontend/template-partials/views/partials/maincontent-left.html +10 -0
  153. package/package/frontend/template-partials/views/partials/navigation.html +8 -0
  154. package/package/frontend/template-partials/views/partials/page.html +23 -0
  155. package/package/frontend/template-partials/views/partials/panel-indent.html +3 -0
  156. package/package/frontend/template-partials/views/partials/session-cookies-table.html +28 -0
  157. package/package/frontend/template-partials/views/partials/session-timeout-warning.html +38 -0
  158. package/package/frontend/template-partials/views/partials/summary-table-row.html +14 -0
  159. package/package/frontend/template-partials/views/partials/summary-table.html +8 -0
  160. package/package/frontend/template-partials/views/partials/table.html +18 -0
  161. package/package/frontend/template-partials/views/partials/validation-list.html +3 -0
  162. package/package/frontend/template-partials/views/partials/validation-summary.html +25 -0
  163. package/package/frontend/template-partials/views/partials/warn.html +7 -0
  164. package/package/frontend/template-partials/views/rate-limit-error.html +10 -0
  165. package/package/frontend/template-partials/views/save-and-exit.html +17 -0
  166. package/package/frontend/template-partials/views/service-unavailable.html +13 -0
  167. package/package/frontend/template-partials/views/session-timeout.html +7 -0
  168. package/package/frontend/template-partials/views/step.html +14 -0
  169. package/package/frontend/template-partials/views/terms.html +26 -0
  170. package/package/frontend/themes/gov-uk/client-js/cookieSettings.js +145 -0
  171. package/package/frontend/themes/gov-uk/client-js/govuk-cookies.js +121 -0
  172. package/package/frontend/themes/gov-uk/client-js/index.js +27 -0
  173. package/package/frontend/themes/gov-uk/client-js/session-timeout-dialog.js +348 -0
  174. package/package/frontend/themes/gov-uk/client-js/skip-to-main.js +19 -0
  175. package/package/frontend/themes/gov-uk/index.js +9 -0
  176. package/package/frontend/themes/gov-uk/styles/_base.scss +17 -0
  177. package/package/frontend/themes/gov-uk/styles/_check_your_answers.scss +155 -0
  178. package/package/frontend/themes/gov-uk/styles/_cookie-banner.scss +111 -0
  179. package/package/frontend/themes/gov-uk/styles/_cookie-settings.scss +33 -0
  180. package/package/frontend/themes/gov-uk/styles/_govuk_frontend_toolkit.scss +23 -0
  181. package/package/frontend/themes/gov-uk/styles/_helpers.scss +5 -0
  182. package/package/frontend/themes/gov-uk/styles/_layout.scss +125 -0
  183. package/package/frontend/themes/gov-uk/styles/_panel-indent.scss +27 -0
  184. package/package/frontend/themes/gov-uk/styles/_pdf.scss +42 -0
  185. package/package/frontend/themes/gov-uk/styles/_session-timeout-dialog.scss +121 -0
  186. package/package/frontend/themes/gov-uk/styles/_typography.scss +27 -0
  187. package/package/frontend/themes/gov-uk/styles/_variables.scss +11 -0
  188. package/package/frontend/themes/gov-uk/styles/govuk.scss +43 -0
  189. package/package/frontend/themes/gov-uk/styles/mixins.scss +16 -0
  190. package/package/frontend/themes/gov-uk/styles/modules/_alerts.scss +73 -0
  191. package/package/frontend/themes/gov-uk/styles/modules/_buttons.scss +5 -0
  192. package/package/frontend/themes/gov-uk/styles/modules/_character-count.scss +8 -0
  193. package/package/frontend/themes/gov-uk/styles/modules/_confirm-page.scss +20 -0
  194. package/package/frontend/themes/gov-uk/styles/modules/_lists.scss +5 -0
  195. package/package/frontend/themes/gov-uk/styles/modules/_progressive-reveal.scss +17 -0
  196. package/package/frontend/themes/gov-uk/styles/modules/_validation.scss +51 -0
  197. package/package/frontend/themes/index.js +5 -0
  198. package/package/frontend/toolkit/assets/images/passports/new-window-link-blue.png +0 -0
  199. package/package/frontend/toolkit/assets/images/passports/new-window-link.png +0 -0
  200. package/package/frontend/toolkit/assets/images/spinner.gif +0 -0
  201. package/package/frontend/toolkit/assets/javascript/character-count.js +99 -0
  202. package/package/frontend/toolkit/assets/javascript/form-focus.js +101 -0
  203. package/package/frontend/toolkit/assets/javascript/helpers.js +177 -0
  204. package/package/frontend/toolkit/assets/javascript/progressive-reveal.js +72 -0
  205. package/package/frontend/toolkit/assets/javascript/validation.js +71 -0
  206. package/package/frontend/toolkit/assets/javascript/vendor/details.polyfill.js +189 -0
  207. package/package/frontend/toolkit/assets/javascript/vendor/indexof.polyfill.js +39 -0
  208. package/package/frontend/toolkit/assets/javascript/vendor/safari-cachebuster.js +6 -0
  209. package/package/frontend/toolkit/assets/stylesheets/_base.scss +17 -0
  210. package/package/frontend/toolkit/assets/stylesheets/_helpers.scss +5 -0
  211. package/package/frontend/toolkit/assets/stylesheets/_layout.scss +118 -0
  212. package/package/frontend/toolkit/assets/stylesheets/_typography.scss +27 -0
  213. package/package/frontend/toolkit/assets/stylesheets/_variables.scss +11 -0
  214. package/package/frontend/toolkit/assets/stylesheets/app.scss +30 -0
  215. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_buttons.scss +47 -0
  216. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_details.scss +38 -0
  217. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_elements-typography.scss +175 -0
  218. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_forms.scss +167 -0
  219. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_helpers.scss +39 -0
  220. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_layout.scss +67 -0
  221. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_lists.scss +40 -0
  222. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_panels.scss +29 -0
  223. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_reset.scss +33 -0
  224. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/_tables.scss +33 -0
  225. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/forms/_form-block-labels.scss +63 -0
  226. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/forms/_form-date.scss +39 -0
  227. package/package/frontend/toolkit/assets/stylesheets/govuk-elements/forms/_form-validation.scss +63 -0
  228. package/package/frontend/toolkit/assets/stylesheets/mixins.scss +16 -0
  229. package/package/frontend/toolkit/assets/stylesheets/modules/_alerts.scss +73 -0
  230. package/package/frontend/toolkit/assets/stylesheets/modules/_buttons.scss +5 -0
  231. package/package/frontend/toolkit/assets/stylesheets/modules/_confirm-page.scss +20 -0
  232. package/package/frontend/toolkit/assets/stylesheets/modules/_lists.scss +5 -0
  233. package/package/frontend/toolkit/assets/stylesheets/modules/_progressive-reveal.scss +17 -0
  234. package/package/frontend/toolkit/assets/stylesheets/modules/_validation.scss +51 -0
  235. package/package/frontend/toolkit/index.js +10 -0
  236. package/package/index.js +351 -0
  237. package/package/lib/deindex.js +18 -0
  238. package/package/lib/encryption.js +23 -0
  239. package/package/lib/ga-tag.js +89 -0
  240. package/package/lib/health.js +26 -0
  241. package/package/lib/helpers.js +66 -0
  242. package/package/lib/logger.js +65 -0
  243. package/package/lib/markdown.js +46 -0
  244. package/package/lib/router.js +111 -0
  245. package/package/lib/serve-static.js +12 -0
  246. package/package/lib/sessions.js +82 -0
  247. package/package/lib/settings.js +74 -0
  248. package/package/lib/which.js +28 -0
  249. package/package/middleware/cookies.js +43 -0
  250. package/package/middleware/deep-translate.js +32 -0
  251. package/package/middleware/errors.js +119 -0
  252. package/package/middleware/index.js +10 -0
  253. package/package/middleware/not-found.js +38 -0
  254. package/package/middleware/rate-limiter.js +98 -0
  255. package/package/middleware/service-unavailable.js +64 -0
  256. package/package/model/apis/axios-settings.js +21 -0
  257. package/package/model/apis/html-to-pdf-converter.js +35 -0
  258. package/package/model/apis/index.js +5 -0
  259. package/package/model/index.js +348 -0
  260. package/package/package.json +147 -0
  261. package/package/sandbox/README.md +66 -0
  262. package/package/sandbox/apps/sandbox/behaviours/clear-session.js +8 -0
  263. package/package/sandbox/apps/sandbox/behaviours/country-select.js +10 -0
  264. package/package/sandbox/apps/sandbox/behaviours/international-number.js +22 -0
  265. package/package/sandbox/apps/sandbox/fields.js +128 -0
  266. package/package/sandbox/apps/sandbox/index.js +99 -0
  267. package/package/sandbox/apps/sandbox/lib/staticAppealStages.js +189 -0
  268. package/package/sandbox/apps/sandbox/sections/summary-data-sections.js +43 -0
  269. package/package/sandbox/apps/sandbox/translations/src/en/errors.json +5 -0
  270. package/package/sandbox/apps/sandbox/translations/src/en/exit.json +4 -0
  271. package/package/sandbox/apps/sandbox/translations/src/en/fields.json +101 -0
  272. package/package/sandbox/apps/sandbox/translations/src/en/journey.json +7 -0
  273. package/package/sandbox/apps/sandbox/translations/src/en/pages.json +72 -0
  274. package/package/sandbox/apps/sandbox/translations/src/en/validation.json +54 -0
  275. package/package/sandbox/apps/sandbox/views/form-guidance-link.html +8 -0
  276. package/package/sandbox/apps/sandbox/views/save-and-exit.html +19 -0
  277. package/package/sandbox/assets/images/icons/icon-caret-left.png +0 -0
  278. package/package/sandbox/assets/images/icons/icon-complete.png +0 -0
  279. package/package/sandbox/assets/images/icons/icon-cross-remove-sign.png +0 -0
  280. package/package/sandbox/assets/js/index.js +70 -0
  281. package/package/sandbox/assets/scss/app.scss +27 -0
  282. package/package/sandbox/codecept.conf.js +15 -0
  283. package/package/sandbox/config.js +17 -0
  284. package/package/sandbox/package.json +26 -0
  285. package/package/sandbox/server.js +23 -0
  286. package/package/sandbox/yarn.lock +262 -0
  287. package/package/transpiler/index.js +2 -0
  288. package/package/transpiler/lib/aggregate.js +32 -0
  289. package/package/transpiler/lib/build.js +15 -0
  290. package/package/transpiler/lib/compile.js +12 -0
  291. package/package/transpiler/lib/expand-dirs.js +14 -0
  292. package/package/transpiler/lib/write-files.js +24 -0
  293. package/package/utilities/autofill/index.js +189 -0
  294. package/package/utilities/autofill/inputs.js +60 -0
  295. package/package/utilities/countries.js +12 -0
  296. package/package/utilities/helpers/index.js +189 -0
  297. package/package/utilities/index.js +9 -0
  298. package/package/utilities/reqres/index.js +18 -0
  299. package/package/utilities/test-data/data/domain.json +7 -0
  300. package/package/utilities/test-data/data/firstname.json +16 -0
  301. package/package/utilities/test-data/data/lastname.json +10 -0
  302. package/package/utilities/test-data/data/postcode.json +12 -0
  303. package/package/utilities/test-data/data/streetname.json +8 -0
  304. package/package/utilities/test-data/data/streetsuffix.json +7 -0
  305. package/package/utilities/test-data/data/town.json +9 -0
  306. package/package/utilities/test-data/index.js +67 -0
  307. package/package/wizard/behaviours/complete.js +20 -0
  308. package/package/wizard/behaviours/index.js +5 -0
  309. package/package/wizard/index.js +124 -0
  310. package/package/wizard/middleware/back-links.js +68 -0
  311. package/package/wizard/middleware/check-complete.js +13 -0
  312. package/package/wizard/middleware/check-progress.js +139 -0
  313. package/package/wizard/middleware/check-session.js +17 -0
  314. package/package/wizard/middleware/csrf.js +47 -0
  315. package/package/wizard/middleware/session-model.js +11 -0
  316. package/package/wizard/middleware/session.js +9 -0
  317. package/package/wizard/model.js +29 -0
  318. package/package/wizard/util/constants.js +5 -0
  319. package/package/wizard/util/helpers.js +19 -0
  320. package/package.json +5 -6
  321. package/.editorconfig +0 -10
  322. package/.github/workflows/automate-publish.yml +0 -38
  323. package/.github/workflows/automate-tag.yml +0 -78
  324. package/.istanbul.yml +0 -20
  325. package/codeReviewChecklist.md +0 -22
  326. package/pull_request_template.md +0 -16
@@ -0,0 +1,258 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const i18nLookup = require('i18n-lookup');
5
+ const Mustache = require('mustache');
6
+ const BaseController = require('./base-controller');
7
+ const Helpers = require('../utilities').helpers;
8
+
9
+ const omitField = (field, req) => field.useWhen && (typeof field.useWhen === 'string'
10
+ ? req.sessionModel.get(field.useWhen) !== 'true'
11
+ : req.sessionModel.get(field.useWhen.field) !== field.useWhen.value);
12
+
13
+ module.exports = class Controller extends BaseController {
14
+ constructor(options) {
15
+ super(options);
16
+ this.confirmStep = options.confirmStep || '/confirm';
17
+ }
18
+
19
+ configure(req, res, callback) {
20
+ const removeFields = Object.keys(_.pickBy(req.form.options.fields, field => omitField(field, req)));
21
+ if (removeFields.length) {
22
+ req.form.options.fields = _.omit(req.form.options.fields, removeFields);
23
+ req.sessionModel.unset(removeFields);
24
+ }
25
+ callback();
26
+ }
27
+
28
+ get(req, res, callback) {
29
+ const template = this.options.template || '';
30
+ res.render(template, err => {
31
+ if (err && err.message.match(/^Failed to lookup view/)) {
32
+ this.options.template = res.locals.partials.step;
33
+ }
34
+ });
35
+ super.get(req, res, callback);
36
+ }
37
+
38
+ getNextStep(req, res) {
39
+ let next = super.getNextStep(req, res);
40
+ const forks = req.form.options.forks || [];
41
+
42
+ const normaliseUrl = url => req.baseUrl === '/' ? url : req.baseUrl + url;
43
+
44
+ const confirmStep = normaliseUrl(this.confirmStep);
45
+
46
+ const completed = s => {
47
+ let step = s;
48
+ if (req.baseUrl !== '/') {
49
+ const re = new RegExp('^' + req.baseUrl);
50
+ step = step.replace(re, '');
51
+ }
52
+ // Has the user already completed the step?
53
+ return _.includes(req.sessionModel.get('steps'), step);
54
+ };
55
+
56
+ // If a form condition is met, its target supercedes the next property
57
+ next = _.reduce(forks, (result, value) => {
58
+ if (Helpers.isFieldValueInPageOrSessionValid(req, res, value.condition)) {
59
+ if (value.continueOnEdit) {
60
+ req.form.options.continueOnEdit = true;
61
+ }
62
+ return normaliseUrl(value.target);
63
+ }
64
+ return result;
65
+ }, next);
66
+
67
+
68
+ if (req.params.action === 'edit') {
69
+ if (!req.form.options.continueOnEdit && completed(next)) {
70
+ next = confirmStep;
71
+ }
72
+ if (next !== confirmStep) {
73
+ next += '/edit';
74
+ }
75
+ }
76
+
77
+ return next;
78
+ }
79
+
80
+ getBackLink(req, res) {
81
+ const backLink = res.locals.backLink;
82
+ const trailingEdit = req.params.action === 'edit' ? '/edit' : '';
83
+ const leadingSlash = /^\/?\w+/.test(req.baseUrl) ? '' : '/';
84
+
85
+ if (!backLink) {
86
+ return backLink;
87
+ }
88
+
89
+ return `${leadingSlash}${backLink}${trailingEdit}`;
90
+ }
91
+
92
+ getErrorStep(err, req) {
93
+ let redirect = super.getErrorStep(err, req);
94
+ if (req.params.action === 'edit' && !redirect.match(/\/edit$|\/edit\//)) {
95
+ redirect += '/edit';
96
+ }
97
+ return redirect;
98
+ }
99
+
100
+ locals(req, res) {
101
+ const lookup = i18nLookup(req.translate, Mustache.render);
102
+ const route = req.form.options.route.replace(/^\//, '');
103
+ const locals = super.locals(req, res);
104
+ const stepLocals = req.form.options.locals || {};
105
+
106
+ let fields = _.map(req.form.options.fields, (field, key) =>
107
+ Object.assign({}, field, { key })
108
+ );
109
+ // only include fields that aren't dependents to mitigate duplicate fields on the page
110
+ fields = fields.filter(field => !req.form.options.fields[field.key].dependent);
111
+
112
+ const exitStep = req.form.options.exitStep || '/exit';
113
+ const saveAndExitStep = req.form.options.saveAndExitStep || '/save-and-exit';
114
+ return _.extend({}, locals, {
115
+ fields,
116
+ route,
117
+ baseUrl: req.baseUrl,
118
+ exitStep,
119
+ saveAndExitStep,
120
+ skipToMain: this.getFirstFormItem(req.form.options.fields),
121
+ title: this.getTitle(route, lookup, req.form.options.fields, res.locals),
122
+ journeyHeaderURL: this.getJourneyHeaderURL(req.baseUrl),
123
+ header: this.getHeader(route, lookup, res.locals),
124
+ serviceName: this.getServiceName(lookup, res.locals),
125
+ captionHeading: this.getCaptionHeading(route, lookup, res.locals),
126
+ warning: this.getWarning(route, lookup, res.locals),
127
+ subHeading: this.getSubHeading(route, lookup, res.locals),
128
+ intro: this.getIntro(route, lookup, res.locals),
129
+ backLink: this.getBackLink(req, res),
130
+ nextPage: this.getNextStep(req, res),
131
+ errorLength: this.getErrorLength(req, res)
132
+ }, stepLocals);
133
+ }
134
+
135
+ getJourneyHeaderURL(url) {
136
+ return url === '' ? '/' : url;
137
+ }
138
+
139
+ getFirstFormItem(fields) {
140
+ let firstFieldKey;
141
+ if (_.size(fields)) {
142
+ firstFieldKey = Object.keys(fields)[0];
143
+ }
144
+ return firstFieldKey | 'main-content';
145
+ }
146
+
147
+ getHeader(route, lookup, locals) {
148
+ return lookup(`pages.${route}.header`, locals);
149
+ }
150
+
151
+ getServiceName(lookup, locals) {
152
+ return lookup([
153
+ 'journey.serviceName',
154
+ 'journey.header'
155
+ ], locals);
156
+ }
157
+
158
+ getCaptionHeading(route, lookup, locals) {
159
+ return lookup(`pages.${route}.captionHeading`, locals);
160
+ }
161
+
162
+ getSubHeading(route, lookup, locals) {
163
+ return lookup(`pages.${route}.subHeading`, locals);
164
+ }
165
+
166
+ getWarning(route, lookup, locals) {
167
+ return lookup(`pages.${route}.warning`, locals);
168
+ }
169
+
170
+ getTitle(route, lookup, fields, locals) {
171
+ let fieldName = '';
172
+ if (_.size(fields)) {
173
+ fieldName = Object.keys(fields)[0];
174
+ }
175
+ return lookup([
176
+ `pages.${route}.title`,
177
+ `pages.${route}.header`,
178
+ `fields.${fieldName}.label`,
179
+ `fields.${fieldName}.legend`
180
+ ], locals);
181
+ }
182
+
183
+ getIntro(route, lookup, locals) {
184
+ return lookup([
185
+ `pages.${route}.intro`
186
+ ], locals);
187
+ }
188
+
189
+ _getErrors(req, res, callback) {
190
+ super._getErrors(req, res, () => {
191
+ Object.keys(req.form.errors).forEach(key => {
192
+ if (req.form && req.form.options && req.form.options.fields) {
193
+ const field = req.form.options.fields[key];
194
+ // get first option for radios and checkbox
195
+ if (field.mixin === 'radio-group' || field.mixin === 'checkbox-group') {
196
+ // get first option for radios and checkbox where there is a toggle
197
+ if (typeof field.options[0] === 'object') {
198
+ req.form.errors[key].errorLinkId = key + '-' + field.options[0].value;
199
+ } else {
200
+ req.form.errors[key].errorLinkId = key + '-' + field.options[0];
201
+ }
202
+ // eslint-disable-next-line brace-style
203
+ }
204
+ // get first field for date input control
205
+ else if (field && field.mixin === 'input-date') {
206
+ req.form.errors[key].errorLinkId = key + '-day';
207
+ } else {
208
+ req.form.errors[key].errorLinkId = key;
209
+ }
210
+ }
211
+
212
+ req.form.errors[key].message = this.getErrorMessage(req.form.errors[key], req, res);
213
+ });
214
+ callback();
215
+ });
216
+ }
217
+
218
+ getErrorMessage(error, req, res) {
219
+ error.type = error.type || 'default';
220
+ error.options = error.options || {};
221
+ const keys = [
222
+ `fields.${error.key}.validation.${error.type}`,
223
+ `validation.${error.key}.${error.type}`,
224
+ `validation.${error.key}.default`,
225
+ `validation.${error.type}`,
226
+ 'validation.default'
227
+ ];
228
+ const getArgs = () => {
229
+ if (error.type === 'before' || error.type === 'after') {
230
+ return { diff: error.arguments.join(' ') };
231
+ } else if (error.arguments && Array.isArray(error.arguments)) {
232
+ return { [error.type]: error.arguments[0] };
233
+ }
234
+ return {};
235
+ };
236
+ const context = Object.assign({
237
+ label: req.translate(`fields.${error.key}.label`).toLowerCase(),
238
+ legend: req.translate(`fields.${error.key}.legend`).toLowerCase()
239
+ }, res.locals, getArgs(error));
240
+
241
+ return i18nLookup(req.translate, Mustache.render)(keys, context);
242
+ }
243
+
244
+ // eslint-disable-next-line no-unused-vars
245
+ getErrorLength(req, res) {
246
+ if (!req.form.errors) {
247
+ return undefined;
248
+ }
249
+
250
+ const errorLength = Object.keys(req.form.errors).length;
251
+
252
+ const propName = errorLength === 1 ? 'single' : 'multiple';
253
+
254
+ return errorLength ? {
255
+ [propName]: true
256
+ } : undefined;
257
+ }
258
+ };
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ const deprecate = require('deprecate');
4
+
5
+ module.exports = superclass => class DeprecatedError extends superclass {
6
+ constructor(options) {
7
+ deprecate('this.Error is deprecated in favour of this.ValidationError');
8
+ super(options);
9
+ }
10
+ };
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+
5
+ trim(value) {
6
+ return typeof value === 'string' ? value.trim() : value;
7
+ },
8
+
9
+ boolean(value) {
10
+ if (value === true || value === 'true') {
11
+ return true;
12
+ } else if (value === false || value === 'false') {
13
+ return false;
14
+ }
15
+ return undefined;
16
+ },
17
+
18
+ uppercase(value) {
19
+ return typeof value === 'string' ? value.toUpperCase() : value;
20
+ },
21
+
22
+ lowercase(value) {
23
+ return typeof value === 'string' ? value.toLowerCase() : value;
24
+ },
25
+
26
+ removespaces(value) {
27
+ return typeof value === 'string' ? value.replace(/\s+/g, '') : value;
28
+ },
29
+
30
+ singlespaces(value) {
31
+ return typeof value === 'string' ? value.replace(/\s+/g, ' ') : value;
32
+ },
33
+
34
+ hyphens(value) {
35
+ return typeof value === 'string' ? value.replace(/[–—-]+/g, '-') : value;
36
+ },
37
+
38
+ removeroundbrackets(value) {
39
+ return typeof value === 'string' ? value.replace(/[\(\)]/g, '') : value;
40
+ },
41
+
42
+ removehyphens(value) {
43
+ return typeof value === 'string' ? value.replace(/[–—-]+/g, '') : value;
44
+ },
45
+
46
+ removeslashes(value) {
47
+ return typeof value === 'string' ? value.replace(/[\/\\]/g, '') : value;
48
+ },
49
+
50
+ ukphoneprefix(value) {
51
+ return typeof value === 'string' ? value.replace(/^\+44\(?0?\)?/, '0') : value;
52
+ },
53
+
54
+ base64decode(value) {
55
+ return Buffer.from(value, 'base64').toString();
56
+ },
57
+
58
+ ukPostcode(value) {
59
+ if (typeof value !== 'string' || value === '') {
60
+ return value;
61
+ }
62
+
63
+ const postcode = this.uppercase(this.removespaces(value));
64
+ const firstPart = postcode.slice(0, -3);
65
+ const secondPart = postcode.slice(-3);
66
+
67
+ return `${firstPart} ${secondPart}`;
68
+ }
69
+
70
+ };
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+
5
+ function format(f, val, formatters) {
6
+ let value = val;
7
+ if (typeof f === 'string' && formatters[f]) {
8
+ return formatters[f](value);
9
+ } else if (typeof f === 'function') {
10
+ return f(value);
11
+ } else if (Array.isArray(f)) {
12
+ f.forEach(v => {
13
+ value = format(v, value, formatters);
14
+ });
15
+ }
16
+ return value;
17
+ }
18
+
19
+ module.exports = (fields, _default, f) => (key, v) => {
20
+ const formatters = f || require('./formatters');
21
+ let value = _.castArray(v);
22
+ if (_default && !(fields[key] && fields[key]['ignore-defaults'])) {
23
+ value = value.map(item => format(_default, item, formatters));
24
+ }
25
+ if (fields[key] && fields[key].formatter) {
26
+ value = value.map(item => format(fields[key].formatter, item, formatters));
27
+ }
28
+ if (value.length === 1) {
29
+ value = value[0];
30
+ }
31
+ return value;
32
+ };
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const Session = require('./behaviour-session');
4
+ const Hooks = require('./behaviour-hooks');
5
+ const Controller = require('./controller');
6
+ const mix = require('mixwith').mix;
7
+
8
+ class FormController extends mix(Controller).with(Session, Hooks) {}
9
+
10
+ FormController.validators = require('./validation/validators');
11
+ FormController.formatters = require('./formatting/formatters');
12
+
13
+ FormController.ValidationError = require('./validation-error');
14
+
15
+ module.exports = FormController;
16
+ module.exports.Controller = Controller;
17
+ module.exports.BaseController = require('./base-controller');
@@ -0,0 +1,30 @@
1
+ /* eslint-disable vars-on-top */
2
+ 'use strict';
3
+
4
+ // eslint-disable-next-line max-len
5
+ const tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
6
+
7
+ module.exports = function (email) {
8
+ if (!email || !(typeof email === 'string')) return false;
9
+
10
+ const emailParts = email.split('@');
11
+
12
+ if (emailParts.length !== 2) return false;
13
+
14
+ const account = emailParts[0];
15
+ const address = emailParts[1];
16
+
17
+ if (account.length > 64) return false;
18
+
19
+ else if (address.length > 255) return false;
20
+
21
+ const domainParts = address.split('.');
22
+ if (domainParts.some(function (part) {
23
+ return part.length > 63;
24
+ })) return false;
25
+
26
+
27
+ if (!tester.test(email)) return false;
28
+
29
+ return true;
30
+ };
@@ -0,0 +1,101 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+ const debug = require('debug')('hmpo:validation');
5
+
6
+ const validators = require('./validators');
7
+
8
+ function applyValidator(vlt, value, key) {
9
+ const validator = typeof vlt === 'string' ? { type: vlt } : vlt;
10
+
11
+ function doValidate() {
12
+ debug('Applying %s validator with value "%s"', validator.type, value);
13
+ let args = [value];
14
+ if (!Array.isArray(validator.arguments)) {
15
+ validator.arguments = [validator.arguments];
16
+ }
17
+ args = args.concat(validator.arguments);
18
+ if (!validators[validator.type].apply(null, args)) {
19
+ return Object.assign({ key }, validator, { arguments: validator.arguments });
20
+ }
21
+ return undefined;
22
+ }
23
+
24
+ function customValidate() {
25
+ debug('Applying custom %s validator with value %s', validator.name, value);
26
+ if (!validator(value)) {
27
+ return Object.assign({ key }, { type: validator.name });
28
+ }
29
+ return undefined;
30
+ }
31
+
32
+ if (validators[validator.type]) {
33
+ return doValidate();
34
+ } else if (typeof validator === 'function') {
35
+ if (validator.name) {
36
+ return customValidate();
37
+ }
38
+ throw new Error('Custom validator needs to be a named function');
39
+ } else {
40
+ throw new Error(`Undefined validator:${validator.type}`);
41
+ }
42
+ }
43
+
44
+ function validate(fields) {
45
+ _.each(fields, (field, key) => {
46
+ if (typeof fields[key].validate === 'string') {
47
+ fields[key].validate = [fields[key].validate];
48
+ }
49
+
50
+ if (fields[key].options) {
51
+ fields[key].validate = fields[key].validate || [];
52
+ fields[key].validate.push({
53
+ type: 'equal',
54
+ arguments: _.map(fields[key].options, option =>
55
+ typeof option === 'string' ? option : option.value
56
+ )
57
+ });
58
+ }
59
+ });
60
+
61
+ // eslint-disable-next-line consistent-return
62
+ return (key, value, values, emptyValue) => {
63
+ debug(`Validating field: "${key}" with value: "${value}"`);
64
+
65
+ function shouldValidate() {
66
+ // validationLink used to validates multiple dependent fields in checkbox-group
67
+ let dependent = fields[key].dependent || fields[key].validationLink;
68
+
69
+ if (typeof dependent === 'string') {
70
+ dependent = {
71
+ field: dependent,
72
+ value: true
73
+ };
74
+ }
75
+ if (!dependent
76
+ || (dependent && !fields[dependent.field])
77
+ || (dependent && (Array.isArray(values[dependent.field])
78
+ ? values[dependent.field].indexOf(dependent.value) > -1
79
+ : values[dependent.field] === dependent.value))
80
+ ) {
81
+ return true;
82
+ }
83
+ return false;
84
+ }
85
+
86
+ if (fields[key]) {
87
+ if (shouldValidate()) {
88
+ debug(`Applying validation on field ${key} with ${value}`);
89
+ return _.reduce(fields[key].validate, (err, validator) =>
90
+ err || applyValidator(validator, value, key)
91
+ , null);
92
+ }
93
+ values[key] = emptyValue === undefined ? '' : emptyValue;
94
+ debug('Skipping validation for field %s', key);
95
+ }
96
+ };
97
+ }
98
+
99
+ validate.validators = validators;
100
+
101
+ module.exports = validate;
@@ -0,0 +1,181 @@
1
+ 'use strict';
2
+
3
+ const moment = require('moment');
4
+ const _ = require('lodash');
5
+ const libPhoneNumber = require('libphonenumber-js/max');
6
+ const deprecate = require('deprecate');
7
+ const emailValidator = require('./email');
8
+
9
+ // validator methods should return false (or falsy value) for *invalid* input
10
+ // and true (or truthy value) for *valid* input.
11
+ const dateFormat = 'YYYY-MM-DD';
12
+ let Validators;
13
+
14
+ module.exports = Validators = {
15
+
16
+ string(value) {
17
+ return typeof value === 'string';
18
+ },
19
+
20
+ decimal(value) {
21
+ return value === '' || Validators.regex(value, /^-?[\d]*\.?\d{0,2}$/);
22
+ },
23
+
24
+ regex(value, match) {
25
+ return Validators.string(value) && !!value.match(match);
26
+ },
27
+
28
+ required(value) {
29
+ return value !== undefined && value !== '';
30
+ },
31
+
32
+ url(value) {
33
+ return value === '' ||
34
+ Validators.regex(value, /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/);
35
+ },
36
+
37
+ notUrl(value) {
38
+ return value === '' || !(Validators.url(value));
39
+ },
40
+
41
+ email(value) {
42
+ return value === '' || emailValidator(value);
43
+ },
44
+
45
+ between(value, min, max) {
46
+ return value === '' || (value <= max && value >= min);
47
+ },
48
+
49
+ min(value, min) {
50
+ return value === '' || value >= min;
51
+ },
52
+
53
+ max(value, max) {
54
+ return value === '' || value <= max;
55
+ },
56
+
57
+ minlength(value, length) {
58
+ return Validators.string(value) && (value === '' || value.length >= (length || 0));
59
+ },
60
+
61
+ maxlength(value, length) {
62
+ return Validators.string(value) && (value === '' || value.length <= length);
63
+ },
64
+
65
+ maxword(value, length) {
66
+ return Validators.string(value) && (value === '' || value.split(/\s+/).length <= length);
67
+ },
68
+
69
+ exactlength(value, length) {
70
+ return Validators.string(value) && (value === '' || value.length === length);
71
+ },
72
+
73
+ alphanum(value) {
74
+ return Validators.regex(value, /^[a-zA-Z0-9]*$/);
75
+ },
76
+
77
+ numeric(value) {
78
+ return Validators.regex(value, /^\d*$/);
79
+ },
80
+
81
+ equal(v) {
82
+ const values = [].slice.call(arguments, 1);
83
+ const value = _.castArray(v);
84
+ return values.length && _.every(value, item =>
85
+ item === '' || values.indexOf(item) > -1
86
+ );
87
+ },
88
+
89
+ phonenumber(value) {
90
+ deprecate('phonenumber validator', 'Recommend using internationalPhoneNumber for validating ' +
91
+ 'international numbers or a different phone validator for more specific requirements.');
92
+ return value === '' || Validators.regex(value, /^\(?\+?[\d()-]{0,15}$/);
93
+ },
94
+
95
+ /**
96
+ * Validates international phone numbers (fixed and mobile) including UK.
97
+ * Non-GB phone numbers will require country code to validate.
98
+ */
99
+ internationalPhoneNumber(value, code) {
100
+ const countryCode = code || 'GB';
101
+ const phoneNumber = libPhoneNumber.parsePhoneNumberFromString(value, countryCode) || '';
102
+ const isValidComparedToGivenCountry = code ? phoneNumber.country === countryCode : true;
103
+ return value === '' || (phoneNumber && phoneNumber.isValid() && isValidComparedToGivenCountry);
104
+ },
105
+
106
+ ukPhoneNumber(value) {
107
+ const phoneNumber = libPhoneNumber.parsePhoneNumberFromString(value, 'GB') || '';
108
+ return value === '' || (phoneNumber && phoneNumber.isValid() && phoneNumber.country === 'GB');
109
+ },
110
+
111
+ ukmobilephone(value) {
112
+ return value === '' || Validators.regex(value, /^(?:\+447|07)\d{9}$/);
113
+ },
114
+
115
+ date(value) {
116
+ return value === '' || Validators.regex(value, /\d{4}\-\d{2}\-\d{2}/) && moment(value, dateFormat).isValid();
117
+ },
118
+
119
+ 'date-year'(value) {
120
+ return Validators.regex(value, /^\d{4}$/);
121
+ },
122
+
123
+ 'date-month'(value) {
124
+ return Validators.regex(value, /^\d{2}$/) && parseInt(value, 10) > 0 && parseInt(value, 10) < 13;
125
+ },
126
+
127
+ 'date-day'(value) {
128
+ return Validators.regex(value, /^\d{2}$/) && parseInt(value, 10) > 0 && parseInt(value, 10) < 32;
129
+ },
130
+
131
+ // eslint-disable-next-line no-inline-comments, spaced-comment
132
+ before(value, date) {
133
+ // validator can also do before(value, [diff, unit][, diff, unit])
134
+ let valueDate = moment(value, dateFormat);
135
+ let comparator;
136
+ if (arguments.length === 2) {
137
+ comparator = date;
138
+ } else {
139
+ comparator = moment();
140
+ const args = [].slice.call(arguments, 1);
141
+ let diff;
142
+ let unit;
143
+ while (args.length) {
144
+ diff = args.shift();
145
+ unit = args.shift() || 'years';
146
+ valueDate = valueDate.add(diff, unit);
147
+ }
148
+ }
149
+ return value === '' || Validators.date(value) && valueDate.isBefore(comparator);
150
+ },
151
+
152
+ after(value, date) {
153
+ // validator can also do after(value, [diff, unit][, diff, unit])
154
+ let valueDate = moment(value, dateFormat);
155
+ let comparator;
156
+ if (arguments.length === 2) {
157
+ comparator = date;
158
+ } else {
159
+ comparator = moment();
160
+ const args = [].slice.call(arguments, 1);
161
+ let diff;
162
+ let unit;
163
+ while (args.length) {
164
+ diff = args.shift();
165
+ unit = args.shift() || 'years';
166
+ valueDate = valueDate.add(diff, unit);
167
+ }
168
+ }
169
+ return value === '' || Validators.date(value) && valueDate.isAfter(comparator);
170
+ },
171
+
172
+ over18(value) {
173
+ return Validators.before(value, '18', 'years');
174
+ },
175
+
176
+ postcode(value) {
177
+ // eslint-disable-next-line max-len
178
+ return value === '' || Validators.regex(value, /^(([GIR] ?0[A]{2})|((([A-Z][0-9]{1,2})|(([A-Z][A-HJ-Y][0-9]{1,2})|(([A-Z][0-9][A-Z])|([A-Z][A-HJ-Y][0-9]?[A-Z])))) ?[0-9][A-Z]{2}))$/i);
179
+ }
180
+
181
+ };