@webtypen/webframez-core 0.2.32 → 0.2.33

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.
@@ -16,6 +16,7 @@ export type DataBuilderSchema = {
16
16
  [key: string]: any;
17
17
  };
18
18
  newDataHandler?: Function;
19
+ canDelete: any;
19
20
  };
20
21
  export type DataBuilderType = {
21
22
  key: string;
@@ -48,7 +49,7 @@ export declare class DataBuilder {
48
49
  getFieldTypesFrontend(): any;
49
50
  getFieldsFrontend(fields: any, payload?: any): Promise<any>;
50
51
  getTypeFromRequest(req: any): DataBuilderType;
51
- validateFields(fields: any, data: any, errors?: any, path?: string): Promise<any>;
52
+ validateFields(db: any, type: any, fields: any, req: Request, errors?: any, path?: string): Promise<any>;
52
53
  typeForFrontend(type: any, req: any): Promise<any>;
53
54
  loadType(req: Request): Promise<{
54
55
  status: string;
@@ -73,16 +74,11 @@ export declare class DataBuilder {
73
74
  errors?: undefined;
74
75
  }>;
75
76
  delete(db: any, req: any): Promise<{
76
- status: string;
77
- errors: any;
78
- data?: undefined;
79
- } | {
80
77
  status: string;
81
78
  data: {
82
79
  _id: any;
83
80
  redirect: any;
84
81
  };
85
- errors?: undefined;
86
82
  }>;
87
83
  getField(req: Request, type: DataBuilderType, path: string): Promise<any>;
88
84
  removeArrayIndicators(str: string): string;
@@ -94,7 +94,7 @@ class DataBuilder {
94
94
  }
95
95
  return type;
96
96
  }
97
- validateFields(fields, data, errors, path) {
97
+ validateFields(db, type, fields, req, errors, path) {
98
98
  return __awaiter(this, void 0, void 0, function* () {
99
99
  if (!errors) {
100
100
  errors = {};
@@ -104,11 +104,12 @@ class DataBuilder {
104
104
  }
105
105
  for (let key in fields) {
106
106
  const fieldPath = (path ? path + "." : "") + key;
107
- const value = lodash_1.default.get(data, fieldPath);
107
+ const value = lodash_1.default.get(req.body.data, fieldPath);
108
+ // Check-Required
108
109
  if (fields[key].required) {
109
110
  let check = true;
110
111
  if (typeof fields[key].required === "function") {
111
- check = yield fields[key].required(data);
112
+ check = yield fields[key].required(req.body.data);
112
113
  }
113
114
  if (check) {
114
115
  if (value === null ||
@@ -121,11 +122,25 @@ class DataBuilder {
121
122
  }
122
123
  }
123
124
  }
125
+ // Check-Unique
126
+ if (fields[key].unique) {
127
+ let isUnique = false;
128
+ if (typeof fields[key].unique === "function") {
129
+ yield fields[key].unique(req.body.data, req);
130
+ }
131
+ else {
132
+ throw new Error("Missing unique-function ...");
133
+ }
134
+ if (isUnique) {
135
+ errors[fieldPath] = "Es gibt bereits einen anderen Datensatz mit diesem Wert.";
136
+ continue;
137
+ }
138
+ }
124
139
  if (typeof fields[key].schema === "object") {
125
140
  if (value && value.length > 0) {
126
141
  for (let i in value) {
127
142
  const entryPath = fieldPath + "[" + i + "]";
128
- errors = yield this.validateFields(fields[key].schema, data, errors, entryPath);
143
+ errors = yield this.validateFields(db, type, fields[key].schema, req.body.data, errors, entryPath);
129
144
  }
130
145
  }
131
146
  }
@@ -277,7 +292,7 @@ class DataBuilder {
277
292
  throw new Error("Missing schema fields ...");
278
293
  }
279
294
  const schemaFields = typeof type.schema.fields === "function" ? yield type.schema.fields(req) : type.schema.fields;
280
- const errors = yield this.validateFields(schemaFields, req.body.data);
295
+ const errors = yield this.validateFields(db, type, schemaFields, req);
281
296
  if (errors && Object.keys(errors).length > 0) {
282
297
  return {
283
298
  status: "error",
@@ -387,13 +402,9 @@ class DataBuilder {
387
402
  if (!type || !type.schema || !type.schema.fields) {
388
403
  throw new Error("Missing schema fields ...");
389
404
  }
390
- const schemaFields = typeof type.schema.fields === "function" ? yield type.schema.fields(req) : type.schema.fields;
391
- const errors = yield this.validateFields(schemaFields, req.body.data);
392
- if (errors && Object.keys(errors).length > 0) {
393
- return {
394
- status: "error",
395
- errors: errors,
396
- };
405
+ const canDelete = typeof type.schema.canDelete === "function" ? yield type.schema.canDelete(req) : true;
406
+ if (!canDelete) {
407
+ throw new Error("Cannot delete entry ...");
397
408
  }
398
409
  const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;
399
410
  if (!collection || collection.trim() === "") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.32",
3
+ "version": "0.2.33",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -15,6 +15,7 @@ export type DataBuilderSchema = {
15
15
  events?: { [key: string]: any };
16
16
  fields: { [key: string]: any };
17
17
  newDataHandler?: Function;
18
+ canDelete: any;
18
19
  };
19
20
 
20
21
  export type DataBuilderType = {
@@ -132,7 +133,7 @@ export class DataBuilder {
132
133
  return type;
133
134
  }
134
135
 
135
- async validateFields(fields: any, data: any, errors?: any, path?: string) {
136
+ async validateFields(db: any, type: any, fields: any, req: Request, errors?: any, path?: string) {
136
137
  if (!errors) {
137
138
  errors = {};
138
139
  }
@@ -143,11 +144,13 @@ export class DataBuilder {
143
144
 
144
145
  for (let key in fields) {
145
146
  const fieldPath = (path ? path + "." : "") + key;
146
- const value = lodash.get(data, fieldPath);
147
+ const value = lodash.get(req.body.data, fieldPath);
148
+
149
+ // Check-Required
147
150
  if (fields[key].required) {
148
151
  let check = true;
149
152
  if (typeof fields[key].required === "function") {
150
- check = await fields[key].required(data);
153
+ check = await fields[key].required(req.body.data);
151
154
  }
152
155
 
153
156
  if (check) {
@@ -164,11 +167,26 @@ export class DataBuilder {
164
167
  }
165
168
  }
166
169
 
170
+ // Check-Unique
171
+ if (fields[key].unique) {
172
+ let isUnique = false;
173
+ if (typeof fields[key].unique === "function") {
174
+ await fields[key].unique(req.body.data, req);
175
+ } else {
176
+ throw new Error("Missing unique-function ...");
177
+ }
178
+
179
+ if (isUnique) {
180
+ errors[fieldPath] = "Es gibt bereits einen anderen Datensatz mit diesem Wert.";
181
+ continue;
182
+ }
183
+ }
184
+
167
185
  if (typeof fields[key].schema === "object") {
168
186
  if (value && value.length > 0) {
169
187
  for (let i in value) {
170
188
  const entryPath = fieldPath + "[" + i + "]";
171
- errors = await this.validateFields(fields[key].schema, data, errors, entryPath);
189
+ errors = await this.validateFields(db, type, fields[key].schema, req.body.data, errors, entryPath);
172
190
  }
173
191
  }
174
192
  }
@@ -348,7 +366,7 @@ export class DataBuilder {
348
366
  }
349
367
 
350
368
  const schemaFields = typeof type.schema.fields === "function" ? await type.schema.fields(req) : type.schema.fields;
351
- const errors: any = await this.validateFields(schemaFields, req.body.data);
369
+ const errors: any = await this.validateFields(db, type, schemaFields, req);
352
370
  if (errors && Object.keys(errors).length > 0) {
353
371
  return {
354
372
  status: "error",
@@ -473,13 +491,9 @@ export class DataBuilder {
473
491
  throw new Error("Missing schema fields ...");
474
492
  }
475
493
 
476
- const schemaFields = typeof type.schema.fields === "function" ? await type.schema.fields(req) : type.schema.fields;
477
- const errors: any = await this.validateFields(schemaFields, req.body.data);
478
- if (errors && Object.keys(errors).length > 0) {
479
- return {
480
- status: "error",
481
- errors: errors,
482
- };
494
+ const canDelete = typeof type.schema.canDelete === "function" ? await type.schema.canDelete(req) : true;
495
+ if (!canDelete) {
496
+ throw new Error("Cannot delete entry ...");
483
497
  }
484
498
 
485
499
  const collection = type.schema && type.schema.collection ? type.schema.collection : undefined;