@decaf-ts/decorator-validation 1.4.21 → 1.5.2
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/LICENSE.md +0 -0
- package/README.md +0 -0
- package/package.json +15 -9
- package/dist/decorator-validation.bundle.min.js +0 -2
- package/dist/decorator-validation.bundle.min.js.LICENSE.txt +0 -14
- package/dist/esm/decorator-validation.bundle.min.esm.js +0 -2
- package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +0 -14
- package/lib/esm/index.d.ts +0 -42
- package/lib/esm/index.js +0 -42
- package/lib/esm/model/Model.d.ts +0 -139
- package/lib/esm/model/Model.js +0 -297
- package/lib/esm/model/ModelErrorDefinition.d.ts +0 -22
- package/lib/esm/model/ModelErrorDefinition.js +0 -52
- package/lib/esm/model/Registry.d.ts +0 -59
- package/lib/esm/model/Registry.js +0 -73
- package/lib/esm/model/constants.d.ts +0 -56
- package/lib/esm/model/constants.js +0 -69
- package/lib/esm/model/construction.d.ts +0 -29
- package/lib/esm/model/construction.js +0 -63
- package/lib/esm/model/decorators.d.ts +0 -25
- package/lib/esm/model/decorators.js +0 -62
- package/lib/esm/model/index.d.ts +0 -9
- package/lib/esm/model/index.js +0 -9
- package/lib/esm/model/types.d.ts +0 -79
- package/lib/esm/model/types.js +0 -1
- package/lib/esm/model/utils.d.ts +0 -11
- package/lib/esm/model/utils.js +0 -25
- package/lib/esm/model/validation.d.ts +0 -14
- package/lib/esm/model/validation.js +0 -134
- package/lib/esm/utils/constants.d.ts +0 -26
- package/lib/esm/utils/constants.js +0 -27
- package/lib/esm/utils/dates.d.ts +0 -76
- package/lib/esm/utils/dates.js +0 -243
- package/lib/esm/utils/decorators.d.ts +0 -2
- package/lib/esm/utils/decorators.js +0 -18
- package/lib/esm/utils/hashing.d.ts +0 -52
- package/lib/esm/utils/hashing.js +0 -99
- package/lib/esm/utils/index.d.ts +0 -7
- package/lib/esm/utils/index.js +0 -7
- package/lib/esm/utils/registry.d.ts +0 -68
- package/lib/esm/utils/registry.js +0 -1
- package/lib/esm/utils/serialization.d.ts +0 -79
- package/lib/esm/utils/serialization.js +0 -88
- package/lib/esm/utils/strings.d.ts +0 -25
- package/lib/esm/utils/strings.js +0 -31
- package/lib/esm/validation/Validation.d.ts +0 -51
- package/lib/esm/validation/Validation.js +0 -71
- package/lib/esm/validation/Validators/DateValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/DateValidator.js +0 -53
- package/lib/esm/validation/Validators/EmailValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/EmailValidator.js +0 -48
- package/lib/esm/validation/Validators/ListValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/ListValidator.js +0 -66
- package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +0 -29
- package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -53
- package/lib/esm/validation/Validators/MaxValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/MaxValidator.js +0 -57
- package/lib/esm/validation/Validators/MinLengthValidator.d.ts +0 -29
- package/lib/esm/validation/Validators/MinLengthValidator.js +0 -53
- package/lib/esm/validation/Validators/MinValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/MinValidator.js +0 -57
- package/lib/esm/validation/Validators/PasswordValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/PasswordValidator.js +0 -48
- package/lib/esm/validation/Validators/PatternValidator.d.ts +0 -37
- package/lib/esm/validation/Validators/PatternValidator.js +0 -70
- package/lib/esm/validation/Validators/RequiredValidator.d.ts +0 -27
- package/lib/esm/validation/Validators/RequiredValidator.js +0 -55
- package/lib/esm/validation/Validators/StepValidator.d.ts +0 -28
- package/lib/esm/validation/Validators/StepValidator.js +0 -52
- package/lib/esm/validation/Validators/TypeValidator.d.ts +0 -27
- package/lib/esm/validation/Validators/TypeValidator.js +0 -60
- package/lib/esm/validation/Validators/URLValidator.d.ts +0 -26
- package/lib/esm/validation/Validators/URLValidator.js +0 -46
- package/lib/esm/validation/Validators/Validator.d.ts +0 -40
- package/lib/esm/validation/Validators/Validator.js +0 -47
- package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +0 -47
- package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -80
- package/lib/esm/validation/Validators/constants.d.ts +0 -96
- package/lib/esm/validation/Validators/constants.js +0 -134
- package/lib/esm/validation/Validators/decorators.d.ts +0 -12
- package/lib/esm/validation/Validators/decorators.js +0 -25
- package/lib/esm/validation/Validators/index.d.ts +0 -52
- package/lib/esm/validation/Validators/index.js +0 -52
- package/lib/esm/validation/Validators/types.d.ts +0 -51
- package/lib/esm/validation/Validators/types.js +0 -1
- package/lib/esm/validation/decorators.d.ts +0 -178
- package/lib/esm/validation/decorators.js +0 -288
- package/lib/esm/validation/index.d.ts +0 -4
- package/lib/esm/validation/index.js +0 -4
- package/lib/esm/validation/types.d.ts +0 -46
- package/lib/esm/validation/types.js +0 -1
- package/lib/index.cjs +0 -59
- package/lib/index.d.ts +0 -42
- package/lib/model/Model.cjs +0 -301
- package/lib/model/Model.d.ts +0 -139
- package/lib/model/ModelErrorDefinition.cjs +0 -56
- package/lib/model/ModelErrorDefinition.d.ts +0 -22
- package/lib/model/Registry.cjs +0 -78
- package/lib/model/Registry.d.ts +0 -59
- package/lib/model/constants.cjs +0 -72
- package/lib/model/constants.d.ts +0 -56
- package/lib/model/construction.cjs +0 -68
- package/lib/model/construction.d.ts +0 -29
- package/lib/model/decorators.cjs +0 -67
- package/lib/model/decorators.d.ts +0 -25
- package/lib/model/index.cjs +0 -25
- package/lib/model/index.d.ts +0 -9
- package/lib/model/types.cjs +0 -2
- package/lib/model/types.d.ts +0 -79
- package/lib/model/utils.cjs +0 -29
- package/lib/model/utils.d.ts +0 -11
- package/lib/model/validation.cjs +0 -137
- package/lib/model/validation.d.ts +0 -14
- package/lib/utils/constants.cjs +0 -30
- package/lib/utils/constants.d.ts +0 -26
- package/lib/utils/dates.cjs +0 -251
- package/lib/utils/dates.d.ts +0 -76
- package/lib/utils/decorators.cjs +0 -22
- package/lib/utils/decorators.d.ts +0 -2
- package/lib/utils/hashing.cjs +0 -106
- package/lib/utils/hashing.d.ts +0 -52
- package/lib/utils/index.cjs +0 -23
- package/lib/utils/index.d.ts +0 -7
- package/lib/utils/registry.cjs +0 -2
- package/lib/utils/registry.d.ts +0 -68
- package/lib/utils/serialization.cjs +0 -93
- package/lib/utils/serialization.d.ts +0 -79
- package/lib/utils/strings.cjs +0 -35
- package/lib/utils/strings.d.ts +0 -25
- package/lib/validation/Validation.cjs +0 -75
- package/lib/validation/Validation.d.ts +0 -51
- package/lib/validation/Validators/DateValidator.cjs +0 -56
- package/lib/validation/Validators/DateValidator.d.ts +0 -28
- package/lib/validation/Validators/EmailValidator.cjs +0 -51
- package/lib/validation/Validators/EmailValidator.d.ts +0 -28
- package/lib/validation/Validators/ListValidator.cjs +0 -69
- package/lib/validation/Validators/ListValidator.d.ts +0 -28
- package/lib/validation/Validators/MaxLengthValidator.cjs +0 -56
- package/lib/validation/Validators/MaxLengthValidator.d.ts +0 -29
- package/lib/validation/Validators/MaxValidator.cjs +0 -60
- package/lib/validation/Validators/MaxValidator.d.ts +0 -28
- package/lib/validation/Validators/MinLengthValidator.cjs +0 -56
- package/lib/validation/Validators/MinLengthValidator.d.ts +0 -29
- package/lib/validation/Validators/MinValidator.cjs +0 -60
- package/lib/validation/Validators/MinValidator.d.ts +0 -28
- package/lib/validation/Validators/PasswordValidator.cjs +0 -51
- package/lib/validation/Validators/PasswordValidator.d.ts +0 -28
- package/lib/validation/Validators/PatternValidator.cjs +0 -73
- package/lib/validation/Validators/PatternValidator.d.ts +0 -37
- package/lib/validation/Validators/RequiredValidator.cjs +0 -58
- package/lib/validation/Validators/RequiredValidator.d.ts +0 -27
- package/lib/validation/Validators/StepValidator.cjs +0 -55
- package/lib/validation/Validators/StepValidator.d.ts +0 -28
- package/lib/validation/Validators/TypeValidator.cjs +0 -63
- package/lib/validation/Validators/TypeValidator.d.ts +0 -27
- package/lib/validation/Validators/URLValidator.cjs +0 -49
- package/lib/validation/Validators/URLValidator.d.ts +0 -26
- package/lib/validation/Validators/Validator.cjs +0 -51
- package/lib/validation/Validators/Validator.d.ts +0 -40
- package/lib/validation/Validators/ValidatorRegistry.cjs +0 -85
- package/lib/validation/Validators/ValidatorRegistry.d.ts +0 -47
- package/lib/validation/Validators/constants.cjs +0 -137
- package/lib/validation/Validators/constants.d.ts +0 -96
- package/lib/validation/Validators/decorators.cjs +0 -28
- package/lib/validation/Validators/decorators.d.ts +0 -12
- package/lib/validation/Validators/index.cjs +0 -69
- package/lib/validation/Validators/types.cjs +0 -2
- package/lib/validation/decorators.cjs +0 -304
- package/lib/validation/decorators.d.ts +0 -178
- package/lib/validation/index.cjs +0 -20
- package/lib/validation/index.d.ts +0 -4
- package/lib/validation/types.cjs +0 -2
- package/lib/validation/types.d.ts +0 -46
package/lib/model/constants.cjs
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jsTypes = exports.ReservedModels = exports.Primitives = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* @summary References the relevant JS primitives
|
|
6
|
-
*
|
|
7
|
-
* @property {string} STRING references the string primitive
|
|
8
|
-
* @property {string} NUMBER references the number primitive
|
|
9
|
-
* @property {string} BOOLEAN references the boolean primitive
|
|
10
|
-
* @property {string} BIGINT references the bigint primitive
|
|
11
|
-
*
|
|
12
|
-
* @constant Primitives
|
|
13
|
-
* @memberOf module:decorator-validation.Model
|
|
14
|
-
*/
|
|
15
|
-
var Primitives;
|
|
16
|
-
(function (Primitives) {
|
|
17
|
-
Primitives["STRING"] = "string";
|
|
18
|
-
Primitives["NUMBER"] = "number";
|
|
19
|
-
Primitives["BOOLEAN"] = "boolean";
|
|
20
|
-
Primitives["BIGINT"] = "bigint";
|
|
21
|
-
})(Primitives || (exports.Primitives = Primitives = {}));
|
|
22
|
-
/**
|
|
23
|
-
* @summary References the Reserved model names to ignore during Model rebuilding
|
|
24
|
-
*
|
|
25
|
-
* @property {string} STRING
|
|
26
|
-
* @property {string} OBJECT
|
|
27
|
-
* @property {string} NUMBER
|
|
28
|
-
* @property {string} BOOLEAN
|
|
29
|
-
* @property {string} BIGINT
|
|
30
|
-
* @property {string} DATE
|
|
31
|
-
*
|
|
32
|
-
* @constant ReservedModels
|
|
33
|
-
* @memberOf module:decorator-validation.Model
|
|
34
|
-
*/
|
|
35
|
-
var ReservedModels;
|
|
36
|
-
(function (ReservedModels) {
|
|
37
|
-
ReservedModels["STRING"] = "string";
|
|
38
|
-
ReservedModels["OBJECT"] = "object";
|
|
39
|
-
ReservedModels["NUMBER"] = "number";
|
|
40
|
-
ReservedModels["BOOLEAN"] = "boolean";
|
|
41
|
-
ReservedModels["BIGINT"] = "bigint";
|
|
42
|
-
ReservedModels["DATE"] = "date";
|
|
43
|
-
})(ReservedModels || (exports.ReservedModels = ReservedModels = {}));
|
|
44
|
-
/**
|
|
45
|
-
* @summary References the basic supported js types
|
|
46
|
-
*
|
|
47
|
-
* @property {string} string
|
|
48
|
-
* @property {string} array
|
|
49
|
-
* @property {string} number
|
|
50
|
-
* @property {string} boolean
|
|
51
|
-
* @property {string} symbol
|
|
52
|
-
* @property {string} function
|
|
53
|
-
* @property {string} object
|
|
54
|
-
* @property {string} undefined
|
|
55
|
-
* @property {string} null
|
|
56
|
-
* @property {string} BIGINT
|
|
57
|
-
*
|
|
58
|
-
* @constant jsTypes
|
|
59
|
-
* @memberOf module:decorator-validation.Model
|
|
60
|
-
*/
|
|
61
|
-
exports.jsTypes = [
|
|
62
|
-
"string",
|
|
63
|
-
"array",
|
|
64
|
-
"number",
|
|
65
|
-
"boolean",
|
|
66
|
-
"symbol",
|
|
67
|
-
"function",
|
|
68
|
-
"object",
|
|
69
|
-
"undefined",
|
|
70
|
-
"null",
|
|
71
|
-
"bigint",
|
|
72
|
-
];
|
package/lib/model/constants.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @summary References the relevant JS primitives
|
|
3
|
-
*
|
|
4
|
-
* @property {string} STRING references the string primitive
|
|
5
|
-
* @property {string} NUMBER references the number primitive
|
|
6
|
-
* @property {string} BOOLEAN references the boolean primitive
|
|
7
|
-
* @property {string} BIGINT references the bigint primitive
|
|
8
|
-
*
|
|
9
|
-
* @constant Primitives
|
|
10
|
-
* @memberOf module:decorator-validation.Model
|
|
11
|
-
*/
|
|
12
|
-
export declare enum Primitives {
|
|
13
|
-
STRING = "string",
|
|
14
|
-
NUMBER = "number",
|
|
15
|
-
BOOLEAN = "boolean",
|
|
16
|
-
BIGINT = "bigint"
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* @summary References the Reserved model names to ignore during Model rebuilding
|
|
20
|
-
*
|
|
21
|
-
* @property {string} STRING
|
|
22
|
-
* @property {string} OBJECT
|
|
23
|
-
* @property {string} NUMBER
|
|
24
|
-
* @property {string} BOOLEAN
|
|
25
|
-
* @property {string} BIGINT
|
|
26
|
-
* @property {string} DATE
|
|
27
|
-
*
|
|
28
|
-
* @constant ReservedModels
|
|
29
|
-
* @memberOf module:decorator-validation.Model
|
|
30
|
-
*/
|
|
31
|
-
export declare enum ReservedModels {
|
|
32
|
-
STRING = "string",
|
|
33
|
-
OBJECT = "object",
|
|
34
|
-
NUMBER = "number",
|
|
35
|
-
BOOLEAN = "boolean",
|
|
36
|
-
BIGINT = "bigint",
|
|
37
|
-
DATE = "date"
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* @summary References the basic supported js types
|
|
41
|
-
*
|
|
42
|
-
* @property {string} string
|
|
43
|
-
* @property {string} array
|
|
44
|
-
* @property {string} number
|
|
45
|
-
* @property {string} boolean
|
|
46
|
-
* @property {string} symbol
|
|
47
|
-
* @property {string} function
|
|
48
|
-
* @property {string} object
|
|
49
|
-
* @property {string} undefined
|
|
50
|
-
* @property {string} null
|
|
51
|
-
* @property {string} BIGINT
|
|
52
|
-
*
|
|
53
|
-
* @constant jsTypes
|
|
54
|
-
* @memberOf module:decorator-validation.Model
|
|
55
|
-
*/
|
|
56
|
-
export declare const jsTypes: string[];
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.construct = construct;
|
|
4
|
-
exports.findLastProtoBeforeObject = findLastProtoBeforeObject;
|
|
5
|
-
exports.bindModelPrototype = bindModelPrototype;
|
|
6
|
-
const Model_1 = require("./Model.cjs");
|
|
7
|
-
/**
|
|
8
|
-
* @summary Helper Function to override constructors
|
|
9
|
-
*
|
|
10
|
-
* @param {Function} constructor
|
|
11
|
-
* @param {any[]} [args]
|
|
12
|
-
* @return {T} the new instance
|
|
13
|
-
*
|
|
14
|
-
* @function construct
|
|
15
|
-
* @memberOf module:decorator-validation.Construction
|
|
16
|
-
*/
|
|
17
|
-
function construct(constructor, ...args) {
|
|
18
|
-
const _constr = (...argz) => new constructor(...argz);
|
|
19
|
-
_constr.prototype = constructor.prototype;
|
|
20
|
-
return _constr(...args);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* @summary Recursively finds the last prototype before Object
|
|
24
|
-
* @param {object} obj
|
|
25
|
-
*
|
|
26
|
-
* @function findLastProtoBeforeObject
|
|
27
|
-
* @memberOf module:decorator-validation.Construction
|
|
28
|
-
*/
|
|
29
|
-
function findLastProtoBeforeObject(obj) {
|
|
30
|
-
let prototype = Object.getPrototypeOf(obj);
|
|
31
|
-
if (prototype === Object.prototype)
|
|
32
|
-
return obj;
|
|
33
|
-
while (prototype !== Object.prototype) {
|
|
34
|
-
prototype = Object.getPrototypeOf(prototype);
|
|
35
|
-
if (prototype === Object.prototype)
|
|
36
|
-
return prototype;
|
|
37
|
-
if (Object.getPrototypeOf(prototype) === Object.prototype)
|
|
38
|
-
return prototype;
|
|
39
|
-
}
|
|
40
|
-
throw new Error("Could not find proper prototype");
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* @sumary binds the {@link Model} class as a root prototype of the provided instance
|
|
44
|
-
*
|
|
45
|
-
* @param {unknown} obj
|
|
46
|
-
*
|
|
47
|
-
* @function bindModelPrototype
|
|
48
|
-
* @memberOf module:decorator-validation.Construction
|
|
49
|
-
*/
|
|
50
|
-
function bindModelPrototype(obj) {
|
|
51
|
-
if (obj instanceof Model_1.Model)
|
|
52
|
-
return;
|
|
53
|
-
function bindPrototype(objToOverride, prototype) {
|
|
54
|
-
Object.setPrototypeOf(objToOverride, prototype);
|
|
55
|
-
}
|
|
56
|
-
const prototype = Object.getPrototypeOf(obj);
|
|
57
|
-
if (prototype === Object.prototype) {
|
|
58
|
-
return bindPrototype(obj, Model_1.Model.prototype);
|
|
59
|
-
}
|
|
60
|
-
while (prototype !== Object.prototype) {
|
|
61
|
-
const prot = Object.getPrototypeOf(prototype);
|
|
62
|
-
if (prot === Object.prototype ||
|
|
63
|
-
Object.getPrototypeOf(prot) === Object.prototype) {
|
|
64
|
-
return bindPrototype(prototype, Model_1.Model.prototype);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
throw new Error("Could not find proper prototype to bind");
|
|
68
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Model } from "./Model";
|
|
2
|
-
/**
|
|
3
|
-
* @summary Helper Function to override constructors
|
|
4
|
-
*
|
|
5
|
-
* @param {Function} constructor
|
|
6
|
-
* @param {any[]} [args]
|
|
7
|
-
* @return {T} the new instance
|
|
8
|
-
*
|
|
9
|
-
* @function construct
|
|
10
|
-
* @memberOf module:decorator-validation.Construction
|
|
11
|
-
*/
|
|
12
|
-
export declare function construct<T extends Model>(constructor: any, ...args: any[]): T;
|
|
13
|
-
/**
|
|
14
|
-
* @summary Recursively finds the last prototype before Object
|
|
15
|
-
* @param {object} obj
|
|
16
|
-
*
|
|
17
|
-
* @function findLastProtoBeforeObject
|
|
18
|
-
* @memberOf module:decorator-validation.Construction
|
|
19
|
-
*/
|
|
20
|
-
export declare function findLastProtoBeforeObject(obj: object): object;
|
|
21
|
-
/**
|
|
22
|
-
* @sumary binds the {@link Model} class as a root prototype of the provided instance
|
|
23
|
-
*
|
|
24
|
-
* @param {unknown} obj
|
|
25
|
-
*
|
|
26
|
-
* @function bindModelPrototype
|
|
27
|
-
* @memberOf module:decorator-validation.Construction
|
|
28
|
-
*/
|
|
29
|
-
export declare function bindModelPrototype(obj: unknown): void;
|
package/lib/model/decorators.cjs
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.model = model;
|
|
4
|
-
exports.hashedBy = hashedBy;
|
|
5
|
-
exports.serializedBy = serializedBy;
|
|
6
|
-
const construction_1 = require("./construction.cjs");
|
|
7
|
-
const constants_1 = require("../utils/constants.cjs");
|
|
8
|
-
const Model_1 = require("./Model.cjs");
|
|
9
|
-
const reflection_1 = require("@decaf-ts/reflection");
|
|
10
|
-
/**
|
|
11
|
-
* @summary Defines a class as a Model class
|
|
12
|
-
* @description
|
|
13
|
-
*
|
|
14
|
-
* - Registers the class under the model registry so it can be easily rebuilt;
|
|
15
|
-
* - Overrides the class constructor;
|
|
16
|
-
* - Runs the global {@link ModelBuilderFunction} if defined;
|
|
17
|
-
* - Runs the optional {@link InstanceCallback} if provided;
|
|
18
|
-
*
|
|
19
|
-
* @param {InstanceCallback} [instanceCallback] optional callback that will be called with the instance upon instantiation. defaults to undefined
|
|
20
|
-
*
|
|
21
|
-
* @function model
|
|
22
|
-
*
|
|
23
|
-
* @memberOf module:decorator-validation.Model
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
function model(instanceCallback) {
|
|
27
|
-
return ((original) => {
|
|
28
|
-
// the new constructor behaviour
|
|
29
|
-
const newConstructor = function (...args) {
|
|
30
|
-
const instance = (0, construction_1.construct)(original, ...args);
|
|
31
|
-
(0, construction_1.bindModelPrototype)(instance);
|
|
32
|
-
// run a builder function if defined with the first argument (The ModelArg)
|
|
33
|
-
const builder = Model_1.Model.getBuilder();
|
|
34
|
-
if (builder)
|
|
35
|
-
builder(instance, args.length ? args[0] : undefined);
|
|
36
|
-
(0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.MODEL), original.name)(instance.constructor);
|
|
37
|
-
if (instanceCallback)
|
|
38
|
-
instanceCallback(instance, ...args);
|
|
39
|
-
return instance;
|
|
40
|
-
};
|
|
41
|
-
// copy prototype so instanceof operator still works
|
|
42
|
-
newConstructor.prototype = original.prototype;
|
|
43
|
-
// Sets the proper constructor name for type verification
|
|
44
|
-
Object.defineProperty(newConstructor, "name", {
|
|
45
|
-
writable: false,
|
|
46
|
-
enumerable: true,
|
|
47
|
-
configurable: false,
|
|
48
|
-
value: original.prototype.constructor.name,
|
|
49
|
-
});
|
|
50
|
-
(0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.MODEL), original.name)(original);
|
|
51
|
-
Model_1.Model.register(newConstructor, original.name);
|
|
52
|
-
// return new constructor (will override original)
|
|
53
|
-
return newConstructor;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function hashedBy(algorithm, ...args) {
|
|
57
|
-
return (0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.HASHING), {
|
|
58
|
-
algorithm: algorithm,
|
|
59
|
-
args: args,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
function serializedBy(serializer, ...args) {
|
|
63
|
-
return (0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.SERIALIZATION), {
|
|
64
|
-
serializer: serializer,
|
|
65
|
-
args: args,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @summary defines the tpe os an InstanceCallback function
|
|
3
|
-
*
|
|
4
|
-
* @memberOf module:decorator-validation.Model
|
|
5
|
-
*/
|
|
6
|
-
export type InstanceCallback = (instance: any, ...args: any[]) => void;
|
|
7
|
-
/**
|
|
8
|
-
* @summary Defines a class as a Model class
|
|
9
|
-
* @description
|
|
10
|
-
*
|
|
11
|
-
* - Registers the class under the model registry so it can be easily rebuilt;
|
|
12
|
-
* - Overrides the class constructor;
|
|
13
|
-
* - Runs the global {@link ModelBuilderFunction} if defined;
|
|
14
|
-
* - Runs the optional {@link InstanceCallback} if provided;
|
|
15
|
-
*
|
|
16
|
-
* @param {InstanceCallback} [instanceCallback] optional callback that will be called with the instance upon instantiation. defaults to undefined
|
|
17
|
-
*
|
|
18
|
-
* @function model
|
|
19
|
-
*
|
|
20
|
-
* @memberOf module:decorator-validation.Model
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
export declare function model(instanceCallback?: InstanceCallback): any;
|
|
24
|
-
export declare function hashedBy(algorithm: string, ...args: any[]): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
|
|
25
|
-
export declare function serializedBy(serializer: string, ...args: any[]): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
|
package/lib/model/index.cjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./constants.cjs"), exports);
|
|
18
|
-
__exportStar(require("./construction.cjs"), exports);
|
|
19
|
-
__exportStar(require("./decorators.cjs"), exports);
|
|
20
|
-
__exportStar(require("./Model.cjs"), exports);
|
|
21
|
-
__exportStar(require("./ModelErrorDefinition.cjs"), exports);
|
|
22
|
-
__exportStar(require("./Registry.cjs"), exports);
|
|
23
|
-
__exportStar(require("./types.cjs"), exports);
|
|
24
|
-
__exportStar(require("./utils.cjs"), exports);
|
|
25
|
-
__exportStar(require("./validation.cjs"), exports);
|
package/lib/model/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export * from "./constants";
|
|
2
|
-
export * from "./construction";
|
|
3
|
-
export * from "./decorators";
|
|
4
|
-
export * from "./Model";
|
|
5
|
-
export * from "./ModelErrorDefinition";
|
|
6
|
-
export * from "./Registry";
|
|
7
|
-
export * from "./types";
|
|
8
|
-
export * from "./utils";
|
|
9
|
-
export * from "./validation";
|
package/lib/model/types.cjs
DELETED
package/lib/model/types.d.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { ModelErrorDefinition } from "./ModelErrorDefinition";
|
|
2
|
-
import { Model } from "./Model";
|
|
3
|
-
/**
|
|
4
|
-
* @summary Typo of a Model builder function
|
|
5
|
-
* @memberOf module:decorator-validation.Construction
|
|
6
|
-
* @category Construction
|
|
7
|
-
*/
|
|
8
|
-
export type ModelBuilderFunction = <T extends Model>(self: T, obj?: T | Record<string, any>) => T;
|
|
9
|
-
/**
|
|
10
|
-
* @summary Definition of a Model Constructor Argument
|
|
11
|
-
*
|
|
12
|
-
* @memberOf module:decorator-validation.Model
|
|
13
|
-
* @category Model
|
|
14
|
-
*
|
|
15
|
-
* @see ModelConstructor
|
|
16
|
-
*/
|
|
17
|
-
export type ModelArg<T> = T | Record<string, any>;
|
|
18
|
-
/**
|
|
19
|
-
* @summary Definition of a Class Constructor
|
|
20
|
-
* @description Generic type for Constructor functions
|
|
21
|
-
*
|
|
22
|
-
* @typedef Constructor
|
|
23
|
-
*
|
|
24
|
-
* @param {any[]} [args]
|
|
25
|
-
* @memberOf module:decorator-validation.Model
|
|
26
|
-
* @category Model
|
|
27
|
-
*/
|
|
28
|
-
export type Constructor<T> = {
|
|
29
|
-
new (...args: any[]): T;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* @summary Definition of a Model Constructor
|
|
33
|
-
* @description Generic type for all Model Constructor functions
|
|
34
|
-
*
|
|
35
|
-
* @typedef ModelConstructor
|
|
36
|
-
*
|
|
37
|
-
* @param {ModelArg<T>} [model]
|
|
38
|
-
* @param {any[]} [args]
|
|
39
|
-
* @memberOf module:decorator-validation.Model
|
|
40
|
-
* @category Construction
|
|
41
|
-
*/
|
|
42
|
-
export type ModelConstructor<T extends Model> = {
|
|
43
|
-
new (model?: ModelArg<T>, ...args: any[]): T;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* @summary Defines the Validation API for validatable models
|
|
47
|
-
* @interface Validatable
|
|
48
|
-
*
|
|
49
|
-
* @category Validation
|
|
50
|
-
*/
|
|
51
|
-
export interface Validatable {
|
|
52
|
-
/**
|
|
53
|
-
* @summary Validates the model and returns the {@link ModelErrorDefinition} if any
|
|
54
|
-
* @param {any} [args]
|
|
55
|
-
*
|
|
56
|
-
* @method
|
|
57
|
-
*/
|
|
58
|
-
hasErrors(...args: any[]): ModelErrorDefinition | undefined;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* @summary Serializable interface
|
|
62
|
-
*
|
|
63
|
-
* @interface Serializable
|
|
64
|
-
*
|
|
65
|
-
* @category Serialization
|
|
66
|
-
*/
|
|
67
|
-
export interface Serializable {
|
|
68
|
-
/**
|
|
69
|
-
* @summary serializes the model
|
|
70
|
-
* @method
|
|
71
|
-
*/
|
|
72
|
-
serialize(): string;
|
|
73
|
-
}
|
|
74
|
-
export interface Hashable {
|
|
75
|
-
hash(): string;
|
|
76
|
-
}
|
|
77
|
-
export interface Comparable<T> {
|
|
78
|
-
equals(other: T, ...args: any[]): boolean;
|
|
79
|
-
}
|
package/lib/model/utils.cjs
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isPropertyModel = isPropertyModel;
|
|
4
|
-
exports.isModel = isModel;
|
|
5
|
-
const constants_1 = require("../utils/constants.cjs");
|
|
6
|
-
const Model_1 = require("./Model.cjs");
|
|
7
|
-
function isPropertyModel(target, attribute) {
|
|
8
|
-
if (isModel(target[attribute]))
|
|
9
|
-
return true;
|
|
10
|
-
const metadata = Reflect.getMetadata(constants_1.ModelKeys.TYPE, target, attribute);
|
|
11
|
-
return Model_1.Model.get(metadata.name) ? metadata.name : undefined;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* @summary For Serialization/deserialization purposes.
|
|
15
|
-
* @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
|
|
16
|
-
*
|
|
17
|
-
* @function isModel
|
|
18
|
-
* @memberOf module:decorator-validation.Validation
|
|
19
|
-
* @category Validation
|
|
20
|
-
*/
|
|
21
|
-
function isModel(target) {
|
|
22
|
-
try {
|
|
23
|
-
return target instanceof Model_1.Model || !!Model_1.Model.getMetadata(target);
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
package/lib/model/utils.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Model } from "./Model";
|
|
2
|
-
export declare function isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
|
|
3
|
-
/**
|
|
4
|
-
* @summary For Serialization/deserialization purposes.
|
|
5
|
-
* @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
|
|
6
|
-
*
|
|
7
|
-
* @function isModel
|
|
8
|
-
* @memberOf module:decorator-validation.Validation
|
|
9
|
-
* @category Validation
|
|
10
|
-
*/
|
|
11
|
-
export declare function isModel(target: Record<string, any>): boolean;
|
package/lib/model/validation.cjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validate = validate;
|
|
4
|
-
const ModelErrorDefinition_1 = require("./ModelErrorDefinition.cjs");
|
|
5
|
-
const reflection_1 = require("@decaf-ts/reflection");
|
|
6
|
-
const constants_1 = require("../utils/constants.cjs");
|
|
7
|
-
const strings_1 = require("../utils/strings.cjs");
|
|
8
|
-
const constants_2 = require("./constants.cjs");
|
|
9
|
-
const Validation_1 = require("../validation/Validation.cjs");
|
|
10
|
-
const constants_3 = require("../validation/Validators/constants.cjs");
|
|
11
|
-
const utils_1 = require("./utils.cjs");
|
|
12
|
-
/**
|
|
13
|
-
* @summary Analyses the decorations of the properties and validates the obj according to them
|
|
14
|
-
*
|
|
15
|
-
* @typedef T extends Model
|
|
16
|
-
* @prop {T} obj Model object to validate
|
|
17
|
-
* @prop {string[]} [propsToIgnore] object properties to ignore in the validation
|
|
18
|
-
*
|
|
19
|
-
* @function validate
|
|
20
|
-
* @memberOf module:decorator-validation.Validation
|
|
21
|
-
* @category Validation
|
|
22
|
-
*/
|
|
23
|
-
function validate(obj, ...propsToIgnore) {
|
|
24
|
-
const decoratedProperties = [];
|
|
25
|
-
for (const prop in obj)
|
|
26
|
-
if (Object.prototype.hasOwnProperty.call(obj, prop) &&
|
|
27
|
-
propsToIgnore.indexOf(prop) === -1)
|
|
28
|
-
decoratedProperties.push(reflection_1.Reflection.getPropertyDecorators(constants_3.ValidationKeys.REFLECT, obj, prop));
|
|
29
|
-
let result = undefined;
|
|
30
|
-
for (const decoratedProperty of decoratedProperties) {
|
|
31
|
-
const { prop, decorators } = decoratedProperty;
|
|
32
|
-
if (!decorators || !decorators.length)
|
|
33
|
-
continue;
|
|
34
|
-
const defaultTypeDecorator = decorators[0];
|
|
35
|
-
// tries to find any type decorators or other decorators that already enforce type (the ones with the allowed types property defined). if so, skip the default type verification
|
|
36
|
-
if (decorators.find((d) => {
|
|
37
|
-
if (d.key === constants_3.ValidationKeys.TYPE)
|
|
38
|
-
return true;
|
|
39
|
-
return !!d.props.types?.find((t) => t === defaultTypeDecorator.props.name);
|
|
40
|
-
})) {
|
|
41
|
-
decorators.shift(); // remove the design:type decorator, since the type will already be checked
|
|
42
|
-
}
|
|
43
|
-
let errs = undefined;
|
|
44
|
-
for (const decorator of decorators) {
|
|
45
|
-
const validator = Validation_1.Validation.get(decorator.key);
|
|
46
|
-
if (!validator) {
|
|
47
|
-
throw new Error(`Missing validator for ${decorator.key}`);
|
|
48
|
-
}
|
|
49
|
-
const err = validator.hasErrors(obj[prop.toString()], ...(decorator.key === constants_1.ModelKeys.TYPE
|
|
50
|
-
? [decorator.props]
|
|
51
|
-
: Object.values(decorator.props)));
|
|
52
|
-
if (err) {
|
|
53
|
-
errs = errs || {};
|
|
54
|
-
errs[decorator.key] = err;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
if (errs) {
|
|
58
|
-
result = result || {};
|
|
59
|
-
result[decoratedProperty.prop.toString()] = errs;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
// tests nested classes
|
|
63
|
-
for (const prop of Object.keys(obj).filter((k) => !result || !result[k])) {
|
|
64
|
-
let err;
|
|
65
|
-
// if a nested Model
|
|
66
|
-
const allDecorators = reflection_1.Reflection.getPropertyDecorators(constants_3.ValidationKeys.REFLECT, obj, prop).decorators;
|
|
67
|
-
const decorators = reflection_1.Reflection.getPropertyDecorators(constants_3.ValidationKeys.REFLECT, obj, prop).decorators.filter((d) => [constants_1.ModelKeys.TYPE, constants_3.ValidationKeys.TYPE].indexOf(d.key) !== -1);
|
|
68
|
-
if (!decorators || !decorators.length)
|
|
69
|
-
continue;
|
|
70
|
-
const dec = decorators.pop();
|
|
71
|
-
const clazz = dec.props.name
|
|
72
|
-
? [dec.props.name]
|
|
73
|
-
: Array.isArray(dec.props.customTypes)
|
|
74
|
-
? dec.props.customTypes
|
|
75
|
-
: [dec.props.customTypes];
|
|
76
|
-
const reserved = Object.values(constants_2.ReservedModels).map((v) => v.toLowerCase());
|
|
77
|
-
for (const c of clazz) {
|
|
78
|
-
if (reserved.indexOf(c.toLowerCase()) === -1) {
|
|
79
|
-
const typeDecoratorKey = Array.isArray(obj[prop])
|
|
80
|
-
? constants_3.ValidationKeys.LIST
|
|
81
|
-
: constants_3.ValidationKeys.TYPE;
|
|
82
|
-
const types = allDecorators.find((d) => d.key === typeDecoratorKey) || {};
|
|
83
|
-
let allowedTypes = [];
|
|
84
|
-
if (types && types.props) {
|
|
85
|
-
const customTypes = Array.isArray(obj[prop])
|
|
86
|
-
? types.props.class
|
|
87
|
-
: types.props.customTypes;
|
|
88
|
-
if (customTypes)
|
|
89
|
-
allowedTypes = Array.isArray(customTypes)
|
|
90
|
-
? customTypes.map((t) => `${t}`.toLowerCase())
|
|
91
|
-
: [customTypes.toLowerCase()];
|
|
92
|
-
}
|
|
93
|
-
const validate = (prop, value) => {
|
|
94
|
-
if (typeof value === "object" || typeof value === "function")
|
|
95
|
-
return (0, utils_1.isModel)(value)
|
|
96
|
-
? value.hasErrors()
|
|
97
|
-
: allowedTypes.includes(typeof value)
|
|
98
|
-
? undefined
|
|
99
|
-
: "Value has no validatable type";
|
|
100
|
-
};
|
|
101
|
-
switch (c) {
|
|
102
|
-
case Array.name:
|
|
103
|
-
case Set.name:
|
|
104
|
-
if (allDecorators.length) {
|
|
105
|
-
const listDec = allDecorators.find((d) => d.key === constants_3.ValidationKeys.LIST);
|
|
106
|
-
if (listDec) {
|
|
107
|
-
err = (c === Array.name
|
|
108
|
-
? obj[prop]
|
|
109
|
-
: // If it's a Set
|
|
110
|
-
obj[prop].values())
|
|
111
|
-
.map((v) => validate(prop, v))
|
|
112
|
-
.filter((e) => !!e);
|
|
113
|
-
if (!err?.length) {
|
|
114
|
-
// if the result is an empty list...
|
|
115
|
-
err = undefined;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
default:
|
|
121
|
-
try {
|
|
122
|
-
if (obj[prop])
|
|
123
|
-
err = validate(prop, obj[prop]);
|
|
124
|
-
}
|
|
125
|
-
catch (e) {
|
|
126
|
-
console.warn((0, strings_1.sf)("Model should be validatable but its not: " + e));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (err) {
|
|
131
|
-
result = result || {};
|
|
132
|
-
result[prop] = err;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return result ? new ModelErrorDefinition_1.ModelErrorDefinition(result) : undefined;
|
|
137
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ModelErrorDefinition } from "./ModelErrorDefinition";
|
|
2
|
-
import { Model } from "./Model";
|
|
3
|
-
/**
|
|
4
|
-
* @summary Analyses the decorations of the properties and validates the obj according to them
|
|
5
|
-
*
|
|
6
|
-
* @typedef T extends Model
|
|
7
|
-
* @prop {T} obj Model object to validate
|
|
8
|
-
* @prop {string[]} [propsToIgnore] object properties to ignore in the validation
|
|
9
|
-
*
|
|
10
|
-
* @function validate
|
|
11
|
-
* @memberOf module:decorator-validation.Validation
|
|
12
|
-
* @category Validation
|
|
13
|
-
*/
|
|
14
|
-
export declare function validate<T extends Model>(obj: T, ...propsToIgnore: string[]): ModelErrorDefinition | undefined;
|
package/lib/utils/constants.cjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ModelKeys = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* @summary Defines the various Model keys used for reflection
|
|
6
|
-
*
|
|
7
|
-
* @property {string} REFLECT prefix to all other keys
|
|
8
|
-
* @property {string} TYPE type key
|
|
9
|
-
* @property {string} PARAMS method params key
|
|
10
|
-
* @property {string} RETURN method return key
|
|
11
|
-
* @property {string} MODEL model key
|
|
12
|
-
* @property {string} ANCHOR anchor key. will serve as a ghost property in the model
|
|
13
|
-
*
|
|
14
|
-
* @constant ModelKeys
|
|
15
|
-
* @memberOf module:decorator-validation.Model
|
|
16
|
-
* @category Model
|
|
17
|
-
*/
|
|
18
|
-
var ModelKeys;
|
|
19
|
-
(function (ModelKeys) {
|
|
20
|
-
ModelKeys["REFLECT"] = "decaf.model.";
|
|
21
|
-
ModelKeys["TYPE"] = "design:type";
|
|
22
|
-
ModelKeys["PARAMS"] = "design:paramtypes";
|
|
23
|
-
ModelKeys["RETURN"] = "design:returntype";
|
|
24
|
-
ModelKeys["MODEL"] = "model";
|
|
25
|
-
ModelKeys["ANCHOR"] = "__model";
|
|
26
|
-
ModelKeys["CONSTRUCTION"] = "constructed-by";
|
|
27
|
-
ModelKeys["ATTRIBUTE"] = "__attributes";
|
|
28
|
-
ModelKeys["HASHING"] = "hashing";
|
|
29
|
-
ModelKeys["SERIALIZATION"] = "serialization";
|
|
30
|
-
})(ModelKeys || (exports.ModelKeys = ModelKeys = {}));
|