@dwtechs/antity 0.7.0 → 0.8.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 +18 -17
- package/dist/antity.d.ts +5 -17
- package/dist/antity.js +18 -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 {
|
|
@@ -223,13 +224,13 @@ Any of these can be passed into the options object for each function.
|
|
|
223
224
|
| required | boolean | Is this property required on insert | false
|
|
224
225
|
| safe | boolean | Is this property safe to send to the client | true
|
|
225
226
|
| typeCheck | boolean | Type is checked if true | false
|
|
226
|
-
|
|
|
227
|
+
| methods | Method[] | SQL DML operations concerned by the property | [ "select", "insert", "update", "merge", "delete" ]
|
|
227
228
|
| sanitize | boolean | Sanitize the property if true | true
|
|
228
229
|
| normalize | boolean | Normalize the property if true | false
|
|
229
|
-
|
|
|
230
|
+
| validate | boolean | validate the property if true | true
|
|
230
231
|
| sanitizer | ((v:any) => any) \| null | Sanitizer function if sanitize is true | null
|
|
231
232
|
| normalizer | ((v:any) => any) \| null | Normalizer function if normalize is true | null
|
|
232
|
-
|
|
|
233
|
+
| validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) \| null | validator function if validate is true | null
|
|
233
234
|
|
|
234
235
|
* *Min and max parameters are not used for boolean type*
|
|
235
236
|
* *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;
|
|
@@ -91,10 +81,10 @@ declare class Property {
|
|
|
91
81
|
methods: Method[];
|
|
92
82
|
sanitize: boolean;
|
|
93
83
|
normalize: boolean;
|
|
94
|
-
|
|
84
|
+
validate: boolean;
|
|
95
85
|
sanitizer: ((v: any) => any) | null;
|
|
96
86
|
normalizer: ((v: any) => any) | null;
|
|
97
|
-
|
|
87
|
+
validator: ((v: any) => any) | null;
|
|
98
88
|
constructor(
|
|
99
89
|
key: string,
|
|
100
90
|
type: Type,
|
|
@@ -106,10 +96,10 @@ declare class Property {
|
|
|
106
96
|
methods: Method[],
|
|
107
97
|
sanitize: boolean,
|
|
108
98
|
normalize: boolean,
|
|
109
|
-
|
|
99
|
+
validate: boolean,
|
|
110
100
|
sanitizer: ((v: any) => any) | null,
|
|
111
101
|
normalizer: ((v: any) => any) | null,
|
|
112
|
-
|
|
102
|
+
validator: ((v: any) => any) | null
|
|
113
103
|
);
|
|
114
104
|
}
|
|
115
105
|
|
|
@@ -117,7 +107,5 @@ export type { Type, Method };
|
|
|
117
107
|
export {
|
|
118
108
|
Entity,
|
|
119
109
|
Property,
|
|
120
|
-
Types,
|
|
121
|
-
Methods,
|
|
122
110
|
};
|
|
123
111
|
|
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);
|
|
@@ -179,7 +179,7 @@ class Entity {
|
|
|
179
179
|
normalize(req, _res, next) {
|
|
180
180
|
var _a;
|
|
181
181
|
const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
|
|
182
|
-
if (!
|
|
182
|
+
if (!isArray(rows, "!0"))
|
|
183
183
|
return next({ status: 400, msg: "Normalize: no rows found in request body" });
|
|
184
184
|
for (const r of rows) {
|
|
185
185
|
for (const { key, type, sanitize, normalize, sanitizer, normalizer, } of this.properties) {
|
|
@@ -189,7 +189,7 @@ class Entity {
|
|
|
189
189
|
log.debug(`sanitize ${key}: ${type} = ${v}`);
|
|
190
190
|
v = this.sanitize(v, sanitizer);
|
|
191
191
|
}
|
|
192
|
-
if (normalize && normalizer) {
|
|
192
|
+
if (normalize && isFunction(normalizer)) {
|
|
193
193
|
log.debug(`normalize ${key}: ${type} = ${v}`);
|
|
194
194
|
v = normalizer(v);
|
|
195
195
|
}
|
|
@@ -201,9 +201,9 @@ class Entity {
|
|
|
201
201
|
}
|
|
202
202
|
validate(req, _res, next) {
|
|
203
203
|
var _a;
|
|
204
|
-
const rows = req.body;
|
|
204
|
+
const rows = (_a = req.body) === null || _a === void 0 ? void 0 : _a.rows;
|
|
205
205
|
const method = req.method;
|
|
206
|
-
if (!
|
|
206
|
+
if (!isArray(rows, "!0"))
|
|
207
207
|
return next({ status: 400, msg: "Sanitize: no rows found in request body" });
|
|
208
208
|
if (!isIn(Methods, method))
|
|
209
209
|
return next({
|
|
@@ -211,7 +211,7 @@ class Entity {
|
|
|
211
211
|
msg: `Invalid REST method. Received: ${method}. Must be one of: ${Methods.toString()}`
|
|
212
212
|
});
|
|
213
213
|
for (const r of rows) {
|
|
214
|
-
for (const { key, type, min, max, required, typeCheck, methods,
|
|
214
|
+
for (const { key, type, min, max, required, typeCheck, methods, validate, validator } of this.properties) {
|
|
215
215
|
const v = r[key];
|
|
216
216
|
if (isIn(methods, method)) {
|
|
217
217
|
if (required) {
|
|
@@ -219,8 +219,8 @@ class Entity {
|
|
|
219
219
|
if (rq)
|
|
220
220
|
return next(rq);
|
|
221
221
|
}
|
|
222
|
-
if (v &&
|
|
223
|
-
const ct = this.control(v, key, type, min, max, typeCheck,
|
|
222
|
+
if (v && validate) {
|
|
223
|
+
const ct = this.control(v, key, type, min, max, typeCheck, validator);
|
|
224
224
|
if (ct)
|
|
225
225
|
return next(ct);
|
|
226
226
|
}
|
|
@@ -240,7 +240,12 @@ class Entity {
|
|
|
240
240
|
val = cb(v);
|
|
241
241
|
else
|
|
242
242
|
val = Types[type].validate(v, min, max, typeCheck);
|
|
243
|
-
|
|
243
|
+
let c = "";
|
|
244
|
+
if (!isNil(min))
|
|
245
|
+
c += ` and >= ${min}`;
|
|
246
|
+
if (!isNil(max))
|
|
247
|
+
c += ` and <= ${max}`;
|
|
248
|
+
return val ? null : { status: 400, msg: `Invalid ${key}, must be of type ${type}${c}` };
|
|
244
249
|
}
|
|
245
250
|
sanitize(v, cb) {
|
|
246
251
|
if (cb)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dwtechs/antity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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/"
|