xjs-common 7.0.0 → 8.0.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 +7 -7
- package/dist/func/decorator/d-type.js +2 -2
- package/dist/func/u-type.d.ts +6 -2
- package/dist/func/u-type.js +14 -9
- package/package.json +1 -1
package/README.md
CHANGED
@@ -181,28 +181,28 @@ class Cls_B {
|
|
181
181
|
console.log(!!cropped.id && !cropped.p && !!cropped.objA.aryB && !cropped.objA.q) // true;
|
182
182
|
|
183
183
|
// validation. below are valid cases.
|
184
|
-
console.log(UType.validate(valid1)); //
|
184
|
+
console.log(UType.validate(valid1)); // []
|
185
185
|
|
186
186
|
const valid2 = { id: 0 };
|
187
|
-
console.log(UType.validate(Object.assign(new Cls_A(), valid2))); //
|
187
|
+
console.log(UType.validate(Object.assign(new Cls_A(), valid2))); // []
|
188
188
|
|
189
189
|
// validation. below are invalid cases.
|
190
190
|
const invalid1 = {};
|
191
|
-
console.log(UType.validate(Object.assign(new Cls_A(), invalid1))); //
|
191
|
+
console.log(UType.validate(Object.assign(new Cls_A(), invalid1))); // [ 'id' ]
|
192
192
|
|
193
193
|
const invalid3 = { id: 0, strA: [], objA: valid_b1 };
|
194
|
-
console.log(UType.validate(Object.assign(new Cls_A(), invalid3))); //
|
194
|
+
console.log(UType.validate(Object.assign(new Cls_A(), invalid3))); // [ 'strA' ]
|
195
195
|
|
196
196
|
const invalid4 = { id: "0", strA: "a", objA: valid_b1 };
|
197
|
-
console.log(UType.validate(Object.assign(new Cls_A(), invalid4))); //
|
197
|
+
console.log(UType.validate(Object.assign(new Cls_A(), invalid4))); // [ 'id' ]
|
198
198
|
|
199
199
|
const invalid_b1 = Object.assign(new Cls_B(), { aryB: [1, 2, 3], boolB: 1 });
|
200
200
|
const invalid5 = { id: 0, strA: "a", objA: invalid_b1 };
|
201
|
-
console.log(UType.validate(Object.assign(new Cls_A(), invalid5))); //
|
201
|
+
console.log(UType.validate(Object.assign(new Cls_A(), invalid5))); // [ 'objA.boolB' ]
|
202
202
|
|
203
203
|
const invalid_b2 = Object.assign(new Cls_B(), { aryB: ["1"], boolB: true });
|
204
204
|
const invalid6 = { id: 0, strA: "a", objA: invalid_b2 };
|
205
|
-
console.log(UType.validate(Object.assign(new Cls_A(), invalid6))); //
|
205
|
+
console.log(UType.validate(Object.assign(new Cls_A(), invalid6))); // [ 'objA.aryB' ]
|
206
206
|
})();
|
207
207
|
```
|
208
208
|
## Error definition
|
@@ -47,7 +47,7 @@ var DType;
|
|
47
47
|
}
|
48
48
|
DType.keep = keep;
|
49
49
|
function setDesc(target, propKey, setter) {
|
50
|
-
const map = target[exports.smbl_tm]
|
50
|
+
const map = target[exports.smbl_tm] ? Object.assign({}, target[exports.smbl_tm]) : {};
|
51
51
|
map[propKey] ??= { t: null, req: false, rec: false, ary: null };
|
52
52
|
const td = map[propKey];
|
53
53
|
setter(td);
|
@@ -66,6 +66,6 @@ var DType;
|
|
66
66
|
}
|
67
67
|
if (ex1 && ex2)
|
68
68
|
throw new xjs_err_1.XjsErr(s_errCode, `decorator to express ${ex1} and ${ex2} are exclusive.`);
|
69
|
-
Object.defineProperty(target, exports.smbl_tm, { value: map });
|
69
|
+
Object.defineProperty(target, exports.smbl_tm, { value: map, configurable: true });
|
70
70
|
}
|
71
71
|
})(DType = exports.DType || (exports.DType = {}));
|
package/dist/func/u-type.d.ts
CHANGED
@@ -15,7 +15,11 @@ export declare namespace UType {
|
|
15
15
|
function isArray(v: any, t: Type.symbol): v is symbol[];
|
16
16
|
function isArray(v: any, t: Type.object): v is object[];
|
17
17
|
function isArray(v: any): v is any[];
|
18
|
-
/**
|
19
|
-
|
18
|
+
/**
|
19
|
+
* validate properties which attached decorators in {@link DType}.
|
20
|
+
* @param o object to be validated.
|
21
|
+
* @returns invalid property keys. returns an empty array if `o` is valid.
|
22
|
+
*/
|
23
|
+
function validate(o: any): string[];
|
20
24
|
function takeAsArray<T>(v: T | T[]): T[];
|
21
25
|
}
|
package/dist/func/u-type.js
CHANGED
@@ -29,22 +29,27 @@ var UType;
|
|
29
29
|
return Array.isArray(v) && (!t || v.every(e => typeof e === t));
|
30
30
|
}
|
31
31
|
UType.isArray = isArray;
|
32
|
-
/**
|
32
|
+
/**
|
33
|
+
* validate properties which attached decorators in {@link DType}.
|
34
|
+
* @param o object to be validated.
|
35
|
+
* @returns invalid property keys. returns an empty array if `o` is valid.
|
36
|
+
*/
|
33
37
|
function validate(o) {
|
34
|
-
|
35
|
-
|
38
|
+
if (!o[d_type_1.smbl_tm])
|
39
|
+
return [];
|
40
|
+
return Object.entries(o[d_type_1.smbl_tm]).flatMap(e => validateProp(e[0], o[e[0]], e[1]));
|
36
41
|
}
|
37
42
|
UType.validate = validate;
|
38
|
-
function validateProp(prop, td) {
|
43
|
+
function validateProp(k, prop, td) {
|
39
44
|
if (isEmpty(prop))
|
40
|
-
return
|
45
|
+
return td.req ? [k] : [];
|
41
46
|
if (td.t && typeof prop !== td.t)
|
42
|
-
return
|
47
|
+
return [k];
|
43
48
|
if (td.ary)
|
44
|
-
return Array.isArray(prop)
|
49
|
+
return Array.isArray(prop) ? prop.flatMap(e => validateProp(k, e, td.ary)) : [k];
|
45
50
|
if (td.rec)
|
46
|
-
return validate(prop);
|
47
|
-
return
|
51
|
+
return validate(prop).flatMap(k2 => `${k}.${k2}`);
|
52
|
+
return [];
|
48
53
|
}
|
49
54
|
function takeAsArray(v) {
|
50
55
|
return Array.isArray(v) ? v : [v];
|