@dwp/govuk-casa 8.2.7 → 8.4.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/dist/lib/field.js CHANGED
@@ -45,7 +45,7 @@ const { isFunction } = lodash_1.default;
45
45
  */
46
46
  // Quick check to see if the field name corresponds to a non-primitive complex
47
47
  // type. For example, `my_field[nested]`.
48
- const reComplexType = /\[/;
48
+ const reComplexType = /^([^[]+)\[([^\]]+)\]/;
49
49
  const reInvalidName = /[^a-z0-9_.\-[\]]/i;
50
50
  /**
51
51
  * This class is not exposed via the public API. Instances should instead be
@@ -86,18 +86,40 @@ class PageField {
86
86
  if (!name) {
87
87
  throw new SyntaxError('A name for this field is required, i.e. "field(\'myField\')".');
88
88
  }
89
- else if (reInvalidName.test(String(name))) {
90
- throw new SyntaxError(`Field '${String(name)}' name contains invalid characters.`);
91
- }
92
- __classPrivateFieldSet(this, _PageField_name, String(name), "f");
89
+ __classPrivateFieldSet(this, _PageField_name, undefined, "f");
93
90
  __classPrivateFieldSet(this, _PageField_validators, [], "f");
94
91
  __classPrivateFieldSet(this, _PageField_processors, [], "f");
95
92
  __classPrivateFieldSet(this, _PageField_conditions, [], "f");
96
93
  __classPrivateFieldSet(this, _PageField_meta, {
97
94
  optional,
98
95
  persist,
99
- complex: reComplexType.test(name),
96
+ complex: undefined,
97
+ complexFieldName: undefined,
98
+ complexFieldProperty: undefined,
100
99
  }, "f");
100
+ // Apply name
101
+ this.rename(name);
102
+ }
103
+ /**
104
+ * Clone this field.
105
+ *
106
+ * @returns {PageField} Cloned field
107
+ */
108
+ clone() {
109
+ const clone = new PageField(__classPrivateFieldGet(this, _PageField_name, "f"), {
110
+ optional: __classPrivateFieldGet(this, _PageField_meta, "f").optional,
111
+ persist: __classPrivateFieldGet(this, _PageField_meta, "f").persist,
112
+ });
113
+ if (this.validators()) {
114
+ clone.validators(this.validators());
115
+ }
116
+ if (this.conditions()) {
117
+ clone.conditions(this.conditions());
118
+ }
119
+ if (this.processors()) {
120
+ clone.processors(this.processors());
121
+ }
122
+ return clone;
101
123
  }
102
124
  /**
103
125
  * Extract this field's value from the given object.
@@ -107,29 +129,35 @@ class PageField {
107
129
  *
108
130
  * @param {object} obj Object from which to extract the value
109
131
  * @returns {any} Value extracted from object
110
- * @throws {Error} When run on a complex field (not yet supported)
111
132
  */
112
133
  getValue(obj = Object.create(null)) {
113
- if (!__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
114
- return obj[__classPrivateFieldGet(this, _PageField_name, "f")];
134
+ var _a;
135
+ if (__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
136
+ return (_a = obj[__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldName]) === null || _a === void 0 ? void 0 : _a[__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldProperty];
115
137
  }
116
- throw new Error('Not yet supporting complex field types');
138
+ return obj[__classPrivateFieldGet(this, _PageField_name, "f")];
117
139
  }
118
140
  /**
119
141
  * Store this field's value in the given object, using its name as the key.
120
142
  *
143
+ * For complex fields, the field object will be created if it does not yet
144
+ * exist, before then storing the property within that object.
145
+ *
121
146
  * @param {object} obj Object from which to extract the value
122
147
  * @param {any} value Value to be stored
123
148
  * @returns {any} Value extracted from object
124
- * @throws {Error} When run on a complex field (not yet supported)
125
149
  */
126
150
  putValue(obj = Object.create(null), value = undefined) {
127
- if (!__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
151
+ var _a;
152
+ if (__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
153
+ /* eslint-disable-next-line no-param-reassign */
154
+ obj[__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldName] = Object.assign(Object.assign({}, ((_a = obj[__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldName]) !== null && _a !== void 0 ? _a : {})), { [__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldProperty]: value });
155
+ }
156
+ else {
128
157
  /* eslint-disable-next-line no-param-reassign */
129
158
  obj[__classPrivateFieldGet(this, _PageField_name, "f")] = value;
130
- return this;
131
159
  }
132
- throw new Error('Not yet supporting complex field types');
160
+ return this;
133
161
  }
134
162
  /* -------------------------------------------------------------- configure */
135
163
  get name() {
@@ -138,6 +166,33 @@ class PageField {
138
166
  get meta() {
139
167
  return __classPrivateFieldGet(this, _PageField_meta, "f");
140
168
  }
169
+ /**
170
+ * Rename this field.
171
+ *
172
+ * @param {string} name New name to be applied
173
+ * @returns {PageField} Chain
174
+ * @throws {SyntaxError} When the name is invalid in some way
175
+ */
176
+ rename(name) {
177
+ if (reInvalidName.test(String(name))) {
178
+ throw new SyntaxError(`Field '${String(name)}' name contains invalid characters.`);
179
+ }
180
+ // Complex names are only supported to one level deep. For example,
181
+ // `field[prop]` is supported, whilst `field[prop][subprop]` is not. Throw
182
+ // early to aid developer.
183
+ const isComplex = reComplexType.test(name);
184
+ if (isComplex && name.match(/\[/g).length > 1) {
185
+ throw new SyntaxError('Complex field names are only supported to 1 property depth. E.g. a[b] is ok, a[b][c] is not');
186
+ }
187
+ __classPrivateFieldSet(this, _PageField_name, String(name), "f");
188
+ __classPrivateFieldGet(this, _PageField_meta, "f").complex = isComplex;
189
+ // Extract the field name and property from a complex type for later use
190
+ if (isComplex) {
191
+ const parts = name.match(reComplexType);
192
+ [, __classPrivateFieldGet(this, _PageField_meta, "f").complexFieldName, __classPrivateFieldGet(this, _PageField_meta, "f").complexFieldProperty] = parts;
193
+ }
194
+ return this;
195
+ }
141
196
  /**
142
197
  * Add/get value validators
143
198
  * Some validators will include a `sanitise()` method which will be run at the
@@ -1,6 +1,15 @@
1
1
  /**
2
2
  * Generate a URL pointing at a particular waypoint.
3
3
  *
4
+ * @example
5
+ * // generates: /path/details?edit&editorigin=%2Fsomewhere%2Felse
6
+ * waypointUrl({
7
+ * mountUrl: '/path/',
8
+ * waypoint: 'details',
9
+ * edit: true,
10
+ * editOrigin: '/somewhere/else'
11
+ * })
12
+ *
4
13
  * @memberof module:@dwp/govuk-casa
5
14
  * @param {object} obj Options
6
15
  * @param {string} obj.waypoint Waypoint
@@ -17,6 +17,15 @@ const sanitiseWaypoint = (w) => w.replace(/[^/a-z0-9_-]/ig, '').replace(/\/+/g,
17
17
  /**
18
18
  * Generate a URL pointing at a particular waypoint.
19
19
  *
20
+ * @example
21
+ * // generates: /path/details?edit&editorigin=%2Fsomewhere%2Felse
22
+ * waypointUrl({
23
+ * mountUrl: '/path/',
24
+ * waypoint: 'details',
25
+ * edit: true,
26
+ * editOrigin: '/somewhere/else'
27
+ * })
28
+ *
20
29
  * @memberof module:@dwp/govuk-casa
21
30
  * @param {object} obj Options
22
31
  * @param {string} obj.waypoint Waypoint
@@ -57,7 +57,13 @@ exports.default = ({ waypoint, plan, }) => [
57
57
  startWaypoint: waypoint,
58
58
  stopCondition: () => (true), // stop at the first one
59
59
  });
60
- res.locals.casa.journeyPreviousUrl = prevRoute.target ? (0, waypoint_url_js_1.default)({ mountUrl, waypoint: prevRoute.target, routeName: 'prev' }) : undefined;
60
+ res.locals.casa.journeyPreviousUrl = prevRoute.target ? (0, waypoint_url_js_1.default)({
61
+ mountUrl,
62
+ waypoint: prevRoute.target,
63
+ routeName: 'prev',
64
+ edit: req.casa.editMode,
65
+ editOrigin: req.casa.editOrigin,
66
+ }) : undefined;
61
67
  return next();
62
68
  },
63
69
  ];
@@ -2,7 +2,17 @@
2
2
  "rule": {
3
3
  "dateObject": {
4
4
  "inline": "Rhowch ddyddiad dilys",
5
- "summary": "Rhowch ddyddiad dilys"
5
+ "summary": "Rhowch ddyddiad dilys",
6
+
7
+ "afterOffset": {
8
+ "inline": "Rhowch ddyddiad o fewn yr ystod benodol",
9
+ "summary": "Rhowch ddyddiad o fewn yr ystod benodol"
10
+ },
11
+
12
+ "beforeOffset": {
13
+ "inline": "Rhowch ddyddiad o fewn yr ystod benodol",
14
+ "summary": "Rhowch ddyddiad o fewn yr ystod benodol"
15
+ }
6
16
  },
7
17
 
8
18
  "email": {
@@ -2,7 +2,17 @@
2
2
  "rule": {
3
3
  "dateObject": {
4
4
  "inline": "Enter a valid date",
5
- "summary": "Enter a valid date"
5
+ "summary": "Enter a valid date",
6
+
7
+ "afterOffset": {
8
+ "inline": "Enter a date within the given range",
9
+ "summary": "Enter a date within the given range"
10
+ },
11
+
12
+ "beforeOffset": {
13
+ "inline": "Enter a date within the given range",
14
+ "summary": "Enter a date within the given range"
15
+ }
6
16
  },
7
17
 
8
18
  "email": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwp/govuk-casa",
3
- "version": "8.2.7",
3
+ "version": "8.4.0",
4
4
  "description": "A framework for building GOVUK Collect-And-Submit-Applications",
5
5
  "repository": {
6
6
  "type": "git",
@@ -48,52 +48,52 @@
48
48
  "deepmerge": "4.2.2",
49
49
  "express": "4.18.1",
50
50
  "express-session": "1.17.3",
51
- "govuk-frontend": "4.0.1",
51
+ "govuk-frontend": "4.2.0",
52
52
  "graphlib": "2.1.8",
53
53
  "helmet": "5.1.0",
54
- "i18next": "21.8.8",
54
+ "i18next": "21.8.14",
55
55
  "i18next-http-middleware": "3.2.1",
56
56
  "js-yaml": "4.1.0",
57
57
  "lodash": "4.17.21",
58
- "luxon": "2.4.0",
58
+ "luxon": "2.5.0",
59
59
  "nunjucks": "3.2.3",
60
60
  "path-to-regexp": "6.2.1",
61
61
  "uuid": "8.3.2",
62
62
  "validator": "13.7.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@babel/core": "7.18.2",
66
- "@babel/eslint-parser": "7.18.2",
67
- "@babel/preset-env": "7.18.2",
68
- "@ckeditor/jsdoc-plugins": "30.2.0",
69
- "@commitlint/config-conventional": "17.0.2",
70
- "@dwp/casa-spiderplan": "2.4.0",
65
+ "@babel/core": "7.18.9",
66
+ "@babel/eslint-parser": "7.18.9",
67
+ "@babel/preset-env": "7.18.9",
68
+ "@ckeditor/jsdoc-plugins": "30.3.2",
69
+ "@commitlint/config-conventional": "17.0.3",
70
+ "@dwp/casa-spiderplan": "2.4.1",
71
71
  "@dwp/casa-spiderplan-a11y-plugin": "0.1.4",
72
72
  "@dwp/casa-spiderplan-zap-plugin": "0.1.1",
73
73
  "@dwp/eslint-config-base": "6.0.0",
74
74
  "@types/express": "4.17.13",
75
- "@types/node": "17.0.40",
75
+ "@types/node": "18.0.0",
76
76
  "@types/nunjucks": "3.2.1",
77
77
  "babel-eslint": "10.1.0",
78
78
  "c8": "7.11.3",
79
79
  "chai": "4.3.6",
80
- "cheerio": "1.0.0-rc.11",
81
- "commitlint": "17.0.2",
80
+ "cheerio": "1.0.0-rc.12",
81
+ "commitlint": "17.0.3",
82
82
  "docdash": "1.2.0",
83
- "eslint": "8.17.0",
83
+ "eslint": "8.20.0",
84
84
  "eslint-plugin-no-unsafe-regex": "1.0.0",
85
85
  "eslint-plugin-security": "1.5.0",
86
86
  "eslint-plugin-sonarjs": "0.13.0",
87
- "fast-check": "3.0.0",
87
+ "fast-check": "3.0.1",
88
88
  "husky": "8.0.1",
89
89
  "jsdoc": "3.6.10",
90
90
  "jsdoc-tsimport-plugin": "1.0.5",
91
91
  "mocha": "10.0.0",
92
- "sass": "1.52.2",
92
+ "sass": "1.53.0",
93
93
  "sinon": "14.0.0",
94
94
  "sinon-chai": "3.7.0",
95
95
  "standard-version": "9.5.0",
96
- "supertest": "6.2.3",
97
- "typescript": "4.7.3"
96
+ "supertest": "6.2.4",
97
+ "typescript": "4.7.4"
98
98
  }
99
99
  }