@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,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -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
- export default function validateData({ body = {}, schema = {} }) {
5
- const requiredFields = Object.keys(schema).filter(key => schema[key].validators?.includes('required'));
6
-
7
- const emptyRequiredKey = Object.keys(body).find(key => requiredFields.includes(key) && !body[key]);
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
- const emailFields = Object.keys(schema).filter(key => schema[key].type === 'Email');
14
-
15
- const invalidEmailKey = Object.keys(body).find(key => body[key] && emailFields.includes(key) && !body[key].match(emailReg));
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
- if (invalidEmailKey) {
18
- return { error: 'invalid email', key: emptyRequiredKey, val: body[invalidEmailKey] };
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' };