@dwtechs/antity 0.15.0 → 0.16.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 +19 -19
- package/dist/antity.d.ts +8 -8
- package/dist/antity.js +17 -17
- package/package.json +1 -1
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
isTypeChecked: true,
|
|
91
|
+
requiredFor: ["PUT"],
|
|
92
|
+
isPrivate: true,
|
|
93
93
|
sanitizer: null,
|
|
94
94
|
normalizer: normalizeNickname,
|
|
95
95
|
validator: null,
|
|
@@ -145,9 +145,9 @@ class Property {
|
|
|
145
145
|
type: Type;
|
|
146
146
|
min: number | Date | null;
|
|
147
147
|
max: number | Date | null;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
requiredFor: Method[];
|
|
149
|
+
isPrivate: boolean;
|
|
150
|
+
isTypeChecked: boolean;
|
|
151
151
|
sanitizer: Function | null;
|
|
152
152
|
normalizer: Function | null;
|
|
153
153
|
validator: Function | null;
|
|
@@ -156,7 +156,7 @@ class Property {
|
|
|
156
156
|
class Entity {
|
|
157
157
|
constructor(name: string, properties: Property[]);
|
|
158
158
|
get name(): string;
|
|
159
|
-
get
|
|
159
|
+
get privateProps(): string[];
|
|
160
160
|
get properties(): Property[];
|
|
161
161
|
set name(name: string);
|
|
162
162
|
|
|
@@ -376,9 +376,9 @@ Any of these can be passed into the options object for each function.
|
|
|
376
376
|
| type | Type | Type of the property |
|
|
377
377
|
| min | number \| Date | Minimum value if applicable | 0 \| 1900-01-01
|
|
378
378
|
| max | number \| Date | Maximum value if applicable | 999999999 \| 2200-12-31
|
|
379
|
-
|
|
|
380
|
-
|
|
|
381
|
-
|
|
|
379
|
+
| requiredFor | Methods[] | Property is required for the listed methods only | [ "POST", "PUT", "PATCH" ]
|
|
380
|
+
| isPrivate | boolean | Property should not be sent in the response | false
|
|
381
|
+
| isTypeChecked | boolean | Strict type check at validation | false
|
|
382
382
|
| sanitizer | ((v:any) => any) \| null | Custom sanitizer function | null
|
|
383
383
|
| normalizer | ((v:any) => any) \| null | Custom Normalizer function | null
|
|
384
384
|
| validator | ((v:any, min:number, max:number, typeCheck:boolean) => any) \| null | Custom validator | null
|
package/dist/antity.d.ts
CHANGED
|
@@ -59,11 +59,11 @@ type Type =
|
|
|
59
59
|
|
|
60
60
|
declare class Entity {
|
|
61
61
|
private _name;
|
|
62
|
-
private
|
|
62
|
+
private _privateProps;
|
|
63
63
|
private _properties;
|
|
64
64
|
constructor(name: string, properties: Property[]);
|
|
65
65
|
get name(): string;
|
|
66
|
-
get
|
|
66
|
+
get privateProps(): string[];
|
|
67
67
|
get properties(): Property[];
|
|
68
68
|
set name(name: string);
|
|
69
69
|
getProp(key: string): Property | undefined;
|
|
@@ -80,9 +80,9 @@ declare class Property {
|
|
|
80
80
|
type: Type;
|
|
81
81
|
min: number | Date | null;
|
|
82
82
|
max: number | Date | null;
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
isPrivate: boolean;
|
|
84
|
+
requiredFor: Method[];
|
|
85
|
+
isTypeChecked: boolean;
|
|
86
86
|
sanitizer: ((v: any) => any) | null;
|
|
87
87
|
normalizer: ((v: any) => any) | null;
|
|
88
88
|
validator: ((v: any) => any) | null;
|
|
@@ -91,9 +91,9 @@ declare class Property {
|
|
|
91
91
|
type: Type,
|
|
92
92
|
min: number | Date | null,
|
|
93
93
|
max: number | Date | null,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
isPrivate: boolean,
|
|
95
|
+
requiredFor: Method[],
|
|
96
|
+
isTypeChecked: boolean,
|
|
97
97
|
sanitizer: ((v: any) => any) | null,
|
|
98
98
|
normalizer: ((v: any) => any) | null,
|
|
99
99
|
validator: ((v: any) => any) | null
|
package/dist/antity.js
CHANGED
|
@@ -135,7 +135,7 @@ const Types = {
|
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
class Property {
|
|
138
|
-
constructor(key, type, min, max,
|
|
138
|
+
constructor(key, type, min, max, isPrivate, requiredFor, isTypeChecked, sanitizer, normalizer, validator) {
|
|
139
139
|
try {
|
|
140
140
|
isString(key, "!0", null, true);
|
|
141
141
|
}
|
|
@@ -148,13 +148,13 @@ class Property {
|
|
|
148
148
|
catch (err) {
|
|
149
149
|
throw new Error(`${LOGS_PREFIX}Property "type" must be a valid type - caused by: ${err.message}`);
|
|
150
150
|
}
|
|
151
|
-
if (isArray(
|
|
152
|
-
for (const m of
|
|
151
|
+
if (isArray(requiredFor)) {
|
|
152
|
+
for (const m of requiredFor) {
|
|
153
153
|
try {
|
|
154
154
|
isIn(METHODS, m, 0, true);
|
|
155
155
|
}
|
|
156
156
|
catch (err) {
|
|
157
|
-
throw new Error(`${LOGS_PREFIX}Property "
|
|
157
|
+
throw new Error(`${LOGS_PREFIX}Property "requiredFor" must be an array of REST methods - caused by: ${err.message}`);
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
}
|
|
@@ -162,9 +162,9 @@ class Property {
|
|
|
162
162
|
this.type = type;
|
|
163
163
|
this.min = this.interval(min, type, 0, "1900-01-01T00:00:00Z");
|
|
164
164
|
this.max = this.interval(max, type, 999999999, "2200-12-31T00:00:00Z");
|
|
165
|
-
this.
|
|
166
|
-
this.
|
|
167
|
-
this.
|
|
165
|
+
this.requiredFor = isArray(requiredFor) ? requiredFor : [];
|
|
166
|
+
this.isPrivate = isBoolean(isPrivate) ? isPrivate : false;
|
|
167
|
+
this.isTypeChecked = isBoolean(isTypeChecked) ? isTypeChecked : false;
|
|
168
168
|
this.sanitizer = isFunction(sanitizer) ? sanitizer : null;
|
|
169
169
|
this.normalizer = isFunction(normalizer) ? normalizer : null;
|
|
170
170
|
this.validator = isFunction(validator) ? validator : null;
|
|
@@ -245,15 +245,15 @@ function require(v, key, type) {
|
|
|
245
245
|
}
|
|
246
246
|
|
|
247
247
|
function validate(record, properties, method) {
|
|
248
|
-
for (const { key, type, min, max,
|
|
248
|
+
for (const { key, type, min, max, requiredFor, isTypeChecked, validator } of properties) {
|
|
249
249
|
const v = record[key];
|
|
250
|
-
if (isIn(
|
|
250
|
+
if (isIn(requiredFor, method)) {
|
|
251
251
|
const rq = require(v, key, type);
|
|
252
252
|
if (rq)
|
|
253
253
|
return rq;
|
|
254
254
|
}
|
|
255
255
|
if (v) {
|
|
256
|
-
const ct = control(v, key, type, min, max,
|
|
256
|
+
const ct = control(v, key, type, min, max, isTypeChecked, validator);
|
|
257
257
|
if (ct)
|
|
258
258
|
return ct;
|
|
259
259
|
}
|
|
@@ -319,20 +319,20 @@ class Entity {
|
|
|
319
319
|
};
|
|
320
320
|
this._name = name;
|
|
321
321
|
this._properties = [];
|
|
322
|
-
this.
|
|
322
|
+
this._privateProps = [];
|
|
323
323
|
for (const p of properties) {
|
|
324
|
-
const prop = new Property(p.key, p.type, p.min, p.max, p.
|
|
324
|
+
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
325
|
Object.assign(prop, p);
|
|
326
326
|
this._properties.push(prop);
|
|
327
|
-
if (
|
|
328
|
-
this.
|
|
327
|
+
if (prop.isPrivate)
|
|
328
|
+
this._privateProps.push(prop.key);
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
331
|
get name() {
|
|
332
332
|
return this._name;
|
|
333
333
|
}
|
|
334
|
-
get
|
|
335
|
-
return this.
|
|
334
|
+
get privateProps() {
|
|
335
|
+
return this._privateProps;
|
|
336
336
|
}
|
|
337
337
|
get properties() {
|
|
338
338
|
return this._properties;
|
|
@@ -348,7 +348,7 @@ class Entity {
|
|
|
348
348
|
getPropsByMethod(method) {
|
|
349
349
|
const props = [];
|
|
350
350
|
for (const p of this.properties) {
|
|
351
|
-
if (isIn(p.
|
|
351
|
+
if (isIn(p.requiredFor, method, 0))
|
|
352
352
|
props.push(p);
|
|
353
353
|
}
|
|
354
354
|
return props;
|