@defra/fcp-sfd-frontend-engine 0.2.3 → 0.2.5
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/index.cjs +70 -2
- package/dist/index.js +57 -1
- package/package.json +1 -1
- package/src/index.js +3 -1
- package/src/mappers/mappers.js +5 -0
- package/src/schemas/business/business-sbi-schema.js +11 -0
- package/src/schemas/business/business-schemas.js +5 -0
- package/src/schemas/schemas.js +5 -0
- package/src/utils/format-validation-errors.js +43 -0
- package/src/utils/utils.js +5 -0
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
7
|
var __export = (target, all) => {
|
|
6
8
|
for (var name in all)
|
|
@@ -14,12 +16,22 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
16
|
}
|
|
15
17
|
return to;
|
|
16
18
|
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
17
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
28
|
|
|
19
29
|
// src/index.js
|
|
20
30
|
var index_exports = {};
|
|
21
31
|
__export(index_exports, {
|
|
22
|
-
|
|
32
|
+
mappers: () => mappers,
|
|
33
|
+
schemas: () => schemas,
|
|
34
|
+
utils: () => utils
|
|
23
35
|
});
|
|
24
36
|
module.exports = __toCommonJS(index_exports);
|
|
25
37
|
|
|
@@ -41,7 +53,63 @@ var mapPersonalBusinessDetails = (value) => {
|
|
|
41
53
|
}
|
|
42
54
|
};
|
|
43
55
|
};
|
|
56
|
+
|
|
57
|
+
// src/mappers/mappers.js
|
|
58
|
+
var mappers = {
|
|
59
|
+
mapPersonalBusinessDetails
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// src/schemas/business/business-sbi-schema.js
|
|
63
|
+
var import_joi = __toESM(require("joi"), 1);
|
|
64
|
+
var businessSbiSchema = import_joi.default.object({
|
|
65
|
+
sbi: import_joi.default.string().pattern(/^\d{9}$/).allow("").optional().messages({
|
|
66
|
+
"string.pattern.base": "Enter the full SBI"
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// src/schemas/business/business-schemas.js
|
|
71
|
+
var businessSchemas = {
|
|
72
|
+
sbi: businessSbiSchema
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/schemas/schemas.js
|
|
76
|
+
var schemas = {
|
|
77
|
+
business: businessSchemas
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/utils/format-validation-errors.js
|
|
81
|
+
var formatValidationErrors = (errors) => {
|
|
82
|
+
const formattedErrors = {};
|
|
83
|
+
errors.forEach((error) => {
|
|
84
|
+
const { type, context, message: text, path } = error;
|
|
85
|
+
if (Array.isArray(path) && path.length > 1) {
|
|
86
|
+
for (const field of path) {
|
|
87
|
+
if (!formattedErrors[field]) {
|
|
88
|
+
formattedErrors[field] = { text };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (type === "object.missing" && Array.isArray(context == null ? void 0 : context.peers)) {
|
|
94
|
+
context.peers.forEach((peer) => {
|
|
95
|
+
formattedErrors[peer] = { text };
|
|
96
|
+
});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (!formattedErrors[path[0]]) {
|
|
100
|
+
formattedErrors[path[0]] = { text };
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return formattedErrors;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// src/utils/utils.js
|
|
107
|
+
var utils = {
|
|
108
|
+
formatValidationErrors
|
|
109
|
+
};
|
|
44
110
|
// Annotate the CommonJS export names for ESM import in node:
|
|
45
111
|
0 && (module.exports = {
|
|
46
|
-
|
|
112
|
+
mappers,
|
|
113
|
+
schemas,
|
|
114
|
+
utils
|
|
47
115
|
});
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,62 @@ var mapPersonalBusinessDetails = (value) => {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
// src/mappers/mappers.js
|
|
21
|
+
var mappers = {
|
|
20
22
|
mapPersonalBusinessDetails
|
|
21
23
|
};
|
|
24
|
+
|
|
25
|
+
// src/schemas/business/business-sbi-schema.js
|
|
26
|
+
import Joi from "joi";
|
|
27
|
+
var businessSbiSchema = Joi.object({
|
|
28
|
+
sbi: Joi.string().pattern(/^\d{9}$/).allow("").optional().messages({
|
|
29
|
+
"string.pattern.base": "Enter the full SBI"
|
|
30
|
+
})
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// src/schemas/business/business-schemas.js
|
|
34
|
+
var businessSchemas = {
|
|
35
|
+
sbi: businessSbiSchema
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/schemas/schemas.js
|
|
39
|
+
var schemas = {
|
|
40
|
+
business: businessSchemas
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/utils/format-validation-errors.js
|
|
44
|
+
var formatValidationErrors = (errors) => {
|
|
45
|
+
const formattedErrors = {};
|
|
46
|
+
errors.forEach((error) => {
|
|
47
|
+
const { type, context, message: text, path } = error;
|
|
48
|
+
if (Array.isArray(path) && path.length > 1) {
|
|
49
|
+
for (const field of path) {
|
|
50
|
+
if (!formattedErrors[field]) {
|
|
51
|
+
formattedErrors[field] = { text };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (type === "object.missing" && Array.isArray(context == null ? void 0 : context.peers)) {
|
|
57
|
+
context.peers.forEach((peer) => {
|
|
58
|
+
formattedErrors[peer] = { text };
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!formattedErrors[path[0]]) {
|
|
63
|
+
formattedErrors[path[0]] = { text };
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return formattedErrors;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/utils/utils.js
|
|
70
|
+
var utils = {
|
|
71
|
+
formatValidationErrors
|
|
72
|
+
};
|
|
73
|
+
export {
|
|
74
|
+
mappers,
|
|
75
|
+
schemas,
|
|
76
|
+
utils
|
|
77
|
+
};
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transforms raw validation errors from Joi into a format suitable for form display
|
|
3
|
+
*
|
|
4
|
+
* @param {Array<Object>} errors - Array of validation error objects from Joi
|
|
5
|
+
*
|
|
6
|
+
* @returns {Object} Formatted errors object where keys are field names and values are { text: "error message" }
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const formatValidationErrors = (errors) => {
|
|
10
|
+
const formattedErrors = {}
|
|
11
|
+
|
|
12
|
+
errors.forEach(error => {
|
|
13
|
+
const { type, context, message: text, path } = error
|
|
14
|
+
|
|
15
|
+
// Multi-field validation for a single combined input (used for date inputs where one error message applies to
|
|
16
|
+
// multiple fields)
|
|
17
|
+
if (Array.isArray(path) && path.length > 1) {
|
|
18
|
+
for (const field of path) {
|
|
19
|
+
if (!formattedErrors[field]) {
|
|
20
|
+
formattedErrors[field] = { text }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Multi-field validation: show error on all related fields (e.g., businessTelephone + businessMobile)
|
|
27
|
+
if (type === 'object.missing' && Array.isArray(context?.peers)) {
|
|
28
|
+
// Apply the same error message to all related fields
|
|
29
|
+
context.peers.forEach(peer => {
|
|
30
|
+
formattedErrors[peer] = { text }
|
|
31
|
+
})
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Single-field validation: show error on the specific field that failed
|
|
36
|
+
if (!formattedErrors[path[0]]) {
|
|
37
|
+
// Only add error if this field doesn't already have one (prevents overwriting)
|
|
38
|
+
formattedErrors[path[0]] = { text }
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return formattedErrors
|
|
43
|
+
}
|