@dwp/govuk-casa 8.2.8 → 8.3.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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [8.3.0](https://github.com/dwp/govuk-casa/compare/8.2.8...8.3.0) (2022-06-30)
6
+
7
+
8
+ ### Features
9
+
10
+ * add support for complex field names ([a380b01](https://github.com/dwp/govuk-casa/commit/a380b01ce22b8b7957ad4950e4189cc404b5d9c5))
11
+
5
12
  ### [8.2.8](https://github.com/dwp/govuk-casa/compare/8.2.7...8.2.8) (2022-06-29)
6
13
 
7
14
  ### [8.2.7](https://github.com/dwp/govuk-casa/compare/8.2.6...8.2.7) (2022-06-15)
@@ -39,20 +39,29 @@ export class PageField {
39
39
  *
40
40
  * @param {object} obj Object from which to extract the value
41
41
  * @returns {any} Value extracted from object
42
- * @throws {Error} When run on a complex field (not yet supported)
43
42
  */
44
43
  getValue(obj?: object): any;
45
44
  /**
46
45
  * Store this field's value in the given object, using its name as the key.
47
46
  *
47
+ * For complex fields, the field object will be created if it does not yet
48
+ * exist, before then storing the property within that object.
49
+ *
48
50
  * @param {object} obj Object from which to extract the value
49
51
  * @param {any} value Value to be stored
50
52
  * @returns {any} Value extracted from object
51
- * @throws {Error} When run on a complex field (not yet supported)
52
53
  */
53
54
  putValue(obj?: object, value?: any): any;
54
55
  get name(): string;
55
56
  get meta(): object;
57
+ /**
58
+ * Rename this field.
59
+ *
60
+ * @param {string} name New name to be applied
61
+ * @returns {PageField} Chain
62
+ * @throws {SyntaxError} When the name is invalid in some way
63
+ */
64
+ rename(name: string): PageField;
56
65
  /**
57
66
  * Add/get value validators
58
67
  * Some validators will include a `sanitise()` method which will be run at the
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,19 @@ 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);
101
102
  }
102
103
  /**
103
104
  * Extract this field's value from the given object.
@@ -107,29 +108,35 @@ class PageField {
107
108
  *
108
109
  * @param {object} obj Object from which to extract the value
109
110
  * @returns {any} Value extracted from object
110
- * @throws {Error} When run on a complex field (not yet supported)
111
111
  */
112
112
  getValue(obj = Object.create(null)) {
113
- if (!__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
114
- return obj[__classPrivateFieldGet(this, _PageField_name, "f")];
113
+ var _a;
114
+ if (__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
115
+ return (_a = obj[__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldName]) === null || _a === void 0 ? void 0 : _a[__classPrivateFieldGet(this, _PageField_meta, "f").complexFieldProperty];
115
116
  }
116
- throw new Error('Not yet supporting complex field types');
117
+ return obj[__classPrivateFieldGet(this, _PageField_name, "f")];
117
118
  }
118
119
  /**
119
120
  * Store this field's value in the given object, using its name as the key.
120
121
  *
122
+ * For complex fields, the field object will be created if it does not yet
123
+ * exist, before then storing the property within that object.
124
+ *
121
125
  * @param {object} obj Object from which to extract the value
122
126
  * @param {any} value Value to be stored
123
127
  * @returns {any} Value extracted from object
124
- * @throws {Error} When run on a complex field (not yet supported)
125
128
  */
126
129
  putValue(obj = Object.create(null), value = undefined) {
127
- if (!__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
130
+ var _a;
131
+ if (__classPrivateFieldGet(this, _PageField_meta, "f").complex) {
132
+ /* eslint-disable-next-line no-param-reassign */
133
+ 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 });
134
+ }
135
+ else {
128
136
  /* eslint-disable-next-line no-param-reassign */
129
137
  obj[__classPrivateFieldGet(this, _PageField_name, "f")] = value;
130
- return this;
131
138
  }
132
- throw new Error('Not yet supporting complex field types');
139
+ return this;
133
140
  }
134
141
  /* -------------------------------------------------------------- configure */
135
142
  get name() {
@@ -138,6 +145,33 @@ class PageField {
138
145
  get meta() {
139
146
  return __classPrivateFieldGet(this, _PageField_meta, "f");
140
147
  }
148
+ /**
149
+ * Rename this field.
150
+ *
151
+ * @param {string} name New name to be applied
152
+ * @returns {PageField} Chain
153
+ * @throws {SyntaxError} When the name is invalid in some way
154
+ */
155
+ rename(name) {
156
+ if (reInvalidName.test(String(name))) {
157
+ throw new SyntaxError(`Field '${String(name)}' name contains invalid characters.`);
158
+ }
159
+ // Complex names are only supported to one level deep. For example,
160
+ // `field[prop]` is supported, whilst `field[prop][subprop]` is not. Throw
161
+ // early to aid developer.
162
+ const isComplex = reComplexType.test(name);
163
+ if (isComplex && name.match(/\[/g).length > 1) {
164
+ throw new SyntaxError('Complex field names are only supported to 1 property depth. E.g. a[b] is ok, a[b][c] is not');
165
+ }
166
+ __classPrivateFieldSet(this, _PageField_name, String(name), "f");
167
+ __classPrivateFieldGet(this, _PageField_meta, "f").complex = isComplex;
168
+ // Extract the field name and property from a complex type for later use
169
+ if (isComplex) {
170
+ const parts = name.match(reComplexType);
171
+ [, __classPrivateFieldGet(this, _PageField_meta, "f").complexFieldName, __classPrivateFieldGet(this, _PageField_meta, "f").complexFieldProperty] = parts;
172
+ }
173
+ return this;
174
+ }
141
175
  /**
142
176
  * Add/get value validators
143
177
  * Some validators will include a `sanitise()` method which will be run at the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwp/govuk-casa",
3
- "version": "8.2.8",
3
+ "version": "8.3.0",
4
4
  "description": "A framework for building GOVUK Collect-And-Submit-Applications",
5
5
  "repository": {
6
6
  "type": "git",