@dwtechs/antity 0.9.0 → 0.9.2

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
@@ -216,7 +216,7 @@ class Entity {
216
216
 
217
217
  ```
218
218
  normalize() and validate() methods are made to be used as Express.js middlewares.
219
- Each method will look for data to work on in the **req.body.rows** parmeter.
219
+ Each method will look for data to work on in the **req.body.rows** parameter.
220
220
 
221
221
 
222
222
  ### Available options for a property
package/dist/antity.d.ts CHANGED
@@ -67,8 +67,8 @@ declare class Entity {
67
67
  set name(name: string);
68
68
  getProp(key: string): Property | undefined;
69
69
  getPropsByMethod(method: Method): Property[];
70
- normalize(req: Request, _res: Response, next: NextFunction): void;
71
- validate(req: Request, _res: Response, next: NextFunction): void;
70
+ normalize: (req: Request, _res: Response, next: NextFunction) => void;
71
+ validate: (req: Request, _res: Response, next: NextFunction) => void;
72
72
  }
73
73
 
74
74
  declare class Property {
package/dist/antity.js CHANGED
@@ -147,8 +147,107 @@ class Property {
147
147
  }
148
148
  }
149
149
 
150
+ function sanitize(v, cb) {
151
+ if (cb)
152
+ return cb(v);
153
+ if (isArray(v, null, null)) {
154
+ for (let d of v) {
155
+ d = trim(d);
156
+ }
157
+ return v;
158
+ }
159
+ return trim(v);
160
+ }
161
+ function trim(v) {
162
+ if (isString(v, "!0"))
163
+ return v.trim();
164
+ if (isObject(v, true)) {
165
+ for (const k in v) {
166
+ if (Object.prototype.hasOwnProperty.call(v, k)) {
167
+ let o = v[k];
168
+ if (isString(o, "!0", null))
169
+ o = o.trim();
170
+ }
171
+ }
172
+ }
173
+ return v;
174
+ }
175
+
176
+ function control(v, key, type, min, max, typeCheck, cb) {
177
+ log.debug(`control ${key}: ${type} = ${v}`);
178
+ let val;
179
+ if (cb)
180
+ val = cb(v);
181
+ else
182
+ val = Types[type].validate(v, min, max, typeCheck);
183
+ let c = "";
184
+ if (!isNil(min))
185
+ c += ` and >= ${min}`;
186
+ if (!isNil(max))
187
+ c += ` and <= ${max}`;
188
+ return val ? null : { status: 400, msg: `Invalid ${key}, must be of type ${type}${c}` };
189
+ }
190
+
191
+ function require(v, key, type) {
192
+ log.debug(`require ${key}: ${type} = ${v}`);
193
+ return isNil(v) ? { status: 400, msg: `Missing ${key} of type ${type}` } : null;
194
+ }
195
+
150
196
  class Entity {
151
197
  constructor(name, properties) {
198
+ this.normalize = (req, _res, next) => {
199
+ var _a;
200
+ const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
201
+ if (!isArray(rows, "!0"))
202
+ return next({ status: 400, msg: "Normalize: no rows found in request body" });
203
+ for (const r of rows) {
204
+ for (const { key, type, sanitize: sanitize$1, normalize, sanitizer, normalizer, } of this._properties) {
205
+ let v = r[key];
206
+ if (v) {
207
+ if (sanitize$1) {
208
+ log.debug(`sanitize ${key}: ${type} = ${v}`);
209
+ v = sanitize(v, sanitizer);
210
+ }
211
+ if (normalize && isFunction(normalizer)) {
212
+ log.debug(`normalize ${key}: ${type} = ${v}`);
213
+ v = normalizer(v);
214
+ }
215
+ r[key] = v;
216
+ }
217
+ }
218
+ }
219
+ next();
220
+ };
221
+ this.validate = (req, _res, next) => {
222
+ var _a;
223
+ const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
224
+ const method = req.method;
225
+ if (!isArray(rows, "!0"))
226
+ return next({ status: 400, msg: "Sanitize: no rows found in request body" });
227
+ if (!isIn(Methods, method))
228
+ return next({
229
+ status: 400,
230
+ msg: `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`
231
+ });
232
+ for (const r of rows) {
233
+ for (const { key, type, min, max, required, typeCheck, methods, validate, validator } of this._properties) {
234
+ const v = r[key];
235
+ if (isIn(methods, method)) {
236
+ if (required) {
237
+ const rq = require(v, key, type);
238
+ if (rq)
239
+ return next(rq);
240
+ }
241
+ if (v && validate) {
242
+ const ct = control(v, key, type, min, max, typeCheck, validator);
243
+ if (ct)
244
+ return next(ct);
245
+ }
246
+ }
247
+ }
248
+ }
249
+ next();
250
+ };
152
251
  this._name = name;
153
252
  this._properties = [];
154
253
  this._unsafeProps = [];
@@ -184,102 +283,6 @@ class Entity {
184
283
  }
185
284
  return props;
186
285
  }
187
- normalize(req, _res, next) {
188
- var _a;
189
- const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
190
- if (!isArray(rows, "!0"))
191
- return next({ status: 400, msg: "Normalize: no rows found in request body" });
192
- for (const r of rows) {
193
- for (const { key, type, sanitize, normalize, sanitizer, normalizer, } of this.properties) {
194
- let v = r[key];
195
- if (v) {
196
- if (sanitize) {
197
- log.debug(`sanitize ${key}: ${type} = ${v}`);
198
- v = this.sanitize(v, sanitizer);
199
- }
200
- if (normalize && isFunction(normalizer)) {
201
- log.debug(`normalize ${key}: ${type} = ${v}`);
202
- v = normalizer(v);
203
- }
204
- r[key] = v;
205
- }
206
- }
207
- }
208
- next();
209
- }
210
- validate(req, _res, next) {
211
- var _a;
212
- const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
213
- const method = req.method;
214
- if (!isArray(rows, "!0"))
215
- return next({ status: 400, msg: "Sanitize: no rows found in request body" });
216
- if (!isIn(Methods, method))
217
- return next({
218
- status: 400,
219
- msg: `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`
220
- });
221
- for (const r of rows) {
222
- for (const { key, type, min, max, required, typeCheck, methods, validate, validator } of this.properties) {
223
- const v = r[key];
224
- if (isIn(methods, method)) {
225
- if (required) {
226
- const rq = this.require(v, key, type);
227
- if (rq)
228
- return next(rq);
229
- }
230
- if (v && validate) {
231
- const ct = this.control(v, key, type, min, max, typeCheck, validator);
232
- if (ct)
233
- return next(ct);
234
- }
235
- }
236
- }
237
- }
238
- next();
239
- }
240
- require(v, key, type) {
241
- log.debug(`require ${key}: ${type} = ${v}`);
242
- return isNil(v) ? { status: 400, msg: `Missing ${key} of type ${type}` } : null;
243
- }
244
- control(v, key, type, min, max, typeCheck, cb) {
245
- log.debug(`control ${key}: ${type} = ${v}`);
246
- let val;
247
- if (cb)
248
- val = cb(v);
249
- else
250
- val = Types[type].validate(v, min, max, typeCheck);
251
- let c = "";
252
- if (!isNil(min))
253
- c += ` and >= ${min}`;
254
- if (!isNil(max))
255
- c += ` and <= ${max}`;
256
- return val ? null : { status: 400, msg: `Invalid ${key}, must be of type ${type}${c}` };
257
- }
258
- sanitize(v, cb) {
259
- if (cb)
260
- return cb(v);
261
- if (isArray(v, null, null)) {
262
- for (let d of v) {
263
- d = this.trim(d);
264
- }
265
- return v;
266
- }
267
- return this.trim(v);
268
- }
269
- trim(v) {
270
- if (isString(v, "!0"))
271
- return v.trim();
272
- if (isObject(v, true)) {
273
- for (const k in v) {
274
- if (Object.prototype.hasOwnProperty.call(v, k)) {
275
- let o = v[k];
276
- if (isString(o, "!0", null))
277
- o = o.trim();
278
- }
279
- }
280
- }
281
- return v;
282
- }
283
286
  }
284
287
 
285
288
  export { Entity };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Open source library for easy entity management",
5
5
  "keywords": [
6
6
  "entities"
@@ -53,6 +53,6 @@
53
53
  "core-js": "3.33.0",
54
54
  "jest": "29.7.0",
55
55
  "rollup": "4.24.0",
56
- "typescript": "5.6.3"
56
+ "typescript": "5.8.3"
57
57
  }
58
58
  }