@dwtechs/antity 0.15.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
@@ -51,9 +51,9 @@ const entity = new Entity("consumers", [
51
51
  type: "integer",
52
52
  min: 0,
53
53
  max: 120,
54
- typeCheck: true,
55
- need: ["PUT"],
56
- safe: true,
54
+ isTypeChecked: true,
55
+ requiredFor: ["PUT"],
56
+ isPrivate: true,
57
57
  sanitizer: null,
58
58
  normalizer: null,
59
59
  validator: null,
@@ -63,9 +63,9 @@ const entity = new Entity("consumers", [
63
63
  type: "string",
64
64
  min: 0,
65
65
  max: 255,
66
- typeCheck: true,
67
- need: ["PUT"],
68
- safe: true,
66
+ isTypeChecked: true,
67
+ requiredFor: ["PUT"],
68
+ isPrivate: true,
69
69
  sanitizer: null,
70
70
  normalizer: normalizeName,
71
71
  validator: null,
@@ -75,9 +75,9 @@ const entity = new Entity("consumers", [
75
75
  type: "string",
76
76
  min: 0,
77
77
  max: 255,
78
- typeCheck: true,
79
- need: ["PUT"],
80
- safe: true,
78
+ isTypeChecked: true,
79
+ requiredFor: ["PUT"],
80
+ isPrivate: true,
81
81
  sanitizer: null,
82
82
  normalizer: normalizeName,
83
83
  validator: null,
@@ -87,9 +87,9 @@ const entity = new Entity("consumers", [
87
87
  type: "string",
88
88
  min: 0,
89
89
  max: 255,
90
- typeCheck: true,
91
- need: ["PUT"],
92
- safe: true,
90
+ isTypeChecked: true,
91
+ requiredFor: ["PUT"],
92
+ isPrivate: true,
93
93
  sanitizer: null,
94
94
  normalizer: normalizeNickname,
95
95
  validator: null,
@@ -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
 
@@ -145,9 +148,9 @@ class Property {
145
148
  type: Type;
146
149
  min: number | Date | null;
147
150
  max: number | Date | null;
148
- need: Method[];
149
- send: boolean;
150
- typeCheck: boolean;
151
+ requiredFor: Method[];
152
+ isPrivate: boolean;
153
+ isTypeChecked: boolean;
151
154
  sanitizer: Function | null;
152
155
  normalizer: Function | null;
153
156
  validator: Function | null;
@@ -156,7 +159,7 @@ class Property {
156
159
  class Entity {
157
160
  constructor(name: string, properties: Property[]);
158
161
  get name(): string;
159
- get unsafeProps(): string[];
162
+ get privateProps(): string[];
160
163
  get properties(): Property[];
161
164
  set name(name: string);
162
165
 
@@ -376,9 +379,9 @@ Any of these can be passed into the options object for each function.
376
379
  | type | Type | Type of the property |
377
380
  | min | number \| Date | Minimum value if applicable | 0 \| 1900-01-01
378
381
  | 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
+ | requiredFor | Methods[] | Property is required for the listed methods only | [ "POST", "PUT", "PATCH" ]
383
+ | isPrivate | boolean | Property should not be sent in the response | false
384
+ | isTypeChecked | boolean | Strict type check at validation | false
382
385
  | sanitizer | ((v:any) => any) \| null | Custom sanitizer function | null
383
386
  | normalizer | ((v:any) => any) \| null | Custom Normalizer function | null
384
387
  | validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) \| null | Custom validator | null
package/dist/antity.d.ts CHANGED
@@ -55,15 +55,18 @@ 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;
62
- private _unsafeProps;
65
+ private _privateProps;
63
66
  private _properties;
64
67
  constructor(name: string, properties: Property[]);
65
68
  get name(): string;
66
- get unsafeProps(): string[];
69
+ get privateProps(): string[];
67
70
  get properties(): Property[];
68
71
  set name(name: string);
69
72
  getProp(key: string): Property | undefined;
@@ -80,9 +83,9 @@ declare class Property {
80
83
  type: Type;
81
84
  min: number | Date | null;
82
85
  max: number | Date | null;
83
- send: boolean;
84
- need: Method[];
85
- typeCheck: boolean;
86
+ isPrivate: boolean;
87
+ requiredFor: Method[];
88
+ isTypeChecked: boolean;
86
89
  sanitizer: ((v: any) => any) | null;
87
90
  normalizer: ((v: any) => any) | null;
88
91
  validator: ((v: any) => any) | null;
@@ -91,9 +94,9 @@ declare class Property {
91
94
  type: Type,
92
95
  min: number | Date | null,
93
96
  max: number | Date | null,
94
- send: boolean,
95
- need: Method[],
96
- typeCheck: boolean,
97
+ isPrivate: boolean,
98
+ requiredFor: Method[],
99
+ isTypeChecked: boolean,
97
100
  sanitizer: ((v: any) => any) | null,
98
101
  normalizer: ((v: any) => any) | null,
99
102
  validator: ((v: any) => any) | null
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,11 +131,30 @@ 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 {
138
- constructor(key, type, min, max, send, need, typeCheck, sanitizer, normalizer, validator) {
147
+ key;
148
+ type;
149
+ min;
150
+ max;
151
+ isPrivate;
152
+ requiredFor;
153
+ isTypeChecked;
154
+ sanitizer;
155
+ normalizer;
156
+ validator;
157
+ constructor(key, type, min, max, isPrivate, requiredFor, isTypeChecked, sanitizer, normalizer, validator) {
139
158
  try {
140
159
  isString(key, "!0", null, true);
141
160
  }
@@ -148,13 +167,13 @@ class Property {
148
167
  catch (err) {
149
168
  throw new Error(`${LOGS_PREFIX}Property "type" must be a valid type - caused by: ${err.message}`);
150
169
  }
151
- if (isArray(need)) {
152
- for (const m of need) {
170
+ if (isArray(requiredFor)) {
171
+ for (const m of requiredFor) {
153
172
  try {
154
173
  isIn(METHODS, m, 0, true);
155
174
  }
156
175
  catch (err) {
157
- throw new Error(`${LOGS_PREFIX}Property "need" must be an array of REST methods - caused by: ${err.message}`);
176
+ throw new Error(`${LOGS_PREFIX}Property "requiredFor" must be an array of REST methods - caused by: ${err.message}`);
158
177
  }
159
178
  }
160
179
  }
@@ -162,9 +181,9 @@ class Property {
162
181
  this.type = type;
163
182
  this.min = this.interval(min, type, 0, "1900-01-01T00:00:00Z");
164
183
  this.max = this.interval(max, type, 999999999, "2200-12-31T00:00:00Z");
165
- this.need = isArray(need) ? need : [];
166
- this.send = isBoolean(send) ? send : true;
167
- this.typeCheck = isBoolean(typeCheck) ? typeCheck : false;
184
+ this.requiredFor = isArray(requiredFor) ? requiredFor : [];
185
+ this.isPrivate = isBoolean(isPrivate) ? isPrivate : false;
186
+ this.isTypeChecked = isBoolean(isTypeChecked) ? isTypeChecked : false;
168
187
  this.sanitizer = isFunction(sanitizer) ? sanitizer : null;
169
188
  this.normalizer = isFunction(normalizer) ? normalizer : null;
170
189
  this.validator = isFunction(validator) ? validator : null;
@@ -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
  }
@@ -245,15 +265,15 @@ function require(v, key, type) {
245
265
  }
246
266
 
247
267
  function validate(record, properties, method) {
248
- for (const { key, type, min, max, need, typeCheck, validator } of properties) {
268
+ for (const { key, type, min, max, requiredFor, isTypeChecked, validator } of properties) {
249
269
  const v = record[key];
250
- if (isIn(need, method)) {
270
+ if (requiredFor.includes(method)) {
251
271
  const rq = require(v, key, type);
252
272
  if (rq)
253
273
  return rq;
254
274
  }
255
275
  if (v) {
256
- const ct = control(v, key, type, min, max, typeCheck, validator);
276
+ const ct = control(v, key, type, min, max, isTypeChecked, validator);
257
277
  if (ct)
258
278
  return ct;
259
279
  }
@@ -262,77 +282,26 @@ 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
- this._unsafeProps = [];
291
+ this._privateProps = [];
323
292
  for (const p of properties) {
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);
293
+ const prop = new Property(p.key, p.type, p.min, p.max, p.isPrivate, p.requiredFor, p.isTypeChecked, p.sanitizer, p.normalizer, p.validator);
325
294
  Object.assign(prop, p);
326
295
  this._properties.push(prop);
327
- if (!prop.safe)
328
- this._unsafeProps.push(prop.key);
296
+ if (prop.isPrivate)
297
+ this._privateProps.push(prop.key);
329
298
  }
330
299
  }
331
300
  get name() {
332
301
  return this._name;
333
302
  }
334
- get unsafeProps() {
335
- return this._unsafeProps;
303
+ get privateProps() {
304
+ return this._privateProps;
336
305
  }
337
306
  get properties() {
338
307
  return this._properties;
@@ -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.need, 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.15.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
  }