@dwtechs/antity 0.6.0 → 0.7.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 CHANGED
@@ -117,13 +117,11 @@ const entity = new Entity("consumers", [
117
117
  },
118
118
  ]);
119
119
 
120
- req.body = entity.normalize(req.body); // will also sanitize if true
121
- const check = entity.validate(req.body, "GET");
122
- const unsafeProps = entity.getUnsafeProps();
120
+ // add a consumer. Used when loggin in from user service
121
+ router.post("/", entity.normalize, entity.validate, ...);
123
122
 
124
123
  ```
125
124
 
126
-
127
125
  ## API Reference
128
126
 
129
127
 
@@ -195,40 +193,21 @@ class Entity {
195
193
  /**
196
194
  * Normalizes an array of records by applying sanitization and normalization
197
195
  * rules defined in the `properties` of the class.
198
- *
199
- * @param rows - An array of records where each record is a key-value pair.
200
- * The keys represent property names, and the values are the data
201
- * to be sanitized and normalized.
202
- * @returns An array of records with sanitized and normalized values.
203
196
  */
204
- normalize(rows: Record<string, unknown>[]): Record<string, unknown>[];
197
+ normalize(req: Request, _res: Response, next: NextFunction): void;
205
198
 
206
199
  /**
207
200
  * Validates a set of rows against the defined properties and operation/method.
208
201
  *
209
- * @param rows - An array of objects where each object represents a row to validate.
210
- * Each row is a record with string keys and unknown values.
211
- * @param operation - The operation or method to validate against. It can be of type `Operation` or `Method`.
212
- *
213
- * @returns A string containing the validation error message if validation fails, or `null` if validation passes.
214
- *
215
202
  * If a property is required and missing, or if it fails the control checks, the function returns an error message.
216
203
  * Otherwise, it returns `null` indicating successful validation.
217
204
  */
218
- validate(rows: Record<string, unknown>[], method: Method): string | null;
205
+ validate(req: Request, _res: Response, next: NextFunction): void;
219
206
  }
220
207
 
221
208
  ```
222
-
223
- examples :
224
-
225
- ```javascript
226
-
227
- entity.validate(req.body, "GET"); // will validate properties for GET requests
228
- entity.validate(req.body, "PUT"); // will validate properties for PUT requests
229
- entity.validate(req.body, "DELETE"); // will validate properties for DELETE requests
230
-
231
- ```
209
+ normalize() and validate() methods are made to be used as Express.js middlewares.
210
+ Each method will look for data to work on in the **req.body.rows** parmeter.
232
211
 
233
212
 
234
213
  ### Available options for a property
package/dist/antity.d.ts CHANGED
@@ -24,7 +24,7 @@ SOFTWARE.
24
24
  https://github.com/DWTechs/Antity.js
25
25
  */
26
26
 
27
-
27
+ import type { Request, Response, NextFunction } from 'express';
28
28
  type Method = typeof Methods[number];
29
29
  type Type =
30
30
  "boolean" |
@@ -66,10 +66,6 @@ declare const Types: Record<Type, {
66
66
  ) => boolean;
67
67
  }>;
68
68
 
