@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 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
- mapPersonalBusinessDetails: () => mapPersonalBusinessDetails
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
- mapPersonalBusinessDetails
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
- export {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/fcp-sfd-frontend-engine",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Shared frontend engine used by both the internal and external frontend applications.",
5
5
  "main": "./dist/index.cjs",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -1 +1,3 @@
1
- export { mapPersonalBusinessDetails } from './mappers/personal-business-details-mapper.js'
1
+ export { mappers } from './mappers/mappers.js'
2
+ export { schemas } from './schemas/schemas.js'
3
+ export { utils } from './utils/utils.js'
@@ -0,0 +1,5 @@
1
+ import { mapPersonalBusinessDetails } from './personal-business-details-mapper.js'
2
+
3
+ export const mappers = {
4
+ mapPersonalBusinessDetails
5
+ }
@@ -0,0 +1,11 @@
1
+ import Joi from 'joi'
2
+
3
+ export const businessSbiSchema = Joi.object({
4
+ sbi: Joi.string()
5
+ .pattern(/^\d{9}$/)
6
+ .allow('')
7
+ .optional()
8
+ .messages({
9
+ 'string.pattern.base': 'Enter the full SBI'
10
+ })
11
+ })
@@ -0,0 +1,5 @@
1
+ import { businessSbiSchema } from './business-sbi-schema.js'
2
+
3
+ export const businessSchemas = {
4
+ sbi: businessSbiSchema
5
+ }
@@ -0,0 +1,5 @@
1
+ import { businessSchemas } from './business/business-schemas.js'
2
+
3
+ export const schemas = {
4
+ business: businessSchemas
5
+ }
@@ -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
+ }
@@ -0,0 +1,5 @@
1
+ import { formatValidationErrors } from './format-validation-errors'
2
+
3
+ export const utils = {
4
+ formatValidationErrors
5
+ }