@decaf-ts/decorator-validation 1.5.7 → 1.5.9
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/decorator-validation.bundle.min.js +2 -0
- package/dist/esm/decorator-validation.bundle.min.esm.js +2 -0
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/model/Model.d.ts +66 -0
- package/lib/esm/model/Model.js +93 -2
- package/lib/esm/model/index.d.ts +0 -2
- package/lib/esm/model/index.js +0 -2
- package/lib/esm/model/validation.js +2 -2
- package/lib/esm/utils/hashing.d.ts +0 -14
- package/lib/esm/utils/hashing.js +0 -17
- package/lib/esm/utils/index.d.ts +1 -0
- package/lib/esm/utils/index.js +1 -0
- package/lib/esm/utils/serialization.d.ts +4 -30
- package/lib/esm/utils/serialization.js +2 -1
- package/lib/esm/utils/types.d.ts +29 -0
- package/lib/esm/validation/Validation.d.ts +1 -1
- package/lib/esm/validation/Validators/DateValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/EmailValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/ListValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/MaxValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/MinLengthValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/MinValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/PasswordValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/PatternValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/RequiredValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/StepValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/TypeValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/URLValidator.d.ts +1 -1
- package/lib/esm/validation/Validators/Validator.d.ts +1 -1
- package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +2 -2
- package/lib/esm/validation/Validators/index.d.ts +0 -1
- package/lib/esm/validation/Validators/index.js +0 -1
- package/lib/esm/validation/types.d.ts +88 -0
- package/lib/{index.js → index.cjs} +4 -4
- package/lib/index.d.ts +1 -1
- package/lib/model/{Model.js → Model.cjs} +107 -12
- package/lib/model/Model.d.ts +66 -0
- package/lib/model/{construction.js → construction.cjs} +1 -1
- package/lib/model/{decorators.js → decorators.cjs} +3 -3
- package/lib/{utils/index.js → model/index.cjs} +7 -7
- package/lib/model/index.d.ts +0 -2
- package/lib/model/{validation.js → validation.cjs} +9 -9
- package/lib/utils/{dates.js → dates.cjs} +2 -2
- package/lib/utils/{decorators.js → decorators.cjs} +1 -1
- package/lib/utils/{hashing.js → hashing.cjs} +0 -18
- package/lib/utils/hashing.d.ts +0 -14
- package/lib/{model/index.js → utils/index.cjs} +8 -9
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/{serialization.js → serialization.cjs} +5 -4
- package/lib/utils/serialization.d.ts +4 -30
- package/lib/utils/types.d.ts +29 -0
- package/lib/validation/{Validation.js → Validation.cjs} +2 -2
- package/lib/validation/Validation.d.ts +1 -1
- package/lib/validation/Validators/{DateValidator.js → DateValidator.cjs} +3 -3
- package/lib/validation/Validators/DateValidator.d.ts +1 -1
- package/lib/validation/Validators/{EmailValidator.js → EmailValidator.cjs} +3 -3
- package/lib/validation/Validators/EmailValidator.d.ts +1 -1
- package/lib/validation/Validators/{ListValidator.js → ListValidator.cjs} +3 -3
- package/lib/validation/Validators/ListValidator.d.ts +1 -1
- package/lib/validation/Validators/{MaxLengthValidator.js → MaxLengthValidator.cjs} +3 -3
- package/lib/validation/Validators/MaxLengthValidator.d.ts +1 -1
- package/lib/validation/Validators/{MaxValidator.js → MaxValidator.cjs} +3 -3
- package/lib/validation/Validators/MaxValidator.d.ts +1 -1
- package/lib/validation/Validators/{MinLengthValidator.js → MinLengthValidator.cjs} +3 -3
- package/lib/validation/Validators/MinLengthValidator.d.ts +1 -1
- package/lib/validation/Validators/{MinValidator.js → MinValidator.cjs} +3 -3
- package/lib/validation/Validators/MinValidator.d.ts +1 -1
- package/lib/validation/Validators/{PasswordValidator.js → PasswordValidator.cjs} +3 -3
- package/lib/validation/Validators/PasswordValidator.d.ts +1 -1
- package/lib/validation/Validators/{PatternValidator.js → PatternValidator.cjs} +3 -3
- package/lib/validation/Validators/PatternValidator.d.ts +1 -1
- package/lib/validation/Validators/{RequiredValidator.js → RequiredValidator.cjs} +3 -3
- package/lib/validation/Validators/RequiredValidator.d.ts +1 -1
- package/lib/validation/Validators/{StepValidator.js → StepValidator.cjs} +3 -3
- package/lib/validation/Validators/StepValidator.d.ts +1 -1
- package/lib/validation/Validators/{TypeValidator.js → TypeValidator.cjs} +5 -5
- package/lib/validation/Validators/TypeValidator.d.ts +1 -1
- package/lib/validation/Validators/{URLValidator.js → URLValidator.cjs} +3 -3
- package/lib/validation/Validators/URLValidator.d.ts +1 -1
- package/lib/validation/Validators/{Validator.js → Validator.cjs} +2 -2
- package/lib/validation/Validators/Validator.d.ts +1 -1
- package/lib/validation/Validators/ValidatorRegistry.d.ts +2 -2
- package/lib/validation/Validators/{constants.js → constants.cjs} +1 -1
- package/lib/validation/Validators/{decorators.js → decorators.cjs} +2 -2
- package/lib/validation/Validators/index.cjs +68 -0
- package/lib/validation/{decorators.js → decorators.cjs} +5 -5
- package/lib/validation/{index.js → index.cjs} +4 -4
- package/lib/validation/types.d.ts +88 -0
- package/package.json +1 -1
- package/dist/decorator-validation.js +0 -2
- package/dist/esm/decorator-validation.js +0 -2
- package/lib/esm/model/Registry.d.ts +0 -59
- package/lib/esm/model/Registry.js +0 -73
- package/lib/esm/model/utils.d.ts +0 -11
- package/lib/esm/model/utils.js +0 -25
- package/lib/esm/validation/Validators/types.d.ts +0 -88
- package/lib/model/Registry.d.ts +0 -59
- package/lib/model/Registry.js +0 -78
- package/lib/model/utils.d.ts +0 -11
- package/lib/model/utils.js +0 -29
- package/lib/validation/Validators/decorators.d.ts +0 -12
- package/lib/validation/Validators/index.d.ts +0 -52
- package/lib/validation/Validators/index.js +0 -69
- /package/dist/{decorator-validation.js.LICENSE.txt → decorator-validation.bundle.min.js.LICENSE.txt} +0 -0
- /package/dist/esm/{decorator-validation.js.LICENSE.txt → decorator-validation.bundle.min.esm.js.LICENSE.txt} +0 -0
- /package/lib/esm/{validation/Validators → utils}/types.js +0 -0
- /package/lib/model/{ModelErrorDefinition.js → ModelErrorDefinition.cjs} +0 -0
- /package/lib/model/{constants.js → constants.cjs} +0 -0
- /package/lib/model/{types.js → types.cjs} +0 -0
- /package/lib/utils/{constants.js → constants.cjs} +0 -0
- /package/lib/utils/{registry.js → registry.cjs} +0 -0
- /package/lib/utils/{strings.js → strings.cjs} +0 -0
- /package/lib/{validation/Validators/types.js → utils/types.cjs} +0 -0
- /package/lib/validation/Validators/{ValidatorRegistry.js → ValidatorRegistry.cjs} +0 -0
- /package/lib/validation/{types.js → types.cjs} +0 -0
|
@@ -1,33 +1,7 @@
|
|
|
1
|
-
import { Model } from "../model/Model";
|
|
2
1
|
import { Constructor } from "../model/types";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @interface Serializer
|
|
7
|
-
* @category Serialization
|
|
8
|
-
*/
|
|
9
|
-
export interface Serializer<T extends Model> {
|
|
10
|
-
/**
|
|
11
|
-
* @summary Serializes a model
|
|
12
|
-
* @param {T} model
|
|
13
|
-
*
|
|
14
|
-
* @param args
|
|
15
|
-
* @method
|
|
16
|
-
*
|
|
17
|
-
* @throws {Error}
|
|
18
|
-
*/
|
|
19
|
-
serialize(model: T, ...args: any[]): string;
|
|
20
|
-
/**
|
|
21
|
-
* @summary Rebuilds a model from serialization
|
|
22
|
-
* @param {string} str
|
|
23
|
-
*
|
|
24
|
-
* @param args
|
|
25
|
-
* @method
|
|
26
|
-
*
|
|
27
|
-
* @throws {Error}
|
|
28
|
-
*/
|
|
29
|
-
deserialize(str: string, ...args: any[]): T;
|
|
30
|
-
}
|
|
2
|
+
import { Serializer } from "./types";
|
|
3
|
+
import { Model } from "../model/Model";
|
|
4
|
+
export declare const DefaultSerializationMethod = "json";
|
|
31
5
|
/**
|
|
32
6
|
* @summary Concrete implementation of a {@link Serializer} in JSON format
|
|
33
7
|
* @description JS's native JSON.stringify (used here) is not deterministic
|
|
@@ -42,6 +16,7 @@ export interface Serializer<T extends Model> {
|
|
|
42
16
|
* @category Serialization
|
|
43
17
|
*/
|
|
44
18
|
export declare class JSONSerializer<T extends Model> implements Serializer<T> {
|
|
19
|
+
constructor();
|
|
45
20
|
/**
|
|
46
21
|
* @summary prepares the model for serialization
|
|
47
22
|
* @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
|
|
@@ -66,7 +41,6 @@ export declare class JSONSerializer<T extends Model> implements Serializer<T> {
|
|
|
66
41
|
*/
|
|
67
42
|
serialize(model: T): string;
|
|
68
43
|
}
|
|
69
|
-
export declare const DefaultSerializationMethod = "json";
|
|
70
44
|
export declare class Serialization {
|
|
71
45
|
private static current;
|
|
72
46
|
private static cache;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Model } from "../model/Model";
|
|
2
2
|
import { ModelKeys } from "./constants";
|
|
3
|
+
export const DefaultSerializationMethod = "json";
|
|
3
4
|
/**
|
|
4
5
|
* @summary Concrete implementation of a {@link Serializer} in JSON format
|
|
5
6
|
* @description JS's native JSON.stringify (used here) is not deterministic
|
|
@@ -14,6 +15,7 @@ import { ModelKeys } from "./constants";
|
|
|
14
15
|
* @category Serialization
|
|
15
16
|
*/
|
|
16
17
|
export class JSONSerializer {
|
|
18
|
+
constructor() { }
|
|
17
19
|
/**
|
|
18
20
|
* @summary prepares the model for serialization
|
|
19
21
|
* @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
|
|
@@ -53,7 +55,6 @@ export class JSONSerializer {
|
|
|
53
55
|
return JSON.stringify(this.preSerialize(model));
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
|
-
export const DefaultSerializationMethod = "json";
|
|
57
58
|
export class Serialization {
|
|
58
59
|
static { this.current = DefaultSerializationMethod; }
|
|
59
60
|
static { this.cache = {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Model } from "../model";
|
|
2
|
+
/**
|
|
3
|
+
* @summary Helper in serialization
|
|
4
|
+
*
|
|
5
|
+
* @interface Serializer
|
|
6
|
+
* @category Serialization
|
|
7
|
+
*/
|
|
8
|
+
export interface Serializer<T extends Model> {
|
|
9
|
+
/**
|
|
10
|
+
* @summary Serializes a model
|
|
11
|
+
* @param {T} model
|
|
12
|
+
*
|
|
13
|
+
* @param args
|
|
14
|
+
* @method
|
|
15
|
+
*
|
|
16
|
+
* @throws {Error}
|
|
17
|
+
*/
|
|
18
|
+
serialize(model: T, ...args: any[]): string;
|
|
19
|
+
/**
|
|
20
|
+
* @summary Rebuilds a model from serialization
|
|
21
|
+
* @param {string} str
|
|
22
|
+
*
|
|
23
|
+
* @param args
|
|
24
|
+
* @method
|
|
25
|
+
*
|
|
26
|
+
* @throws {Error}
|
|
27
|
+
*/
|
|
28
|
+
deserialize(str: string, ...args: any[]): T;
|
|
29
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Validator } from "./Validators/Validator";
|
|
2
|
-
import { IValidatorRegistry, ValidatorDefinition } from "./
|
|
2
|
+
import { IValidatorRegistry, ValidatorDefinition } from "./types";
|
|
3
3
|
/**
|
|
4
4
|
* @summary Static class acting as a namespace for the Validation
|
|
5
5
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PatternValidator } from "./PatternValidator";
|
|
2
|
-
import { PatternValidatorOptions } from "
|
|
2
|
+
import { PatternValidatorOptions } from "../types";
|
|
3
3
|
/**
|
|
4
4
|
* @summary URL Validator
|
|
5
5
|
* @description Pattern from {@link https://gist.github.com/dperini/729294}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ValidatorDefinition } from "
|
|
2
|
-
import { IValidatorRegistry } from "
|
|
1
|
+
import { ValidatorDefinition } from "../types";
|
|
2
|
+
import { IValidatorRegistry } from "../types";
|
|
3
3
|
import type { Validator } from "./Validator";
|
|
4
4
|
/**
|
|
5
5
|
* @summary Duck typing for Validators
|
|
@@ -24,7 +24,6 @@ export * from "./PasswordValidator";
|
|
|
24
24
|
export * from "./PatternValidator";
|
|
25
25
|
export * from "./RequiredValidator";
|
|
26
26
|
export * from "./StepValidator";
|
|
27
|
-
export * from "./types";
|
|
28
27
|
export * from "./TypeValidator";
|
|
29
28
|
export * from "./URLValidator";
|
|
30
29
|
export * from "./Validator";
|
|
@@ -24,7 +24,6 @@ export * from "./PasswordValidator";
|
|
|
24
24
|
export * from "./PatternValidator";
|
|
25
25
|
export * from "./RequiredValidator";
|
|
26
26
|
export * from "./StepValidator";
|
|
27
|
-
export * from "./types";
|
|
28
27
|
export * from "./TypeValidator";
|
|
29
28
|
export * from "./URLValidator";
|
|
30
29
|
export * from "./Validator";
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { DecoratorMetadata } from "@decaf-ts/reflection";
|
|
2
|
+
import { Constructor } from "../model";
|
|
3
|
+
import { Validator } from "./Validators";
|
|
4
|
+
import { IRegistry } from "../utils";
|
|
2
5
|
/**
|
|
3
6
|
* @summary Type for validation decorator metadata
|
|
4
7
|
* @memberOf module:decorator-validation.Reflection
|
|
@@ -44,3 +47,88 @@ export type ValidationElementDefinition = {
|
|
|
44
47
|
* @category Validation
|
|
45
48
|
*/
|
|
46
49
|
export type ModelErrors = Record<string, Record<string, string | undefined>>;
|
|
50
|
+
/**
|
|
51
|
+
* @summary Util type for {@link Validator} configuration
|
|
52
|
+
* @memberOf module:decorator-validation.Validation
|
|
53
|
+
* @category Validation
|
|
54
|
+
*/
|
|
55
|
+
export type ValidatorDefinition = {
|
|
56
|
+
validator: Constructor<Validator>;
|
|
57
|
+
validationKey: string;
|
|
58
|
+
save: boolean;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* @summary Base API for a {@link Validator} registry
|
|
62
|
+
*
|
|
63
|
+
* @interface ValidatorRegistry
|
|
64
|
+
* @extends IRegistry
|
|
65
|
+
*
|
|
66
|
+
* @category Validation
|
|
67
|
+
*/
|
|
68
|
+
export interface IValidatorRegistry<T extends Validator> extends IRegistry<T> {
|
|
69
|
+
/**
|
|
70
|
+
* @summary retrieves the custom keys
|
|
71
|
+
* @method
|
|
72
|
+
*/
|
|
73
|
+
getCustomKeys(): Record<string, string>;
|
|
74
|
+
/**
|
|
75
|
+
* @summary Retrieves the Registered validator keys
|
|
76
|
+
* @return {string[]} the registered validators keys
|
|
77
|
+
* @method
|
|
78
|
+
*/
|
|
79
|
+
getKeys(): string[];
|
|
80
|
+
/**
|
|
81
|
+
* @summary Registers the provided validators onto the registry
|
|
82
|
+
*
|
|
83
|
+
* @typedef T extends Validator
|
|
84
|
+
* @param {T[] | ValidatorDefinition[]} validator
|
|
85
|
+
* @method
|
|
86
|
+
*/
|
|
87
|
+
register<T extends Validator>(...validator: (T | ValidatorDefinition)[]): void;
|
|
88
|
+
/**
|
|
89
|
+
* @summary Retrieves the Validator constructor if registered
|
|
90
|
+
*
|
|
91
|
+
* @typedef T extends Validator
|
|
92
|
+
* @param {string} key one of the {@link ValidationKeys}
|
|
93
|
+
* @return {Validator | undefined} the registered Validator or undefined if there is nono matching the provided key
|
|
94
|
+
* @method
|
|
95
|
+
*/
|
|
96
|
+
get<T extends Validator>(key: string): T | undefined;
|
|
97
|
+
}
|
|
98
|
+
export type ValidatorOptions = {
|
|
99
|
+
message?: string;
|
|
100
|
+
};
|
|
101
|
+
export interface URLValidatorOptions extends ValidatorOptions {
|
|
102
|
+
types: string | string[] | {
|
|
103
|
+
name: string;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export interface TypeValidatorOptions extends ValidatorOptions {
|
|
107
|
+
types: string | string[] | {
|
|
108
|
+
name: string;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export interface StepValidatorOptions extends ValidatorOptions {
|
|
112
|
+
step: number | string;
|
|
113
|
+
}
|
|
114
|
+
export interface PatternValidatorOptions extends ValidatorOptions {
|
|
115
|
+
pattern?: RegExp | string;
|
|
116
|
+
}
|
|
117
|
+
export interface MinValidatorOptions extends ValidatorOptions {
|
|
118
|
+
min: number | Date | string;
|
|
119
|
+
}
|
|
120
|
+
export interface MinLengthValidatorOptions extends ValidatorOptions {
|
|
121
|
+
minLength: number;
|
|
122
|
+
}
|
|
123
|
+
export interface MaxValidatorOptions extends ValidatorOptions {
|
|
124
|
+
max: number | Date | string;
|
|
125
|
+
}
|
|
126
|
+
export interface MaxLengthValidatorOptions extends ValidatorOptions {
|
|
127
|
+
maxLength: number;
|
|
128
|
+
}
|
|
129
|
+
export interface ListValidatorOptions extends ValidatorOptions {
|
|
130
|
+
clazz: string[];
|
|
131
|
+
}
|
|
132
|
+
export interface DateValidatorOptions extends ValidatorOptions {
|
|
133
|
+
format?: string;
|
|
134
|
+
}
|
|
@@ -53,7 +53,7 @@ exports.VERSION = void 0;
|
|
|
53
53
|
* @namespace Format
|
|
54
54
|
* @memberOf module:decorator-validation
|
|
55
55
|
*/
|
|
56
|
-
__exportStar(require("./utils"), exports);
|
|
57
|
-
__exportStar(require("./validation"), exports);
|
|
58
|
-
__exportStar(require("./model"), exports);
|
|
59
|
-
exports.VERSION = "1.5.
|
|
56
|
+
__exportStar(require("./utils/index.cjs"), exports);
|
|
57
|
+
__exportStar(require("./validation/index.cjs"), exports);
|
|
58
|
+
__exportStar(require("./model/index.cjs"), exports);
|
|
59
|
+
exports.VERSION = "1.5.8";
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,113 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Model = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
exports.Model = exports.ModelRegistryManager = void 0;
|
|
4
|
+
exports.isPropertyModel = isPropertyModel;
|
|
5
|
+
exports.isModel = isModel;
|
|
6
|
+
exports.bulkModelRegister = bulkModelRegister;
|
|
7
|
+
const serialization_1 = require("../utils/serialization.cjs");
|
|
6
8
|
const reflection_1 = require("@decaf-ts/reflection");
|
|
7
|
-
const validation_1 = require("./validation");
|
|
8
|
-
const hashing_1 = require("../utils/hashing");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const constants_3 = require("./constants");
|
|
9
|
+
const validation_1 = require("./validation.cjs");
|
|
10
|
+
const hashing_1 = require("../utils/hashing.cjs");
|
|
11
|
+
const constants_1 = require("../utils/constants.cjs");
|
|
12
|
+
const constants_2 = require("../validation/Validators/constants.cjs");
|
|
13
|
+
const strings_1 = require("../utils/strings.cjs");
|
|
14
|
+
const constants_3 = require("./constants.cjs");
|
|
14
15
|
let modelBuilderFunction;
|
|
15
16
|
let actingModelRegistry;
|
|
17
|
+
function isPropertyModel(target, attribute) {
|
|
18
|
+
if (isModel(target[attribute]))
|
|
19
|
+
return true;
|
|
20
|
+
const metadata = Reflect.getMetadata(constants_1.ModelKeys.TYPE, target, attribute);
|
|
21
|
+
return Model.get(metadata.name) ? metadata.name : undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @summary For Serialization/deserialization purposes.
|
|
25
|
+
* @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
|
|
26
|
+
*
|
|
27
|
+
* @function isModel
|
|
28
|
+
* @memberOf module:decorator-validation.Validation
|
|
29
|
+
* @category Validation
|
|
30
|
+
*/
|
|
31
|
+
function isModel(target) {
|
|
32
|
+
try {
|
|
33
|
+
return target instanceof Model || !!Model.getMetadata(target);
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @summary Util class to enable serialization and correct rebuilding
|
|
42
|
+
*
|
|
43
|
+
* @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
|
|
44
|
+
* @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
|
|
45
|
+
*
|
|
46
|
+
* @class ModelRegistryManager
|
|
47
|
+
* @implements ModelRegistry
|
|
48
|
+
*
|
|
49
|
+
* @category Model
|
|
50
|
+
*/
|
|
51
|
+
class ModelRegistryManager {
|
|
52
|
+
constructor(testFunction = isModel) {
|
|
53
|
+
this.cache = {};
|
|
54
|
+
this.testFunction = testFunction;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* @summary register new Models
|
|
58
|
+
* @param {any} constructor
|
|
59
|
+
* @param {string} [name] when not defined, the name of the constructor will be used
|
|
60
|
+
*/
|
|
61
|
+
register(constructor, name) {
|
|
62
|
+
if (typeof constructor !== "function")
|
|
63
|
+
throw new Error("Model registering failed. Missing Class name or constructor");
|
|
64
|
+
name = name || constructor.name;
|
|
65
|
+
this.cache[name] = constructor;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @summary Gets a registered Model {@link ModelConstructor}
|
|
69
|
+
* @param {string} name
|
|
70
|
+
*/
|
|
71
|
+
get(name) {
|
|
72
|
+
try {
|
|
73
|
+
return this.cache[name];
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @param {Record<string, any>} obj
|
|
82
|
+
* @param {string} [clazz] when provided, it will attempt to find the matching constructor
|
|
83
|
+
*
|
|
84
|
+
* @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
|
|
85
|
+
*/
|
|
86
|
+
build(obj = {}, clazz) {
|
|
87
|
+
if (!clazz && !this.testFunction(obj))
|
|
88
|
+
throw new Error("Provided obj is not a Model object");
|
|
89
|
+
const name = clazz || Model.getMetadata(obj);
|
|
90
|
+
if (!(name in this.cache))
|
|
91
|
+
throw new Error((0, strings_1.sf)("Provided class {0} is not a registered Model object", name));
|
|
92
|
+
return new this.cache[name](obj);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.ModelRegistryManager = ModelRegistryManager;
|
|
96
|
+
/**
|
|
97
|
+
* @summary Bulk Registers Models
|
|
98
|
+
* @description Useful when using bundlers that might not evaluate all the code at once
|
|
99
|
+
*
|
|
100
|
+
* @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
|
|
101
|
+
*
|
|
102
|
+
* @memberOf module:decorator-validation.Model
|
|
103
|
+
* @category Model
|
|
104
|
+
*/
|
|
105
|
+
function bulkModelRegister(...models) {
|
|
106
|
+
models.forEach((m) => {
|
|
107
|
+
const constructor = (m.constructor ? m.constructor : m);
|
|
108
|
+
Model.register(constructor, m.name);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
16
111
|
/**
|
|
17
112
|
* @summary Abstract class representing a Validatable Model object
|
|
18
113
|
* @description Meant to be used as a base class for all Model classes
|
|
@@ -122,7 +217,7 @@ class Model {
|
|
|
122
217
|
obj[prop] || undefined;
|
|
123
218
|
if (typeof self[prop] !== "object")
|
|
124
219
|
continue;
|
|
125
|
-
const propM =
|
|
220
|
+
const propM = isPropertyModel(self, prop);
|
|
126
221
|
if (propM) {
|
|
127
222
|
try {
|
|
128
223
|
self[prop] = Model.build(self[prop], typeof propM === "string" ? propM : undefined);
|
|
@@ -209,7 +304,7 @@ class Model {
|
|
|
209
304
|
*/
|
|
210
305
|
static getRegistry() {
|
|
211
306
|
if (!actingModelRegistry)
|
|
212
|
-
actingModelRegistry = new
|
|
307
|
+
actingModelRegistry = new ModelRegistryManager();
|
|
213
308
|
return actingModelRegistry;
|
|
214
309
|
}
|
|
215
310
|
/**
|
package/lib/model/Model.d.ts
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
import { BuilderRegistry } from "../utils/registry";
|
|
2
2
|
import { ModelErrorDefinition } from "./ModelErrorDefinition";
|
|
3
3
|
import { Comparable, Constructor, Hashable, ModelArg, ModelBuilderFunction, ModelConstructor, Serializable, Validatable } from "./types";
|
|
4
|
+
export declare function isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* @summary For Serialization/deserialization purposes.
|
|
7
|
+
* @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
|
|
8
|
+
*
|
|
9
|
+
* @function isModel
|
|
10
|
+
* @memberOf module:decorator-validation.Validation
|
|
11
|
+
* @category Validation
|
|
12
|
+
*/
|
|
13
|
+
export declare function isModel(target: Record<string, any>): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* @summary ModelRegistry Interface
|
|
16
|
+
*
|
|
17
|
+
* @interface ModelRegistry
|
|
18
|
+
* @extends BuilderRegistry<Model>
|
|
19
|
+
*
|
|
20
|
+
* @category Model
|
|
21
|
+
*/
|
|
22
|
+
export type ModelRegistry<T extends Model> = BuilderRegistry<T>;
|
|
23
|
+
/**
|
|
24
|
+
* @summary Util class to enable serialization and correct rebuilding
|
|
25
|
+
*
|
|
26
|
+
* @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
|
|
27
|
+
* @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
|
|
28
|
+
*
|
|
29
|
+
* @class ModelRegistryManager
|
|
30
|
+
* @implements ModelRegistry
|
|
31
|
+
*
|
|
32
|
+
* @category Model
|
|
33
|
+
*/
|
|
34
|
+
export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
|
|
35
|
+
private cache;
|
|
36
|
+
private readonly testFunction;
|
|
37
|
+
constructor(testFunction?: (obj: Record<string, any>) => boolean);
|
|
38
|
+
/**
|
|
39
|
+
* @summary register new Models
|
|
40
|
+
* @param {any} constructor
|
|
41
|
+
* @param {string} [name] when not defined, the name of the constructor will be used
|
|
42
|
+
*/
|
|
43
|
+
register(constructor: ModelConstructor<T>, name?: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* @summary Gets a registered Model {@link ModelConstructor}
|
|
46
|
+
* @param {string} name
|
|
47
|
+
*/
|
|
48
|
+
get(name: string): ModelConstructor<T> | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* @param {Record<string, any>} obj
|
|
51
|
+
* @param {string} [clazz] when provided, it will attempt to find the matching constructor
|
|
52
|
+
*
|
|
53
|
+
* @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
|
|
54
|
+
*/
|
|
55
|
+
build(obj?: Record<string, any>, clazz?: string): T;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @summary Bulk Registers Models
|
|
59
|
+
* @description Useful when using bundlers that might not evaluate all the code at once
|
|
60
|
+
*
|
|
61
|
+
* @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
|
|
62
|
+
*
|
|
63
|
+
* @memberOf module:decorator-validation.Model
|
|
64
|
+
* @category Model
|
|
65
|
+
*/
|
|
66
|
+
export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
|
|
67
|
+
name: string;
|
|
68
|
+
constructor: Constructor<T>;
|
|
69
|
+
})[]): void;
|
|
4
70
|
/**
|
|
5
71
|
* @summary Abstract class representing a Validatable Model object
|
|
6
72
|
* @description Meant to be used as a base class for all Model classes
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.construct = construct;
|
|
4
4
|
exports.findLastProtoBeforeObject = findLastProtoBeforeObject;
|
|
5
5
|
exports.bindModelPrototype = bindModelPrototype;
|
|
6
|
-
const Model_1 = require("./Model");
|
|
6
|
+
const Model_1 = require("./Model.cjs");
|
|
7
7
|
/**
|
|
8
8
|
* @summary Helper Function to override constructors
|
|
9
9
|
*
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.model = model;
|
|
4
4
|
exports.hashedBy = hashedBy;
|
|
5
5
|
exports.serializedBy = serializedBy;
|
|
6
|
-
const construction_1 = require("./construction");
|
|
7
|
-
const constants_1 = require("../utils/constants");
|
|
8
|
-
const Model_1 = require("./Model");
|
|
6
|
+
const construction_1 = require("./construction.cjs");
|
|
7
|
+
const constants_1 = require("../utils/constants.cjs");
|
|
8
|
+
const Model_1 = require("./Model.cjs");
|
|
9
9
|
const reflection_1 = require("@decaf-ts/reflection");
|
|
10
10
|
/**
|
|
11
11
|
* @summary Defines a class as a Model class
|
|
@@ -14,10 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./constants"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
19
|
-
__exportStar(require("./decorators"), exports);
|
|
20
|
-
__exportStar(require("./
|
|
21
|
-
__exportStar(require("./
|
|
22
|
-
__exportStar(require("./
|
|
23
|
-
__exportStar(require("./
|
|
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("./types.cjs"), exports);
|
|
23
|
+
__exportStar(require("./validation.cjs"), exports);
|
package/lib/model/index.d.ts
CHANGED