@planning-inspectorate/dynamic-forms 2.0.3 → 2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@planning-inspectorate/dynamic-forms",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "This package is for building GDS forms using a configuration-based approach.",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
@@ -2,7 +2,7 @@ import { body } from 'express-validator';
2
2
 
3
3
  import BaseValidator from './base-validator.js';
4
4
 
5
- const validatePostcode = (postcode, errorMessage = 'Enter a valid postcode') => {
5
+ export const validatePostcode = (postcode, errorMessage = 'Enter a valid postcode') => {
6
6
  const pattern =
7
7
  /([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})/;
8
8
  const result = pattern.exec(postcode);
@@ -21,7 +21,7 @@ export const townCityMaxLength = 250;
21
21
  export const townCityMinLength = 0;
22
22
  export const countyMaxLength = 250;
23
23
  export const countyMinLength = 0;
24
- export const postcodeMaxLength = 7;
24
+ export const postcodeMaxLength = 8;
25
25
  export const postcodeMinLength = 5;
26
26
 
27
27
  /**
@@ -113,6 +113,86 @@ describe('AddressValidator', () => {
113
113
  `Postcode must be between ${postcodeMinLength} and ${postcodeMaxLength} characters`
114
114
  );
115
115
  });
116
+ it('should validate a valid 8-character postcode without errors', async () => {
117
+ const question = {
118
+ fieldName: 'testField'
119
+ };
120
+
121
+ const req = {
122
+ body: {
123
+ testField_addressLine1: 'A Building',
124
+ testField_addressLine2: 'A Street',
125
+ testField_townCity: 'Test town',
126
+ testField_postcode: 'DN55 1PT'
127
+ }
128
+ };
129
+
130
+ const errors = await _validationMappedErrors(req, question);
131
+
132
+ assert.deepStrictEqual(errors, {});
133
+ });
134
+ // New postcode tests
135
+ it('should validate a valid 5-character postcode without a space', async () => {
136
+ const question = { fieldName: 'testField' };
137
+ const req = {
138
+ body: {
139
+ testField_addressLine1: 'A Building',
140
+ testField_addressLine2: 'A Street',
141
+ testField_townCity: 'Test town',
142
+ testField_postcode: 'M11AA'
143
+ }
144
+ };
145
+ const errors = await _validationMappedErrors(req, question);
146
+ assert.deepStrictEqual(errors, {});
147
+ });
148
+
149
+ it('should reject a postcode with multiple spaces', async () => {
150
+ const question = { fieldName: 'testField' };
151
+ const req = {
152
+ body: {
153
+ testField_addressLine1: 'A Building',
154
+ testField_addressLine2: 'A Street',
155
+ testField_townCity: 'Test town',
156
+ testField_postcode: 'AB1 2CD'
157
+ }
158
+ };
159
+ const errors = await _validationMappedErrors(req, question);
160
+ assert.strictEqual(errors.testField_postcode.msg, 'Enter a valid postcode');
161
+ });
162
+
163
+ it('should reject a postcode shorter than 5 characters', async () => {
164
+ const question = { fieldName: 'testField' };
165
+ const req = {
166
+ body: {
167
+ testField_addressLine1: 'A Building',
168
+ testField_addressLine2: 'A Street',
169
+ testField_townCity: 'Test town',
170
+ testField_postcode: 'A1 1'
171
+ }
172
+ };
173
+ const errors = await _validationMappedErrors(req, question);
174
+ assert.strictEqual(
175
+ errors.testField_postcode.msg,
176
+ `Postcode must be between ${postcodeMinLength} and ${postcodeMaxLength} characters`
177
+ );
178
+ });
179
+
180
+ it('should reject a postcode longer than 8 characters', async () => {
181
+ const question = { fieldName: 'testField' };
182
+ const req = {
183
+ body: {
184
+ testField_addressLine1: 'A Building',
185
+ testField_addressLine2: 'A Street',
186
+ testField_townCity: 'Test town',
187
+ testField_postcode: 'DN55 1PTA'
188
+ }
189
+ };
190
+ const errors = await _validationMappedErrors(req, question);
191
+ assert.strictEqual(
192
+ errors.testField_postcode.msg,
193
+ `Postcode must be between ${postcodeMinLength} and ${postcodeMaxLength} characters`
194
+ );
195
+ });
116
196
  });
117
197
 
118
198
  const _validationMappedErrors = async (req, question) => {