@dwtechs/antity 0.1.0 → 0.2.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
@@ -124,17 +124,44 @@ const check = entity.validate(req.body, req.method);
124
124
 
125
125
  ```javascript
126
126
 
127
- type Type = "boolean" | "string" | "number" | "integer" | "array";
127
+ type Type = "boolean" |
128
+ "string" |
129
+ "number" |
130
+ "integer" |
131
+ "float" |
132
+ "even" |
133
+ "odd" |
134
+ "positve" |
135
+ "negative" |
136
+ "powerOfTwo" |
137
+ "ascii" |
138
+ "array" |
139
+ "jwt" |
140
+ "symbol" |
141
+ "email" |
142
+ "regex" |
143
+ "json" |
144
+ "ipAddress" |
145
+ "slug" |
146
+ "hexadecimal" |
147
+ "date" |
148
+ "timestamp" |
149
+ "function" |
150
+ "htmlElement" |
151
+ "htmlEventAttribute" |
152
+ "node" |
153
+ "object";
154
+
128
155
  type Method = "GET" | "PATCH" | "PUT" | "POST" | "DELETE";
129
156
 
130
157
  class Property {
131
158
  key: string;
132
159
  type: Type;
133
- min: number;
134
- max: number;
160
+ min: number | Date;
161
+ max: number | Date;
135
162
  required: boolean;
136
163
  typeCheck: boolean;
137
- verbs: Verb[];
164
+ methods: Method[];
138
165
  sanitize: boolean;
139
166
  normalize: boolean;
140
167
  control: boolean;
@@ -84,12 +84,40 @@ const comparisons = {
84
84
  '<=': (a, b) => a <= b,
85
85
  '>=': (a, b) => a >= b
86
86
  };
87
+
88
+ function isAscii(c, ext = true) {
89
+ return isInteger(c, false) && ((ext && c >= 0 && c <= 255) || (c >= 0 && c <= 127));
90
+ }
87
91
  function isInteger(n, type = true) {
88
92
  if (!isNumber(n, type))
89
93
  return false;
90
94
  const int = Number.parseInt(String(n), 10);
91
95
  return type ? n === int : n == int;
92
96
  }
97
+ function isFloat(n, type = true) {
98
+ if (isSymbol(n))
99
+ return false;
100
+ const modulo = n % 1 !== 0;
101
+ return type ? (Number(n) === n && modulo) : (Number(n) == n && modulo);
102
+ }
103
+ function isEven(n, type = true) {
104
+ return isInteger(n, type) && !(n & 1);
105
+ }
106
+ function isOdd(n, type = true) {
107
+ return isInteger(n, type) && Boolean(n & 1);
108
+ }
109
+ function isOrigin(n, type = true) {
110
+ return type ? n === 0 : n == 0;
111
+ }
112
+ function isPositive(n, type = true) {
113
+ return isNumber(n, type) && n > 0;
114
+ }
115
+ function isNegative(n, type = true) {
116
+ return isNumber(n, type) && n < 0;
117
+ }
118
+ function isPowerOfTwo(n, type = true) {
119
+ return isInteger(n, type) && !isOrigin(n, type) && (n & (n - 1)) === 0;
120
+ }
93
121
 
94
122
  function isValidNumber(n, min = -999999999, max = 999999999, type = true) {
95
123
  return isNumber(n, type) && n >= min && n <= max;
@@ -97,6 +125,9 @@ function isValidNumber(n, min = -999999999, max = 999999999, type = true) {
97
125
  function isValidInteger(n, min = -999999999, max = 999999999, type = true) {
98
126
  return isInteger(n, type) && n >= min && n <= max;
99
127
  }
128
+ function isValidFloat(n, min = -999999999.9, max = 999999999.9, type = true) {
129
+ return isFloat(n, type) && n >= min && n <= max;
130
+ }
100
131
 
101
132
  function isArray(a, comp, len) {
102
133
  return (a === null || a === void 0 ? void 0 : a.constructor) === Array ? (comp && isValidInteger(len, 0, 999999999)) ? comparisons.hasOwnProperty(comp) ? comparisons[comp](a.length, len) : false : true : false;
@@ -122,6 +153,64 @@ function isStringOfLength(s, min = 0, max = 999999999) {
122
153
  }
123
154
  return false;
124
155
  }
156
+ function isJson(s) {
157
+ if (!isString(s))
158
+ return false;
159
+ try {
160
+ JSON.parse(s);
161
+ }
162
+ catch (e) {
163
+ return false;
164
+ }
165
+ return true;
166
+ }
167
+ function isRegex(r, type = true) {
168
+ if (type)
169
+ return r instanceof RegExp;
170
+ try {
171
+ new RegExp(r);
172
+ }
173
+ catch (e) {
174
+ return false;
175
+ }
176
+ return true;
177
+ }
178
+ const emailReg = /^(?=[a-z0-9@.!$%&'*+\/=?^_‘{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+\/=?^_‘{|}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^‘{|}~]+(?:[\._-][a-z0-9!#$%&'*+\/=?^‘{|}~]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{2,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
179
+ function isEmail(e) {
180
+ return !isSymbol(e) && emailReg.test(String(e).toLowerCase());
181
+ }
182
+ const ipReg = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
183
+ function isIpAddress(i) {
184
+ return !isSymbol(i) && ipReg.test(i);
185
+ }
186
+ const b64Reg = /^[A-Za-z0-9\-_]+={0,2}$/;
187
+ function isJWT(t) {
188
+ if (!isString(t))
189
+ return false;
190
+ const p = t.split('.');
191
+ if (p.length !== 3)
192
+ return false;
193
+ const header = p[0];
194
+ const payload = p[1];
195
+ const signature = p[3];
196
+ if (b64Reg.test(header) && b64Reg.test(payload) && b64Reg.test(signature)) {
197
+ try {
198
+ return isJson(atob(header)) && isJson(atob(payload));
199
+ }
200
+ catch (e) {
201
+ return false;
202
+ }
203
+ }
204
+ return false;
205
+ }
206
+ const slugReg = /^[^\s-_](?!.*?[-_]{2,})[a-z0-9-\\][^\s]*[^-_\s]$/;
207
+ function isSlug(s) {
208
+ return isString(s) && slugReg.test(s);
209
+ }
210
+ const hexadecimal = /^(#|0x|0h)?[0-9A-F]+$/i;
211
+ function isHexadecimal(s) {
212
+ return isString(s) && hexadecimal.test(s);
213
+ }
125
214
 
126
215
  function isObject(o, empty = false) {
127
216
  return o !== null && typeof o === "object" && !isArray(o) && (empty ? !!Object.keys(o).length : true);
@@ -131,6 +220,117 @@ function isProperty(val, obj) {
131
220
  return isString(v, true) && isObject(obj) ? Object.keys(obj).includes(v) : false;
132
221
  }
133
222
 
223
+ function isHtmlElement(h) {
224
+ return Boolean(typeof HTMLElement === "object"
225
+ ? h instanceof HTMLElement
226
+ : h &&
227
+ typeof h === "object" &&
228
+ h.nodeType === 1 &&
229
+ typeof h.nodeName === "string");
230
+ }
231
+ function isHtmlEventAttribute(h) {
232
+ switch (h) {
233
+ case "onafterprint":
234
+ case "onbeforeprint":
235
+ case "onbeforeunload":
236
+ case "onerror":
237
+ case "onhashchange":
238
+ case "onload":
239
+ case "onmessage":
240
+ case "onoffline":
241
+ case "ononline":
242
+ case "onpagehide":
243
+ case "onpageshow":
244
+ case "onpopstate":
245
+ case "onresize":
246
+ case "onstorage":
247
+ case "onunload":
248
+ case "onblur":
249
+ case "onchange":
250
+ case "oncontextmenu":
251
+ case "onfocus":
252
+ case "oninput":
253
+ case "oninvalid":
254
+ case "onreset":
255
+ case "onsearch":
256
+ case "onselect":
257
+ case "onsubmit":
258
+ case "onkeydown":
259
+ case "onkeypress":
260
+ case "onkeyup":
261
+ case "onclick":
262
+ case "ondblclick":
263
+ case "onmousedown":
264
+ case "onmousemove":
265
+ case "onmouseout":
266
+ case "onmouseover":
267
+ case "onmouseup":
268
+ case "onmousewheel":
269
+ case "onwheel":
270
+ case "ondrag":
271
+ case "ondragend":
272
+ case "ondragenter":
273
+ case "ondragleave":
274
+ case "ondragover":
275
+ case "ondragstart":
276
+ case "ondrop":
277
+ case "onscroll":
278
+ case "oncopy":
279
+ case "oncut":
280
+ case "onpaste":
281
+ case "onabort":
282
+ case "oncanplay":
283
+ case "oncanplaythrough":
284
+ case "oncuechange":
285
+ case "ondurationchange":
286
+ case "onemptied":
287
+ case "onended":
288
+ case "onerror":
289
+ case "onloadeddata":
290
+ case "onloadedmetadata":
291
+ case "onloadstart":
292
+ case "onpause":
293
+ case "onplay":
294
+ case "onplaying":
295
+ case "onprogress":
296
+ case "onratechange":
297
+ case "onseeked":
298
+ case "onseeking":
299
+ case "onstalled":
300
+ case "onsuspend":
301
+ case "ontimeupdate":
302
+ case "onvolumechange":
303
+ case "onwaiting":
304
+ case "ontoggle":
305
+ return true;
306
+ default:
307
+ return false;
308
+ }
309
+ }
310
+ function isNode(n) {
311
+ return Boolean(typeof Node === "object"
312
+ ? n instanceof Node
313
+ : n &&
314
+ typeof n === "object" &&
315
+ typeof n.nodeType === "number" &&
316
+ typeof n.nodeName === "string");
317
+ }
318
+
319
+ function isDate(d) {
320
+ return !isNaN(d) && d instanceof Date;
321
+ }
322
+ const minDate = new Date('1/1/1900');
323
+ const maxDate = new Date('1/1/2200');
324
+ function isValidDate(d, min = minDate, max = maxDate) {
325
+ return isDate(d) && d >= min && d <= max;
326
+ }
327
+ function isTimestamp(t, type = true) {
328
+ return isInteger(t, type) && isNumeric(new Date(parseInt(t + '')).getTime());
329
+ }
330
+ function isValidTimestamp(t, min = -2208989361000, max = 7258114800000, type = true) {
331
+ return isTimestamp(t, type) && t >= min && t <= max;
332
+ }
333
+
134
334
  const Methods = ["GET", "PATCH", "PUT", "POST", "DELETE"];
135
335
 
136
336
  const Types = {
@@ -146,8 +346,74 @@ const Types = {
146
346
  integer: {
147
347
  validate: (v, min, max, typeCheck) => isValidInteger(v, min || undefined, max || undefined, typeCheck || undefined)
148
348
  },
349
+ float: {
350
+ validate: (v, min, max, typeCheck) => isValidFloat(v, min || undefined, max || undefined, typeCheck || undefined)
351
+ },
352
+ even: {
353
+ validate: (v, _min, _max, typeCheck) => isEven(v, typeCheck || undefined)
354
+ },
355
+ odd: {
356
+ validate: (v, _min, _max, typeCheck) => isOdd(v, typeCheck || undefined)
357
+ },
358
+ positive: {
359
+ validate: (v, _min, _max, typeCheck) => isPositive(v, typeCheck || undefined)
360
+ },
361
+ negative: {
362
+ validate: (v, _min, _max, typeCheck) => isNegative(v, typeCheck || undefined)
363
+ },
364
+ powerOfTwo: {
365
+ validate: (v, _min, _max, typeCheck) => isPowerOfTwo(v, typeCheck || undefined)
366
+ },
367
+ ascii: {
368
+ validate: (v, _min, _max, typeCheck) => isAscii(v, typeCheck || undefined)
369
+ },
149
370
  array: {
150
- validate: (v, min, max, _typeCheck) => isArrayOfLength(v, min, max)
371
+ validate: (v, min, max, _typeCheck) => isArrayOfLength(v, min || undefined, max || undefined)
372
+ },
373
+ email: {
374
+ validate: (v, _min, _max, _typeCheck) => isEmail(v)
375
+ },
376
+ regex: {
377
+ validate: (v, _min, _max, typeCheck) => isRegex(v, typeCheck || undefined)
378
+ },
379
+ json: {
380
+ validate: (v, _min, _max, _typeCheck) => isJson(v)
381
+ },
382
+ jwt: {
383
+ validate: (v, _min, _max, _typeCheck) => isJWT(v)
384
+ },
385
+ symbol: {
386
+ validate: (v, _min, _max, _typeCheck) => isSymbol(v)
387
+ },
388
+ ipAddress: {
389
+ validate: (v, _min, _max, _typeCheck) => isIpAddress(v)
390
+ },
391
+ slug: {
392
+ validate: (v, _min, _max, _typeCheck) => isSlug(v)
393
+ },
394
+ hexadecimal: {
395
+ validate: (v, _min, _max, _typeCheck) => isHexadecimal(v)
396
+ },
397
+ date: {
398
+ validate: (v, min, max, _typeCheck) => isValidDate(v, min || undefined, max || undefined)
399
+ },
400
+ timestamp: {
401
+ validate: (v, min, max, typeCheck) => isValidTimestamp(v, min || undefined, max || undefined, typeCheck || undefined)
402
+ },
403
+ function: {
404
+ validate: (v, _min, _max, _typeCheck) => isFunction(v)
405
+ },
406
+ htmlElement: {
407
+ validate: (v, _min, _max, _typeCheck) => isHtmlElement(v)
408
+ },
409
+ htmlEventAttribute: {
410
+ validate: (v, _min, _max, _typeCheck) => isHtmlEventAttribute(v)
411
+ },
412
+ node: {
413
+ validate: (v, _min, _max, _typeCheck) => isNode(v)
414
+ },
415
+ object: {
416
+ validate: (v, _min, _max, _typeCheck) => isObject(v)
151
417
  }
152
418
  };
153
419
  const Required = {
@@ -233,7 +499,7 @@ class Entity {
233
499
  if (rq)
234
500
  return rq;
235
501
  }
236
- if (control) {
502
+ if (v && control) {
237
503
  const ct = this.control(v, key, type, min, max, typeCheck, controller);
238
504
  if (ct)
239
505
  return ct;
package/dist/antity.d.ts CHANGED
@@ -31,20 +31,8 @@ type Method = typeof Methods[number];
31
31
  declare const Methods: readonly ["GET", "PATCH", "PUT", "POST", "DELETE"];
32
32
 
33
33
  declare const Types: {
34
- readonly boolean: {
35
- readonly validate: (v: any, _min: number, _max: number, _typeCheck: boolean) => v is boolean;
36
- };
37
- readonly string: {
38
- readonly validate: (v: any, min: number, max: number, _typeCheck: boolean) => v is string;
39
- };
40
- readonly number: {
41
- readonly validate: (v: any, min: number, max: number, typeCheck: boolean) => v is number;
42
- };
43
- readonly integer: {
44
- readonly validate: (v: any, min: number, max: number, typeCheck: boolean) => v is number;
45
- };
46
- readonly array: {
47
- readonly validate: (v: any, min: number, max: number, _typeCheck: boolean) => v is any[];
34
+ [key: string]: {
35
+ validate: (v: any, min: number | Date, max: number | Date, typeCheck: boolean) => boolean;
48
36
  };
49
37
  };
50
38
 
@@ -88,8 +76,8 @@ declare class Property {
88
76
  controller: ((v: any) => any) | null;
89
77
  constructor(key: string,
90
78
  type: Type,
91
- min: number,
92
- max: number,
79
+ min: number | Date,
80
+ max: number | Date,
93
81
  required: boolean,
94
82
  typeCheck: boolean,
95
83
  methods: Method[],
@@ -101,6 +89,7 @@ declare class Property {
101
89
  controller: ((v: any) => any) | null
102
90
  );
103
91
  }
92
+
104
93
  export type { Type, Method };
105
94
  export {
106
95
  Entity,
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, isArrayOfLength, isNil, isString, isProperty, isArray, isIn, isInteger, isFunction, isObject } from '@dwtechs/checkard';
27
+ import { isBoolean, isStringOfLength, isValidNumber, isValidInteger, isValidFloat, isEven, isOdd, isPositive, isNegative, isPowerOfTwo, isAscii, isArrayOfLength, isEmail, isRegex, isJson, isJWT, isSymbol, isIpAddress, isSlug, isHexadecimal, isValidDate, isValidTimestamp, isFunction, isHtmlElement, isHtmlEventAttribute, isNode, isObject, isNil, isString, isProperty, isArray, isIn, isInteger } from '@dwtechs/checkard';
28
28
 
29
29
  const Methods = ["GET", "PATCH", "PUT", "POST", "DELETE"];
30
30
 
@@ -41,8 +41,74 @@ const Types = {
41
41
  integer: {
42
42
  validate: (v, min, max, typeCheck) => isValidInteger(v, min || undefined, max || undefined, typeCheck || undefined)
43
43
  },
44
+ float: {
45
+ validate: (v, min, max, typeCheck) => isValidFloat(v, min || undefined, max || undefined, typeCheck || undefined)
46
+ },
47
+ even: {
48
+ validate: (v, _min, _max, typeCheck) => isEven(v, typeCheck || undefined)
49
+ },
50
+ odd: {
51
+ validate: (v, _min, _max, typeCheck) => isOdd(v, typeCheck || undefined)
52
+ },
53
+ positive: {
54
+ validate: (v, _min, _max, typeCheck) => isPositive(v, typeCheck || undefined)
55
+ },
56
+ negative: {
57
+ validate: (v, _min, _max, typeCheck) => isNegative(v, typeCheck || undefined)
58
+ },
59
+ powerOfTwo: {
60
+ validate: (v, _min, _max, typeCheck) => isPowerOfTwo(v, typeCheck || undefined)
61
+ },
62
+ ascii: {
63
+ validate: (v, _min, _max, typeCheck) => isAscii(v, typeCheck || undefined)
64
+ },
44
65
  array: {
45
- validate: (v, min, max, _typeCheck) => isArrayOfLength(v, min, max)
66
+ validate: (v, min, max, _typeCheck) => isArrayOfLength(v, min || undefined, max || undefined)
67
+ },
68
+ email: {
69
+ validate: (v, _min, _max, _typeCheck) => isEmail(v)
70
+ },
71
+ regex: {
72
+ validate: (v, _min, _max, typeCheck) => isRegex(v, typeCheck || undefined)
73
+ },
74
+ json: {
75
+ validate: (v, _min, _max, _typeCheck) => isJson(v)
76
+ },
77
+ jwt: {
78
+ validate: (v, _min, _max, _typeCheck) => isJWT(v)
79
+ },
80
+ symbol: {
81
+ validate: (v, _min, _max, _typeCheck) => isSymbol(v)
82
+ },
83
+ ipAddress: {
84
+ validate: (v, _min, _max, _typeCheck) => isIpAddress(v)
85
+ },
86
+ slug: {
87
+ validate: (v, _min, _max, _typeCheck) => isSlug(v)
88
+ },
89
+ hexadecimal: {
90
+ validate: (v, _min, _max, _typeCheck) => isHexadecimal(v)
91
+ },
92
+ date: {
93
+ validate: (v, min, max, _typeCheck) => isValidDate(v, min || undefined, max || undefined)
94
+ },
95
+ timestamp: {
96
+ validate: (v, min, max, typeCheck) => isValidTimestamp(v, min || undefined, max || undefined, typeCheck || undefined)
97
+ },
98
+ function: {
99
+ validate: (v, _min, _max, _typeCheck) => isFunction(v)
100
+ },
101
+ htmlElement: {
102
+ validate: (v, _min, _max, _typeCheck) => isHtmlElement(v)
103
+ },
104
+ htmlEventAttribute: {
105
+ validate: (v, _min, _max, _typeCheck) => isHtmlEventAttribute(v)
106
+ },
107
+ node: {
108
+ validate: (v, _min, _max, _typeCheck) => isNode(v)
109
+ },
110
+ object: {
111
+ validate: (v, _min, _max, _typeCheck) => isObject(v)
46
112
  }
47
113
  };
48
114
  const Required = {
@@ -128,7 +194,7 @@ class Entity {
128
194
  if (rq)
129
195
  return rq;
130
196
  }
131
- if (control) {
197
+ if (v && control) {
132
198
  const ct = this.control(v, key, type, min, max, typeCheck, controller);
133
199
  if (ct)
134
200
  return ct;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Open source library for easy entity management",
5
5
  "keywords": [
6
6
  "entities"