@dwp/govuk-casa 8.15.0 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/assets/css/casa.css +2 -1
- package/dist/assets/css/casa.css.map +1 -0
- package/dist/casa.d.ts +122 -99
- package/dist/casa.js +120 -88
- package/dist/casa.js.map +1 -1
- package/dist/lib/CasaTemplateLoader.d.ts +4 -4
- package/dist/lib/CasaTemplateLoader.js +16 -16
- package/dist/lib/CasaTemplateLoader.js.map +1 -1
- package/dist/lib/JourneyContext.d.ts +38 -40
- package/dist/lib/JourneyContext.js +89 -75
- package/dist/lib/JourneyContext.js.map +1 -1
- package/dist/lib/MutableRouter.d.ts +40 -41
- package/dist/lib/MutableRouter.js +64 -71
- package/dist/lib/MutableRouter.js.map +1 -1
- package/dist/lib/Plan.d.ts +29 -26
- package/dist/lib/Plan.js +85 -71
- package/dist/lib/Plan.js.map +1 -1
- package/dist/lib/ValidationError.d.ts +16 -15
- package/dist/lib/ValidationError.js +21 -20
- package/dist/lib/ValidationError.js.map +1 -1
- package/dist/lib/ValidatorFactory.d.ts +15 -13
- package/dist/lib/ValidatorFactory.js +14 -12
- package/dist/lib/ValidatorFactory.js.map +1 -1
- package/dist/lib/configuration-ingestor.d.ts +37 -40
- package/dist/lib/configuration-ingestor.js +93 -93
- package/dist/lib/configuration-ingestor.js.map +1 -1
- package/dist/lib/configure.d.ts +6 -6
- package/dist/lib/configure.js +14 -12
- package/dist/lib/configure.js.map +1 -1
- package/dist/lib/constants.d.ts +1 -3
- package/dist/lib/constants.js +9 -11
- package/dist/lib/constants.js.map +1 -1
- package/dist/lib/context-id-generators.d.ts +3 -5
- package/dist/lib/context-id-generators.js +7 -6
- package/dist/lib/context-id-generators.js.map +1 -1
- package/dist/lib/end-session.d.ts +4 -4
- package/dist/lib/end-session.js +5 -5
- package/dist/lib/field.d.ts +24 -29
- package/dist/lib/field.js +41 -70
- package/dist/lib/field.js.map +1 -1
- package/dist/lib/index.d.ts +13 -13
- package/dist/lib/logger.d.ts +7 -6
- package/dist/lib/logger.js +7 -7
- package/dist/lib/logger.js.map +1 -1
- package/dist/lib/mount.d.ts +5 -5
- package/dist/lib/mount.js +12 -17
- package/dist/lib/mount.js.map +1 -1
- package/dist/lib/nunjucks-filters.d.ts +10 -12
- package/dist/lib/nunjucks-filters.js +35 -35
- package/dist/lib/nunjucks-filters.js.map +1 -1
- package/dist/lib/nunjucks.d.ts +7 -5
- package/dist/lib/nunjucks.js +10 -8
- package/dist/lib/nunjucks.js.map +1 -1
- package/dist/lib/utils.d.ts +19 -19
- package/dist/lib/utils.js +62 -55
- package/dist/lib/utils.js.map +1 -1
- package/dist/lib/validators/dateObject.d.ts +29 -22
- package/dist/lib/validators/dateObject.js +58 -49
- package/dist/lib/validators/dateObject.js.map +1 -1
- package/dist/lib/validators/email.d.ts +4 -4
- package/dist/lib/validators/email.js +4 -4
- package/dist/lib/validators/inArray.d.ts +4 -4
- package/dist/lib/validators/inArray.js +7 -8
- package/dist/lib/validators/inArray.js.map +1 -1
- package/dist/lib/validators/index.d.ts +10 -10
- package/dist/lib/validators/index.js +1 -3
- package/dist/lib/validators/index.js.map +1 -1
- package/dist/lib/validators/nino.d.ts +9 -8
- package/dist/lib/validators/nino.js +14 -10
- package/dist/lib/validators/nino.js.map +1 -1
- package/dist/lib/validators/postalAddressObject.d.ts +37 -24
- package/dist/lib/validators/postalAddressObject.js +65 -46
- package/dist/lib/validators/postalAddressObject.js.map +1 -1
- package/dist/lib/validators/range.d.ts +12 -8
- package/dist/lib/validators/range.js +11 -9
- package/dist/lib/validators/range.js.map +1 -1
- package/dist/lib/validators/regex.d.ts +4 -4
- package/dist/lib/validators/regex.js +5 -5
- package/dist/lib/validators/required.d.ts +6 -6
- package/dist/lib/validators/required.js +9 -11
- package/dist/lib/validators/required.js.map +1 -1
- package/dist/lib/validators/strlen.d.ts +12 -8
- package/dist/lib/validators/strlen.js +13 -11
- package/dist/lib/validators/strlen.js.map +1 -1
- package/dist/lib/validators/wordCount.d.ts +12 -8
- package/dist/lib/validators/wordCount.js +15 -11
- package/dist/lib/validators/wordCount.js.map +1 -1
- package/dist/lib/waypoint-url.d.ts +16 -13
- package/dist/lib/waypoint-url.js +39 -36
- package/dist/lib/waypoint-url.js.map +1 -1
- package/dist/middleware/body-parser.d.ts +1 -1
- package/dist/middleware/body-parser.js +6 -6
- package/dist/middleware/body-parser.js.map +1 -1
- package/dist/middleware/data.d.ts +1 -1
- package/dist/middleware/data.js +8 -7
- package/dist/middleware/data.js.map +1 -1
- package/dist/middleware/gather-fields.d.ts +2 -2
- package/dist/middleware/gather-fields.js +6 -4
- package/dist/middleware/gather-fields.js.map +1 -1
- package/dist/middleware/i18n.js +13 -15
- package/dist/middleware/i18n.js.map +1 -1
- package/dist/middleware/post.js +30 -18
- package/dist/middleware/post.js.map +1 -1
- package/dist/middleware/pre.d.ts +2 -2
- package/dist/middleware/pre.js +46 -26
- package/dist/middleware/pre.js.map +1 -1
- package/dist/middleware/progress-journey.d.ts +1 -1
- package/dist/middleware/progress-journey.js +5 -5
- package/dist/middleware/progress-journey.js.map +1 -1
- package/dist/middleware/sanitise-fields.d.ts +1 -1
- package/dist/middleware/sanitise-fields.js +13 -11
- package/dist/middleware/sanitise-fields.js.map +1 -1
- package/dist/middleware/serve-first-waypoint.d.ts +3 -3
- package/dist/middleware/serve-first-waypoint.js +8 -6
- package/dist/middleware/serve-first-waypoint.js.map +1 -1
- package/dist/middleware/session.js +14 -11
- package/dist/middleware/session.js.map +1 -1
- package/dist/middleware/skip-waypoint.d.ts +1 -1
- package/dist/middleware/skip-waypoint.js +3 -3
- package/dist/middleware/skip-waypoint.js.map +1 -1
- package/dist/middleware/steer-journey.d.ts +1 -1
- package/dist/middleware/steer-journey.js +16 -14
- package/dist/middleware/steer-journey.js.map +1 -1
- package/dist/middleware/strip-proxy-path.d.ts +1 -1
- package/dist/middleware/strip-proxy-path.js +3 -3
- package/dist/middleware/strip-proxy-path.js.map +1 -1
- package/dist/middleware/validate-fields.d.ts +1 -1
- package/dist/middleware/validate-fields.js +2 -5
- package/dist/middleware/validate-fields.js.map +1 -1
- package/dist/routes/ancillary.d.ts +3 -3
- package/dist/routes/ancillary.js +4 -4
- package/dist/routes/ancillary.js.map +1 -1
- package/dist/routes/journey.d.ts +2 -2
- package/dist/routes/journey.js +91 -39
- package/dist/routes/journey.js.map +1 -1
- package/dist/routes/static.d.ts +7 -5
- package/dist/routes/static.js +20 -20
- package/dist/routes/static.js.map +1 -1
- package/package.json +17 -16
- package/src/casa.js +134 -102
- package/src/lib/CasaTemplateLoader.js +24 -19
- package/src/lib/JourneyContext.js +147 -107
- package/src/lib/MutableRouter.js +72 -74
- package/src/lib/Plan.js +145 -97
- package/src/lib/ValidationError.js +25 -21
- package/src/lib/ValidatorFactory.js +17 -13
- package/src/lib/configuration-ingestor.js +147 -110
- package/src/lib/configure.js +34 -32
- package/src/lib/constants.js +9 -11
- package/src/lib/context-id-generators.js +40 -43
- package/src/lib/end-session.js +6 -6
- package/src/lib/field.js +74 -78
- package/src/lib/index.js +12 -12
- package/src/lib/logger.js +9 -9
- package/src/lib/mount.js +70 -80
- package/src/lib/nunjucks-filters.js +56 -59
- package/src/lib/nunjucks.js +23 -18
- package/src/lib/utils.js +78 -57
- package/src/lib/validators/dateObject.js +71 -60
- package/src/lib/validators/email.js +8 -8
- package/src/lib/validators/inArray.js +10 -11
- package/src/lib/validators/index.js +12 -14
- package/src/lib/validators/nino.js +29 -15
- package/src/lib/validators/postalAddressObject.js +87 -63
- package/src/lib/validators/range.js +14 -12
- package/src/lib/validators/regex.js +8 -8
- package/src/lib/validators/required.js +16 -16
- package/src/lib/validators/strlen.js +16 -14
- package/src/lib/validators/wordCount.js +22 -14
- package/src/lib/waypoint-url.js +64 -46
- package/src/middleware/body-parser.js +10 -10
- package/src/middleware/csrf.js +1 -1
- package/src/middleware/data.js +28 -24
- package/src/middleware/gather-fields.js +10 -9
- package/src/middleware/i18n.js +35 -37
- package/src/middleware/post.js +41 -21
- package/src/middleware/pre.js +62 -40
- package/src/middleware/progress-journey.js +32 -18
- package/src/middleware/sanitise-fields.js +43 -20
- package/src/middleware/serve-first-waypoint.js +14 -12
- package/src/middleware/session.js +74 -61
- package/src/middleware/skip-waypoint.js +7 -9
- package/src/middleware/steer-journey.js +40 -28
- package/src/middleware/strip-proxy-path.js +8 -7
- package/src/middleware/validate-fields.js +5 -12
- package/src/routes/ancillary.js +5 -7
- package/src/routes/journey.js +159 -85
- package/src/routes/static.js +62 -30
- package/views/casa/components/character-count/README.md +2 -2
- package/views/casa/components/checkboxes/README.md +6 -6
- package/views/casa/components/date-input/README.md +7 -7
- package/views/casa/components/input/README.md +2 -2
- package/views/casa/components/journey-form/README.md +33 -14
- package/views/casa/components/postal-address-object/README.md +4 -4
- package/views/casa/components/radios/README.md +6 -6
- package/views/casa/components/select/README.md +6 -6
- package/views/casa/components/textarea/README.md +2 -2
- package/views/casa/partials/scripts.njk +5 -3
- package/views/casa/partials/styles.njk +1 -4
- package/dist/assets/css/casa-ie8.css +0 -1
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
-
import ValidationError from
|
|
3
|
-
import ValidatorFactory from
|
|
4
|
-
import { stringifyInput } from
|
|
2
|
+
import ValidationError from "../ValidationError.js";
|
|
3
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
4
|
+
import { stringifyInput } from "../utils.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
7
8
|
* @access private
|
|
8
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @typedef {object} NinoConfigOptions
|
|
13
13
|
* @property {ErrorMessageConfig} errorMsg Error message config
|
|
14
|
-
* @property {boolean} allowWhitespace Will permit input values that contain
|
|
14
|
+
* @property {boolean} allowWhitespace Will permit input values that contain
|
|
15
|
+
* spaces.
|
|
15
16
|
*/
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* UK National Insurance number.
|
|
19
20
|
*
|
|
20
|
-
* Ref:
|
|
21
|
-
* https://en.wikipedia.org/wiki/National_Insurance_number#Format
|
|
21
|
+
* Ref: https://en.wikipedia.org/wiki/National_Insurance_number#Format
|
|
22
22
|
* https://design-system.service.gov.uk/patterns/national-insurance-numbers/
|
|
23
23
|
*
|
|
24
24
|
* See {@link NinoConfigOptions} for `make()` options.
|
|
@@ -27,23 +27,37 @@ import { stringifyInput } from '../utils.js';
|
|
|
27
27
|
* @augments ValidatorFactory
|
|
28
28
|
*/
|
|
29
29
|
export default class Nino extends ValidatorFactory {
|
|
30
|
-
name =
|
|
30
|
+
name = "nino";
|
|
31
31
|
|
|
32
32
|
validate(value, dataContext = {}) {
|
|
33
33
|
const {
|
|
34
34
|
allowWhitespace,
|
|
35
35
|
errorMsg = {
|
|
36
|
-
inline:
|
|
37
|
-
summary:
|
|
36
|
+
inline: "validation:rule.nino.inline",
|
|
37
|
+
summary: "validation:rule.nino.summary",
|
|
38
38
|
},
|
|
39
39
|
} = this.config;
|
|
40
40
|
|
|
41
|
-
if (
|
|
42
|
-
|
|
41
|
+
if (
|
|
42
|
+
typeof allowWhitespace !== "undefined" &&
|
|
43
|
+
typeof allowWhitespace !== "boolean"
|
|
44
|
+
) {
|
|
45
|
+
throw new TypeError(
|
|
46
|
+
`NINO validation rule option "allowWhitespace" must been a boolean. received ${typeof allowWhitespace}`,
|
|
47
|
+
);
|
|
43
48
|
}
|
|
44
|
-
const valid =
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
const valid =
|
|
50
|
+
typeof value === "string" &&
|
|
51
|
+
value
|
|
52
|
+
.replace(
|
|
53
|
+
typeof allowWhitespace !== "undefined" && allowWhitespace
|
|
54
|
+
? /\u0020/g
|
|
55
|
+
: "",
|
|
56
|
+
"",
|
|
57
|
+
)
|
|
58
|
+
.match(
|
|
59
|
+
/^(?!BG|GB|NK|KN|TN|NT|ZZ)[ABCEGHJ-PRSTW-Z][ABCEGHJ-NPRSTW-Z]\d{6}[A-D]$/i,
|
|
60
|
+
);
|
|
47
61
|
|
|
48
62
|
return valid ? [] : [ValidationError.make({ errorMsg, dataContext })];
|
|
49
63
|
}
|
|
@@ -1,32 +1,38 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
-
import lodash from
|
|
3
|
-
import ValidationError from
|
|
4
|
-
import ValidatorFactory from
|
|
5
|
-
import { stringifyInput } from
|
|
2
|
+
import lodash from "lodash";
|
|
3
|
+
import ValidationError from "../ValidationError.js";
|
|
4
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
5
|
+
import { stringifyInput } from "../utils.js";
|
|
6
6
|
|
|
7
7
|
const { isPlainObject } = lodash; // CommonjS
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
10
11
|
* @access private
|
|
11
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* @typedef {object} PostalAddressObjectConfigOptions
|
|
16
|
-
* @property {ErrorMessageConfig} [errorMsg] General error message for the
|
|
17
|
-
*
|
|
18
|
-
* @property {string|object} [
|
|
19
|
-
*
|
|
20
|
-
* @property {string|object} [
|
|
21
|
-
*
|
|
22
|
-
* @property {
|
|
23
|
-
*
|
|
24
|
-
*
|
|
16
|
+
* @property {ErrorMessageConfig} [errorMsg] General error message for the
|
|
17
|
+
* entire address block
|
|
18
|
+
* @property {string | object} [errorMsgAddress1] Error message for address1
|
|
19
|
+
* part
|
|
20
|
+
* @property {string | object} [errorMsgAddress2] Error message for address2
|
|
21
|
+
* part
|
|
22
|
+
* @property {string | object} [errorMsgAddress3] Error message for address3
|
|
23
|
+
* part
|
|
24
|
+
* @property {string | object} [errorMsgAddress4] Error message for address4
|
|
25
|
+
* part
|
|
26
|
+
* @property {string | object} [errorMsgPostcode] Error message for postcode
|
|
27
|
+
* part
|
|
28
|
+
* @property {number} [strlenmax] Max. String length for each of the inputs
|
|
29
|
+
* address[1-4]
|
|
30
|
+
* @property {string[]} [requiredFields] Field parts required (others become
|
|
31
|
+
* optional). One of 'address1'|'address2'|'address3'|'address4'|'postcode'
|
|
25
32
|
*/
|
|
26
33
|
|
|
27
34
|
/**
|
|
28
|
-
* Works hand in hand with the core CASA `postalAddressObject` form
|
|
29
|
-
* macro.
|
|
35
|
+
* Works hand in hand with the core CASA `postalAddressObject` form macro.
|
|
30
36
|
*
|
|
31
37
|
* The errors sent back from this validator are specific to each subfield. For
|
|
32
38
|
* example, if the field name being tested is "address", any errors related to
|
|
@@ -38,73 +44,84 @@ const { isPlainObject } = lodash; // CommonjS
|
|
|
38
44
|
* @augments ValidatorFactory
|
|
39
45
|
*/
|
|
40
46
|
export default class PostalAddressObject extends ValidatorFactory {
|
|
41
|
-
name =
|
|
47
|
+
name = "postalAddressObject";
|
|
42
48
|
|
|
43
49
|
validate(value, dataContext = {}) {
|
|
44
50
|
const cfg = {
|
|
45
|
-
requiredFields: [
|
|
51
|
+
requiredFields: ["address1", "address3", "postcode"],
|
|
46
52
|
strlenmax: undefined,
|
|
47
53
|
errorMsgAddress1: {
|
|
48
|
-
inline:
|
|
49
|
-
summary:
|
|
50
|
-
focusSuffix:
|
|
54
|
+
inline: "validation:rule.postalAddressObject.address1.inline",
|
|
55
|
+
summary: "validation:rule.postalAddressObject.address1.summary",
|
|
56
|
+
focusSuffix: "[address1]",
|
|
51
57
|
},
|
|
52
58
|
errorMsgAddress2: {
|
|
53
|
-
inline:
|
|
54
|
-
summary:
|
|
55
|
-
focusSuffix:
|
|
59
|
+
inline: "validation:rule.postalAddressObject.address2.inline",
|
|
60
|
+
summary: "validation:rule.postalAddressObject.address2.summary",
|
|
61
|
+
focusSuffix: "[address2]",
|
|
56
62
|
},
|
|
57
63
|
errorMsgAddress3: {
|
|
58
|
-
inline:
|
|
59
|
-
summary:
|
|
60
|
-
focusSuffix:
|
|
64
|
+
inline: "validation:rule.postalAddressObject.address3.inline",
|
|
65
|
+
summary: "validation:rule.postalAddressObject.address3.summary",
|
|
66
|
+
focusSuffix: "[address3]",
|
|
61
67
|
},
|
|
62
68
|
errorMsgAddress4: {
|
|
63
|
-
inline:
|
|
64
|
-
summary:
|
|
65
|
-
focusSuffix:
|
|
69
|
+
inline: "validation:rule.postalAddressObject.address4.inline",
|
|
70
|
+
summary: "validation:rule.postalAddressObject.address4.summary",
|
|
71
|
+
focusSuffix: "[address4]",
|
|
66
72
|
},
|
|
67
73
|
errorMsgPostcode: {
|
|
68
|
-
inline:
|
|
69
|
-
summary:
|
|
70
|
-
focusSuffix:
|
|
74
|
+
inline: "validation:rule.postalAddressObject.postcode.inline",
|
|
75
|
+
summary: "validation:rule.postalAddressObject.postcode.summary",
|
|
76
|
+
focusSuffix: "[postcode]",
|
|
71
77
|
},
|
|
72
78
|
errorMsg: {
|
|
73
|
-
inline:
|
|
74
|
-
summary:
|
|
75
|
-
focusSuffix:
|
|
79
|
+
inline: "validation:rule.postalAddressObject.group.inline",
|
|
80
|
+
summary: "validation:rule.postalAddressObject.group.summary",
|
|
81
|
+
focusSuffix: "[address1]",
|
|
76
82
|
},
|
|
77
83
|
...this.config,
|
|
78
84
|
};
|
|
79
85
|
|
|
80
86
|
/* eslint-disable-next-line require-jsdoc */
|
|
81
|
-
const objectifyError = (err) =>
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
const objectifyError = (err) =>
|
|
88
|
+
typeof err === "string"
|
|
89
|
+
? {
|
|
90
|
+
inline: err,
|
|
91
|
+
summary: err,
|
|
92
|
+
}
|
|
93
|
+
: err;
|
|
85
94
|
|
|
86
95
|
// Work out required/optional parts based on config
|
|
87
96
|
const reqF = Object.create(null);
|
|
88
97
|
const reqC = cfg.requiredFields;
|
|
89
|
-
[
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
["address1", "address2", "address3", "address4", "postcode"].forEach(
|
|
99
|
+
(k) => {
|
|
100
|
+
// ESLint disabled as `k` is a known value from a constant list
|
|
101
|
+
/* eslint-disable-next-line security/detect-object-injection */
|
|
102
|
+
reqF[k] = reqC.indexOf(k) > -1;
|
|
103
|
+
},
|
|
104
|
+
);
|
|
94
105
|
|
|
95
106
|
let valid = true;
|
|
96
107
|
const errorMsgs = [];
|
|
97
108
|
|
|
98
|
-
if (typeof value ===
|
|
109
|
+
if (typeof value === "object") {
|
|
99
110
|
const reAddr = /^[^\s]+[a-z0-9\-,.&#()/\\:;'" ]+$/i;
|
|
100
111
|
const reAddrLine1 = /^\d+|[^\s]+[a-z0-9\-,.&#()/\\:;'" ]+$/i;
|
|
101
112
|
// UK Postcode regex taken from the dwp java pc checker
|
|
102
113
|
// https://github.com/dwp/postcode-format-validation
|
|
103
|
-
const rePostcode =
|
|
114
|
+
const rePostcode =
|
|
115
|
+
/^(?![QVX])[A-Z]((?![IJZ])[A-Z][0-9](([0-9]?)|([ABEHMNPRVWXY]?))|([0-9]([0-9]?|[ABCDEFGHJKPSTUW]?))) ?[0-9]((?![CIKMOV])[A-Z]){2}$|^(BFPO)[ ]?[0-9]{1,4}$/i;
|
|
104
116
|
|
|
105
117
|
// [required, regex, strlenmax, error message]
|
|
106
118
|
const attributes = {
|
|
107
|
-
address1: [
|
|
119
|
+
address1: [
|
|
120
|
+
reqF.address1,
|
|
121
|
+
reAddrLine1,
|
|
122
|
+
cfg.strlenmax,
|
|
123
|
+
cfg.errorMsgAddress1,
|
|
124
|
+
],
|
|
108
125
|
address2: [reqF.address2, reAddr, cfg.strlenmax, cfg.errorMsgAddress2],
|
|
109
126
|
address3: [reqF.address3, reAddr, cfg.strlenmax, cfg.errorMsgAddress3],
|
|
110
127
|
address4: [reqF.address4, reAddr, cfg.strlenmax, cfg.errorMsgAddress4],
|
|
@@ -117,16 +134,18 @@ export default class PostalAddressObject extends ValidatorFactory {
|
|
|
117
134
|
const hasProperty = Object.prototype.hasOwnProperty.call(value, k);
|
|
118
135
|
const hasContent = hasProperty && value[k].length > 0;
|
|
119
136
|
|
|
120
|
-
const condMissingOrRegexMismatch =
|
|
121
|
-
&& (!hasProperty || !value[k].match(attr[1]));
|
|
122
|
-
const condExceedStrlen =
|
|
123
|
-
&& String(value[k]).length > attr[2];
|
|
137
|
+
const condMissingOrRegexMismatch =
|
|
138
|
+
(attr[0] || hasContent) && (!hasProperty || !value[k].match(attr[1]));
|
|
139
|
+
const condExceedStrlen =
|
|
140
|
+
attr[2] > 0 && hasContent && String(value[k]).length > attr[2];
|
|
124
141
|
|
|
125
142
|
if (condMissingOrRegexMismatch || condExceedStrlen) {
|
|
126
143
|
valid = false;
|
|
127
|
-
errorMsgs.push(
|
|
128
|
-
|
|
129
|
-
|
|
144
|
+
errorMsgs.push(
|
|
145
|
+
Object.assign(Object.create(null), objectifyError(attr[3]), {
|
|
146
|
+
fieldKeySuffix: `[${k}]`,
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
130
149
|
}
|
|
131
150
|
});
|
|
132
151
|
/* eslint-enable security/detect-object-injection */
|
|
@@ -136,8 +155,9 @@ export default class PostalAddressObject extends ValidatorFactory {
|
|
|
136
155
|
}
|
|
137
156
|
|
|
138
157
|
// Build ValidationErrorGroup
|
|
139
|
-
const errorGroup = errorMsgs.map((err) =>
|
|
140
|
-
ValidationError.make({ errorMsg: err, dataContext })
|
|
158
|
+
const errorGroup = errorMsgs.map((err) =>
|
|
159
|
+
ValidationError.make({ errorMsg: err, dataContext }),
|
|
160
|
+
);
|
|
141
161
|
|
|
142
162
|
return valid ? [] : [...errorGroup];
|
|
143
163
|
}
|
|
@@ -149,13 +169,17 @@ export default class PostalAddressObject extends ValidatorFactory {
|
|
|
149
169
|
}
|
|
150
170
|
|
|
151
171
|
// Prune unrecognised attributes, and coerce to Strings
|
|
152
|
-
const validKeys = [
|
|
172
|
+
const validKeys = [
|
|
173
|
+
"address1",
|
|
174
|
+
"address2",
|
|
175
|
+
"address3",
|
|
176
|
+
"address4",
|
|
177
|
+
"postcode",
|
|
178
|
+
];
|
|
153
179
|
const pruned = Object.fromEntries(
|
|
154
|
-
Object.entries(value)
|
|
155
|
-
([k]) =>
|
|
156
|
-
|
|
157
|
-
([k, v]) => ([k, stringifyInput(v)]),
|
|
158
|
-
),
|
|
180
|
+
Object.entries(value)
|
|
181
|
+
.filter(([k]) => validKeys.includes(k))
|
|
182
|
+
.map(([k, v]) => [k, stringifyInput(v)]),
|
|
159
183
|
);
|
|
160
184
|
return Object.assign(Object.create(null), pruned);
|
|
161
185
|
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
2
|
|
|
3
|
-
import ValidatorFactory from
|
|
4
|
-
import ValidationError from
|
|
5
|
-
import { coerceInputToInteger } from
|
|
3
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
4
|
+
import ValidationError from "../ValidationError.js";
|
|
5
|
+
import { coerceInputToInteger } from "../utils.js";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
8
9
|
* @access private
|
|
9
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @typedef {object} RangeConfigOptions
|
|
14
|
-
* @property {ErrorMessageConfig} errorMsgMax Error message to use on max
|
|
15
|
-
*
|
|
14
|
+
* @property {ErrorMessageConfig} errorMsgMax Error message to use on max
|
|
15
|
+
* failure
|
|
16
|
+
* @property {ErrorMessageConfig} errorMsgMin Error message to use on min
|
|
17
|
+
* failure
|
|
16
18
|
* @property {number} max Maximum integer value
|
|
17
19
|
* @property {number} min Minimum integer value
|
|
18
20
|
*/
|
|
@@ -26,17 +28,17 @@ import { coerceInputToInteger } from '../utils.js';
|
|
|
26
28
|
* @augments ValidatorFactory
|
|
27
29
|
*/
|
|
28
30
|
export default class Range extends ValidatorFactory {
|
|
29
|
-
name =
|
|
31
|
+
name = "range";
|
|
30
32
|
|
|
31
33
|
validate(inputValue, dataContext = {}) {
|
|
32
34
|
const {
|
|
33
35
|
errorMsgMax = {
|
|
34
|
-
inline:
|
|
35
|
-
summary:
|
|
36
|
+
inline: "validation:rule.range.max.inline",
|
|
37
|
+
summary: "validation:rule.range.max.summary",
|
|
36
38
|
},
|
|
37
39
|
errorMsgMin = {
|
|
38
|
-
inline:
|
|
39
|
-
summary:
|
|
40
|
+
inline: "validation:rule.range.min.inline",
|
|
41
|
+
summary: "validation:rule.range.min.summary",
|
|
40
42
|
},
|
|
41
43
|
min = Number.MIN_VALUE,
|
|
42
44
|
max = Number.MAX_VALUE,
|
|
@@ -61,7 +63,7 @@ export default class Range extends ValidatorFactory {
|
|
|
61
63
|
sanitise(value) {
|
|
62
64
|
// treat an empty string as undefined
|
|
63
65
|
// when user submits empty form, it stores an empty string
|
|
64
|
-
if (value !==
|
|
66
|
+
if (value !== "" && value !== undefined) {
|
|
65
67
|
// add to custom validator docs to ensure not to return a falsy value as
|
|
66
68
|
// it doesn't show on screen
|
|
67
69
|
return coerceInputToInteger(value)?.toString();
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
-
import ValidatorFactory from
|
|
3
|
-
import ValidationError from
|
|
4
|
-
import { stringifyInput } from
|
|
2
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
3
|
+
import ValidationError from "../ValidationError.js";
|
|
4
|
+
import { stringifyInput } from "../utils.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
7
8
|
* @access private
|
|
8
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -24,16 +24,16 @@ import { stringifyInput } from '../utils.js';
|
|
|
24
24
|
* @augments ValidatorFactory
|
|
25
25
|
*/
|
|
26
26
|
export default class Regex extends ValidatorFactory {
|
|
27
|
-
name =
|
|
27
|
+
name = "regex";
|
|
28
28
|
|
|
29
|
-
validate(value =
|
|
29
|
+
validate(value = "", dataContext = {}) {
|
|
30
30
|
const invert = this.config.invert || false;
|
|
31
31
|
const match = value.match(this.config.pattern || /.*/);
|
|
32
32
|
const valid = invert ? !match : match;
|
|
33
33
|
|
|
34
34
|
const errorMsg = this.config.errorMsg || {
|
|
35
|
-
inline:
|
|
36
|
-
summary:
|
|
35
|
+
inline: "validation:rule.regex.inline",
|
|
36
|
+
summary: "validation:rule.regex.summary",
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
return valid ? [] : [ValidationError.make({ errorMsg, dataContext })];
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
-
import lodash from
|
|
3
|
-
import { isEmpty, isStringable, stringifyInput } from
|
|
4
|
-
import ValidatorFactory from
|
|
5
|
-
import ValidationError from
|
|
2
|
+
import lodash from "lodash";
|
|
3
|
+
import { isEmpty, isStringable, stringifyInput } from "../utils.js";
|
|
4
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
5
|
+
import ValidationError from "../ValidationError.js";
|
|
6
6
|
|
|
7
7
|
const { isPlainObject } = lodash; // CommonJS
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
10
11
|
* @access private
|
|
11
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -19,8 +19,8 @@ const { isPlainObject } = lodash; // CommonJS
|
|
|
19
19
|
/**
|
|
20
20
|
* Test if value is present.
|
|
21
21
|
*
|
|
22
|
-
* Value is required. The following values will fail this rule:
|
|
23
|
-
*
|
|
22
|
+
* Value is required. The following values will fail this rule: (all values that
|
|
23
|
+
* satisfy `isEmpty()`) plus '\s'
|
|
24
24
|
*
|
|
25
25
|
* See {@link RequiredConfigOptions} for `make()` options.
|
|
26
26
|
*
|
|
@@ -28,29 +28,27 @@ const { isPlainObject } = lodash; // CommonJS
|
|
|
28
28
|
* @augments ValidatorFactory
|
|
29
29
|
*/
|
|
30
30
|
export default class Required extends ValidatorFactory {
|
|
31
|
-
name =
|
|
31
|
+
name = "required";
|
|
32
32
|
|
|
33
33
|
validate(value, dataContext = {}) {
|
|
34
34
|
const {
|
|
35
35
|
errorMsg = {
|
|
36
|
-
inline:
|
|
37
|
-
summary:
|
|
36
|
+
inline: "validation:rule.required.inline",
|
|
37
|
+
summary: "validation:rule.required.summary",
|
|
38
38
|
},
|
|
39
39
|
} = this.config;
|
|
40
40
|
|
|
41
41
|
if (!isEmpty(value)) {
|
|
42
|
-
return []
|
|
42
|
+
return [];
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
return [
|
|
46
|
-
ValidationError.make({ errorMsg, dataContext }),
|
|
47
|
-
];
|
|
45
|
+
return [ValidationError.make({ errorMsg, dataContext })];
|
|
48
46
|
}
|
|
49
47
|
|
|
50
48
|
sanitise(value) {
|
|
51
49
|
const coerce = (val) => {
|
|
52
50
|
const s = stringifyInput(val, undefined);
|
|
53
|
-
return s === undefined ? undefined : s.replace(/^\s+$/,
|
|
51
|
+
return s === undefined ? undefined : s.replace(/^\s+$/, "");
|
|
54
52
|
};
|
|
55
53
|
|
|
56
54
|
if (isStringable(value)) {
|
|
@@ -66,7 +64,9 @@ export default class Required extends ValidatorFactory {
|
|
|
66
64
|
// Coerce all elements to Strings.
|
|
67
65
|
// This only supports a one dimensional object, with stringable elements.
|
|
68
66
|
if (isPlainObject(value)) {
|
|
69
|
-
return Object.fromEntries(
|
|
67
|
+
return Object.fromEntries(
|
|
68
|
+
Object.entries(value).map(([k, v]) => [k, coerce(v)]),
|
|
69
|
+
);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
return undefined;
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
-
import ValidatorFactory from
|
|
3
|
-
import ValidationError from
|
|
4
|
-
import { stringifyInput } from
|
|
2
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
3
|
+
import ValidationError from "../ValidationError.js";
|
|
4
|
+
import { stringifyInput } from "../utils.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
7
8
|
* @access private
|
|
8
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @typedef {object} StrlenConfigOptions
|
|
13
|
-
* @property {ErrorMessageConfig} errorMsgMax Error message to use on max length
|
|
14
|
-
*
|
|
13
|
+
* @property {ErrorMessageConfig} errorMsgMax Error message to use on max length
|
|
14
|
+
* failure
|
|
15
|
+
* @property {ErrorMessageConfig} errorMsgMin Error message to use on min length
|
|
16
|
+
* failure
|
|
15
17
|
* @property {number} max Maximum string length allowed
|
|
16
18
|
* @property {number} min Minimum string length required
|
|
17
19
|
*/
|
|
@@ -25,17 +27,17 @@ import { stringifyInput } from '../utils.js';
|
|
|
25
27
|
* @augments ValidatorFactory
|
|
26
28
|
*/
|
|
27
29
|
export default class Strlen extends ValidatorFactory {
|
|
28
|
-
name =
|
|
30
|
+
name = "strlen";
|
|
29
31
|
|
|
30
|
-
validate(inputValue =
|
|
32
|
+
validate(inputValue = "", dataContext = {}) {
|
|
31
33
|
const {
|
|
32
34
|
errorMsgMax = {
|
|
33
|
-
inline:
|
|
34
|
-
summary:
|
|
35
|
+
inline: "validation:rule.strlen.max.inline",
|
|
36
|
+
summary: "validation:rule.strlen.max.summary",
|
|
35
37
|
},
|
|
36
38
|
errorMsgMin = {
|
|
37
|
-
inline:
|
|
38
|
-
summary:
|
|
39
|
+
inline: "validation:rule.strlen.min.inline",
|
|
40
|
+
summary: "validation:rule.strlen.min.summary",
|
|
39
41
|
},
|
|
40
42
|
min,
|
|
41
43
|
max,
|
|
@@ -44,12 +46,12 @@ export default class Strlen extends ValidatorFactory {
|
|
|
44
46
|
let errorMsg;
|
|
45
47
|
let valid = true;
|
|
46
48
|
|
|
47
|
-
if (typeof max !==
|
|
49
|
+
if (typeof max !== "undefined" && inputValue.length > max) {
|
|
48
50
|
valid = false;
|
|
49
51
|
errorMsg = errorMsgMax;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
if (typeof min !==
|
|
54
|
+
if (typeof min !== "undefined" && inputValue.length < min) {
|
|
53
55
|
valid = false;
|
|
54
56
|
errorMsg = errorMsgMin;
|
|
55
57
|
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
-
import ValidatorFactory from
|
|
3
|
-
import ValidationError from
|
|
4
|
-
import { stringifyInput } from
|
|
2
|
+
import ValidatorFactory from "../ValidatorFactory.js";
|
|
3
|
+
import ValidationError from "../ValidationError.js";
|
|
4
|
+
import { stringifyInput } from "../utils.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
+
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
|
|
7
8
|
* @access private
|
|
8
|
-
* @typedef {import('../../casa').ErrorMessageConfig} ErrorMessageConfig
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @typedef {object} WordcountConfigOptions
|
|
13
|
-
* @property {ErrorMessageConfig} errorMsgMax Error message to use on max length
|
|
14
|
-
*
|
|
13
|
+
* @property {ErrorMessageConfig} errorMsgMax Error message to use on max length
|
|
14
|
+
* failure
|
|
15
|
+
* @property {ErrorMessageConfig} errorMsgMin Error message to use on min length
|
|
16
|
+
* failure
|
|
15
17
|
* @property {number} max Maximum string length allowed
|
|
16
18
|
* @property {number} min Minimum string length required
|
|
17
19
|
*/
|
|
@@ -25,21 +27,21 @@ import { stringifyInput } from '../utils.js';
|
|
|
25
27
|
* @augments ValidatorFactory
|
|
26
28
|
*/
|
|
27
29
|
export default class WordCount extends ValidatorFactory {
|
|
28
|
-
name =
|
|
30
|
+
name = "wordCount";
|
|
29
31
|
|
|
30
32
|
count(input) {
|
|
31
33
|
return (input.match(/\S+/g) || []).length;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
validate(inputValue =
|
|
36
|
+
validate(inputValue = "", dataContext = {}) {
|
|
35
37
|
const {
|
|
36
38
|
errorMsgMax = {
|
|
37
|
-
inline:
|
|
38
|
-
summary:
|
|
39
|
+
inline: "validation:rule.wordCount.max.inline",
|
|
40
|
+
summary: "validation:rule.wordCount.max.summary",
|
|
39
41
|
},
|
|
40
42
|
errorMsgMin = {
|
|
41
|
-
inline:
|
|
42
|
-
summary:
|
|
43
|
+
inline: "validation:rule.wordCount.min.inline",
|
|
44
|
+
summary: "validation:rule.wordCount.min.summary",
|
|
43
45
|
},
|
|
44
46
|
min,
|
|
45
47
|
max,
|
|
@@ -48,12 +50,18 @@ export default class WordCount extends ValidatorFactory {
|
|
|
48
50
|
let errorMsg;
|
|
49
51
|
let valid = true;
|
|
50
52
|
|
|
51
|
-
if (
|
|
53
|
+
if (
|
|
54
|
+
typeof max !== "undefined" &&
|
|
55
|
+
(inputValue.match(/\S+/g) || []).length > max
|
|
56
|
+
) {
|
|
52
57
|
valid = false;
|
|
53
58
|
errorMsg = errorMsgMax;
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
if (
|
|
61
|
+
if (
|
|
62
|
+
typeof min !== "undefined" &&
|
|
63
|
+
(inputValue.match(/\S+/g) || []).length < min
|
|
64
|
+
) {
|
|
57
65
|
valid = false;
|
|
58
66
|
errorMsg = errorMsgMin;
|
|
59
67
|
}
|