@dwtechs/antity 0.16.0 → 0.17.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
@@ -136,7 +136,10 @@ type Type =
136
136
  "htmlElement" |
137
137
  "htmlEventAttribute" |
138
138
  "node" |
139
- "object";
139
+ "object" |
140
+ "ansiEscapeCode" |
141
+ "locale" |
142
+ "timeZone";
140
143
 
141
144
  type Method = "PATCH" | "PUT" | "POST";
142
145
 
package/dist/antity.d.ts CHANGED
@@ -55,7 +55,10 @@ type Type =
55
55
  "htmlElement" |
56
56
  "htmlEventAttribute" |
57
57
  "node" |
58
- "object";
58
+ "object" |
59
+ "ansiEscapeCode" |
60
+ "locale" |
61
+ "timeZone";
59
62
 
60
63
  declare class Entity {
61
64
  private _name;
package/dist/antity.js CHANGED
@@ -24,7 +24,7 @@ SOFTWARE.
24
24
  https://github.com/DWTechs/Antity.js
25
25
  */
26
26
 
27
- import { isBoolean, isStringOfLength, isValidNumber, isValidInteger, isValidFloat, isEven, isOdd, isPositive, isNegative, isPowerOfTwo, isAscii, isArrayOfLength, isValidPassword, isEmail, isRegex, isJson, isJWT, isSymbol, isIpAddress, isSlug, isHexadecimal, isValidDate, isValidTimestamp, isFunction, isHtmlElement, isHtmlEventAttribute, isNode, isObject, isString, isProperty, isArray, isIn, isDate, isNumber, isInteger, isNil } from '@dwtechs/checkard';
27
+ import { isBoolean, isStringOfLength, isValidNumber, isValidInteger, isValidFloat, isEven, isOdd, isPositive, isNegative, isPowerOfTwo, isAscii, isArrayOfLength, isValidPassword, isEmail, isRegex, isJson, isJWT, isSymbol, isIpAddress, isSlug, isHexadecimal, isValidDate, isValidTimestamp, isFunction, isHtmlElement, isHtmlEventAttribute, isNode, isObject, isAnsiEscapeCode, isLocale, isTimeZone, isString, isProperty, isArray, isIn, isDate, isNumber, isInteger, isNil } from '@dwtechs/checkard';
28
28
  import { log } from '@dwtechs/winstan';
29
29
 
30
30
  const LOGS_PREFIX = "Antity: ";
@@ -48,7 +48,7 @@ const Types = {
48
48
  validate: (v, min, max, typeCheck) => isValidNumber(v, min || undefined, max || undefined, typeCheck || undefined, true)
49
49
  },
50
50
  integer: {
51
- validate: (v, min, max, typeCheck) => isValidInteger(v, min !== null && min !== void 0 ? min : undefined, max !== null && max !== void 0 ? max : undefined, typeCheck || undefined, true)
51
+ validate: (v, min, max, typeCheck) => isValidInteger(v, min ?? undefined, max ?? undefined, typeCheck || undefined, true)
52
52
  },
53
53
  float: {
54
54
  validate: (v, min, max, typeCheck) => isValidFloat(v, min || undefined, max || undefined, typeCheck || undefined, true)
@@ -131,10 +131,29 @@ const Types = {
131
131
  },
132
132
  object: {
133
133
  validate: (v, _min, _max, _typeCheck) => isObject(v, true)
134
+ },
135
+ ansiEscapeCode: {
136
+ validate: (v, _min, _max, _typeCheck) => isAnsiEscapeCode(v, true)
137
+ },
138
+ locale: {
139
+ validate: (v, _min, _max, typeCheck) => isLocale(v, typeCheck || undefined, true)
140
+ },
141
+ timeZone: {
142
+ validate: (v, _min, _max, _typeCheck) => isTimeZone(v, true)
134
143
  }
135
144
  };
136
145
 
137
146
  class Property {
147
+ key;
148
+ type;
149
+ min;
150
+ max;
151
+ isPrivate;
152
+ requiredFor;
153
+ isTypeChecked;
154
+ sanitizer;
155
+ normalizer;
156
+ validator;
138
157
  constructor(key, type, min, max, isPrivate, requiredFor, isTypeChecked, sanitizer, normalizer, validator) {
139
158
  try {
140
159
  isString(key, "!0", null, true);
@@ -180,8 +199,8 @@ function sanitize(v, cb) {
180
199
  if (cb)
181
200
  return cb(v);
182
201
  if (isArray(v, null, null)) {
183
- for (let d of v) {
184
- d = trim(d);
202
+ for (let i = 0; i < v.length; i++) {
203
+ v[i] = trim(v[i]);
185
204
  }
186
205
  return v;
187
206
  }
@@ -191,11 +210,12 @@ function trim(v) {
191
210
  if (isString(v, "!0"))
192
211
  return v.trim();
193
212
  if (isObject(v, true)) {
194
- for (const k in v) {
195
- if (Object.prototype.hasOwnProperty.call(v, k)) {
196
- let o = v[k];
213
+ const obj = v;
214
+ for (const k in obj) {
215
+ if (Object.prototype.hasOwnProperty.call(obj, k)) {
216
+ const o = obj[k];
197
217
  if (isString(o, "!0", null))
198
- o = o.trim();
218
+ obj[k] = o.trim();
199
219
  }
200
220
  }
201
221
  }
@@ -208,7 +228,7 @@ function normalize(record, properties) {
208
228
  if (v) {
209
229
  log.debug(`sanitize ${key}: ${type} = ${v}`);
210
230
  v = sanitize(v, sanitizer);
211
- if (isFunction(normalizer)) {
231
+ if (normalizer) {
212
232
  log.debug(`normalize ${key}: ${type} = ${v}`);
213
233
  v = normalizer(v);
214
234
  }
@@ -247,7 +267,7 @@ function require(v, key, type) {
247
267
  function validate(record, properties, method) {
248
268
  for (const { key, type, min, max, requiredFor, isTypeChecked, validator } of properties) {
249
269
  const v = record[key];
250
- if (isIn(requiredFor, method)) {
270
+ if (requiredFor.includes(method)) {
251
271
  const rq = require(v, key, type);
252
272
  if (rq)
253
273
  return rq;
@@ -262,61 +282,10 @@ function validate(record, properties, method) {
262
282
  }
263
283
 
264
284
  class Entity {
285
+ _name;
286
+ _privateProps;
287
+ _properties;
265
288
  constructor(name, properties) {
266
- this.normalizeArray = (req, _res, next) => {
267
- var _a;
268
- log.debug(`normalizeArray ${this.name}`);
269
- const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
270
- if (!isArray(rows, ">", 0))
271
- return next({ statusCode: 400, message: `${LOGS_PREFIX}Normalize: no rows found in request body` });
272
- for (const r of rows) {
273
- normalize(r, this._properties);
274
- }
275
- next();
276
- };
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) => {
286
- var _a;
287
- log.debug(`validateArray ${this.name}`);
288
- const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
289
- const method = req.method;
290
- if (!isArray(rows, ">", 0))
291
- return next({ statusCode: 400, message: `${LOGS_PREFIX}Validate: no rows found in request body` });
292
- if (!isIn(METHODS, method))
293
- return next({
294
- statusCode: 400,
295
- message: `${LOGS_PREFIX}Invalid REST method. Received: ${method}. Must be one of: ${METHODS.toString()}`
296
- });
297
- for (const r of rows) {
298
- const error = validate(r, this._properties, method);
299
- if (error)
300
- return next(error);
301
- }
302
- next();
303
- };
304
- this.validateOne = (req, _res, next) => {
305
- log.debug(`validateOne ${this.name}`);
306
- const record = req.body;
307
- const method = req.method;
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
- return next({
312
- statusCode: 400,
313
- message: `${LOGS_PREFIX}Invalid REST method. Received: ${method}. Must be one of: ${METHODS.toString()}`
314
- });
315
- const error = validate(record, this._properties, method);
316
- if (error)
317
- return next(error);
318
- next();
319
- };
320
289
  this._name = name;
321
290
  this._properties = [];
322
291
  this._privateProps = [];
@@ -348,11 +317,73 @@ class Entity {
348
317
  getPropsByMethod(method) {
349
318
  const props = [];
350
319
  for (const p of this.properties) {
351
- if (isIn(p.requiredFor, method, 0))
320
+ if (p.requiredFor.includes(method))
352
321
  props.push(p);
353
322
  }
354
323
  return props;
355
324
  }
325
+ normalizeArray = (req, _res, next) => {
326
+ log.debug(`normalizeArray ${this.name}`);
327
+ const rows = req.body?.rows;
328
+ if (!isArray(rows, ">", 0)) {
329
+ next({ statusCode: 400, message: `${LOGS_PREFIX}Normalize: no rows found in request body` });
330
+ return;
331
+ }
332
+ for (const r of rows) {
333
+ normalize(r, this._properties);
334
+ }
335
+ next();
336
+ };
337
+ normalizeOne = (req, _res, next) => {
338
+ log.debug(`normalizeOne ${this.name}`);
339
+ const r = req.body;
340
+ if (!isObject(r, true)) {
341
+ next({ statusCode: 400, message: `${LOGS_PREFIX}Normalize: no data found in request body` });
342
+ return;
343
+ }
344
+ normalize(r, this._properties);
345
+ next();
346
+ };
347
+ validateArray = (req, _res, next) => {
348
+ log.debug(`validateArray ${this.name}`);
349
+ const rows = req.body?.rows;
350
+ const method = req.method;
351
+ if (!isArray(rows, ">", 0)) {
352
+ next({ statusCode: 400, message: `${LOGS_PREFIX}Validate: no rows found in request body` });
353
+ return;
354
+ }
355
+ if (!METHODS.includes(method)) {
356
+ next({ statusCode: 400, message: `${LOGS_PREFIX}Invalid REST method. Received: ${method}. Must be one of: ${METHODS.toString()}` });
357
+ return;
358
+ }
359
+ for (const r of rows) {
360
+ const error = validate(r, this._properties, method);
361
+ if (error) {
362
+ next(error);
363
+ return;
364
+ }
365
+ }
366
+ next();
367
+ };
368
+ validateOne = (req, _res, next) => {
369
+ log.debug(`validateOne ${this.name}`);
370
+ const record = req.body;
371
+ const method = req.method;
372
+ if (!isObject(record, true)) {
373
+ next({ statusCode: 400, message: `${LOGS_PREFIX}Validate: no data found in request body` });
374
+ return;
375
+ }
376
+ if (!METHODS.includes(method)) {
377
+ next({ statusCode: 400, message: `${LOGS_PREFIX}Invalid REST method. Received: ${method}. Must be one of: ${METHODS.toString()}` });
378
+ return;
379
+ }
380
+ const error = validate(record, this._properties, method);
381
+ if (error) {
382
+ next(error);
383
+ return;
384
+ }
385
+ next();
386
+ };
356
387
  }
357
388
 
358
389
  export { Entity };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Open source library for easy entity management",
5
5
  "keywords": [
6
6
  "entities"
@@ -13,8 +13,7 @@
13
13
  "url": "https://github.com/DWTechs/Antity.js"
14
14
  },
15
15
  "bugs": {
16
- "url": "https://github.com/DWTechs/Antity.js/issues",
17
- "email": ""
16
+ "url": "https://github.com/DWTechs/Antity.js/issues"
18
17
  },
19
18
  "license": "MIT",
20
19
  "author": {
@@ -22,36 +21,33 @@
22
21
  "email": "http://www.lcluber.com/contact",
23
22
  "url": "http://www.lcluber.com"
24
23
  },
25
- "contributors": [],
26
24
  "scripts": {
27
- "start": "",
28
25
  "prebuild": "npm install",
29
26
  "build": "node ./scripts/clear && tsc && npm run rollup && node ./scripts/copy && npm run test",
30
- "rollup:mjs": "rollup --config rollup.config.mjs",
31
- "rollup:cjs": "rollup --config rollup.config.cjs.mjs",
32
- "rollup": "npm run rollup:mjs",
33
- "test": "jest --coverage --config jest.config.js"
27
+ "rollup": "rollup --config rollup.config.mjs",
28
+ "test": "jest --coverage"
34
29
  },
35
30
  "files": [
36
31
  "dist/"
37
32
  ],
33
+ "engines": {
34
+ "node": ">= 22"
35
+ },
38
36
  "dependencies": {
39
- "@dwtechs/checkard": "3.5.1",
40
- "@dwtechs/winstan": "0.4.0",
41
- "@dwtechs/sparray": "0.2.1"
37
+ "@dwtechs/checkard": "3.6.0",
38
+ "@dwtechs/sparray": "0.2.1",
39
+ "@dwtechs/winstan": "0.7.0"
42
40
  },
43
41
  "devDependencies": {
44
42
  "@babel/core": "7.26.0",
45
43
  "@babel/preset-env": "7.26.0",
46
- "@rollup/plugin-commonjs": "28.0.1",
47
- "@rollup/plugin-node-resolve": "15.3.0",
44
+ "@types/express": "5.0.6",
48
45
  "@types/jest": "29.5.14",
49
- "@types/node": "22.10.1",
50
- "@types/express": "5.0.0",
46
+ "@types/node": "25.6.0",
51
47
  "babel-jest": "29.7.0",
52
48
  "core-js": "3.33.0",
53
49
  "jest": "29.7.0",
54
50
  "rollup": "4.24.0",
55
- "typescript": "5.8.3"
51
+ "typescript": "6.0.3"
56
52
  }
57
53
  }