@dwtechs/antity 0.13.0 → 0.15.0
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/README.md +82 -74
- package/dist/antity.d.ts +9 -16
- package/dist/antity.js +69 -95
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,12 +52,8 @@ const entity = new Entity("consumers", [
|
|
|
52
52
|
min: 0,
|
|
53
53
|
max: 120,
|
|
54
54
|
typeCheck: true,
|
|
55
|
-
|
|
56
|
-
required: true,
|
|
55
|
+
need: ["PUT"],
|
|
57
56
|
safe: true,
|
|
58
|
-
sanitize: true,
|
|
59
|
-
normalize: true,
|
|
60
|
-
validate: true,
|
|
61
57
|
sanitizer: null,
|
|
62
58
|
normalizer: null,
|
|
63
59
|
validator: null,
|
|
@@ -68,12 +64,8 @@ const entity = new Entity("consumers", [
|
|
|
68
64
|
min: 0,
|
|
69
65
|
max: 255,
|
|
70
66
|
typeCheck: true,
|
|
71
|
-
|
|
72
|
-
required: true,
|
|
67
|
+
need: ["PUT"],
|
|
73
68
|
safe: true,
|
|
74
|
-
sanitize: true,
|
|
75
|
-
normalize: true,
|
|
76
|
-
validate: true,
|
|
77
69
|
sanitizer: null,
|
|
78
70
|
normalizer: normalizeName,
|
|
79
71
|
validator: null,
|
|
@@ -84,12 +76,8 @@ const entity = new Entity("consumers", [
|
|
|
84
76
|
min: 0,
|
|
85
77
|
max: 255,
|
|
86
78
|
typeCheck: true,
|
|
87
|
-
|
|
88
|
-
required: true,
|
|
79
|
+
need: ["PUT"],
|
|
89
80
|
safe: true,
|
|
90
|
-
sanitize: true,
|
|
91
|
-
normalize: true,
|
|
92
|
-
validate: true,
|
|
93
81
|
sanitizer: null,
|
|
94
82
|
normalizer: normalizeName,
|
|
95
83
|
validator: null,
|
|
@@ -100,21 +88,17 @@ const entity = new Entity("consumers", [
|
|
|
100
88
|
min: 0,
|
|
101
89
|
max: 255,
|
|
102
90
|
typeCheck: true,
|
|
103
|
-
|
|
104
|
-
required: true,
|
|
91
|
+
need: ["PUT"],
|
|
105
92
|
safe: true,
|
|
106
|
-
sanitize: true,
|
|
107
|
-
normalize: true,
|
|
108
|
-
validate: true,
|
|
109
93
|
sanitizer: null,
|
|
110
94
|
normalizer: normalizeNickname,
|
|
111
95
|
validator: null,
|
|
112
96
|
},
|
|
113
97
|
]);
|
|
114
98
|
|
|
115
|
-
// add a consumer. Used when
|
|
116
|
-
router.post("/", entity.
|
|
117
|
-
// or use check method to
|
|
99
|
+
// add a consumer. Used when logging in from user service
|
|
100
|
+
router.post("/", entity.normalizeArray, entity.validateArray, ...);
|
|
101
|
+
// or use check method to normalize and validate array at once
|
|
118
102
|
router.put("/", entity.check, ...);
|
|
119
103
|
|
|
120
104
|
```
|
|
@@ -154,20 +138,16 @@ type Type =
|
|
|
154
138
|
"node" |
|
|
155
139
|
"object";
|
|
156
140
|
|
|
157
|
-
type Method = "
|
|
141
|
+
type Method = "PATCH" | "PUT" | "POST";
|
|
158
142
|
|
|
159
143
|
class Property {
|
|
160
144
|
key: string;
|
|
161
145
|
type: Type;
|
|
162
146
|
min: number | Date | null;
|
|
163
147
|
max: number | Date | null;
|
|
164
|
-
|
|
165
|
-
|
|
148
|
+
need: Method[];
|
|
149
|
+
send: boolean;
|
|
166
150
|
typeCheck: boolean;
|
|
167
|
-
methods: Method[];
|
|
168
|
-
sanitize: boolean;
|
|
169
|
-
normalize: boolean;
|
|
170
|
-
validate: boolean;
|
|
171
151
|
sanitizer: Function | null;
|
|
172
152
|
normalizer: Function | null;
|
|
173
153
|
validator: Function | null;
|
|
@@ -227,11 +207,12 @@ class Entity {
|
|
|
227
207
|
getPropsByMethod(method: Method): Property[];
|
|
228
208
|
|
|
229
209
|
/**
|
|
230
|
-
* Normalizes
|
|
210
|
+
* Normalizes an array of records by applying sanitization and normalization
|
|
211
|
+
* rules defined in the properties of the class.
|
|
231
212
|
*
|
|
232
|
-
* - Applies sanitization if `sanitize: true`
|
|
233
|
-
* - Applies normalization if `normalize: true`
|
|
234
|
-
* - Mutates req.body.rows with sanitized/normalized values
|
|
213
|
+
* - Applies sanitization if `sanitize: true`
|
|
214
|
+
* - Applies normalization if `normalize: true`
|
|
215
|
+
* - Mutates req.body.rows or req.body with sanitized/normalized values
|
|
235
216
|
* - Calls next(error) on failure, next() on success
|
|
236
217
|
*
|
|
237
218
|
* @param {Request} req - Express request object containing rows
|
|
@@ -241,25 +222,58 @@ class Entity {
|
|
|
241
222
|
* @returns {void}
|
|
242
223
|
*
|
|
243
224
|
* **Input Properties Required:**
|
|
244
|
-
* - `req.body.rows` (array) - Array of objects to normalize
|
|
225
|
+
* - `req.body.rows` (array) or `req.body` (array) - Array of objects to normalize
|
|
245
226
|
* - Each property config can specify sanitize, normalize, etc.
|
|
246
227
|
*
|
|
247
228
|
* **Output Properties:**
|
|
248
|
-
* - Mutates
|
|
249
|
-
* - Calls next(error) if
|
|
229
|
+
* - Mutates array with sanitized/normalized values
|
|
230
|
+
* - Calls next(error) if normalization fails, next() if all pass
|
|
250
231
|
*
|
|
251
232
|
* @example
|
|
252
233
|
* ```typescript
|
|
253
|
-
* router.post('/
|
|
234
|
+
* router.post('/entities', entity.normalizeArray, (req, res) => {
|
|
254
235
|
* // req.body.rows are now sanitized and normalized
|
|
255
236
|
* res.json({ success: true });
|
|
256
237
|
* });
|
|
257
238
|
* ```
|
|
258
239
|
*/
|
|
259
|
-
|
|
240
|
+
normalizeArray: (req: Request, _res: Response, next: NextFunction) => void;
|
|
260
241
|
|
|
261
242
|
/**
|
|
262
|
-
*
|
|
243
|
+
* Normalizes a single record by applying sanitization and normalization
|
|
244
|
+
* rules defined in the properties of the class.
|
|
245
|
+
*
|
|
246
|
+
* - Applies sanitization if `sanitize: true`
|
|
247
|
+
* - Applies normalization if `normalize: true`
|
|
248
|
+
* - Mutates req.body with sanitized/normalized values
|
|
249
|
+
* - Calls next(error) on failure, next() on success
|
|
250
|
+
*
|
|
251
|
+
* @param {Request} req - Express request object containing a single record
|
|
252
|
+
* @param {Response} _res - Express response object (not used)
|
|
253
|
+
* @param {NextFunction} next - Express next function
|
|
254
|
+
*
|
|
255
|
+
* @returns {void}
|
|
256
|
+
*
|
|
257
|
+
* **Input Properties Required:**
|
|
258
|
+
* - `req.body` (object) - Single object to normalize
|
|
259
|
+
* - Each property config can specify sanitize, normalize, etc.
|
|
260
|
+
*
|
|
261
|
+
* **Output Properties:**
|
|
262
|
+
* - Mutates `req.body` with sanitized/normalized values
|
|
263
|
+
* - Calls next(error) if normalization fails, next() if success
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* router.post('/entity', entity.normalizeOne, (req, res) => {
|
|
268
|
+
* // req.body is now sanitized and normalized
|
|
269
|
+
* res.json({ success: true });
|
|
270
|
+
* });
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
normalizeOne: (req: Request, _res: Response, next: NextFunction) => void;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Validates an array of rows according to property config and HTTP method.
|
|
263
277
|
*
|
|
264
278
|
* - Checks required properties and validates values
|
|
265
279
|
* - Calls next(error) on failure, next() on success
|
|
@@ -271,7 +285,7 @@ class Entity {
|
|
|
271
285
|
* @returns {void}
|
|
272
286
|
*
|
|
273
287
|
* **Input Properties Required:**
|
|
274
|
-
* - `req.body.rows` (array) - Array of objects to validate
|
|
288
|
+
* - `req.body.rows` (array) or `req.body` (array) - Array of objects to validate
|
|
275
289
|
* - Each property config can specify validate, required, etc.
|
|
276
290
|
*
|
|
277
291
|
* **Output Properties:**
|
|
@@ -279,50 +293,48 @@ class Entity {
|
|
|
279
293
|
*
|
|
280
294
|
* @example
|
|
281
295
|
* ```typescript
|
|
282
|
-
* router.post('/
|
|
296
|
+
* router.post('/entities', entity.validateArray, (req, res) => {
|
|
283
297
|
* // req.body.rows are now validated
|
|
284
298
|
* res.json({ success: true });
|
|
285
299
|
* });
|
|
286
300
|
* ```
|
|
287
301
|
*/
|
|
288
|
-
|
|
289
|
-
|
|
302
|
+
validateArray: (req: Request, _res: Response, next: NextFunction) => void;
|
|
303
|
+
|
|
290
304
|
/**
|
|
291
|
-
*
|
|
305
|
+
* Validates a single record according to property config and HTTP method.
|
|
292
306
|
*
|
|
293
|
-
* - Applies sanitization if `sanitize: true` and method matches
|
|
294
|
-
* - Applies normalization if `normalize: true` and method matches
|
|
295
307
|
* - Checks required properties and validates values
|
|
296
308
|
* - Calls next(error) on failure, next() on success
|
|
297
309
|
*
|
|
298
|
-
* @param {Request} req - Express request object containing
|
|
310
|
+
* @param {Request} req - Express request object containing a single record
|
|
299
311
|
* @param {Response} _res - Express response object (not used)
|
|
300
312
|
* @param {NextFunction} next - Express next function
|
|
301
313
|
*
|
|
302
314
|
* @returns {void}
|
|
303
315
|
*
|
|
304
316
|
* **Input Properties Required:**
|
|
305
|
-
* - `req.body
|
|
306
|
-
* - Each property config can specify
|
|
317
|
+
* - `req.body` (object) - Single object to validate
|
|
318
|
+
* - Each property config can specify validate, required, etc.
|
|
307
319
|
*
|
|
308
320
|
* **Output Properties:**
|
|
309
|
-
* -
|
|
310
|
-
* - Calls next(error) if any row fails checks, next() if all pass
|
|
321
|
+
* - Calls next(error) if validation fails, next() if success
|
|
311
322
|
*
|
|
312
323
|
* @example
|
|
313
324
|
* ```typescript
|
|
314
|
-
* router.post('/entity', entity.
|
|
315
|
-
* // req.body
|
|
325
|
+
* router.post('/entity', entity.validateOne, (req, res) => {
|
|
326
|
+
* // req.body is now validated
|
|
316
327
|
* res.json({ success: true });
|
|
317
328
|
* });
|
|
318
329
|
* ```
|
|
319
330
|
*/
|
|
320
|
-
|
|
321
|
-
}
|
|
331
|
+
validateOne: (req: Request, _res: Response, next: NextFunction) => void;
|
|
322
332
|
|
|
323
333
|
```
|
|
324
|
-
|
|
325
|
-
|
|
334
|
+
**normalizeArray()**, **normalizeOne()**, **validateArray()**, and **validateOne()** methods are made to be used as Express.js middlewares.
|
|
335
|
+
|
|
336
|
+
- **normalizeArray()** and **validateArray()** will look for data in the **req.body.rows** parameter or **req.body** as an array.
|
|
337
|
+
- **normalizeOne()** and **validateOne()** will look for data in the **req.body** parameter as a single object.
|
|
326
338
|
|
|
327
339
|
|
|
328
340
|
### Password validation
|
|
@@ -358,22 +370,18 @@ Properties **min** and **max** of the password properties will override default
|
|
|
358
370
|
|
|
359
371
|
Any of these can be passed into the options object for each function.
|
|
360
372
|
|
|
361
|
-
| Name | Type
|
|
362
|
-
| :-------------- |
|
|
363
|
-
| key |
|
|
364
|
-
| type |
|
|
365
|
-
| min |
|
|
366
|
-
| max |
|
|
367
|
-
|
|
|
368
|
-
|
|
|
369
|
-
| typeCheck |
|
|
370
|
-
|
|
|
371
|
-
|
|
|
372
|
-
|
|
|
373
|
-
| validate | boolean | validate the property if true | true
|
|
374
|
-
| sanitizer | ((v:any) => any) \| null | Custom sanitizer function if sanitize is true | null
|
|
375
|
-
| normalizer | ((v:any) => any) \| null | Custop Normalizer function if normalize is true | null
|
|
376
|
-
| validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) \| null | validator function if validate is true | null
|
|
373
|
+
| Name | Type | Description | Default value |
|
|
374
|
+
| :-------------- | :----------------------- | :----------------------------------------------- | :-------------- |
|
|
375
|
+
| key | string | Name of the property |
|
|
376
|
+
| type | Type | Type of the property |
|
|
377
|
+
| min | number \| Date | Minimum value if applicable | 0 \| 1900-01-01
|
|
378
|
+
| max | number \| Date | Maximum value if applicable | 999999999 \| 2200-12-31
|
|
379
|
+
| need | Methods[] | Property is required for the listed methods only | [ "POST", "PUT", "PATCH" ]
|
|
380
|
+
| send | boolean | Property is sent in the response | true
|
|
381
|
+
| typeCheck | boolean | Strict type check at validation | false
|
|
382
|
+
| sanitizer | ((v:any) => any) \| null | Custom sanitizer function | null
|
|
383
|
+
| normalizer | ((v:any) => any) \| null | Custom Normalizer function | null
|
|
384
|
+
| validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) \| null | Custom validator | null
|
|
377
385
|
|
|
378
386
|
* *Min and max parameters are not used for boolean type*
|
|
379
387
|
* *TypeCheck Parameter is not used for boolean, string and array types*
|
package/dist/antity.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ https://github.com/DWTechs/Antity.js
|
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
27
|
import type { Request, Response, NextFunction } from 'express';
|
|
28
|
-
declare const Methods: readonly [
|
|
28
|
+
declare const Methods: readonly ["PATCH", "PUT", "POST"];
|
|
29
29
|
type Method = typeof Methods[number];
|
|
30
30
|
type Type =
|
|
31
31
|
"boolean" |
|
|
@@ -68,9 +68,10 @@ declare class Entity {
|
|
|
68
68
|
set name(name: string);
|
|
69
69
|
getProp(key: string): Property | undefined;
|
|
70
70
|
getPropsByMethod(method: Method): Property[];
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
normalizeArray: (req: Request, _res: Response, next: NextFunction) => void;
|
|
72
|
+
normalizeOne: (req: Request, _res: Response, next: NextFunction) => void;
|
|
73
|
+
validateArray: (req: Request, _res: Response, next: NextFunction) => void;
|
|
74
|
+
validateOne: (req: Request, _res: Response, next: NextFunction) => void;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
declare class Property {
|
|
@@ -79,13 +80,9 @@ declare class Property {
|
|
|
79
80
|
type: Type;
|
|
80
81
|
min: number | Date | null;
|
|
81
82
|
max: number | Date | null;
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
send: boolean;
|
|
84
|
+
need: Method[];
|
|
84
85
|
typeCheck: boolean;
|
|
85
|
-
methods: Method[];
|
|
86
|
-
sanitize: boolean;
|
|
87
|
-
normalize: boolean;
|
|
88
|
-
validate: boolean;
|
|
89
86
|
sanitizer: ((v: any) => any) | null;
|
|
90
87
|
normalizer: ((v: any) => any) | null;
|
|
91
88
|
validator: ((v: any) => any) | null;
|
|
@@ -94,13 +91,9 @@ declare class Property {
|
|
|
94
91
|
type: Type,
|
|
95
92
|
min: number | Date | null,
|
|
96
93
|
max: number | Date | null,
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
send: boolean,
|
|
95
|
+
need: Method[],
|
|
99
96
|
typeCheck: boolean,
|
|
100
|
-
methods: Method[],
|
|
101
|
-
sanitize: boolean,
|
|
102
|
-
normalize: boolean,
|
|
103
|
-
validate: boolean,
|
|
104
97
|
sanitizer: ((v: any) => any) | null,
|
|
105
98
|
normalizer: ((v: any) => any) | null,
|
|
106
99
|
validator: ((v: any) => any) | null
|
package/dist/antity.js
CHANGED
|
@@ -28,7 +28,7 @@ import { isBoolean, isStringOfLength, isValidNumber, isValidInteger, isValidFloa
|
|
|
28
28
|
import { log } from '@dwtechs/winstan';
|
|
29
29
|
|
|
30
30
|
const LOGS_PREFIX = "Antity: ";
|
|
31
|
-
const METHODS = ["
|
|
31
|
+
const METHODS = ["PATCH", "PUT", "POST"];
|
|
32
32
|
const { PWD_MIN_LENGTH_POLICY, PWD_MAX_LENGTH_POLICY, PWD_NUMBERS_POLICY, PWD_UPPERCASE_POLICY, PWD_LOWERCASE_POLICY, PWD_SYMBOLS_POLICY } = process.env;
|
|
33
33
|
const PWD_MIN_LENGTH = PWD_MIN_LENGTH_POLICY ? +PWD_MIN_LENGTH_POLICY : 9;
|
|
34
34
|
const PWD_MAX_LENGTH = PWD_MAX_LENGTH_POLICY ? +PWD_MAX_LENGTH_POLICY : 20;
|
|
@@ -135,7 +135,7 @@ const Types = {
|
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
class Property {
|
|
138
|
-
constructor(key, type, min, max,
|
|
138
|
+
constructor(key, type, min, max, send, need, typeCheck, sanitizer, normalizer, validator) {
|
|
139
139
|
try {
|
|
140
140
|
isString(key, "!0", null, true);
|
|
141
141
|
}
|
|
@@ -148,13 +148,13 @@ class Property {
|
|
|
148
148
|
catch (err) {
|
|
149
149
|
throw new Error(`${LOGS_PREFIX}Property "type" must be a valid type - caused by: ${err.message}`);
|
|
150
150
|
}
|
|
151
|
-
if (isArray(
|
|
152
|
-
for (const m of
|
|
151
|
+
if (isArray(need)) {
|
|
152
|
+
for (const m of need) {
|
|
153
153
|
try {
|
|
154
154
|
isIn(METHODS, m, 0, true);
|
|
155
155
|
}
|
|
156
156
|
catch (err) {
|
|
157
|
-
throw new Error(`${LOGS_PREFIX}Property "
|
|
157
|
+
throw new Error(`${LOGS_PREFIX}Property "need" must be an array of REST methods - caused by: ${err.message}`);
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
}
|
|
@@ -162,13 +162,9 @@ class Property {
|
|
|
162
162
|
this.type = type;
|
|
163
163
|
this.min = this.interval(min, type, 0, "1900-01-01T00:00:00Z");
|
|
164
164
|
this.max = this.interval(max, type, 999999999, "2200-12-31T00:00:00Z");
|
|
165
|
-
this.
|
|
166
|
-
this.
|
|
165
|
+
this.need = isArray(need) ? need : [];
|
|
166
|
+
this.send = isBoolean(send) ? send : true;
|
|
167
167
|
this.typeCheck = isBoolean(typeCheck) ? typeCheck : false;
|
|
168
|
-
this.methods = methods || METHODS;
|
|
169
|
-
this.sanitize = isBoolean(sanitize) ? sanitize : true;
|
|
170
|
-
this.normalize = isBoolean(normalize) ? normalize : false;
|
|
171
|
-
this.validate = isBoolean(validate) ? validate : true;
|
|
172
168
|
this.sanitizer = isFunction(sanitizer) ? sanitizer : null;
|
|
173
169
|
this.normalizer = isFunction(normalizer) ? normalizer : null;
|
|
174
170
|
this.validator = isFunction(validator) ? validator : null;
|
|
@@ -206,6 +202,21 @@ function trim(v) {
|
|
|
206
202
|
return v;
|
|
207
203
|
}
|
|
208
204
|
|
|
205
|
+
function normalize(record, properties) {
|
|
206
|
+
for (const { key, type, sanitizer, normalizer, } of properties) {
|
|
207
|
+
let v = record[key];
|
|
208
|
+
if (v) {
|
|
209
|
+
log.debug(`sanitize ${key}: ${type} = ${v}`);
|
|
210
|
+
v = sanitize(v, sanitizer);
|
|
211
|
+
if (isFunction(normalizer)) {
|
|
212
|
+
log.debug(`normalize ${key}: ${type} = ${v}`);
|
|
213
|
+
v = normalizer(v);
|
|
214
|
+
}
|
|
215
|
+
record[key] = v;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
209
220
|
function control(v, key, type, min, max, typeCheck, cb) {
|
|
210
221
|
log.debug(`control ${key}: ${type} = ${v}`);
|
|
211
222
|
let errorMessage = "";
|
|
@@ -233,38 +244,50 @@ function require(v, key, type) {
|
|
|
233
244
|
return isNil(v) ? { statusCode: 400, message: `${LOGS_PREFIX}Missing ${key} of type ${type}` } : null;
|
|
234
245
|
}
|
|
235
246
|
|
|
247
|
+
function validate(record, properties, method) {
|
|
248
|
+
for (const { key, type, min, max, need, typeCheck, validator } of properties) {
|
|
249
|
+
const v = record[key];
|
|
250
|
+
if (isIn(need, method)) {
|
|
251
|
+
const rq = require(v, key, type);
|
|
252
|
+
if (rq)
|
|
253
|
+
return rq;
|
|
254
|
+
}
|
|
255
|
+
if (v) {
|
|
256
|
+
const ct = control(v, key, type, min, max, typeCheck, validator);
|
|
257
|
+
if (ct)
|
|
258
|
+
return ct;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
|
|
236
264
|
class Entity {
|
|
237
265
|
constructor(name, properties) {
|
|
238
|
-
this.
|
|
266
|
+
this.normalizeArray = (req, _res, next) => {
|
|
239
267
|
var _a;
|
|
268
|
+
log.debug(`normalizeArray ${this.name}`);
|
|
240
269
|
const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
|
|
241
|
-
|
|
242
|
-
if (!isArray(rows, "!0"))
|
|
270
|
+
if (!isArray(rows, ">", 0))
|
|
243
271
|
return next({ statusCode: 400, message: `${LOGS_PREFIX}Normalize: no rows found in request body` });
|
|
244
272
|
for (const r of rows) {
|
|
245
|
-
|
|
246
|
-
let v = r[key];
|
|
247
|
-
if (v) {
|
|
248
|
-
if (sanitize$1) {
|
|
249
|
-
log.debug(`sanitize ${key}: ${type} = ${v}`);
|
|
250
|
-
v = sanitize(v, sanitizer);
|
|
251
|
-
}
|
|
252
|
-
if (normalize && isFunction(normalizer)) {
|
|
253
|
-
log.debug(`normalize ${key}: ${type} = ${v}`);
|
|
254
|
-
v = normalizer(v);
|
|
255
|
-
}
|
|
256
|
-
r[key] = v;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
273
|
+
normalize(r, this._properties);
|
|
259
274
|
}
|
|
260
275
|
next();
|
|
261
276
|
};
|
|
262
|
-
this.
|
|
277
|
+
this.normalizeOne = (req, _res, next) => {
|
|
278
|
+
log.debug(`normalizeOne ${this.name}`);
|
|
279
|
+
const r = req.body;
|
|
280
|
+
if (!isObject(r, true))
|
|
281
|
+
return next({ statusCode: 400, message: `${LOGS_PREFIX}Normalize: no data found in request body` });
|
|
282
|
+
normalize(r, this._properties);
|
|
283
|
+
next();
|
|
284
|
+
};
|
|
285
|
+
this.validateArray = (req, _res, next) => {
|
|
263
286
|
var _a;
|
|
287
|
+
log.debug(`validateArray ${this.name}`);
|
|
264
288
|
const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
|
|
265
289
|
const method = req.method;
|
|
266
|
-
|
|
267
|
-
if (!isArray(rows, "!0"))
|
|
290
|
+
if (!isArray(rows, ">", 0))
|
|
268
291
|
return next({ statusCode: 400, message: `${LOGS_PREFIX}Validate: no rows found in request body` });
|
|
269
292
|
if (!isIn(METHODS, method))
|
|
270
293
|
return next({
|
|
@@ -272,82 +295,33 @@ class Entity {
|
|
|
272
295
|
message: `${LOGS_PREFIX}Invalid REST method. Received: ${method}. Must be one of: ${METHODS.toString()}`
|
|
273
296
|
});
|
|
274
297
|
for (const r of rows) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (required) {
|
|
279
|
-
const rq = require(v, key, type);
|
|
280
|
-
if (rq)
|
|
281
|
-
return next(rq);
|
|
282
|
-
}
|
|
283
|
-
if (v && validate) {
|
|
284
|
-
const ct = control(v, key, type, min, max, typeCheck, validator);
|
|
285
|
-
if (ct)
|
|
286
|
-
return next(ct);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
298
|
+
const error = validate(r, this._properties, method);
|
|
299
|
+
if (error)
|
|
300
|
+
return next(error);
|
|
290
301
|
}
|
|
291
302
|
next();
|
|
292
303
|
};
|
|
293
|
-
this.
|
|
294
|
-
|
|
295
|
-
const
|
|
304
|
+
this.validateOne = (req, _res, next) => {
|
|
305
|
+
log.debug(`validateOne ${this.name}`);
|
|
306
|
+
const record = req.body;
|
|
296
307
|
const method = req.method;
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
catch (err) {
|
|
302
|
-
return next({
|
|
303
|
-
statusCode: 400,
|
|
304
|
-
message: `${LOGS_PREFIX}no rows found in request body - caused by: ${err.message}`
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
try {
|
|
308
|
-
isIn(METHODS, method, 0, true);
|
|
309
|
-
}
|
|
310
|
-
catch (err) {
|
|
308
|
+
if (!isObject(record, true))
|
|
309
|
+
return next({ statusCode: 400, message: `${LOGS_PREFIX}Validate: no data found in request body` });
|
|
310
|
+
if (!isIn(METHODS, method))
|
|
311
311
|
return next({
|
|
312
312
|
statusCode: 400,
|
|
313
|
-
message: `${LOGS_PREFIX}Invalid REST method. Must be one of: ${METHODS.toString()}
|
|
313
|
+
message: `${LOGS_PREFIX}Invalid REST method. Received: ${method}. Must be one of: ${METHODS.toString()}`
|
|
314
314
|
});
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
let v = r[key];
|
|
319
|
-
if (isIn(methods, method)) {
|
|
320
|
-
if (v) {
|
|
321
|
-
if (sanitize$1) {
|
|
322
|
-
log.debug(`sanitize ${key}: ${type} = ${v}`);
|
|
323
|
-
v = sanitize(v, sanitizer);
|
|
324
|
-
}
|
|
325
|
-
if (normalize && isFunction(normalizer)) {
|
|
326
|
-
log.debug(`normalize ${key}: ${type} = ${v}`);
|
|
327
|
-
v = normalizer(v);
|
|
328
|
-
}
|
|
329
|
-
r[key] = v;
|
|
330
|
-
if (validate) {
|
|
331
|
-
const ct = control(v, key, type, min, max, typeCheck, validator);
|
|
332
|
-
if (ct)
|
|
333
|
-
return next(ct);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
if (required) {
|
|
337
|
-
const rq = require(v, key, type);
|
|
338
|
-
if (rq)
|
|
339
|
-
return next(rq);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
315
|
+
const error = validate(record, this._properties, method);
|
|
316
|
+
if (error)
|
|
317
|
+
return next(error);
|
|
344
318
|
next();
|
|
345
319
|
};
|
|
346
320
|
this._name = name;
|
|
347
321
|
this._properties = [];
|
|
348
322
|
this._unsafeProps = [];
|
|
349
323
|
for (const p of properties) {
|
|
350
|
-
const prop = new Property(p.key, p.type, p.min, p.max, p.
|
|
324
|
+
const prop = new Property(p.key, p.type, p.min, p.max, p.send, p.need, p.typeCheck, p.sanitizer, p.normalizer, p.validator);
|
|
351
325
|
Object.assign(prop, p);
|
|
352
326
|
this._properties.push(prop);
|
|
353
327
|
if (!prop.safe)
|
|
@@ -374,7 +348,7 @@ class Entity {
|
|
|
374
348
|
getPropsByMethod(method) {
|
|
375
349
|
const props = [];
|
|
376
350
|
for (const p of this.properties) {
|
|
377
|
-
if (isIn(p.
|
|
351
|
+
if (isIn(p.need, method, 0))
|
|
378
352
|
props.push(p);
|
|
379
353
|
}
|
|
380
354
|
return props;
|