@dwtechs/antity 0.8.0 → 0.9.1

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
@@ -190,6 +190,14 @@ class Entity {
190
190
  * @returns {Property | undefined} - The property object if found, otherwise `undefined`.
191
191
  */
192
192
  getProp(key: string): Property | undefined;
193
+
194
+ /**
195
+ * Retrieves a list of properties associated with a specific method.
196
+ *
197
+ * @param {Method} method - The method to filter properties by.
198
+ * @returns {Property[]} An array of properties that are associated with the specified method.
199
+ */
200
+ getPropsByMethod(method: Method): Property[];
193
201
 
194
202
  /**
195
203
  * Normalizes an array of records by applying sanitization and normalization
@@ -208,7 +216,7 @@ class Entity {
208
216
 
209
217
  ```
210
218
  normalize() and validate() methods are made to be used as Express.js middlewares.
211
- 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.
212
220
 
213
221
 
214
222
  ### Available options for a property
package/dist/antity.d.ts CHANGED
@@ -66,6 +66,7 @@ declare class Entity {
66
66
  get properties(): Property[];
67
67
  set name(name: string);
68
68
  getProp(key: string): Property | undefined;
69
+ getPropsByMethod(method: Method): Property[];
69
70
  normalize(req: Request, _res: Response, next: NextFunction): void;
70
71
  validate(req: Request, _res: Response, next: NextFunction): void;
71
72
  }
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 = [];
@@ -176,101 +275,13 @@ class Entity {
176
275
  getProp(key) {
177
276
  return this.properties.find(p => p.key === key);
178
277
  }
179
- normalize(req, _res, next) {
180
- var _a;
181
- const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
182
- if (!isArray(rows, "!0"))
183
- return next({ status: 400, msg: "Normalize: no rows found in request body" });
184
- for (const r of rows) {
185
- for (const { key, type, sanitize, normalize, sanitizer, normalizer, } of this.properties) {
186
- let v = r[key];
187
- if (v) {
188
- if (sanitize) {
189
- log.debug(`sanitize ${key}: ${type} = ${v}`);
190
- v = this.sanitize(v, sanitizer);
191
- }
192
- if (normalize && isFunction(normalizer)) {
193
- log.debug(`normalize ${key}: ${type} = ${v}`);
194
- v = normalizer(v);
195
- }
196
- r[key] = v;
197
- }
198
- }
199
- }
200
- next();
201
- }
202
- validate(req, _res, next) {
203
- var _a;
204
- const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
205
- const method = req.method;
206
- if (!isArray(rows, "!0"))
207
- return next({ status: 400, msg: "Sanitize: no rows found in request body" });
208
- if (!isIn(Methods, method))
209
- return next({
210
- status: 400,
211
- msg: `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`
212
- });
213
- for (const r of rows) {
214
- for (const { key, type, min, max, required, typeCheck, methods, validate, validator } of this.properties) {
215
- const v = r[key];
216
- if (isIn(methods, method)) {
217
- if (required) {
218
- const rq = this.require(v, key, type);
219
- if (rq)
220
- return next(rq);
221
- }
222
- if (v && validate) {
223
- const ct = this.control(v, key, type, min, max, typeCheck, validator);
224
- if (ct)
225
- return next(ct);
226
- }
227
- }
228
- }
278
+ getPropsByMethod(method) {
279
+ const props = [];
280
+ for (const p of this.properties) {
281
+ if (isIn(p.methods, method, 0))
282
+ props.push(p);
229
283
  }
230
- next();
231
- }
232
- require(v, key, type) {
233
- log.debug(`require ${key}: ${type} = ${v}`);
234
- return isNil(v) ? { status: 400, msg: `Missing ${key} of type ${type}` } : null;
235
- }
236
- control(v, key, type, min, max, typeCheck, cb) {
237
- log.debug(`control ${key}: ${type} = ${v}`);
238
- let val;
239
- if (cb)
240
- val = cb(v);
241
- else
242
- val = Types[type].validate(v, min, max, typeCheck);
243
- let c = "";
244
- if (!isNil(min))
245
- c += ` and >= ${min}`;
246
- if (!isNil(max))
247
- c += ` and <= ${max}`;
248
- return val ? null : { status: 400, msg: `Invalid ${key}, must be of type ${type}${c}` };
249
- }
250
- sanitize(v, cb) {
251
- if (cb)
252
- return cb(v);
253
- if (isArray(v, null, null)) {
254
- for (let d of v) {
255
- d = this.trim(d);
256
- }
257
- return v;
258
- }
259
- return this.trim(v);
260
- }
261
- trim(v) {
262
- if (isString(v, "!0"))
263
- return v.trim();
264
- if (isObject(v, true)) {
265
- for (const k in v) {
266
- if (Object.prototype.hasOwnProperty.call(v, k)) {
267
- let o = v[k];
268
- if (isString(o, "!0", null))
269
- o = o.trim();
270
- }
271
- }
272
- }
273
- return v;
284
+ return props;
274
285
  }
275
286
  }
276
287
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "description": "Open source library for easy entity management",
5
5
  "keywords": [
6
6
  "entities"