@becollective/utils 2.0.9 → 2.0.10
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/lib/forms.d.ts +323 -1
- package/lib/forms.js +15 -14
- package/package.json +1 -1
- package/src/forms.ts +2 -2
package/lib/forms.d.ts
CHANGED
|
@@ -1 +1,323 @@
|
|
|
1
|
-
|
|
1
|
+
type ValidateDataNameError = {
|
|
2
|
+
name: string;
|
|
3
|
+
key: string;
|
|
4
|
+
value: any;
|
|
5
|
+
};
|
|
6
|
+
type ValidateDataPropertyError = {
|
|
7
|
+
property: string;
|
|
8
|
+
message: string;
|
|
9
|
+
key: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const validator: {
|
|
12
|
+
validateSchema: (schema: any, uischema: any) => Promise<boolean>;
|
|
13
|
+
validateSchemaPropertiesExist: (properties: any, keys: any, err: any) => (ValidateDataNameError | ValidateDataPropertyError)[] | null;
|
|
14
|
+
/**
|
|
15
|
+
* Check if every field in the form has a unique title
|
|
16
|
+
* @param {object} properties fields properties
|
|
17
|
+
* @param {string} err error name
|
|
18
|
+
*/
|
|
19
|
+
validatePropertiesTitleUniqueness: (properties: any, err: any) => (ValidateDataNameError | ValidateDataPropertyError)[] | null;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new object from the input obj.
|
|
22
|
+
* All entries from the input whose values are empty arrays are missing in the new object
|
|
23
|
+
* @param {Object} data 1-depth obj
|
|
24
|
+
* @returns A new object without empty arrays
|
|
25
|
+
*/
|
|
26
|
+
stripEmptyArraysFromData: (data: any) => {};
|
|
27
|
+
validateData: (schema: any, data: any) => Promise<void>;
|
|
28
|
+
getValidatorForType: (type: any) => any;
|
|
29
|
+
getValidatorForTypeData: (type: any) => any;
|
|
30
|
+
uiSchema: {
|
|
31
|
+
type: string;
|
|
32
|
+
required: string[];
|
|
33
|
+
properties: {
|
|
34
|
+
'ui:title': {
|
|
35
|
+
type: string;
|
|
36
|
+
};
|
|
37
|
+
'ui:helpertext': {
|
|
38
|
+
type: string;
|
|
39
|
+
};
|
|
40
|
+
'ui:bc-helpertext': {
|
|
41
|
+
type: string;
|
|
42
|
+
};
|
|
43
|
+
'ui:bc-required': {
|
|
44
|
+
type: string;
|
|
45
|
+
};
|
|
46
|
+
'ui:bc-multiSelect': {
|
|
47
|
+
type: string;
|
|
48
|
+
};
|
|
49
|
+
'ui:widget': {
|
|
50
|
+
type: string;
|
|
51
|
+
enum: string[];
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
schemaTypes: {
|
|
56
|
+
string: {
|
|
57
|
+
type: string;
|
|
58
|
+
required: string[];
|
|
59
|
+
properties: {
|
|
60
|
+
title: {
|
|
61
|
+
type: string;
|
|
62
|
+
minLength: number;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
number: {
|
|
67
|
+
type: string;
|
|
68
|
+
required: string[];
|
|
69
|
+
properties: {
|
|
70
|
+
title: {
|
|
71
|
+
type: string;
|
|
72
|
+
minLength: number;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
boolean: {
|
|
77
|
+
type: string;
|
|
78
|
+
required: string[];
|
|
79
|
+
properties: {
|
|
80
|
+
title: {
|
|
81
|
+
type: string;
|
|
82
|
+
minLength: number;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
date: {
|
|
87
|
+
type: string;
|
|
88
|
+
required: string[];
|
|
89
|
+
properties: {
|
|
90
|
+
title: {
|
|
91
|
+
type: string;
|
|
92
|
+
minLength: number;
|
|
93
|
+
};
|
|
94
|
+
type: {
|
|
95
|
+
const: string;
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
dateRange: {
|
|
100
|
+
type: string;
|
|
101
|
+
required: string[];
|
|
102
|
+
properties: {
|
|
103
|
+
title: {
|
|
104
|
+
type: string;
|
|
105
|
+
minLength: number;
|
|
106
|
+
};
|
|
107
|
+
type: {
|
|
108
|
+
const: string;
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
phone: {
|
|
113
|
+
type: string;
|
|
114
|
+
required: string[];
|
|
115
|
+
properties: {
|
|
116
|
+
title: {
|
|
117
|
+
type: string;
|
|
118
|
+
minLength: number;
|
|
119
|
+
};
|
|
120
|
+
type: {
|
|
121
|
+
const: string;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
skills: {
|
|
126
|
+
type: string;
|
|
127
|
+
required: string[];
|
|
128
|
+
properties: {
|
|
129
|
+
title: {
|
|
130
|
+
type: string;
|
|
131
|
+
minLength: number;
|
|
132
|
+
};
|
|
133
|
+
uniqueItems: {
|
|
134
|
+
type: string;
|
|
135
|
+
};
|
|
136
|
+
items: {
|
|
137
|
+
type: string;
|
|
138
|
+
properties: {
|
|
139
|
+
anyOf: {
|
|
140
|
+
type: string;
|
|
141
|
+
minItems: number;
|
|
142
|
+
items: {
|
|
143
|
+
type: string;
|
|
144
|
+
properties: {
|
|
145
|
+
type: {
|
|
146
|
+
type: string;
|
|
147
|
+
};
|
|
148
|
+
title: {
|
|
149
|
+
type: string;
|
|
150
|
+
};
|
|
151
|
+
enum: {
|
|
152
|
+
type: string;
|
|
153
|
+
minItems: number;
|
|
154
|
+
items: {
|
|
155
|
+
type: string;
|
|
156
|
+
minLength: number;
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
qualifications: {
|
|
167
|
+
type: string;
|
|
168
|
+
required: string[];
|
|
169
|
+
properties: {
|
|
170
|
+
title: {
|
|
171
|
+
type: string;
|
|
172
|
+
minLength: number;
|
|
173
|
+
};
|
|
174
|
+
uniqueItems: {
|
|
175
|
+
type: string;
|
|
176
|
+
};
|
|
177
|
+
items: {
|
|
178
|
+
type: string;
|
|
179
|
+
properties: {
|
|
180
|
+
type: {
|
|
181
|
+
const: string;
|
|
182
|
+
};
|
|
183
|
+
enum: {
|
|
184
|
+
type: string;
|
|
185
|
+
minItems: number;
|
|
186
|
+
items: {
|
|
187
|
+
type: string;
|
|
188
|
+
}[];
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
array: {
|
|
195
|
+
type: string;
|
|
196
|
+
required: string[];
|
|
197
|
+
properties: {
|
|
198
|
+
title: {
|
|
199
|
+
type: string;
|
|
200
|
+
minLength: number;
|
|
201
|
+
};
|
|
202
|
+
uniqueItems: {
|
|
203
|
+
type: string;
|
|
204
|
+
};
|
|
205
|
+
items: {
|
|
206
|
+
type: string;
|
|
207
|
+
properties: {
|
|
208
|
+
anyOf: {
|
|
209
|
+
type: string;
|
|
210
|
+
minItems: number;
|
|
211
|
+
uniqueItems: boolean;
|
|
212
|
+
items: {
|
|
213
|
+
type: string;
|
|
214
|
+
properties: {
|
|
215
|
+
type: {
|
|
216
|
+
type: string;
|
|
217
|
+
};
|
|
218
|
+
title: {
|
|
219
|
+
type: string;
|
|
220
|
+
};
|
|
221
|
+
enum: {
|
|
222
|
+
type: string;
|
|
223
|
+
minItems: number;
|
|
224
|
+
items: {
|
|
225
|
+
type: string;
|
|
226
|
+
minLength: number;
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
heading: {
|
|
237
|
+
type: string;
|
|
238
|
+
required: string[];
|
|
239
|
+
properties: {
|
|
240
|
+
title: {
|
|
241
|
+
type: string;
|
|
242
|
+
minLength: number;
|
|
243
|
+
};
|
|
244
|
+
type: {
|
|
245
|
+
const: string;
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
spacer: {
|
|
250
|
+
type: string;
|
|
251
|
+
properties: {
|
|
252
|
+
type: {
|
|
253
|
+
const: string;
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
location: {
|
|
258
|
+
type: string;
|
|
259
|
+
required: string[];
|
|
260
|
+
properties: {
|
|
261
|
+
title: {
|
|
262
|
+
type: string;
|
|
263
|
+
minLength: number;
|
|
264
|
+
};
|
|
265
|
+
type: {
|
|
266
|
+
const: string;
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
file: {
|
|
271
|
+
type: string;
|
|
272
|
+
required: string[];
|
|
273
|
+
properties: {
|
|
274
|
+
title: {
|
|
275
|
+
type: string;
|
|
276
|
+
minLength: number;
|
|
277
|
+
};
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
textParagraph: {
|
|
281
|
+
type: string;
|
|
282
|
+
required: string[];
|
|
283
|
+
properties: {
|
|
284
|
+
text: {
|
|
285
|
+
type: string;
|
|
286
|
+
minLength: number;
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
dataTypeIsValid: {
|
|
292
|
+
string: (data: any, schema: any) => any;
|
|
293
|
+
boolean: (data: any) => boolean;
|
|
294
|
+
number: (data: any) => boolean;
|
|
295
|
+
date: (data: any) => boolean;
|
|
296
|
+
dateRange: (data: any) => boolean;
|
|
297
|
+
phone: (data: any) => any;
|
|
298
|
+
skills: (data: any, schema: any) => boolean;
|
|
299
|
+
qualifications: (data: any, schema: any) => boolean;
|
|
300
|
+
array: (data: any, schema: any) => boolean;
|
|
301
|
+
heading: () => boolean;
|
|
302
|
+
spacer: () => boolean;
|
|
303
|
+
location: (data: any) => boolean;
|
|
304
|
+
file: (data: any) => boolean;
|
|
305
|
+
};
|
|
306
|
+
dataTypes: {
|
|
307
|
+
string: (schema: any, data: any) => Promise<void>;
|
|
308
|
+
boolean: (schema: any, data: any) => Promise<void>;
|
|
309
|
+
date: (schema: any, data: any) => Promise<void>;
|
|
310
|
+
dateRange: (schema: any, data: any) => Promise<void>;
|
|
311
|
+
phone: (schema: any, data: any) => Promise<void>;
|
|
312
|
+
skills: (schema: any, data: any) => Promise<void>;
|
|
313
|
+
qualifications: (schema: any, data: any) => Promise<void>;
|
|
314
|
+
array: (schema: any, data: any) => Promise<void>;
|
|
315
|
+
heading: (schema: any, data: any) => Promise<void>;
|
|
316
|
+
spacer: (schema: any, data: any) => Promise<void>;
|
|
317
|
+
location: (schema: any, data: any) => Promise<void>;
|
|
318
|
+
file: (schema: any, data: any) => Promise<void>;
|
|
319
|
+
number: (schema: any, data: any) => Promise<void>;
|
|
320
|
+
textParagraph: (schema: any, data: any) => Promise<void>;
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
export default validator;
|
package/lib/forms.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validator = void 0;
|
|
6
7
|
/* eslint-disable no-prototype-builtins */
|
|
7
8
|
const ajv_1 = __importDefault(require("ajv"));
|
|
8
9
|
const ajv = new ajv_1.default({
|
|
@@ -60,14 +61,14 @@ const isValidYear = (value) => {
|
|
|
60
61
|
};
|
|
61
62
|
const validateType = async (params) => {
|
|
62
63
|
const { type, data, schema, error } = params;
|
|
63
|
-
if (validator.dataTypeIsValid[type](data, schema)) {
|
|
64
|
+
if (exports.validator.dataTypeIsValid[type](data, schema)) {
|
|
64
65
|
return;
|
|
65
66
|
}
|
|
66
67
|
throw {
|
|
67
68
|
name: error || `Property is not a ${type}`
|
|
68
69
|
};
|
|
69
70
|
};
|
|
70
|
-
|
|
71
|
+
exports.validator = {
|
|
71
72
|
validateSchema: async (schema, uischema) => {
|
|
72
73
|
if (!schema.hasOwnProperty('properties')) {
|
|
73
74
|
throw {
|
|
@@ -83,19 +84,19 @@ const validator = {
|
|
|
83
84
|
let result;
|
|
84
85
|
const errors = [];
|
|
85
86
|
const keys = Object.keys(schema.properties);
|
|
86
|
-
result = validator.validateSchemaPropertiesExist(schema.required, keys, 'required-item-not-found');
|
|
87
|
+
result = exports.validator.validateSchemaPropertiesExist(schema.required, keys, 'required-item-not-found');
|
|
87
88
|
if (result) {
|
|
88
89
|
throw result;
|
|
89
90
|
}
|
|
90
|
-
result = validator.validateSchemaPropertiesExist(uischema['ui:order'], keys, 'order-item-not-found');
|
|
91
|
+
result = exports.validator.validateSchemaPropertiesExist(uischema['ui:order'], keys, 'order-item-not-found');
|
|
91
92
|
if (result) {
|
|
92
93
|
throw result;
|
|
93
94
|
}
|
|
94
|
-
result = validator.validateSchemaPropertiesExist(keys, Object.keys(uischema), 'ui-data-schema-mismatch');
|
|
95
|
+
result = exports.validator.validateSchemaPropertiesExist(keys, Object.keys(uischema), 'ui-data-schema-mismatch');
|
|
95
96
|
if (result) {
|
|
96
97
|
throw result;
|
|
97
98
|
}
|
|
98
|
-
result = validator.validatePropertiesTitleUniqueness(schema.properties, 'Titles must be unique.');
|
|
99
|
+
result = exports.validator.validatePropertiesTitleUniqueness(schema.properties, 'Titles must be unique.');
|
|
99
100
|
if (result) {
|
|
100
101
|
throw result;
|
|
101
102
|
}
|
|
@@ -115,7 +116,7 @@ const validator = {
|
|
|
115
116
|
bcRequired: false
|
|
116
117
|
}
|
|
117
118
|
*/
|
|
118
|
-
const f = validator.getValidatorForType(o.bcType || o.type);
|
|
119
|
+
const f = exports.validator.getValidatorForType(o.bcType || o.type);
|
|
119
120
|
if (o.hasOwnProperty('bcReportable')) {
|
|
120
121
|
if (!BC_REPORTABLE_FIELDS[o.bcReportable] && !o.bcReportable.startsWith('USER_')) {
|
|
121
122
|
errors.push({
|
|
@@ -155,7 +156,7 @@ const validator = {
|
|
|
155
156
|
return;
|
|
156
157
|
}
|
|
157
158
|
const data = uischema[o];
|
|
158
|
-
const valid = ajv.validate(validator.uiSchema, data);
|
|
159
|
+
const valid = ajv.validate(exports.validator.uiSchema, data);
|
|
159
160
|
if (valid !== true) {
|
|
160
161
|
errors.push({ name: 'invalid-uischema-property', object: o });
|
|
161
162
|
}
|
|
@@ -268,7 +269,7 @@ const validator = {
|
|
|
268
269
|
// value is the data the user submits for the above property
|
|
269
270
|
// eg. +61434111222
|
|
270
271
|
try {
|
|
271
|
-
const v = validator.getValidatorForTypeData(property.bcType || property.type);
|
|
272
|
+
const v = exports.validator.getValidatorForTypeData(property.bcType || property.type);
|
|
272
273
|
await v(property, value);
|
|
273
274
|
}
|
|
274
275
|
catch (e) {
|
|
@@ -281,10 +282,10 @@ const validator = {
|
|
|
281
282
|
return;
|
|
282
283
|
},
|
|
283
284
|
getValidatorForType: (type) => {
|
|
284
|
-
return validator.schemaTypes[type] || null;
|
|
285
|
+
return exports.validator.schemaTypes[type] || null;
|
|
285
286
|
},
|
|
286
287
|
getValidatorForTypeData: (type) => {
|
|
287
|
-
const v = validator.dataTypes[type];
|
|
288
|
+
const v = exports.validator.dataTypes[type];
|
|
288
289
|
if (v) {
|
|
289
290
|
return v;
|
|
290
291
|
}
|
|
@@ -564,8 +565,8 @@ const validator = {
|
|
|
564
565
|
return typeof data === 'string' && new Date(data).toString() !== 'Invalid Date' && isValidYear(data);
|
|
565
566
|
},
|
|
566
567
|
dateRange: (data) => {
|
|
567
|
-
const fromIsValid = validator.dataTypeIsValid.date(data.from);
|
|
568
|
-
const toIsValid = validator.dataTypeIsValid.date(data.to);
|
|
568
|
+
const fromIsValid = exports.validator.dataTypeIsValid.date(data.from);
|
|
569
|
+
const toIsValid = exports.validator.dataTypeIsValid.date(data.to);
|
|
569
570
|
const toDateIsAFterFromDate = new Date(data.from) <= new Date(data.to);
|
|
570
571
|
return fromIsValid && toIsValid && toDateIsAFterFromDate;
|
|
571
572
|
},
|
|
@@ -725,4 +726,4 @@ const validator = {
|
|
|
725
726
|
textParagraph: (schema, data) => validateType({ type: 'string', data, schema }),
|
|
726
727
|
},
|
|
727
728
|
};
|
|
728
|
-
|
|
729
|
+
exports.default = exports.validator;
|
package/package.json
CHANGED
package/src/forms.ts
CHANGED
|
@@ -81,7 +81,7 @@ const validateType = async (params: { type: string, data: any, schema?: any, err
|
|
|
81
81
|
};
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
const validator = {
|
|
84
|
+
export const validator = {
|
|
85
85
|
validateSchema: async (schema, uischema) => {
|
|
86
86
|
if (!schema.hasOwnProperty('properties')) {
|
|
87
87
|
throw {
|
|
@@ -804,4 +804,4 @@ const validator = {
|
|
|
804
804
|
},
|
|
805
805
|
};
|
|
806
806
|
|
|
807
|
-
|
|
807
|
+
export default validator;
|