@dwtechs/antity 0.7.0 → 0.9.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 +26 -17
- package/dist/antity.d.ts +6 -17
- package/dist/antity.js +26 -13
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
[](https://opensource.org/licenses/MIT)
|
|
3
3
|
[](https://www.npmjs.com/package/@dwtechs/antity)
|
|
4
4
|
[](https://www.npmjs.com/package/@dwtechs/antity)
|
|
5
|
+

|
|
5
6
|
[](https://www.npmjs.com/package/@dwtechs/antity)
|
|
6
7
|
|
|
7
8
|
- [Synopsis](#synopsis)
|
|
@@ -57,15 +58,15 @@ const entity = new Entity("consumers", [
|
|
|
57
58
|
min: 0,
|
|
58
59
|
max: 120,
|
|
59
60
|
typeCheck: true,
|
|
60
|
-
|
|
61
|
+
methods: ["GET", "PUT", "DELETE"],
|
|
61
62
|
required: true,
|
|
62
63
|
safe: true,
|
|
63
64
|
sanitize: true,
|
|
64
65
|
normalize: true,
|
|
65
|
-
|
|
66
|
+
validate: true,
|
|
66
67
|
sanitizer: null,
|
|
67
68
|
normalizer: null,
|
|
68
|
-
|
|
69
|
+
validator: null,
|
|
69
70
|
},
|
|
70
71
|
{
|
|
71
72
|
key: "firstName",
|
|
@@ -73,15 +74,15 @@ const entity = new Entity("consumers", [
|
|
|
73
74
|
min: 0,
|
|
74
75
|
max: 255,
|
|
75
76
|
typeCheck: true,
|
|
76
|
-
|
|
77
|
+
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
77
78
|
required: true,
|
|
78
79
|
safe: true,
|
|
79
80
|
sanitize: true,
|
|
80
81
|
normalize: true,
|
|
81
|
-
|
|
82
|
+
validate: true,
|
|
82
83
|
sanitizer: null,
|
|
83
84
|
normalizer: normalizeName,
|
|
84
|
-
|
|
85
|
+
validator: null,
|
|
85
86
|
},
|
|
86
87
|
{
|
|
87
88
|
key: "lastName",
|
|
@@ -89,15 +90,15 @@ const entity = new Entity("consumers", [
|
|
|
89
90
|
min: 0,
|
|
90
91
|
max: 255,
|
|
91
92
|
typeCheck: true,
|
|
92
|
-
|
|
93
|
+
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
93
94
|
required: true,
|
|
94
95
|
safe: true,
|
|
95
96
|
sanitize: true,
|
|
96
97
|
normalize: true,
|
|
97
|
-
|
|
98
|
+
validate: true,
|
|
98
99
|
sanitizer: null,
|
|
99
100
|
normalizer: normalizeName,
|
|
100
|
-
|
|
101
|
+
validator: null,
|
|
101
102
|
},
|
|
102
103
|
{
|
|
103
104
|
key: "nickname",
|
|
@@ -105,15 +106,15 @@ const entity = new Entity("consumers", [
|
|
|
105
106
|
min: 0,
|
|
106
107
|
max: 255,
|
|
107
108
|
typeCheck: true,
|
|
108
|
-
|
|
109
|
+
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
109
110
|
required: true,
|
|
110
111
|
safe: true,
|
|
111
112
|
sanitize: true,
|
|
112
113
|
normalize: true,
|
|
113
|
-
|
|
114
|
+
validate: true,
|
|
114
115
|
sanitizer: null,
|
|
115
116
|
normalizer: normalizeNickname,
|
|
116
|
-
|
|
117
|
+
validator: null,
|
|
117
118
|
},
|
|
118
119
|
]);
|
|
119
120
|
|
|
@@ -169,10 +170,10 @@ class Property {
|
|
|
169
170
|
methods: Method[];
|
|
170
171
|
sanitize: boolean;
|
|
171
172
|
normalize: boolean;
|
|
172
|
-
|
|
173
|
+
validate: boolean;
|
|
173
174
|
sanitizer: Function | null;
|
|
174
175
|
normalizer: Function | null;
|
|
175
|
-
|
|
176
|
+
validator: Function | null;
|
|
176
177
|
};
|
|
177
178
|
|
|
178
179
|
class Entity {
|
|
@@ -189,6 +190,14 @@ class Entity {
|
|
|
189
190
|
* @returns {Property | undefined} - The property object if found, otherwise `undefined`.
|
|
190
191
|
*/
|
|
191
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[];
|
|
192
201
|
|
|
193
202
|
/**
|
|
194
203
|
* Normalizes an array of records by applying sanitization and normalization
|
|
@@ -223,13 +232,13 @@ Any of these can be passed into the options object for each function.
|
|
|
223
232
|
| required | boolean | Is this property required on insert | false
|
|
224
233
|
| safe | boolean | Is this property safe to send to the client | true
|
|
225
234
|
| typeCheck | boolean | Type is checked if true | false
|
|
226
|
-
|
|
|
235
|
+
| methods | Method[] | SQL DML operations concerned by the property | [ "select", "insert", "update", "merge", "delete" ]
|
|
227
236
|
| sanitize | boolean | Sanitize the property if true | true
|
|
228
237
|
| normalize | boolean | Normalize the property if true | false
|
|
229
|
-
|
|
|
238
|
+
| validate | boolean | validate the property if true | true
|
|
230
239
|
| sanitizer | ((v:any) => any) \| null | Sanitizer function if sanitize is true | null
|
|
231
240
|
| normalizer | ((v:any) => any) \| null | Normalizer function if normalize is true | null
|
|
232
|
-
|
|
|
241
|
+
| validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) \| null | validator function if validate is true | null
|
|
233
242
|
|
|
234
243
|
* *Min and max parameters are not used for boolean type*
|
|
235
244
|
* *TypeCheck Parameter is not used for boolean, string and array types*
|
package/dist/antity.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ https://github.com/DWTechs/Antity.js
|
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
27
|
import type { Request, Response, NextFunction } from 'express';
|
|
28
|
+
declare const Methods: readonly [ "GET", "PATCH", "PUT", "POST", "DELETE" ];
|
|
28
29
|
type Method = typeof Methods[number];
|
|
29
30
|
type Type =
|
|
30
31
|
"boolean" |
|
|
@@ -55,17 +56,6 @@ type Type =
|
|
|
55
56
|
"node" |
|
|
56
57
|
"object";
|
|
57
58
|
|
|
58
|
-
declare const Methods: readonly [ "GET", "PATCH", "PUT", "POST", "DELETE" ];
|
|
59
|
-
|
|
60
|
-
declare const Types: Record<Type, {
|
|
61
|
-
validate: (
|
|
62
|
-
v: any,
|
|
63
|
-
min: number | Date,
|
|
64
|
-
max: number | Date,
|
|
65
|
-
typeCheck: boolean
|
|
66
|
-
) => boolean;
|
|
67
|
-
}>;
|
|
68
|
-
|
|
69
59
|
declare class Entity {
|
|
70
60
|
private _name;
|
|
71
61
|
private _unsafeProps;
|
|
@@ -76,6 +66,7 @@ declare class Entity {
|
|
|
76
66
|
get properties(): Property[];
|
|
77
67
|
set name(name: string);
|
|
78
68
|
getProp(key: string): Property | undefined;
|
|
69
|
+
getPropsByMethod(method: Method): Property[];
|
|
79
70
|
normalize(req: Request, _res: Response, next: NextFunction): void;
|
|
80
71
|
validate(req: Request, _res: Response, next: NextFunction): void;
|
|
81
72
|
}
|
|
@@ -91,10 +82,10 @@ declare class Property {
|
|
|
91
82
|
methods: Method[];
|
|
92
83
|
sanitize: boolean;
|
|
93
84
|
normalize: boolean;
|
|
94
|
-
|
|
85
|
+
validate: boolean;
|
|
95
86
|
sanitizer: ((v: any) => any) | null;
|
|
96
87
|
normalizer: ((v: any) => any) | null;
|
|
97
|
-
|
|
88
|
+
validator: ((v: any) => any) | null;
|
|
98
89
|
constructor(
|
|
99
90
|
key: string,
|
|
100
91
|
type: Type,
|
|
@@ -106,10 +97,10 @@ declare class Property {
|
|
|
106
97
|
methods: Method[],
|
|
107
98
|
sanitize: boolean,
|
|
108
99
|
normalize: boolean,
|
|
109
|
-
|
|
100
|
+
validate: boolean,
|
|
110
101
|
sanitizer: ((v: any) => any) | null,
|
|
111
102
|
normalizer: ((v: any) => any) | null,
|
|
112
|
-
|
|
103
|
+
validator: ((v: any) => any) | null
|
|
113
104
|
);
|
|
114
105
|
}
|
|
115
106
|
|
|
@@ -117,7 +108,5 @@ export type { Type, Method };
|
|
|
117
108
|
export {
|
|
118
109
|
Entity,
|
|
119
110
|
Property,
|
|
120
|
-
Types,
|
|
121
|
-
Methods,
|
|
122
111
|
};
|
|
123
112
|
|
package/dist/antity.js
CHANGED
|
@@ -40,7 +40,7 @@ const Types = {
|
|
|
40
40
|
validate: (v, min, max, typeCheck) => isValidNumber(v, min || undefined, max || undefined, typeCheck || undefined)
|
|
41
41
|
},
|
|
42
42
|
integer: {
|
|
43
|
-
validate: (v, min, max, typeCheck) => isValidInteger(v, min
|
|
43
|
+
validate: (v, min, max, typeCheck) => isValidInteger(v, min !== null && min !== void 0 ? min : undefined, max !== null && max !== void 0 ? max : undefined, typeCheck || undefined)
|
|
44
44
|
},
|
|
45
45
|
float: {
|
|
46
46
|
validate: (v, min, max, typeCheck) => isValidFloat(v, min || undefined, max || undefined, typeCheck || undefined)
|
|
@@ -114,7 +114,7 @@ const Types = {
|
|
|
114
114
|
};
|
|
115
115
|
|
|
116
116
|
class Property {
|
|
117
|
-
constructor(key, type, min, max, required, safe, typeCheck, methods, sanitize, normalize,
|
|
117
|
+
constructor(key, type, min, max, required, safe, typeCheck, methods, sanitize, normalize, validate, sanitizer, normalizer, validator) {
|
|
118
118
|
if (!isString(key, "!0"))
|
|
119
119
|
throw new Error(`Property "key" must be a string. Received ${key}`);
|
|
120
120
|
if (!isProperty(Types, type))
|
|
@@ -135,10 +135,10 @@ class Property {
|
|
|
135
135
|
this.methods = methods || Methods;
|
|
136
136
|
this.sanitize = isBoolean(sanitize) ? sanitize : true;
|
|
137
137
|
this.normalize = isBoolean(normalize) ? normalize : false;
|
|
138
|
-
this.
|
|
138
|
+
this.validate = isBoolean(validate) ? validate : true;
|
|
139
139
|
this.sanitizer = isFunction(sanitizer) ? sanitizer : null;
|
|
140
140
|
this.normalizer = isFunction(normalizer) ? normalizer : null;
|
|
141
|
-
this.
|
|
141
|
+
this.validator = isFunction(validator) ? validator : null;
|
|
142
142
|
}
|
|
143
143
|
interval(val, type, integerDefault, dateDefault) {
|
|
144
144
|
if (type === "date")
|
|
@@ -153,7 +153,7 @@ class Entity {
|
|
|
153
153
|
this._properties = [];
|
|
154
154
|
this._unsafeProps = [];
|
|
155
155
|
for (const p of properties) {
|
|
156
|
-
const prop = new Property(p.key, p.type, p.min, p.max, p.required, p.safe, p.typeCheck, p.methods, p.sanitize, p.normalize, p.
|
|
156
|
+
const prop = new Property(p.key, p.type, p.min, p.max, p.required, p.safe, p.typeCheck, p.methods, p.sanitize, p.normalize, p.validate, p.sanitizer, p.normalizer, p.validator);
|
|
157
157
|
this._properties.push(prop);
|
|
158
158
|
if (!prop.safe)
|
|
159
159
|
this._unsafeProps.push(prop.key);
|
|
@@ -176,10 +176,18 @@ class Entity {
|
|
|
176
176
|
getProp(key) {
|
|
177
177
|
return this.properties.find(p => p.key === key);
|
|
178
178
|
}
|
|
179
|
+
getPropsByMethod(method) {
|
|
180
|
+
const props = [];
|
|
181
|
+
for (const p of this.properties) {
|
|
182
|
+
if (isIn(p.methods, method, 0))
|
|
183
|
+
props.push(p);
|
|
184
|
+
}
|
|
185
|
+
return props;
|
|
186
|
+
}
|
|
179
187
|
normalize(req, _res, next) {
|
|
180
188
|
var _a;
|
|
181
189
|
const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
|
|
182
|
-
if (!
|
|
190
|
+
if (!isArray(rows, "!0"))
|
|
183
191
|
return next({ status: 400, msg: "Normalize: no rows found in request body" });
|
|
184
192
|
for (const r of rows) {
|
|
185
193
|
for (const { key, type, sanitize, normalize, sanitizer, normalizer, } of this.properties) {
|
|
@@ -189,7 +197,7 @@ class Entity {
|
|
|
189
197
|
log.debug(`sanitize ${key}: ${type} = ${v}`);
|
|
190
198
|
v = this.sanitize(v, sanitizer);
|
|
191
199
|
}
|
|
192
|
-
if (normalize && normalizer) {
|
|
200
|
+
if (normalize && isFunction(normalizer)) {
|
|
193
201
|
log.debug(`normalize ${key}: ${type} = ${v}`);
|
|
194
202
|
v = normalizer(v);
|
|
195
203
|
}
|
|
@@ -201,9 +209,9 @@ class Entity {
|
|
|
201
209
|
}
|
|
202
210
|
validate(req, _res, next) {
|
|
203
211
|
var _a;
|
|
204
|
-
const rows = req.body;
|
|
212
|
+
const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
|
|
205
213
|
const method = req.method;
|
|
206
|
-
if (!
|
|
214
|
+
if (!isArray(rows, "!0"))
|
|
207
215
|
return next({ status: 400, msg: "Sanitize: no rows found in request body" });
|
|
208
216
|
if (!isIn(Methods, method))
|
|
209
217
|
return next({
|
|
@@ -211,7 +219,7 @@ class Entity {
|
|
|
211
219
|
msg: `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`
|
|
212
220
|
});
|
|
213
221
|
for (const r of rows) {
|
|
214
|
-
for (const { key, type, min, max, required, typeCheck, methods,
|
|
222
|
+
for (const { key, type, min, max, required, typeCheck, methods, validate, validator } of this.properties) {
|
|
215
223
|
const v = r[key];
|
|
216
224
|
if (isIn(methods, method)) {
|
|
217
225
|
if (required) {
|
|
@@ -219,8 +227,8 @@ class Entity {
|
|
|
219
227
|
if (rq)
|
|
220
228
|
return next(rq);
|
|
221
229
|
}
|
|
222
|
-
if (v &&
|
|
223
|
-
const ct = this.control(v, key, type, min, max, typeCheck,
|
|
230
|
+
if (v && validate) {
|
|
231
|
+
const ct = this.control(v, key, type, min, max, typeCheck, validator);
|
|
224
232
|
if (ct)
|
|
225
233
|
return next(ct);
|
|
226
234
|
}
|
|
@@ -240,7 +248,12 @@ class Entity {
|
|
|
240
248
|
val = cb(v);
|
|
241
249
|
else
|
|
242
250
|
val = Types[type].validate(v, min, max, typeCheck);
|
|
243
|
-
|
|
251
|
+
let c = "";
|
|
252
|
+
if (!isNil(min))
|
|
253
|
+
c += ` and >= ${min}`;
|
|
254
|
+
if (!isNil(max))
|
|
255
|
+
c += ` and <= ${max}`;
|
|
256
|
+
return val ? null : { status: 400, msg: `Invalid ${key}, must be of type ${type}${c}` };
|
|
244
257
|
}
|
|
245
258
|
sanitize(v, cb) {
|
|
246
259
|
if (cb)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dwtechs/antity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Open source library for easy entity management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"entities"
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"rollup:mjs": "rollup --config rollup.config.mjs",
|
|
31
31
|
"rollup:cjs": "rollup --config rollup.config.cjs.mjs",
|
|
32
32
|
"rollup": "npm run rollup:mjs",
|
|
33
|
-
"test": "jest --coverage"
|
|
33
|
+
"test": "jest --coverage --config jest.config.js"
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
36
|
"dist/"
|