69
- declare const Required: {
70
- validate: (v: any) => boolean;
71
- };
72
-
73
69
  declare class Entity {
74
70
  private _name;
75
71
  private _unsafeProps;
@@ -80,15 +76,10 @@ declare class Entity {
80
76
  get properties(): Property[];
81
77
  set name(name: string);
82
78
  getProp(key: string): Property | undefined;
83
- normalize(rows: Record<string, unknown>[]): Record<string, unknown>[];
84
- validate(rows: Record<string, unknown>[], method: Method): string | null;
79
+ normalize(req: Request, _res: Response, next: NextFunction): void;
80
+ validate(req: Request, _res: Response, next: NextFunction): void;
85
81
  }
86
82
 
87
- declare const Messages: {
88
- missing: (key: string) => string;
89
- invalid: (key: string, type: Type) => string;
90
- };
91
-
92
83
  declare class Property {
93
84
  key: string;
94
85
  type: Type;
@@ -126,9 +117,7 @@ export type { Type, Method };
126
117
  export {
127
118
  Entity,
128
119
  Property,
129
- Messages,
130
120
  Types,
131
- Required,
132
121
  Methods,
133
122
  };
134
123
 
package/dist/antity.js CHANGED
@@ -147,11 +147,6 @@ class Property {
147
147
  }
148
148
  }
149
149
 
150
- const Messages = {
151
- missing: (key) => `Missing ${key}`,
152
- invalid: (key, type) => `Invalid ${key}, must be of type ${type}`,
153
- };
154
-
155
150
  class Entity {
156
151
  constructor(name, properties) {
157
152
  this._name = name;
@@ -181,7 +176,11 @@ class Entity {
181
176
  getProp(key) {
182
177
  return this.properties.find(p => p.key === key);
183
178
  }
184
- normalize(rows) {
179
+ normalize(req, _res, next) {
180
+ var _a;
181
+ const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
182
+ if (!isObject(rows))
183
+ return next({ status: 400, msg: "Normalize: no rows found in request body" });
185
184
  for (const r of rows) {
186
185
  for (const { key, type, sanitize, normalize, sanitizer, normalizer, } of this.properties) {
187
186
  let v = r[key];
@@ -198,11 +197,19 @@ class Entity {
198
197
  }
199
198
  }
200
199
  }
201
- return rows;
200
+ next();
202
201
  }
203
- validate(rows, method) {
202
+ validate(req, _res, next) {
203
+ var _a;
204
+ const rows = req.body;
205
+ const method = req.method;
206
+ if (!isObject((_a = req.body) === null || _a === void 0 ? void 0 : _a.rows))
207
+ return next({ status: 400, msg: "Sanitize: no rows found in request body" });
204
208
  if (!isIn(Methods, method))
205
- return `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`;
209
+ return next({
210
+ status: 400,
211
+ msg: `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`
212
+ });
206
213
  for (const r of rows) {
207
214
  for (const { key, type, min, max, required, typeCheck, methods, control, controller } of this.properties) {
208
215
  const v = r[key];
@@ -210,28 +217,30 @@ class Entity {
210
217
  if (required) {
211
218
  const rq = this.require(v, key, type);
212
219
  if (rq)
213
- return rq;
220
+ return next(rq);
214
221
  }
215
222
  if (v && control) {
216
223
  const ct = this.control(v, key, type, min, max, typeCheck, controller);
217
224
  if (ct)
218
- return ct;
225
+ return next(ct);
219
226
  }
220
227
  }
221
228
  }
222
229
  }
223
- return null;
230
+ next();
224
231
  }
225
232
  require(v, key, type) {
226
233
  log.debug(`require ${key}: ${type} = ${v}`);
227
- return isNil(v) ? Messages.missing(key) : null;
234
+ return isNil(v) ? { status: 400, msg: `Missing ${key} of type ${type}` } : null;
228
235
  }
229
236
  control(v, key, type, min, max, typeCheck, cb) {
230
237
  log.debug(`control ${key}: ${type} = ${v}`);
238
+ let val;
231
239
  if (cb)
232
- return cb(v) ? null : Messages.invalid(key, type);
233
- const val = Types[type].validate(v, min, max, typeCheck);
234
- return val ? null : Messages.invalid(key, type);
240
+ val = cb(v);
241
+ else
242
+ val = Types[type].validate(v, min, max, typeCheck);
243
+ return val ? null : { status: 400, msg: `Invalid ${key}, must be of type ${type}` };
235
244
  }
236
245
  sanitize(v, cb) {
237
246
  if (cb)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Open source library for easy entity management",
5
5
  "keywords": [
6
6
  "entities"
@@ -48,6 +48,7 @@
48
48
  "@rollup/plugin-node-resolve": "15.3.0",
49
49
  "@types/jest": "29.5.14",
50
50
  "@types/node": "22.10.1",
51
+ "@types/express": "5.0.0",
51
52
  "babel-jest": "29.7.0",
52
53
  "core-js": "3.33.0",
53
54
  "jest": "29.7.0",