@dwp/govuk-casa 7.0.6 → 8.0.0-alpha1
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 +22 -17
- package/dist/{casa → assets}/css/casa-ie8.css +1 -1
- package/dist/assets/css/casa.css +1 -0
- package/dist/casa.d.ts +10 -0
- package/dist/casa.js +44 -0
- package/dist/lib/CasaTemplateLoader.d.ts +19 -0
- package/dist/lib/CasaTemplateLoader.js +57 -0
- package/dist/lib/JourneyContext.d.ts +255 -0
- package/dist/lib/JourneyContext.js +681 -0
- package/dist/lib/MutableRouter.d.ts +155 -0
- package/dist/lib/MutableRouter.js +272 -0
- package/dist/lib/Plan.d.ts +119 -0
- package/dist/lib/Plan.js +382 -0
- package/dist/lib/ValidationError.d.ts +70 -0
- package/dist/lib/ValidationError.js +156 -0
- package/dist/lib/ValidatorFactory.d.ts +24 -0
- package/dist/lib/ValidatorFactory.js +87 -0
- package/dist/lib/configure.d.ts +205 -0
- package/dist/lib/configure.js +215 -0
- package/dist/lib/dirname.cjs +1 -0
- package/dist/lib/end-session.d.ts +12 -0
- package/dist/lib/end-session.js +24 -0
- package/dist/lib/field.d.ts +79 -0
- package/dist/lib/field.js +223 -0
- package/dist/lib/logger.d.ts +8 -0
- package/dist/lib/logger.js +19 -0
- package/dist/lib/nunjucks-filters.d.ts +26 -0
- package/dist/lib/nunjucks-filters.js +112 -0
- package/dist/lib/nunjucks.d.ts +22 -0
- package/dist/lib/nunjucks.js +49 -0
- package/dist/lib/utils.d.ts +22 -0
- package/dist/lib/utils.js +44 -0
- package/dist/lib/validators/dateObject.d.ts +4 -0
- package/dist/lib/validators/dateObject.js +135 -0
- package/dist/lib/validators/email.d.ts +4 -0
- package/dist/lib/validators/email.js +46 -0
- package/dist/lib/validators/inArray.d.ts +4 -0
- package/dist/lib/validators/inArray.js +60 -0
- package/dist/lib/validators/index.d.ts +21 -0
- package/dist/lib/validators/index.js +47 -0
- package/dist/lib/validators/nino.d.ts +4 -0
- package/dist/lib/validators/nino.js +46 -0
- package/dist/lib/validators/postalAddressObject.d.ts +4 -0
- package/dist/lib/validators/postalAddressObject.js +123 -0
- package/dist/lib/validators/regex.d.ts +4 -0
- package/dist/lib/validators/regex.js +40 -0
- package/dist/lib/validators/required.d.ts +4 -0
- package/dist/lib/validators/required.js +56 -0
- package/dist/lib/validators/strlen.d.ts +4 -0
- package/dist/lib/validators/strlen.js +51 -0
- package/dist/lib/validators/wordCount.d.ts +5 -0
- package/dist/lib/validators/wordCount.js +54 -0
- package/dist/lib/waypoint-url.d.ts +17 -0
- package/dist/lib/waypoint-url.js +46 -0
- package/dist/middleware/body-parser.d.ts +1 -0
- package/dist/middleware/body-parser.js +24 -0
- package/dist/middleware/csrf.d.ts +1 -0
- package/dist/middleware/csrf.js +31 -0
- package/dist/middleware/data.d.ts +6 -0
- package/dist/middleware/data.js +53 -0
- package/dist/middleware/dirname.cjs +1 -0
- package/dist/middleware/gather-fields.d.ts +5 -0
- package/dist/middleware/gather-fields.js +39 -0
- package/dist/middleware/i18n.d.ts +4 -0
- package/dist/middleware/i18n.js +87 -0
- package/dist/middleware/post.d.ts +1 -0
- package/dist/middleware/post.js +42 -0
- package/dist/middleware/pre.d.ts +3 -0
- package/dist/middleware/pre.js +38 -0
- package/dist/middleware/progress-journey.d.ts +6 -0
- package/dist/middleware/progress-journey.js +82 -0
- package/dist/middleware/sanitise-fields.d.ts +5 -0
- package/dist/middleware/sanitise-fields.js +48 -0
- package/dist/middleware/session.d.ts +10 -0
- package/dist/middleware/session.js +115 -0
- package/dist/middleware/skip-waypoint.d.ts +5 -0
- package/dist/middleware/skip-waypoint.js +40 -0
- package/dist/middleware/steer-journey.d.ts +6 -0
- package/dist/middleware/steer-journey.js +44 -0
- package/dist/middleware/validate-fields.d.ts +7 -0
- package/dist/middleware/validate-fields.js +76 -0
- package/dist/mjs/esm-wrapper.js +10 -0
- package/dist/mjs/package.json +3 -0
- package/dist/package.json +3 -0
- package/dist/routes/ancillary.d.ts +4 -0
- package/dist/routes/ancillary.js +19 -0
- package/dist/routes/dirname.cjs +1 -0
- package/dist/routes/journey.d.ts +8 -0
- package/dist/routes/journey.js +130 -0
- package/dist/routes/static.d.ts +26 -0
- package/dist/routes/static.js +67 -0
- package/package.json +45 -86
- package/views/casa/components/checkboxes/template.njk +4 -1
- package/views/casa/components/date-input/template.njk +3 -3
- package/views/casa/components/journey-form/README.md +3 -1
- package/views/casa/components/journey-form/template.njk +1 -1
- package/views/casa/components/postal-address-object/template.njk +5 -5
- package/views/casa/components/radios/template.njk +1 -1
- package/views/casa/layouts/journey.njk +26 -9
- package/views/casa/layouts/main.njk +6 -19
- package/views/casa/partials/scripts.njk +8 -3
- package/views/casa/partials/styles.njk +2 -2
- package/casa.js +0 -208
- package/definitions/review-page.js +0 -60
- package/dist/casa/css/casa.css +0 -1
- package/dist/casa/js/casa.js +0 -1
- package/index.d.ts +0 -121
- package/lib/ConfigIngestor.js +0 -588
- package/lib/GatherModifier.js +0 -14
- package/lib/I18n.js +0 -160
- package/lib/JourneyContext.d.ts +0 -97
- package/lib/JourneyContext.js +0 -552
- package/lib/JourneyMap.js +0 -233
- package/lib/JourneyRoad.js +0 -330
- package/lib/Logger.js +0 -59
- package/lib/PageDictionary.d.ts +0 -11
- package/lib/PageDirectory.js +0 -77
- package/lib/Plan.js +0 -423
- package/lib/RoadConverter.js +0 -153
- package/lib/UserJourney.js +0 -8
- package/lib/Util.js +0 -227
- package/lib/Validation.js +0 -20
- package/lib/bootstrap/end-session.js +0 -44
- package/lib/bootstrap/load-definitions.js +0 -64
- package/lib/commonBodyParser.js +0 -15
- package/lib/enums.js +0 -6
- package/lib/gather-modifiers/index.js +0 -7
- package/lib/gather-modifiers/trimPostalAddressObject.js +0 -75
- package/lib/gather-modifiers/trimWhitespace.js +0 -16
- package/lib/utils/createGetRequest.d.ts +0 -5
- package/lib/utils/createGetRequest.js +0 -59
- package/lib/utils/index.js +0 -11
- package/lib/utils/parseRequest.d.ts +0 -5
- package/lib/utils/parseRequest.js +0 -72
- package/lib/utils/sanitise.js +0 -74
- package/lib/utils/validate.js +0 -32
- package/lib/validation/ArrayObjectField.js +0 -49
- package/lib/validation/ObjectField.js +0 -53
- package/lib/validation/SimpleField.d.ts +0 -11
- package/lib/validation/SimpleField.js +0 -46
- package/lib/validation/ValidationError.d.ts +0 -14
- package/lib/validation/ValidationError.js +0 -170
- package/lib/validation/ValidatorFactory.d.ts +0 -32
- package/lib/validation/ValidatorFactory.js +0 -91
- package/lib/validation/index.js +0 -22
- package/lib/validation/processor/flattenErrorArray.js +0 -24
- package/lib/validation/processor/queue.js +0 -214
- package/lib/validation/processor.js +0 -84
- package/lib/validation/rules/README.md +0 -3
- package/lib/validation/rules/ValidationRules.d.ts +0 -22
- package/lib/validation/rules/dateObject.js +0 -156
- package/lib/validation/rules/email.js +0 -44
- package/lib/validation/rules/inArray.js +0 -61
- package/lib/validation/rules/index.js +0 -23
- package/lib/validation/rules/nino.js +0 -48
- package/lib/validation/rules/optional.js +0 -14
- package/lib/validation/rules/postalAddressObject.js +0 -142
- package/lib/validation/rules/regex.js +0 -39
- package/lib/validation/rules/required.js +0 -57
- package/lib/validation/rules/strlen.js +0 -57
- package/lib/validation/rules/wordCount.js +0 -61
- package/lib/view-filters/formatDateObject.js +0 -35
- package/lib/view-filters/includes.js +0 -10
- package/lib/view-filters/index.js +0 -23
- package/lib/view-filters/mergeObjectsDeep.js +0 -21
- package/lib/view-filters/renderAsAttributes.js +0 -33
- package/middleware/errors/404.js +0 -12
- package/middleware/errors/catch-all.js +0 -27
- package/middleware/errors/index.js +0 -9
- package/middleware/headers/config-defaults.js +0 -57
- package/middleware/headers/headers.js +0 -40
- package/middleware/headers/index.js +0 -9
- package/middleware/i18n/i18n.js +0 -56
- package/middleware/i18n/index.js +0 -16
- package/middleware/index.js +0 -55
- package/middleware/mount/index.js +0 -9
- package/middleware/mount/mount.js +0 -10
- package/middleware/nunjucks/environment.js +0 -57
- package/middleware/nunjucks/index.js +0 -8
- package/middleware/page/csrf.js +0 -37
- package/middleware/page/edit-mode.js +0 -52
- package/middleware/page/gather.js +0 -75
- package/middleware/page/index.js +0 -103
- package/middleware/page/journey-continue.js +0 -157
- package/middleware/page/journey-rails.js +0 -102
- package/middleware/page/prepare-request.js +0 -77
- package/middleware/page/render.js +0 -75
- package/middleware/page/skip.js +0 -72
- package/middleware/page/utils.js +0 -206
- package/middleware/page/validate.js +0 -67
- package/middleware/session/expiry.js +0 -95
- package/middleware/session/genid.js +0 -18
- package/middleware/session/index.js +0 -18
- package/middleware/session/init.js +0 -25
- package/middleware/session/seed.js +0 -50
- package/middleware/session/timeout.js +0 -5
- package/middleware/static/asset-versions.js +0 -23
- package/middleware/static/index.js +0 -104
- package/middleware/static/prepare-assets.js +0 -51
- package/middleware/static/serve-assets.js +0 -58
- package/middleware/variables/index.js +0 -12
- package/middleware/variables/variables.js +0 -35
- package/src/browserconfig.xml +0 -5
- package/src/js/casa.js +0 -132
- package/src/scss/_casaElements.scss +0 -11
- package/src/scss/_casaGovukTemplateJinjaPolyfill.scss +0 -39
- package/src/scss/_casaMountUrl.scss +0 -8
- package/src/scss/casa-ie8.scss +0 -3
- package/src/scss/casa.scss +0 -14
- package/test/unit/templates/README.md +0 -5
- package/test/utils/BaseTestWaypoint.js +0 -106
- package/test/utils/concatWaypoints.js +0 -26
- package/test/utils/index.js +0 -6
- package/test/utils/testTraversal.js +0 -90
- package/views/casa/partials/cookie_message.njk +0 -3
- package/views/casa/partials/phase_banner_alpha.njk +0 -8
- package/views/casa/partials/phase_banner_beta.njk +0 -8
- package/views/casa/review/page-block.njk +0 -8
- package/views/casa/review/review.njk +0 -47
package/lib/utils/sanitise.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
const { URL } = require('url');
|
|
2
|
-
|
|
3
|
-
const reLeadingSlashes = /^\/+/;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Sanitise waypoint.
|
|
7
|
-
*
|
|
8
|
-
* Valid examples:
|
|
9
|
-
* a-waypoint
|
|
10
|
-
* an-origin/a-waypoint
|
|
11
|
-
* an-origin/a-waypoint/with-forward-slash
|
|
12
|
-
*
|
|
13
|
-
* @param {string} waypoint Waypoint, including optional Plan origin
|
|
14
|
-
* @returns {string} Waypoint
|
|
15
|
-
*/
|
|
16
|
-
function sanitiseWaypoint(waypoint = '') {
|
|
17
|
-
if (typeof waypoint !== 'string') {
|
|
18
|
-
return '';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return waypoint.toLowerCase().replace(/[^a-z0-9/-]/g, '').replace(/\/+/g, '/').replace(/^\/+/, '')
|
|
22
|
-
.replace(/\/+$/, '');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Sanitise the pathname of a URL, and prepend with a forward slash.
|
|
27
|
-
*
|
|
28
|
-
* Valid examples:
|
|
29
|
-
* /some-path
|
|
30
|
-
* /mount-url/some-path
|
|
31
|
-
* /further/path/parts
|
|
32
|
-
*
|
|
33
|
-
* @param {string} path Path
|
|
34
|
-
* @returns {string} Path
|
|
35
|
-
*/
|
|
36
|
-
function sanitiseAbsolutePath(path) {
|
|
37
|
-
if (typeof path !== 'string') {
|
|
38
|
-
return '/';
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return `/${path.toLowerCase().replace(/[^a-z0-9/-]/g, '').replace(/\/+/g, '/').replace(/^\/+/, '')
|
|
42
|
-
.replace(/\/+$/, '')}`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Coeerce url to a relative represenation.
|
|
47
|
-
*
|
|
48
|
-
* @param {string} url URL to sanitise
|
|
49
|
-
* @returns {string} Sanitised URL
|
|
50
|
-
*/
|
|
51
|
-
function sanitiseRelativeUrl(url) {
|
|
52
|
-
if (typeof url !== 'string') {
|
|
53
|
-
return '/';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const sanitised = new URL(url, 'https://base.test');
|
|
57
|
-
|
|
58
|
-
return `/${sanitised.pathname}`.replace(reLeadingSlashes, '/') + sanitised.search;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function sanitiseContextId(id) {
|
|
62
|
-
if (typeof id !== 'string') {
|
|
63
|
-
return '';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return String(id).replace(/[^a-z0-9-]/g, '');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
module.exports = {
|
|
70
|
-
sanitiseWaypoint,
|
|
71
|
-
sanitiseAbsolutePath,
|
|
72
|
-
sanitiseRelativeUrl,
|
|
73
|
-
sanitiseContextId,
|
|
74
|
-
};
|
package/lib/utils/validate.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const invalidSlugRegex = /[^a-z0-9-]/;
|
|
2
|
-
const invalidSlugPathRegex = /[^a-z0-9/-]/;
|
|
3
|
-
|
|
4
|
-
function validateMountUrl(mountUrl, { name = 'mountUrl' } = {}) {
|
|
5
|
-
if (typeof mountUrl !== 'string') {
|
|
6
|
-
throw new TypeError(`${name} must be a string`);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
if (invalidSlugPathRegex.test(mountUrl)) {
|
|
10
|
-
throw new SyntaxError(`${name} contains invalid characters`);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (!mountUrl.length || mountUrl[0] !== '/' || mountUrl[mountUrl.length - 1] !== '/') {
|
|
14
|
-
throw new SyntaxError(`${name} must begin and end with a /`)
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function validateWaypoint(waypoint, { name = 'waypoint', withOrigin = false } = {}) {
|
|
19
|
-
if (typeof waypoint !== 'string') {
|
|
20
|
-
throw new TypeError(`${name} must be a string`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const regex = withOrigin ? invalidSlugPathRegex : invalidSlugRegex;
|
|
24
|
-
if (regex.test(waypoint)) {
|
|
25
|
-
throw new SyntaxError(`${name} contains invalid characters`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = {
|
|
30
|
-
validateMountUrl,
|
|
31
|
-
validateWaypoint,
|
|
32
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An array of objects field. This is identical to the `typeObjectField`
|
|
3
|
-
* function, but the data against which it is tested is expected to be an array
|
|
4
|
-
* of objects that match the structure of the `obj` defined here.
|
|
5
|
-
*
|
|
6
|
-
* @param {object} obj Object of fields that will be tested.
|
|
7
|
-
* @param {Array} validators Validation rules that must be satisfied.
|
|
8
|
-
* @param {Function | null} condition Condition to meet before validators are run.
|
|
9
|
-
* @returns {object} Validation object suitable for processing.
|
|
10
|
-
* @throws {TypeError} When arguments are invalid.
|
|
11
|
-
*/
|
|
12
|
-
module.exports = (obj = {}, validators = [], condition) => {
|
|
13
|
-
if (Object.prototype.toString.call(obj) !== '[object Object]') {
|
|
14
|
-
throw new TypeError(`Children object must be an object, got ${typeof Object.prototype.toString.call(obj)}`);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
validators.forEach((v) => {
|
|
18
|
-
if (typeof v !== 'function') {
|
|
19
|
-
throw new TypeError(`Validators must be a function, got ${typeof v}`);
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
if (typeof condition !== 'undefined' && typeof condition !== 'function') {
|
|
24
|
-
throw new TypeError(`Conditional must be a function, got ${typeof condition}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return Object.create(Object.prototype, {
|
|
28
|
-
type: {
|
|
29
|
-
writable: false,
|
|
30
|
-
confirgurable: false,
|
|
31
|
-
value: 'array_object',
|
|
32
|
-
},
|
|
33
|
-
condition: {
|
|
34
|
-
writable: false,
|
|
35
|
-
configurable: false,
|
|
36
|
-
value: condition || (() => true),
|
|
37
|
-
},
|
|
38
|
-
validators: {
|
|
39
|
-
value: validators,
|
|
40
|
-
writable: false,
|
|
41
|
-
configurable: false,
|
|
42
|
-
},
|
|
43
|
-
children: {
|
|
44
|
-
value: obj,
|
|
45
|
-
writable: false,
|
|
46
|
-
configurable: false,
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An object field. The defined `obj` is an object of fieldName:Field mappings.
|
|
3
|
-
* For example:
|
|
4
|
-
* {
|
|
5
|
-
* "contactName": Validation.SimpleField([...]),
|
|
6
|
-
* "contactDob": Validation.SimpleField([...]),
|
|
7
|
-
* "contactAddress": Validation.ObjectField(addressObject, [...])}
|
|
8
|
-
* }
|
|
9
|
-
*
|
|
10
|
-
* @param {object} obj Object of fields that will be tested
|
|
11
|
-
* @param {Array|null} validators Validation rules that must be satisfied
|
|
12
|
-
* @param {Function|null} condition Condition to meet before validators are run
|
|
13
|
-
* @returns {object} Validation object suitable for processing
|
|
14
|
-
* @throws {TypeError} When arguments are invalid
|
|
15
|
-
*/
|
|
16
|
-
module.exports = (obj = {}, validators = [], condition) => {
|
|
17
|
-
if (Object.prototype.toString.call(obj) !== '[object Object]') {
|
|
18
|
-
throw new TypeError(`Children object must be an object, got ${typeof Object.prototype.toString.call(obj)}`);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
validators.forEach((v) => {
|
|
22
|
-
if (typeof v !== 'function') {
|
|
23
|
-
throw new TypeError(`Validators must be a function, got ${typeof v}`);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
if (typeof condition !== 'undefined' && typeof condition !== 'function') {
|
|
28
|
-
throw new TypeError(`Conditional must be a function, got ${typeof condition}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return Object.create(Object.prototype, {
|
|
32
|
-
type: {
|
|
33
|
-
writable: false,
|
|
34
|
-
confirgurable: false,
|
|
35
|
-
value: 'object',
|
|
36
|
-
},
|
|
37
|
-
condition: {
|
|
38
|
-
writable: false,
|
|
39
|
-
configurable: false,
|
|
40
|
-
value: condition || (() => true),
|
|
41
|
-
},
|
|
42
|
-
validators: {
|
|
43
|
-
value: validators,
|
|
44
|
-
writable: false,
|
|
45
|
-
configurable: false,
|
|
46
|
-
},
|
|
47
|
-
children: {
|
|
48
|
-
value: obj,
|
|
49
|
-
writable: false,
|
|
50
|
-
configurable: false,
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ValidatorConditionFunction, ValidatorFunction } from '../../index';
|
|
2
|
-
|
|
3
|
-
export = SimpleField;
|
|
4
|
-
|
|
5
|
-
declare function SimpleField (validators: Array<ValidatorFunction | ValidatorFactory>, condition?: ValidatorConditionFunction): SimpleFieldValidatorConfig | never;
|
|
6
|
-
|
|
7
|
-
interface SimpleFieldValidatorConfig {
|
|
8
|
-
readonly type: 'simple',
|
|
9
|
-
readonly condition: ValidatorConditionFunction,
|
|
10
|
-
readonly validators: Array<ValidatorFunction | ValidatorFactory>,
|
|
11
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/* eslint-disable valid-jsdoc */
|
|
2
|
-
const ValidatorFactory = require('./ValidatorFactory.js');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A simple field.
|
|
6
|
-
*
|
|
7
|
-
* @param {Array<ValidatorFactory | Function | object>} validators Validation rules to satisfy
|
|
8
|
-
* @param {Function | null} condition Condition to meet before validators are run
|
|
9
|
-
* @returns {SimpleFieldValidatorConfig} Validation object suitable for processing
|
|
10
|
-
* @throws {TypeError} When conditional is not the correct type
|
|
11
|
-
*
|
|
12
|
-
* @typedef { import('../../index').ValidatorConditionFunction } ValidatorConditionFunction
|
|
13
|
-
* @typedef { import('../../index').ValidatorFunction } ValidatorFunction
|
|
14
|
-
*
|
|
15
|
-
* @typedef {object} SimpleFieldValidatorConfig Prepared field validator
|
|
16
|
-
* @property {string} type The type of field validator (always 'simple')
|
|
17
|
-
* @property {ValidatorConditionFunction} condition Condition to run validators
|
|
18
|
-
* @property {Array<ValidatorFunction | ValidatorFactory>} validators List of validators to run
|
|
19
|
-
*/
|
|
20
|
-
module.exports = (validators = [], condition) => {
|
|
21
|
-
// Isolate and validate validate functions
|
|
22
|
-
const parsedValidators = validators.map(ValidatorFactory.coerceToValidatorObject);
|
|
23
|
-
|
|
24
|
-
// Validate condition function, if present
|
|
25
|
-
if (typeof condition !== 'undefined' && typeof condition !== 'function') {
|
|
26
|
-
throw new TypeError(`Conditional must be a function, got ${typeof condition}`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return Object.create(Object.prototype, {
|
|
30
|
-
type: {
|
|
31
|
-
writable: false,
|
|
32
|
-
confirgurable: false,
|
|
33
|
-
value: 'simple',
|
|
34
|
-
},
|
|
35
|
-
condition: {
|
|
36
|
-
writable: false,
|
|
37
|
-
configurable: false,
|
|
38
|
-
value: condition || (() => true),
|
|
39
|
-
},
|
|
40
|
-
validators: {
|
|
41
|
-
value: parsedValidators,
|
|
42
|
-
writable: false,
|
|
43
|
-
configurable: false,
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ValidationErrorObject, ValidatorErrorObjectGenerator, ValidatorContext } from '../../index';
|
|
2
|
-
|
|
3
|
-
export = ValidationError;
|
|
4
|
-
|
|
5
|
-
declare class ValidationError {
|
|
6
|
-
constructor(errorParam: ValidationErrorObject | string);
|
|
7
|
-
|
|
8
|
-
static make(params: {
|
|
9
|
-
errorMsg: string | ValidationErrorObject | ValidatorErrorObjectGenerator | Error,
|
|
10
|
-
dataContext?: ValidatorContext
|
|
11
|
-
}): ValidationError;
|
|
12
|
-
|
|
13
|
-
withContext(context: ValidatorContext): ValidationError;
|
|
14
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
const util = require('../Util.js');
|
|
2
|
-
|
|
3
|
-
const params = new WeakMap();
|
|
4
|
-
|
|
5
|
-
class ValidationError {
|
|
6
|
-
/**
|
|
7
|
-
* Make a ValidationError instance from a primitive object (or a function that
|
|
8
|
-
* returns a primitive object) that is specific to the given journey context.
|
|
9
|
-
*
|
|
10
|
-
* The returned `error` (or the function that returns the equivalent) must
|
|
11
|
-
* match the structure required by the ValidationError constructor.
|
|
12
|
-
*
|
|
13
|
-
* `errorMsg` can be one of these formats:
|
|
14
|
-
* String => 'common:errors.my-error-message'
|
|
15
|
-
* Object => (see constructor argument for structure of this object)
|
|
16
|
-
* Function => Function returns object suitable for constructor (see example below)
|
|
17
|
-
* Error => A JavaScript error. It's `message` will be used as the error.
|
|
18
|
-
*
|
|
19
|
-
* `dataContext` is an object containing the same data passed to all validator
|
|
20
|
-
* functions, and contains:
|
|
21
|
-
* waypointId => The current waypoint being requested
|
|
22
|
-
* fieldName => Name of the field being validated
|
|
23
|
-
* journeyContext => The full JourneyContext of the current request.
|
|
24
|
-
*
|
|
25
|
-
* Example function signature that can be used for `errorMsg`:
|
|
26
|
-
* ({ waypointId, fieldName, journeyContext }) => ({
|
|
27
|
-
* summary: 'my-waypoint:some.key.to.say.hello',
|
|
28
|
-
* variables: {
|
|
29
|
-
* name: journeyContext.getDataForPage(waypointId).name,
|
|
30
|
-
* }
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* @param {object} args See args above
|
|
34
|
-
* @param {any} args.errorMsg Error message to seed the ValidationError
|
|
35
|
-
* @param {object} args.dataContext Validation context
|
|
36
|
-
* @returns {object} Primitive error matching structure above
|
|
37
|
-
* @throws {TypeError} If errorMsg is not in a valid type
|
|
38
|
-
*/
|
|
39
|
-
static make({ errorMsg, dataContext = {} }) {
|
|
40
|
-
// Convert strings to the most basic object primitive
|
|
41
|
-
if (typeof errorMsg === 'string') {
|
|
42
|
-
return new ValidationError({
|
|
43
|
-
summary: errorMsg,
|
|
44
|
-
inline: errorMsg,
|
|
45
|
-
focusSuffix: [],
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// No contextual changes applicable; return ValidationErorr made from the
|
|
50
|
-
// original object
|
|
51
|
-
if (Object.prototype.toString.call(errorMsg) === '[object Object]') {
|
|
52
|
-
return new ValidationError(errorMsg);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Use the user-defined function to generate an error primitive for the
|
|
56
|
-
// given context
|
|
57
|
-
if (typeof errorMsg === 'function') {
|
|
58
|
-
return new ValidationError(errorMsg.call(null, { ...dataContext }));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Core Error
|
|
62
|
-
if (errorMsg instanceof Error) {
|
|
63
|
-
return new ValidationError({
|
|
64
|
-
summary: errorMsg.message,
|
|
65
|
-
inline: errorMsg.message,
|
|
66
|
-
focusSuffix: errorMsg.focusSuffix || [],
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Unsupported
|
|
71
|
-
throw new TypeError('errorMsg must be a string, Error, primitive object or function that generates a primitive object');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* `error` may be a simple string, in which case that string reppresents the
|
|
76
|
-
* error mesaage (equivalent to `error.summary` in the structure below).
|
|
77
|
-
*
|
|
78
|
-
* `error`, when passed as an object, must match this structure:
|
|
79
|
-
*
|
|
80
|
-
* {
|
|
81
|
-
* summary: "", // required
|
|
82
|
-
* inline: "", // optional, may be deprecated in future
|
|
83
|
-
* focusSuffix: "", // optional
|
|
84
|
-
* fieldKeySuffix: "", // optional
|
|
85
|
-
* variables: { // optional
|
|
86
|
-
* myVariable: 'a value'
|
|
87
|
-
* }
|
|
88
|
-
* }
|
|
89
|
-
*
|
|
90
|
-
* @param {object|string} errorParam See object structure above
|
|
91
|
-
*/
|
|
92
|
-
constructor(errorParam = {}) {
|
|
93
|
-
if (Object.prototype.toString.call(errorParam) !== '[object Object]' && typeof errorParam !== 'string') {
|
|
94
|
-
throw new TypeError('Constructor requires a string or object');
|
|
95
|
-
}
|
|
96
|
-
const error = typeof errorParam === 'string' ? { summary: errorParam } : errorParam;
|
|
97
|
-
|
|
98
|
-
// Store parameters for later use in applying contexts
|
|
99
|
-
const originals = {
|
|
100
|
-
summary: error.summary,
|
|
101
|
-
// may be deprecated; summary and inline should always match
|
|
102
|
-
inline: error.inline || error.summary,
|
|
103
|
-
focusSuffix: error.focusSuffix || [],
|
|
104
|
-
fieldKeySuffix: error.fieldKeySuffix || undefined,
|
|
105
|
-
variables: error.variables || {},
|
|
106
|
-
message: error.summary,
|
|
107
|
-
field: error.field || undefined,
|
|
108
|
-
fieldHref: error.fieldHref || undefined,
|
|
109
|
-
validator: error.validator || undefined,
|
|
110
|
-
};
|
|
111
|
-
params.set(this, originals);
|
|
112
|
-
|
|
113
|
-
// Duplicate parameters to make them available in public scope. These are
|
|
114
|
-
// the values that will be readable, and reflect any context that may have
|
|
115
|
-
// been applied
|
|
116
|
-
Object.keys(originals).forEach((o) => {
|
|
117
|
-
Object.defineProperty(this, o, {
|
|
118
|
-
value: originals[o],
|
|
119
|
-
enumerable: true,
|
|
120
|
-
writable: true,
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Modifies the error to reflect the given context.
|
|
127
|
-
*
|
|
128
|
-
* `context` is an object containing the following attributes
|
|
129
|
-
* string waypointId
|
|
130
|
-
* string fieldName
|
|
131
|
-
* JourneyContext journeyContext
|
|
132
|
-
*
|
|
133
|
-
* @param {object} context See structure above
|
|
134
|
-
* @returns {ValidationError} Chain
|
|
135
|
-
*/
|
|
136
|
-
withContext(context) {
|
|
137
|
-
// Get original constructor parameters
|
|
138
|
-
const originals = params.get(this);
|
|
139
|
-
|
|
140
|
-
// Expand variables
|
|
141
|
-
if (typeof originals.variables === 'function') {
|
|
142
|
-
this.variables = originals.variables.call(this, context);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Set field name
|
|
146
|
-
if (context.fieldName) {
|
|
147
|
-
let focusSuffix;
|
|
148
|
-
let fieldHref = `#f-${util.normalizeHtmlObjectPath(context.fieldName)}`;
|
|
149
|
-
if (originals.fieldKeySuffix) {
|
|
150
|
-
fieldHref += originals.fieldKeySuffix;
|
|
151
|
-
} else if (originals.focusSuffix && originals.focusSuffix.length) {
|
|
152
|
-
focusSuffix = Array.isArray(originals.focusSuffix)
|
|
153
|
-
? originals.focusSuffix
|
|
154
|
-
: [originals.focusSuffix];
|
|
155
|
-
fieldHref += focusSuffix[0];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
this.field = util.normalizeHtmlObjectPath(context.fieldName + (originals.fieldKeySuffix || ''));
|
|
159
|
-
this.fieldHref = fieldHref;
|
|
160
|
-
this.focusSuffix = focusSuffix || [];
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Set validator name
|
|
164
|
-
this.validator = context.validator || undefined;
|
|
165
|
-
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
module.exports = ValidationError;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { ValidationErrorObject, ValidatorContext } from '../../index';
|
|
2
|
-
|
|
3
|
-
export = ValidatorFactory;
|
|
4
|
-
|
|
5
|
-
declare class ValidatorFactory {
|
|
6
|
-
public config: config;
|
|
7
|
-
|
|
8
|
-
static make(config?: object): {
|
|
9
|
-
validate: Function,
|
|
10
|
-
sanitise: Function,
|
|
11
|
-
config: config,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
static coerceToValidatorObject(input: ValidatorFactory | Function | any): validatorObject;
|
|
15
|
-
|
|
16
|
-
constructor(config?: config);
|
|
17
|
-
|
|
18
|
-
validate(fieldValue: any, context: ValidatorContext): Promise<null | ValidationErrorObject>;
|
|
19
|
-
|
|
20
|
-
sanitise(fieldValue: any, context: ValidatorContext): any;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type config = {
|
|
24
|
-
[key: string]: any,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
type validatorObject = {
|
|
28
|
-
name?: string,
|
|
29
|
-
config?: config,
|
|
30
|
-
sanitise?: Function,
|
|
31
|
-
validate?: Function,
|
|
32
|
-
};
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/* eslint-disable class-methods-use-this */
|
|
2
|
-
const { isObjectType } = require('../Util.js');
|
|
3
|
-
|
|
4
|
-
class ValidatorFactory {
|
|
5
|
-
/**
|
|
6
|
-
* This is a convenience method that will bind and return this class'
|
|
7
|
-
* `validate()` function, so you can call it directly rather than calling the
|
|
8
|
-
* method. i.e.
|
|
9
|
-
*
|
|
10
|
-
* MyValidator.make()('value to validate')
|
|
11
|
-
* versus
|
|
12
|
-
* (new MyValidator()).validate('value to validate')
|
|
13
|
-
*
|
|
14
|
-
* It also attaches the `sanitise()` method as a static property to that
|
|
15
|
-
* function.
|
|
16
|
-
*
|
|
17
|
-
* @param {object} config Validator config
|
|
18
|
-
* @returns {object} Validator object
|
|
19
|
-
* @throws {TypeError} When configurarion is invalid.
|
|
20
|
-
*/
|
|
21
|
-
static make(config = {}) {
|
|
22
|
-
if (!isObjectType(config)) {
|
|
23
|
-
throw new TypeError('Configuration must be an object');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const validator = Reflect.construct(this, [config]);
|
|
27
|
-
|
|
28
|
-
/* eslint-disable-next-line sonarjs/prefer-object-literal */
|
|
29
|
-
const instance = {};
|
|
30
|
-
instance.name = validator.name || validator.constructor.name;
|
|
31
|
-
instance.config = config;
|
|
32
|
-
instance.validate = validator.validate.bind(instance);
|
|
33
|
-
instance.sanitise = validator.sanitise.bind(instance);
|
|
34
|
-
|
|
35
|
-
Object.freeze(instance);
|
|
36
|
-
|
|
37
|
-
return instance;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static coerceToValidatorObject(input) {
|
|
41
|
-
let validator = Object.create(null);
|
|
42
|
-
validator.validate = () => (Promise.reject(
|
|
43
|
-
new Error('validate() method has not been defined'),
|
|
44
|
-
));
|
|
45
|
-
validator.sanitise = (val) => (val);
|
|
46
|
-
validator.config = Object.create(null);
|
|
47
|
-
|
|
48
|
-
// An uninstantied Validator subclass
|
|
49
|
-
if (typeof input === 'function' && Reflect.getPrototypeOf(input) === ValidatorFactory) {
|
|
50
|
-
validator = input.make();
|
|
51
|
-
} else if (typeof input === 'function') {
|
|
52
|
-
// A plain function is assumed to be just the validation logic. We do not
|
|
53
|
-
// bind the function to `validator` here because it may already be bound to
|
|
54
|
-
// another context in userland.
|
|
55
|
-
validator.name = input.name || input.constructor.name || 'unknown';
|
|
56
|
-
validator.validate = input;
|
|
57
|
-
} else if (isObjectType(input)) {
|
|
58
|
-
// A plain object
|
|
59
|
-
validator = {
|
|
60
|
-
validate: input.validate || validator.validate,
|
|
61
|
-
sanitise: input.sanitise || validator.sanitise,
|
|
62
|
-
config: input.config || validator.config,
|
|
63
|
-
name: input.name || validator.name,
|
|
64
|
-
};
|
|
65
|
-
} else {
|
|
66
|
-
// An unsupported scenario
|
|
67
|
-
throw new TypeError(`Cannot coerce input to a validator object (typeof = ${typeof input})`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return validator;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
constructor(config = {}) {
|
|
74
|
-
if (new.target === ValidatorFactory) {
|
|
75
|
-
throw new TypeError('Cannot instantiate the abstract class, ValidatorFactory');
|
|
76
|
-
}
|
|
77
|
-
this.config = config;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/* eslint-disable-next-line no-unused-vars */
|
|
81
|
-
validate(fieldValue, context) {
|
|
82
|
-
throw new Error('validate() method has not been implemented');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/* eslint-disable-next-line no-unused-vars */
|
|
86
|
-
sanitise(fieldValue, context) {
|
|
87
|
-
return fieldValue;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
module.exports = ValidatorFactory;
|
package/lib/validation/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
const SimpleField = require('./SimpleField.js');
|
|
2
|
-
const ObjectField = require('./ObjectField.js');
|
|
3
|
-
const ArrayObjectField = require('./ArrayObjectField.js');
|
|
4
|
-
const ValidationError = require('./ValidationError.js');
|
|
5
|
-
const processor = require('./processor.js');
|
|
6
|
-
const rules = require('./rules/index.js');
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
// Field type constructors
|
|
10
|
-
SimpleField,
|
|
11
|
-
ObjectField,
|
|
12
|
-
ArrayObjectField,
|
|
13
|
-
|
|
14
|
-
// Error classes
|
|
15
|
-
ValidationError,
|
|
16
|
-
|
|
17
|
-
// Core validation processor
|
|
18
|
-
processor,
|
|
19
|
-
|
|
20
|
-
// Core rules
|
|
21
|
-
rules,
|
|
22
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const ValidationError = require('../ValidationError.js');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts a (potentially) deeply nested array of ValidationErrors into a flat
|
|
5
|
-
* array.
|
|
6
|
-
*
|
|
7
|
-
* @param {Array<ValidationError>} errorsArray Array of errors.
|
|
8
|
-
* @returns {Array} The flatted array.
|
|
9
|
-
* @throws {TypeError} When error is not a ValidationError
|
|
10
|
-
*/
|
|
11
|
-
module.exports = (errorsArray = []) => {
|
|
12
|
-
let errors = errorsArray;
|
|
13
|
-
if (!Array.isArray(errorsArray)) {
|
|
14
|
-
errors = [errorsArray];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const flattend = errors.flat(Infinity);
|
|
18
|
-
|
|
19
|
-
if (!flattend.every((e) => (e instanceof ValidationError))) {
|
|
20
|
-
throw new TypeError('All errors must be instances of ValidationError.');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return flattend;
|
|
24
|
-
};
|