@opengis/fastify-table 1.2.4 → 1.2.6

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.6",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -37,13 +37,17 @@ export default async function getAccess({ table, user = {} }) {
37
37
 
38
38
  const { uid, user_type: userType = 'regular' } = user;
39
39
 
40
- if (userType.includes('admin')) {
40
+ if (userType === 'superadmin') {
41
41
  return { actions: ['view', 'edit', 'add', 'del'], query: '1=1' };
42
42
  }
43
43
 
44
44
  const body = await getTemplate('table', table);
45
45
  const tableActions = ['view'].concat(body?.actions || body?.action_default || []);
46
46
 
47
+ if (userType === 'admin') {
48
+ return { actions: body?.actions || body?.action_default ? tableActions : ['view', 'edit', 'add', 'del'], query: '1=1' };
49
+ }
50
+
47
51
  if (body?.public || body?.access === 'public') {
48
52
  return { actions: tableActions, query: '1=1' };
49
53
  }
@@ -1,22 +1,53 @@
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, idx) {
5
+ // validators: [required]
6
+ if (options?.validators?.includes('required') && !val) {
7
+ return {
8
+ error: 'empty required', key, idx,
9
+ };
11
10
  }
12
11
 
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));
16
-
17
- if (invalidEmailKey) {
18
- return { error: 'invalid email', key: emptyRequiredKey, val: body[invalidEmailKey] };
12
+ // validators: [email] / type: Email
13
+ if ((options.type?.toLowerCase() === 'email' || options?.validators?.includes('email')) && val && !val.match(emailReg)) {
14
+ return {
15
+ error: 'invalid email', key, val, idx,
16
+ };
17
+ }
18
+ return { key, val, idx };
19
+ }
20
+
21
+ function checkBody({ body = {}, arr = [], idx }) {
22
+ const res = Object.keys(body).reduce((acc1, key) => {
23
+ const input = arr.find(el => el.key === key) || {};
24
+
25
+ if (input.colModel?.length && input.type?.toLowerCase() === 'datatable') {
26
+ const result = body[key].reduce((acc, item, i) => {
27
+ const check = checkBody({ body: item, arr: input.colModel, idx: i });
28
+ acc.push(check);
29
+ return acc;
30
+ }, []);
31
+ acc1 = acc1.concat(result);
32
+ return acc1;
33
+ }
34
+
35
+ const check = checkField(key, body[key], input, idx) || {};
36
+ acc1.push({ key, val: body[key], ...check });
37
+ return acc1;
38
+ }, []);
39
+
40
+ const invalidField = res.find(el => el.error);
41
+
42
+ if (invalidField) {
43
+ console.warn('invalid field: ', invalidField.key, invalidField.error);
44
+ return invalidField;
19
45
  }
20
46
 
21
47
  return { message: 'ok' };
48
+ }
49
+
50
+ export default function validateData({ body = {}, schema = {} }) {
51
+ const res = checkBody({ body, arr: Object.keys(schema).map(key => ({ ...schema[key], key })) });
52
+ return res;
22
53
  }
@@ -28,7 +28,7 @@ export default async function metaFormat({
28
28
  if (!clsValues) return null;
29
29
 
30
30
  rows.forEach(el => {
31
- const val = el[attr.name]?.map?.(c => clsValues[c.toString()] || clsValues[c] || c) || clsValues[el[attr.name]?.toString()] || clsValues[el[attr.name]] || el[attr.name];
31
+ const val = el[attr.name]?.map?.(c => c ? clsValues[c.toString()] || clsValues[c] || c : null) || clsValues[el[attr.name]?.toString()] || clsValues[el[attr.name]] || el[attr.name];
32
32
  if (!val) return;
33
33
  if (!sufix) {
34
34
  Object.assign(el, { [attr.name]: val.text || val });