@dwtechs/antity 0.1.0 → 0.3.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 +36 -4
- package/dist/antity.cjs.js +283 -9
- package/dist/antity.d.ts +37 -21
- package/dist/antity.js +84 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,6 +116,8 @@ const entity = new Entity("consumer", "consumers", [
|
|
|
116
116
|
req.body = entity.normalize(req.body);
|
|
117
117
|
const check = entity.validate(req.body, req.method);
|
|
118
118
|
|
|
119
|
+
|
|
120
|
+
|
|
119
121
|
```
|
|
120
122
|
|
|
121
123
|
|
|
@@ -124,17 +126,44 @@ const check = entity.validate(req.body, req.method);
|
|
|
124
126
|
|
|
125
127
|
```javascript
|
|
126
128
|
|
|
127
|
-
type Type = "boolean" |
|
|
129
|
+
type Type = "boolean" |
|
|
130
|
+
"string" |
|
|
131
|
+
"number" |
|
|
132
|
+
"integer" |
|
|
133
|
+
"float" |
|
|
134
|
+
"even" |
|
|
135
|
+
"odd" |
|
|
136
|
+
"positve" |
|
|
137
|
+
"negative" |
|
|
138
|
+
"powerOfTwo" |
|
|
139
|
+
"ascii" |
|
|
140
|
+
"array" |
|
|
141
|
+
"jwt" |
|
|
142
|
+
"symbol" |
|
|
143
|
+
"email" |
|
|
144
|
+
"regex" |
|
|
145
|
+
"json" |
|
|
146
|
+
"ipAddress" |
|
|
147
|
+
"slug" |
|
|
148
|
+
"hexadecimal" |
|
|
149
|
+
"date" |
|
|
150
|
+
"timestamp" |
|
|
151
|
+
"function" |
|
|
152
|
+
"htmlElement" |
|
|
153
|
+
"htmlEventAttribute" |
|
|
154
|
+
"node" |
|
|
155
|
+
"object";
|
|
156
|
+
|
|
128
157
|
type Method = "GET" | "PATCH" | "PUT" | "POST" | "DELETE";
|
|
129
158
|
|
|
130
159
|
class Property {
|
|
131
160
|
key: string;
|
|
132
161
|
type: Type;
|
|
133
|
-
min: number;
|
|
134
|
-
max: number;
|
|
162
|
+
min: number | Date;
|
|
163
|
+
max: number | Date;
|
|
135
164
|
required: boolean;
|
|
136
165
|
typeCheck: boolean;
|
|
137
|
-
|
|
166
|
+
methods: Method[];
|
|
138
167
|
sanitize: boolean;
|
|
139
168
|
normalize: boolean;
|
|
140
169
|
control: boolean;
|
|
@@ -146,9 +175,12 @@ class Property {
|
|
|
146
175
|
class Entity {
|
|
147
176
|
name: string;
|
|
148
177
|
table: string;
|
|
178
|
+
cols: Record<Method, string>;
|
|
149
179
|
properties: Property[];
|
|
150
180
|
normalize(rows: Record<string, any>[]): Record<string, any>[];
|
|
151
181
|
validate(rows: Record<string, any>[], method: Method): string | null;
|
|
182
|
+
getTable(): string;
|
|
183
|
+
getCols(method: Method): string;
|
|
152
184
|
}
|
|
153
185
|
|
|
154
186
|
```
|
package/dist/antity.cjs.js
CHANGED
|
@@ -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 = {
|
|
@@ -191,22 +457,30 @@ class Entity {
|
|
|
191
457
|
constructor(name, table, properties) {
|
|
192
458
|
this.name = name;
|
|
193
459
|
this.table = table;
|
|
460
|
+
this.cols = {
|
|
461
|
+
GET: "",
|
|
462
|
+
POST: "",
|
|
463
|
+
PUT: "",
|
|
464
|
+
PATCH: "",
|
|
465
|
+
DELETE: "",
|
|
466
|
+
};
|
|
194
467
|
this.properties = this.init(properties);
|
|
195
468
|
}
|
|
196
469
|
init(properties) {
|
|
197
470
|
const props = [];
|
|
198
471
|
for (const p of properties) {
|
|
199
472
|
props.push(new Property(p.key, p.type, p.min, p.max, p.required, p.typeCheck, p.methods, p.sanitize, p.normalize, p.control, p.sanitizer, p.normalizer, p.controller));
|
|
473
|
+
for (const m of p.methods) {
|
|
474
|
+
this.cols[m] += this.cols[m].length ? `, ${p.key}` : `${p.key}`;
|
|
475
|
+
}
|
|
200
476
|
}
|
|
201
477
|
return props;
|
|
202
478
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
return cols;
|
|
479
|
+
getTable() {
|
|
480
|
+
return this.table;
|
|
481
|
+
}
|
|
482
|
+
getCols(method) {
|
|
483
|
+
return this.cols[method];
|
|
210
484
|
}
|
|
211
485
|
normalize(rows) {
|
|
212
486
|
for (const r of rows) {
|
|
@@ -233,7 +507,7 @@ class Entity {
|
|
|
233
507
|
if (rq)
|
|
234
508
|
return rq;
|
|
235
509
|
}
|
|
236
|
-
if (control) {
|
|
510
|
+
if (v && control) {
|
|
237
511
|
const ct = this.control(v, key, type, min, max, typeCheck, controller);
|
|
238
512
|
if (ct)
|
|
239
513
|
return ct;
|
package/dist/antity.d.ts
CHANGED
|
@@ -25,28 +25,41 @@ https://github.com/DWTechs/Antity.js
|
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
type Type = keyof typeof Types;
|
|
29
28
|
type Method = typeof Methods[number];
|
|
29
|
+
type Type =
|
|
30
|
+
"boolean" |
|
|
31
|
+
"string" |
|
|
32
|
+
"number" |
|
|
33
|
+
"integer" |
|
|
34
|
+
"float" |
|
|
35
|
+
"even" |
|
|
36
|
+
"odd" |
|
|
37
|
+
"positive" |
|
|
38
|
+
"negative" |
|
|
39
|
+
"powerOfTwo" |
|
|
40
|
+
"ascii" |
|
|
41
|
+
"array" |
|
|
42
|
+
"jwt" |
|
|
43
|
+
"symbol" |
|
|
44
|
+
"email" |
|
|
45
|
+
"regex" |
|
|
46
|
+
"json" |
|
|
47
|
+
"ipAddress" |
|
|
48
|
+
"slug" |
|
|
49
|
+
"hexadecimal" |
|
|
50
|
+
"date" |
|
|
51
|
+
"timestamp" |
|
|
52
|
+
"function" |
|
|
53
|
+
"htmlElement" |
|
|
54
|
+
"htmlEventAttribute" |
|
|
55
|
+
"node" |
|
|
56
|
+
"object";
|
|
30
57
|
|
|
31
58
|
declare const Methods: readonly ["GET", "PATCH", "PUT", "POST", "DELETE"];
|
|
32
59
|
|
|
33
|
-
declare const Types: {
|
|
34
|
-
|
|
35
|
-
|
|
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[];
|
|
48
|
-
};
|
|
49
|
-
};
|
|
60
|
+
declare const Types: Record<Type, {
|
|
61
|
+
validate: (v: any, min: number | Date, max: number | Date, typeCheck: boolean) => boolean;
|
|
62
|
+
}>;
|
|
50
63
|
|
|
51
64
|
declare const Required: {
|
|
52
65
|
validate: (v: any) => boolean;
|
|
@@ -55,10 +68,12 @@ declare const Required: {
|
|
|
55
68
|
declare class Entity {
|
|
56
69
|
name: string;
|
|
57
70
|
table: string;
|
|
71
|
+
cols: Record<Method, string>;
|
|
58
72
|
properties: Property[];
|
|
59
73
|
constructor(name: string, table: string, properties: Property[]);
|
|
60
74
|
private init;
|
|
61
|
-
|
|
75
|
+
getTable(): string;
|
|
76
|
+
getCols(method: Method): string;
|
|
62
77
|
normalize(rows: Record<string, any>[]): Record<string, any>[];
|
|
63
78
|
validate(rows: Record<string, any>[], method: Method): string | null;
|
|
64
79
|
private require;
|
|
@@ -88,8 +103,8 @@ declare class Property {
|
|
|
88
103
|
controller: ((v: any) => any) | null;
|
|
89
104
|
constructor(key: string,
|
|
90
105
|
type: Type,
|
|
91
|
-
min: number,
|
|
92
|
-
max: number,
|
|
106
|
+
min: number | Date,
|
|
107
|
+
max: number | Date,
|
|
93
108
|
required: boolean,
|
|
94
109
|
typeCheck: boolean,
|
|
95
110
|
methods: Method[],
|
|
@@ -101,6 +116,7 @@ declare class Property {
|
|
|
101
116
|
controller: ((v: any) => any) | null
|
|
102
117
|
);
|
|
103
118
|
}
|
|
119
|
+
|
|
104
120
|
export type { Type, Method };
|
|
105
121
|
export {
|
|
106
122
|
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
|
|
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 = {
|
|
@@ -86,22 +152,30 @@ class Entity {
|
|
|
86
152
|
constructor(name, table, properties) {
|
|
87
153
|
this.name = name;
|
|
88
154
|
this.table = table;
|
|
155
|
+
this.cols = {
|
|
156
|
+
GET: "",
|
|
157
|
+
POST: "",
|
|
158
|
+
PUT: "",
|
|
159
|
+
PATCH: "",
|
|
160
|
+
DELETE: "",
|
|
161
|
+
};
|
|
89
162
|
this.properties = this.init(properties);
|
|
90
163
|
}
|
|
91
164
|
init(properties) {
|
|
92
165
|
const props = [];
|
|
93
166
|
for (const p of properties) {
|
|
94
167
|
props.push(new Property(p.key, p.type, p.min, p.max, p.required, p.typeCheck, p.methods, p.sanitize, p.normalize, p.control, p.sanitizer, p.normalizer, p.controller));
|
|
168
|
+
for (const m of p.methods) {
|
|
169
|
+
this.cols[m] += this.cols[m].length ? `, ${p.key}` : `${p.key}`;
|
|
170
|
+
}
|
|
95
171
|
}
|
|
96
172
|
return props;
|
|
97
173
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
return cols;
|
|
174
|
+
getTable() {
|
|
175
|
+
return this.table;
|
|
176
|
+
}
|
|
177
|
+
getCols(method) {
|
|
178
|
+
return this.cols[method];
|
|
105
179
|
}
|
|
106
180
|
normalize(rows) {
|
|
107
181
|
for (const r of rows) {
|
|
@@ -128,7 +202,7 @@ class Entity {
|
|
|
128
202
|
if (rq)
|
|
129
203
|
return rq;
|
|
130
204
|
}
|
|
131
|
-
if (control) {
|
|
205
|
+
if (v && control) {
|
|
132
206
|
const ct = this.control(v, key, type, min, max, typeCheck, controller);
|
|
133
207
|
if (ct)
|
|
134
208
|
return ct;
|