@itzworking/decorated-class 0.0.158 → 0.0.159
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/dist/decorated-class.js +15 -11
- package/dist/decorators/attribute/attribute.js +15 -11
- package/dist/decorators/attribute/index.js +7 -4
- package/dist/decorators/attribute/is-attribute.js +7 -3
- package/dist/decorators/attribute/list-attributes.js +7 -3
- package/dist/decorators/attribute/symbol.js +4 -1
- package/dist/decorators/attribute/types/attribute-decorator.js +2 -1
- package/dist/decorators/attribute/types/attribute-definition.js +2 -1
- package/dist/decorators/attribute/types/attribute-type-alias.js +5 -2
- package/dist/decorators/attribute/types/attribute-type.js +6 -3
- package/dist/decorators/attribute/types/index.js +8 -5
- package/dist/decorators/attribute/types/primitive-type.js +5 -2
- package/dist/decorators/attribute/utils/index.js +4 -1
- package/dist/decorators/attribute/utils/update-attributes.js +10 -6
- package/dist/decorators/index.js +7 -4
- package/dist/decorators/validation/index.js +10 -7
- package/dist/decorators/validation/max.js +7 -4
- package/dist/decorators/validation/min.js +7 -4
- package/dist/decorators/validation/not-blank.js +7 -4
- package/dist/decorators/validation/not-null.js +5 -2
- package/dist/decorators/validation/pattern.js +7 -4
- package/dist/decorators/validation/types/index.js +5 -2
- package/dist/decorators/validation/types/validation-definition.js +2 -1
- package/dist/decorators/validation/types/validation-type.js +2 -1
- package/dist/decorators/validation/utils/index.js +4 -1
- package/dist/decorators/validation/utils/register-constraint.js +15 -10
- package/dist/errors/attributes-not-valid-error.js +5 -1
- package/dist/errors/index.js +6 -3
- package/dist/errors/invalid-target-error.js +5 -1
- package/dist/errors/unsupported-decorator-error.js +5 -1
- package/dist/index.js +8 -5
- package/dist/types.js +2 -1
- package/dist/validator/index.js +4 -1
- package/dist/validator/list-validation-errors.js +12 -8
- package/dist/validator/validate.js +9 -5
- package/dist/validator/validator.js +8 -5
- package/package.json +1 -1
package/dist/decorated-class.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DecoratedClass = void 0;
|
|
4
|
+
const decorators_1 = require("./decorators");
|
|
2
5
|
const parseBoolean = (value) => {
|
|
3
6
|
switch (value) {
|
|
4
7
|
case true:
|
|
@@ -20,27 +23,27 @@ const parseBoolean = (value) => {
|
|
|
20
23
|
}
|
|
21
24
|
};
|
|
22
25
|
const isPrimitiveType = (type) => {
|
|
23
|
-
return Object.values(PrimitiveType).includes(type);
|
|
26
|
+
return Object.values(decorators_1.PrimitiveType).includes(type);
|
|
24
27
|
};
|
|
25
28
|
const parseType = (value, type, nestedType) => {
|
|
26
|
-
if (type === AttributeType.Float) {
|
|
29
|
+
if (type === decorators_1.AttributeType.Float) {
|
|
27
30
|
return parseFloat(value);
|
|
28
31
|
}
|
|
29
|
-
if (type === AttributeType.Integer) {
|
|
32
|
+
if (type === decorators_1.AttributeType.Integer) {
|
|
30
33
|
return parseInt(value);
|
|
31
34
|
}
|
|
32
|
-
if (type === AttributeType.Boolean) {
|
|
35
|
+
if (type === decorators_1.AttributeType.Boolean) {
|
|
33
36
|
return parseBoolean(value);
|
|
34
37
|
}
|
|
35
|
-
if (type === AttributeType.Date) {
|
|
38
|
+
if (type === decorators_1.AttributeType.Date) {
|
|
36
39
|
return new Date(value);
|
|
37
40
|
}
|
|
38
|
-
if (type === AttributeType.Object &&
|
|
41
|
+
if (type === decorators_1.AttributeType.Object &&
|
|
39
42
|
nestedType &&
|
|
40
43
|
nestedType.prototype instanceof DecoratedClass) {
|
|
41
44
|
return new nestedType(value);
|
|
42
45
|
}
|
|
43
|
-
if (type === AttributeType.List) {
|
|
46
|
+
if (type === decorators_1.AttributeType.List) {
|
|
44
47
|
if (!value || !Array.isArray(value)) {
|
|
45
48
|
return undefined;
|
|
46
49
|
}
|
|
@@ -49,18 +52,18 @@ const parseType = (value, type, nestedType) => {
|
|
|
49
52
|
return value.map((item) => parseType(item, nestedType));
|
|
50
53
|
}
|
|
51
54
|
else if (nestedType.prototype instanceof DecoratedClass) {
|
|
52
|
-
return value.map((item) => parseType(item, AttributeType.Object, nestedType));
|
|
55
|
+
return value.map((item) => parseType(item, decorators_1.AttributeType.Object, nestedType));
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
return value;
|
|
57
60
|
};
|
|
58
|
-
|
|
61
|
+
class DecoratedClass {
|
|
59
62
|
constructor(props = {}) {
|
|
60
63
|
if (!props) {
|
|
61
64
|
return;
|
|
62
65
|
}
|
|
63
|
-
const attributes = listAttributes(this);
|
|
66
|
+
const attributes = (0, decorators_1.listAttributes)(this);
|
|
64
67
|
const self = this;
|
|
65
68
|
for (const attribute of attributes) {
|
|
66
69
|
const { attributeName: key, attributeType, nestedType } = attribute;
|
|
@@ -75,3 +78,4 @@ export class DecoratedClass {
|
|
|
75
78
|
}
|
|
76
79
|
}
|
|
77
80
|
}
|
|
81
|
+
exports.DecoratedClass = DecoratedClass;
|
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Attribute = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
const Attribute = (attributeType, options = {}) => {
|
|
4
7
|
let isValidType = () => true;
|
|
5
|
-
if (attributeType === AttributeType.String) {
|
|
8
|
+
if (attributeType === types_1.AttributeType.String) {
|
|
6
9
|
isValidType = (value) => value === undefined || value === null || typeof value === "string";
|
|
7
10
|
}
|
|
8
|
-
else if (attributeType === AttributeType.Integer) {
|
|
11
|
+
else if (attributeType === types_1.AttributeType.Integer) {
|
|
9
12
|
isValidType = (value) => value === undefined ||
|
|
10
13
|
value === null ||
|
|
11
14
|
(Number(value) === value && value % 1 === 0);
|
|
12
15
|
}
|
|
13
|
-
else if (attributeType === AttributeType.Float) {
|
|
16
|
+
else if (attributeType === types_1.AttributeType.Float) {
|
|
14
17
|
isValidType = (value) => value === undefined || value === null || Number(value) === value;
|
|
15
18
|
}
|
|
16
|
-
else if (attributeType === AttributeType.Boolean) {
|
|
19
|
+
else if (attributeType === types_1.AttributeType.Boolean) {
|
|
17
20
|
isValidType = (value) => value === undefined || value === null || typeof value === "boolean";
|
|
18
21
|
}
|
|
19
|
-
else if (attributeType === AttributeType.Date) {
|
|
22
|
+
else if (attributeType === types_1.AttributeType.Date) {
|
|
20
23
|
isValidType = (value) => value === undefined || value === null || value instanceof Date;
|
|
21
24
|
}
|
|
22
|
-
else if (attributeType === AttributeType.List) {
|
|
25
|
+
else if (attributeType === types_1.AttributeType.List) {
|
|
23
26
|
isValidType = (value) => value === undefined || value === null || Array.isArray(value);
|
|
24
27
|
}
|
|
25
|
-
else if (attributeType === AttributeType.Object) {
|
|
28
|
+
else if (attributeType === types_1.AttributeType.Object) {
|
|
26
29
|
isValidType = (value) => value === undefined || value === null || typeof value === "object";
|
|
27
30
|
}
|
|
28
|
-
return updateAttributes(attributeType, isValidType, options);
|
|
31
|
+
return (0, utils_1.updateAttributes)(attributeType, isValidType, options);
|
|
29
32
|
};
|
|
33
|
+
exports.Attribute = Attribute;
|
|
30
34
|
// export const OldAttribute = {
|
|
31
35
|
// List: (type: PrimitiveType | any) =>
|
|
32
36
|
// updateAttributes(
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./types"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./attribute"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./is-attribute"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./list-attributes"), exports);
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isAttribute = void 0;
|
|
4
|
+
const symbol_1 = require("./symbol");
|
|
5
|
+
const isAttribute = (target, propertyKey) => {
|
|
6
|
+
return !!Reflect.getMetadata(symbol_1.symbol, target, propertyKey);
|
|
4
7
|
};
|
|
8
|
+
exports.isAttribute = isAttribute;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listAttributes = void 0;
|
|
4
|
+
const symbol_1 = require("./symbol");
|
|
5
|
+
const listAttributes = (target) => {
|
|
3
6
|
let attributes = [];
|
|
4
7
|
let targetToUse = Object.getPrototypeOf(target);
|
|
5
8
|
while (targetToUse && targetToUse !== Object.prototype) {
|
|
6
|
-
const parentAttributes = Reflect.getOwnMetadata(symbol, targetToUse) || [];
|
|
9
|
+
const parentAttributes = Reflect.getOwnMetadata(symbol_1.symbol, targetToUse) || [];
|
|
7
10
|
attributes = [...parentAttributes, ...attributes];
|
|
8
11
|
targetToUse = Object.getPrototypeOf(targetToUse);
|
|
9
12
|
}
|
|
10
13
|
return attributes;
|
|
11
14
|
};
|
|
15
|
+
exports.listAttributes = listAttributes;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AT = void 0;
|
|
4
|
+
const attribute_type_1 = require("./attribute-type");
|
|
5
|
+
Object.defineProperty(exports, "AT", { enumerable: true, get: function () { return attribute_type_1.AttributeType; } });
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AttributeType = void 0;
|
|
4
|
+
const primitive_type_1 = require("./primitive-type");
|
|
5
|
+
var AttributeType;
|
|
3
6
|
(function (AttributeType) {
|
|
4
7
|
AttributeType["String"] = "String";
|
|
5
8
|
AttributeType["Float"] = "Float";
|
|
@@ -8,4 +11,4 @@ export var AttributeType;
|
|
|
8
11
|
AttributeType["Date"] = "Date";
|
|
9
12
|
AttributeType["List"] = "List";
|
|
10
13
|
AttributeType["Object"] = "Object";
|
|
11
|
-
})(AttributeType || (AttributeType = {}));
|
|
14
|
+
})(AttributeType || (exports.AttributeType = AttributeType = {}));
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./attribute-decorator"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./attribute-definition"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./attribute-type"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./attribute-type-alias"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./primitive-type"), exports);
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrimitiveType = void 0;
|
|
4
|
+
var PrimitiveType;
|
|
2
5
|
(function (PrimitiveType) {
|
|
3
6
|
PrimitiveType["String"] = "String";
|
|
4
7
|
PrimitiveType["Integer"] = "Integer";
|
|
5
8
|
PrimitiveType["Float"] = "Float";
|
|
6
9
|
PrimitiveType["Boolean"] = "Boolean";
|
|
7
10
|
PrimitiveType["Date"] = "Date";
|
|
8
|
-
})(PrimitiveType || (PrimitiveType = {}));
|
|
11
|
+
})(PrimitiveType || (exports.PrimitiveType = PrimitiveType = {}));
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.updateAttributes = void 0;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
const symbol_1 = require("../symbol");
|
|
6
|
+
const updateAttributes = (attributeType, isValidType, options = {}) => (target, context) => {
|
|
7
|
+
const attributes = Reflect.getOwnMetadata(symbol_1.symbol, target) || [];
|
|
5
8
|
const attributeName = typeof context === "string" ? context : context.name;
|
|
6
9
|
let currentAttribute = attributes.find((attribute) => attribute.attributeName === attributeName);
|
|
7
10
|
const isValidTypeInTarget = (target) => isValidType(typeof target[attributeName] === "function"
|
|
@@ -24,6 +27,7 @@ export const updateAttributes = (attributeType, isValidType, options = {}) => (t
|
|
|
24
27
|
currentAttribute.nestedType = options?.nestedType;
|
|
25
28
|
currentAttribute.isValidTypeInTarget = isValidTypeInTarget;
|
|
26
29
|
}
|
|
27
|
-
Reflect.defineMetadata(symbol, attributes, target);
|
|
28
|
-
Reflect.defineMetadata(symbol, currentAttribute, target, attributeName);
|
|
30
|
+
Reflect.defineMetadata(symbol_1.symbol, attributes, target);
|
|
31
|
+
Reflect.defineMetadata(symbol_1.symbol, currentAttribute, target, attributeName);
|
|
29
32
|
};
|
|
33
|
+
exports.updateAttributes = updateAttributes;
|
package/dist/decorators/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
tslib_1.__exportStar(require("./attribute"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./attribute/types/primitive-type"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./validation"), exports);
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./types"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./utils"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./max"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./min"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./not-blank"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./not-null"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./pattern"), exports);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Max = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const errors_1 = require("../../errors");
|
|
6
|
+
exports.Max = (0, utils_1.registerConstraintWithProps)("Max", (max) => (value) => {
|
|
4
7
|
if (value === null || value === undefined) {
|
|
5
8
|
return true;
|
|
6
9
|
}
|
|
@@ -13,5 +16,5 @@ export const Max = registerConstraintWithProps("Max", (max) => (value) => {
|
|
|
13
16
|
if (Array.isArray(value)) {
|
|
14
17
|
return value.length <= max;
|
|
15
18
|
}
|
|
16
|
-
throw new UnsupportedDecoratorError();
|
|
19
|
+
throw new errors_1.UnsupportedDecoratorError();
|
|
17
20
|
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Min = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const errors_1 = require("../../errors");
|
|
6
|
+
exports.Min = (0, utils_1.registerConstraintWithProps)("Min", (min) => (value) => {
|
|
4
7
|
if (value === null || value === undefined) {
|
|
5
8
|
return true;
|
|
6
9
|
}
|
|
@@ -13,5 +16,5 @@ export const Min = registerConstraintWithProps("Min", (min) => (value) => {
|
|
|
13
16
|
if (Array.isArray(value)) {
|
|
14
17
|
return value.length >= min;
|
|
15
18
|
}
|
|
16
|
-
throw new UnsupportedDecoratorError();
|
|
19
|
+
throw new errors_1.UnsupportedDecoratorError();
|
|
17
20
|
});
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotBlank = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const errors_1 = require("../../errors");
|
|
6
|
+
exports.NotBlank = (0, utils_1.registerConstraintWithoutProps)("NotBlank", (value) => {
|
|
4
7
|
if (value === null || value === undefined) {
|
|
5
8
|
return true;
|
|
6
9
|
}
|
|
7
10
|
if (typeof value !== "string") {
|
|
8
|
-
throw new UnsupportedDecoratorError();
|
|
11
|
+
throw new errors_1.UnsupportedDecoratorError();
|
|
9
12
|
}
|
|
10
13
|
return value.trim().length > 0;
|
|
11
14
|
});
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotNull = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
exports.NotNull = (0, utils_1.registerConstraintWithoutProps)("NotNull", (value) => value !== null && value !== undefined);
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Pattern = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const errors_1 = require("../../errors");
|
|
6
|
+
exports.Pattern = (0, utils_1.registerConstraintWithProps)("Pattern", (pattern) => (value) => {
|
|
4
7
|
if (value === null || value === undefined) {
|
|
5
8
|
return true;
|
|
6
9
|
}
|
|
7
10
|
if (typeof value !== "string") {
|
|
8
|
-
throw new UnsupportedDecoratorError();
|
|
11
|
+
throw new errors_1.UnsupportedDecoratorError();
|
|
9
12
|
}
|
|
10
13
|
const r = value.match(pattern);
|
|
11
14
|
return !!r && r.length > 0;
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./validation-definition"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./validation-type"), exports);
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerConstraint = exports.registerConstraintWithoutProps = exports.registerConstraintWithProps = void 0;
|
|
4
|
+
const symbol_1 = require("../../attribute/symbol");
|
|
5
|
+
const errors_1 = require("../../../errors");
|
|
3
6
|
/**
|
|
4
7
|
* Registers a constraint validator with properties.
|
|
5
8
|
*
|
|
@@ -28,9 +31,9 @@ import { UnsupportedDecoratorError } from "../../../errors";
|
|
|
28
31
|
* }
|
|
29
32
|
* ```
|
|
30
33
|
*/
|
|
31
|
-
|
|
34
|
+
const registerConstraintWithProps = (constraintId, constraintValidator) => {
|
|
32
35
|
return (props) => (target, context) => {
|
|
33
|
-
const attributes = Reflect.getOwnMetadata(symbol, target) || [];
|
|
36
|
+
const attributes = Reflect.getOwnMetadata(symbol_1.symbol, target) || [];
|
|
34
37
|
const attributeName = typeof context === "string" ? context : context.name;
|
|
35
38
|
let currentAttribute = attributes.find((attribute) => attribute.attributeName === attributeName);
|
|
36
39
|
const constraint = {
|
|
@@ -42,7 +45,7 @@ export const registerConstraintWithProps = (constraintId, constraintValidator) =
|
|
|
42
45
|
: target[attributeName], target);
|
|
43
46
|
}
|
|
44
47
|
catch (e) {
|
|
45
|
-
if (e instanceof UnsupportedDecoratorError) {
|
|
48
|
+
if (e instanceof errors_1.UnsupportedDecoratorError) {
|
|
46
49
|
e.setErrorProps(constraintId, `${target.constructor.name}.${attributeName}`, typeof target[attributeName]);
|
|
47
50
|
}
|
|
48
51
|
throw e;
|
|
@@ -61,10 +64,11 @@ export const registerConstraintWithProps = (constraintId, constraintValidator) =
|
|
|
61
64
|
else {
|
|
62
65
|
currentAttribute.constraints.push(constraint);
|
|
63
66
|
}
|
|
64
|
-
Reflect.defineMetadata(symbol, attributes, target);
|
|
65
|
-
Reflect.defineMetadata(symbol, currentAttribute, target, attributeName);
|
|
67
|
+
Reflect.defineMetadata(symbol_1.symbol, attributes, target);
|
|
68
|
+
Reflect.defineMetadata(symbol_1.symbol, currentAttribute, target, attributeName);
|
|
66
69
|
};
|
|
67
70
|
};
|
|
71
|
+
exports.registerConstraintWithProps = registerConstraintWithProps;
|
|
68
72
|
/**
|
|
69
73
|
* Registers a constraint validator without properties.
|
|
70
74
|
*
|
|
@@ -93,7 +97,8 @@ export const registerConstraintWithProps = (constraintId, constraintValidator) =
|
|
|
93
97
|
* }
|
|
94
98
|
* ```
|
|
95
99
|
*/
|
|
96
|
-
|
|
97
|
-
return registerConstraintWithProps(constraintId, () => constraintValidator)();
|
|
100
|
+
const registerConstraintWithoutProps = (constraintId, constraintValidator) => {
|
|
101
|
+
return (0, exports.registerConstraintWithProps)(constraintId, () => constraintValidator)();
|
|
98
102
|
};
|
|
99
|
-
|
|
103
|
+
exports.registerConstraintWithoutProps = registerConstraintWithoutProps;
|
|
104
|
+
exports.registerConstraint = exports.registerConstraintWithoutProps;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AttributesNotValidError = void 0;
|
|
4
|
+
class AttributesNotValidError extends Error {
|
|
2
5
|
validations;
|
|
3
6
|
constructor(validations) {
|
|
4
7
|
super(validations.map((error) => error.message).join(", "));
|
|
@@ -7,3 +10,4 @@ export class AttributesNotValidError extends Error {
|
|
|
7
10
|
Object.setPrototypeOf(this, AttributesNotValidError.prototype);
|
|
8
11
|
}
|
|
9
12
|
}
|
|
13
|
+
exports.AttributesNotValidError = AttributesNotValidError;
|
package/dist/errors/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./attributes-not-valid-error"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./invalid-target-error"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./unsupported-decorator-error"), exports);
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InvalidTargetError = void 0;
|
|
4
|
+
class InvalidTargetError extends Error {
|
|
2
5
|
constructor(message) {
|
|
3
6
|
super(message);
|
|
4
7
|
this.name = "InvalidTargetError";
|
|
5
8
|
}
|
|
6
9
|
}
|
|
10
|
+
exports.InvalidTargetError = InvalidTargetError;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnsupportedDecoratorError = void 0;
|
|
4
|
+
class UnsupportedDecoratorError extends Error {
|
|
2
5
|
decorator;
|
|
3
6
|
attributePath;
|
|
4
7
|
attributeType;
|
|
@@ -13,3 +16,4 @@ export class UnsupportedDecoratorError extends Error {
|
|
|
13
16
|
this.message = `@${decorator} is not supported for ${attributePath}`;
|
|
14
17
|
}
|
|
15
18
|
}
|
|
19
|
+
exports.UnsupportedDecoratorError = UnsupportedDecoratorError;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./types"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./decorators"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./errors"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./validator"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./decorated-class"), exports);
|
package/dist/types.js
CHANGED
package/dist/validator/index.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listValidationErrors = void 0;
|
|
4
|
+
const attribute_1 = require("../decorators/attribute");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
const decorated_class_1 = require("../decorated-class");
|
|
7
|
+
const listValidationErrors = (target) => {
|
|
5
8
|
if (!target) {
|
|
6
|
-
throw new InvalidTargetError("Target is not defined");
|
|
9
|
+
throw new errors_1.InvalidTargetError("Target is not defined");
|
|
7
10
|
}
|
|
8
|
-
const attributes = listAttributes(target);
|
|
11
|
+
const attributes = (0, attribute_1.listAttributes)(target);
|
|
9
12
|
const validations = [];
|
|
10
13
|
for (const attribute of attributes) {
|
|
11
14
|
if (!attribute.attributeType) {
|
|
@@ -31,9 +34,9 @@ export const listValidationErrors = (target) => {
|
|
|
31
34
|
}
|
|
32
35
|
if (attribute.attributeType === "Object" &&
|
|
33
36
|
attribute.nestedType &&
|
|
34
|
-
attribute.nestedType.prototype instanceof DecoratedClass &&
|
|
37
|
+
attribute.nestedType.prototype instanceof decorated_class_1.DecoratedClass &&
|
|
35
38
|
target[attribute.attributeName]) {
|
|
36
|
-
validations.push(...listValidationErrors(target[attribute.attributeName]));
|
|
39
|
+
validations.push(...(0, exports.listValidationErrors)(target[attribute.attributeName]));
|
|
37
40
|
}
|
|
38
41
|
for (const validation of attribute.constraints) {
|
|
39
42
|
if (!validation.isValid(target)) {
|
|
@@ -48,3 +51,4 @@ export const listValidationErrors = (target) => {
|
|
|
48
51
|
}
|
|
49
52
|
return validations;
|
|
50
53
|
};
|
|
54
|
+
exports.listValidationErrors = listValidationErrors;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validate = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const list_validation_errors_1 = require("./list-validation-errors");
|
|
6
|
+
const validate = (target) => {
|
|
7
|
+
const validations = (0, list_validation_errors_1.listValidationErrors)(target);
|
|
5
8
|
if (validations.length) {
|
|
6
|
-
throw new AttributesNotValidError(validations);
|
|
9
|
+
throw new errors_1.AttributesNotValidError(validations);
|
|
7
10
|
}
|
|
8
11
|
};
|
|
12
|
+
exports.validate = validate;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validator = void 0;
|
|
4
|
+
const validate_1 = require("./validate");
|
|
5
|
+
const list_validation_errors_1 = require("./list-validation-errors");
|
|
6
|
+
exports.validator = {
|
|
7
|
+
validate: (target) => (0, validate_1.validate)(target),
|
|
8
|
+
listValidationErrors: (target) => (0, list_validation_errors_1.listValidationErrors)(target),
|
|
6
9
|
};
|