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