@opengis/fastify-table 1.2.4 → 1.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/package.json
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
|
|
2
2
|
const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g;
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (emptyRequiredKey) {
|
|
10
|
-
return { error: 'empty required', key: emptyRequiredKey };
|
|
4
|
+
function checkField(key, val, options) {
|
|
5
|
+
// validators: [required]
|
|
6
|
+
if (options?.validators?.includes('required') && !val) {
|
|
7
|
+
return { error: 'empty required', key };
|
|
11
8
|
}
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
// validators: [email] / type: Email
|
|
11
|
+
if ((options.type?.toLowerCase() === 'email' || options?.validators?.includes('email')) && val && !val.match(emailReg)) {
|
|
12
|
+
return { error: 'invalid email', key, val };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
export default function validateData({ body = {}, schema = {} }) {
|
|
17
|
+
const arr = Object.keys(schema).reduce((acc, key) => {
|
|
18
|
+
const curr = { ...schema[key], key };
|
|
19
|
+
if (curr.colModel?.length && curr.type?.toLowerCase() === 'datatable') {
|
|
20
|
+
return acc.concat(curr.colModel.map(col => {
|
|
21
|
+
const subkey = col.name || col.key;
|
|
22
|
+
const check = body[curr.key]?.reduce((acc, curr) => Object.assign(acc, checkField(subkey, curr[subkey], col)), {});
|
|
23
|
+
return { ...curr, ...col, check, key: `${curr.key}.${subkey}`, colModel: undefined };
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
const check = checkField(curr.key, body[curr.key], schema[curr.key]);
|
|
27
|
+
return acc.concat({ ...curr, check });
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
const invalidField = arr.find(el => el.check?.error);
|
|
31
|
+
|
|
32
|
+
if (invalidField) {
|
|
33
|
+
return { key: invalidField.key, ...invalidField.check };
|
|
19
34
|
}
|
|
20
35
|
|
|
21
36
|
return { message: 'ok' };
|