@decaf-ts/decorator-validation 1.5.7 → 1.5.8

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.
@@ -1,6 +1,71 @@
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
+ /**
5
+ * @summary For Serialization/deserialization purposes.
6
+ * @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
7
+ *
8
+ * @function isModel
9
+ * @memberOf module:decorator-validation.Validation
10
+ * @category Validation
11
+ */
12
+ export declare function isModel(target: Record<string, any>): boolean;
13
+ /**
14
+ * @summary ModelRegistry Interface
15
+ *
16
+ * @interface ModelRegistry
17
+ * @extends BuilderRegistry<Model>
18
+ *
19
+ * @category Model
20
+ */
21
+ export type ModelRegistry<T extends Model> = BuilderRegistry<T>;
22
+ /**
23
+ * @summary Util class to enable serialization and correct rebuilding
24
+ *
25
+ * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
26
+ * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
27
+ *
28
+ * @class ModelRegistryManager
29
+ * @implements ModelRegistry
30
+ *
31
+ * @category Model
32
+ */
33
+ export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
34
+ private cache;
35
+ private readonly testFunction;
36
+ constructor(testFunction?: (obj: Record<string, any>) => boolean);
37
+ /**
38
+ * @summary register new Models
39
+ * @param {any} constructor
40
+ * @param {string} [name] when not defined, the name of the constructor will be used
41
+ */
42
+ register(constructor: ModelConstructor<T>, name?: string): void;
43
+ /**
44
+ * @summary Gets a registered Model {@link ModelConstructor}
45
+ * @param {string} name
46
+ */
47
+ get(name: string): ModelConstructor<T> | undefined;
48
+ /**
49
+ * @param {Record<string, any>} obj
50
+ * @param {string} [clazz] when provided, it will attempt to find the matching constructor
51
+ *
52
+ * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
53
+ */
54
+ build(obj?: Record<string, any>, clazz?: string): T;
55
+ }
56
+ /**
57
+ * @summary Bulk Registers Models
58
+ * @description Useful when using bundlers that might not evaluate all the code at once
59
+ *
60
+ * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
61
+ *
62
+ * @memberOf module:decorator-validation.Model
63
+ * @category Model
64
+ */
65
+ export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
66
+ name: string;
67
+ constructor: Constructor<T>;
68
+ })[]): void;
4
69
  /**
5
70
  * @summary Abstract class representing a Validatable Model object
6
71
  * @description Meant to be used as a base class for all Model classes
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Model = void 0;
3
+ exports.Model = exports.ModelRegistryManager = void 0;
4
+ exports.isModel = isModel;
5
+ exports.bulkModelRegister = bulkModelRegister;
4
6
  const serialization_1 = require("../utils/serialization");
5
- const Registry_1 = require("./Registry");
6
7
  const reflection_1 = require("@decaf-ts/reflection");
7
8
  const validation_1 = require("./validation");
8
9
  const hashing_1 = require("../utils/hashing");
@@ -13,6 +14,94 @@ const strings_1 = require("../utils/strings");
13
14
  const constants_3 = require("./constants");
14
15
  let modelBuilderFunction;
15
16
  let actingModelRegistry;
17
+ /**
18
+ * @summary For Serialization/deserialization purposes.
19
+ * @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
20
+ *
21
+ * @function isModel
22
+ * @memberOf module:decorator-validation.Validation
23
+ * @category Validation
24
+ */
25
+ function isModel(target) {
26
+ try {
27
+ return target instanceof Model || !!Model.getMetadata(target);
28
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
29
+ }
30
+ catch (e) {
31
+ return false;
32
+ }
33
+ }
34
+ /**
35
+ * @summary Util class to enable serialization and correct rebuilding
36
+ *
37
+ * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
38
+ * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
39
+ *
40
+ * @class ModelRegistryManager
41
+ * @implements ModelRegistry
42
+ *
43
+ * @category Model
44
+ */
45
+ class ModelRegistryManager {
46
+ constructor(testFunction = isModel) {
47
+ this.cache = {};
48
+ this.testFunction = testFunction;
49
+ }
50
+ /**
51
+ * @summary register new Models
52
+ * @param {any} constructor
53
+ * @param {string} [name] when not defined, the name of the constructor will be used
54
+ */
55
+ register(constructor, name) {
56
+ if (typeof constructor !== "function")
57
+ throw new Error("Model registering failed. Missing Class name or constructor");
58
+ name = name || constructor.name;
59
+ this.cache[name] = constructor;
60
+ }
61
+ /**
62
+ * @summary Gets a registered Model {@link ModelConstructor}
63
+ * @param {string} name
64
+ */
65
+ get(name) {
66
+ try {
67
+ return this.cache[name];
68
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
69
+ }
70
+ catch (e) {
71
+ return undefined;
72
+ }
73
+ }
74
+ /**
75
+ * @param {Record<string, any>} obj
76
+ * @param {string} [clazz] when provided, it will attempt to find the matching constructor
77
+ *
78
+ * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
79
+ */
80
+ build(obj = {}, clazz) {
81
+ if (!clazz && !this.testFunction(obj))
82
+ throw new Error("Provided obj is not a Model object");
83
+ const name = clazz || Model.getMetadata(obj);
84
+ if (!(name in this.cache))
85
+ throw new Error((0, strings_1.sf)("Provided class {0} is not a registered Model object", name));
86
+ return new this.cache[name](obj);
87
+ }
88
+ }
89
+ exports.ModelRegistryManager = ModelRegistryManager;
90
+ /**
91
+ * @summary Bulk Registers Models
92
+ * @description Useful when using bundlers that might not evaluate all the code at once
93
+ *
94
+ * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
95
+ *
96
+ * @memberOf module:decorator-validation.Model
97
+ * @category Model
98
+ */
99
+ function bulkModelRegister(...models) {
100
+ models.forEach((m) => {
101
+ const constructor = (m.constructor ? m.constructor : m);
102
+ Model.register(constructor, m.name);
103
+ });
104
+ }
16
105
  /**
17
106
  * @summary Abstract class representing a Validatable Model object
18
107
  * @description Meant to be used as a base class for all Model classes
@@ -209,7 +298,7 @@ class Model {
209
298
  */
210
299
  static getRegistry() {
211
300
  if (!actingModelRegistry)
212
- actingModelRegistry = new Registry_1.ModelRegistryManager();
301
+ actingModelRegistry = new ModelRegistryManager();
213
302
  return actingModelRegistry;
214
303
  }
215
304
  /**
@@ -3,7 +3,6 @@ export * from "./construction";
3
3
  export * from "./decorators";
4
4
  export * from "./Model";
5
5
  export * from "./ModelErrorDefinition";
6
- export * from "./Registry";
7
6
  export * from "./types";
8
7
  export * from "./utils";
9
8
  export * from "./validation";
@@ -19,7 +19,6 @@ __exportStar(require("./construction"), exports);
19
19
  __exportStar(require("./decorators"), exports);
20
20
  __exportStar(require("./Model"), exports);
21
21
  __exportStar(require("./ModelErrorDefinition"), exports);
22
- __exportStar(require("./Registry"), exports);
23
22
  __exportStar(require("./types"), exports);
24
23
  __exportStar(require("./utils"), exports);
25
24
  __exportStar(require("./validation"), exports);
@@ -1,11 +1,2 @@
1
1
  import { Model } from "./Model";
2
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;
@@ -1,29 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isPropertyModel = isPropertyModel;
4
- exports.isModel = isModel;
5
4
  const constants_1 = require("../utils/constants");
6
5
  const Model_1 = require("./Model");
7
6
  function isPropertyModel(target, attribute) {
8
- if (isModel(target[attribute]))
7
+ if ((0, Model_1.isModel)(target[attribute]))
9
8
  return true;
10
9
  const metadata = Reflect.getMetadata(constants_1.ModelKeys.TYPE, target, attribute);
11
10
  return Model_1.Model.get(metadata.name) ? metadata.name : undefined;
12
11
  }
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
- }
@@ -6,9 +6,9 @@ const reflection_1 = require("@decaf-ts/reflection");
6
6
  const constants_1 = require("../utils/constants");
7
7
  const strings_1 = require("../utils/strings");
8
8
  const constants_2 = require("./constants");
9
+ const Model_1 = require("./Model");
9
10
  const Validation_1 = require("../validation/Validation");
10
11
  const constants_3 = require("../validation/Validators/constants");
11
- const utils_1 = require("./utils");
12
12
  /**
13
13
  * @summary Analyses the decorations of the properties and validates the obj according to them
14
14
  *
@@ -48,7 +48,7 @@ function validate(obj, ...propsToIgnore) {
48
48
  }
49
49
  const decoratorProps = decorator.key === constants_1.ModelKeys.TYPE
50
50
  ? [decorator.props]
51
- : (decorator.props || {});
51
+ : decorator.props || {};
52
52
  const err = validator.hasErrors(obj[prop.toString()], decoratorProps);
53
53
  if (err) {
54
54
  errs = errs || {};
@@ -93,7 +93,7 @@ function validate(obj, ...propsToIgnore) {
93
93
  }
94
94
  const validate = (prop, value) => {
95
95
  if (typeof value === "object" || typeof value === "function")
96
- return (0, utils_1.isModel)(value)
96
+ return (0, Model_1.isModel)(value)
97
97
  ? value.hasErrors()
98
98
  : allowedTypes.includes(typeof value)
99
99
  ? undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decaf-ts/decorator-validation",
3
- "version": "1.5.7",
3
+ "version": "1.5.8",
4
4
  "description": "simple decorator based validation engine",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,59 +0,0 @@
1
- import { Model } from "./Model";
2
- import { BuilderRegistry } from "../utils/registry";
3
- import { Constructor, ModelConstructor } from "./types";
4
- /**
5
- * @summary ModelRegistry Interface
6
- *
7
- * @interface ModelRegistry
8
- * @extends BuilderRegistry<Model>
9
- *
10
- * @category Model
11
- */
12
- export type ModelRegistry<T extends Model> = BuilderRegistry<T>;
13
- /**
14
- * @summary Util class to enable serialization and correct rebuilding
15
- *
16
- * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
17
- * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
18
- *
19
- * @class ModelRegistryManager
20
- * @implements ModelRegistry
21
- *
22
- * @category Model
23
- */
24
- export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
25
- private cache;
26
- private readonly testFunction;
27
- constructor(testFunction?: (obj: Record<string, any>) => boolean);
28
- /**
29
- * @summary register new Models
30
- * @param {any} constructor
31
- * @param {string} [name] when not defined, the name of the constructor will be used
32
- */
33
- register(constructor: ModelConstructor<T>, name?: string): void;
34
- /**
35
- * @summary Gets a registered Model {@link ModelConstructor}
36
- * @param {string} name
37
- */
38
- get(name: string): ModelConstructor<T> | undefined;
39
- /**
40
- * @param {Record<string, any>} obj
41
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
42
- *
43
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
44
- */
45
- build(obj?: Record<string, any>, clazz?: string): T;
46
- }
47
- /**
48
- * @summary Bulk Registers Models
49
- * @description Useful when using bundlers that might not evaluate all the code at once
50
- *
51
- * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
52
- *
53
- * @memberOf module:decorator-validation.Model
54
- * @category Model
55
- */
56
- export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
57
- name: string;
58
- constructor: Constructor<T>;
59
- })[]): void;
@@ -1,73 +0,0 @@
1
- import { Model } from "./Model";
2
- import { sf } from "../utils/strings";
3
- import { isModel } from "./utils";
4
- /**
5
- * @summary Util class to enable serialization and correct rebuilding
6
- *
7
- * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
8
- * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
9
- *
10
- * @class ModelRegistryManager
11
- * @implements ModelRegistry
12
- *
13
- * @category Model
14
- */
15
- export class ModelRegistryManager {
16
- constructor(testFunction = isModel) {
17
- this.cache = {};
18
- this.testFunction = testFunction;
19
- }
20
- /**
21
- * @summary register new Models
22
- * @param {any} constructor
23
- * @param {string} [name] when not defined, the name of the constructor will be used
24
- */
25
- register(constructor, name) {
26
- if (typeof constructor !== "function")
27
- throw new Error("Model registering failed. Missing Class name or constructor");
28
- name = name || constructor.name;
29
- this.cache[name] = constructor;
30
- }
31
- /**
32
- * @summary Gets a registered Model {@link ModelConstructor}
33
- * @param {string} name
34
- */
35
- get(name) {
36
- try {
37
- return this.cache[name];
38
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
39
- }
40
- catch (e) {
41
- return undefined;
42
- }
43
- }
44
- /**
45
- * @param {Record<string, any>} obj
46
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
47
- *
48
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
49
- */
50
- build(obj = {}, clazz) {
51
- if (!clazz && !this.testFunction(obj))
52
- throw new Error("Provided obj is not a Model object");
53
- const name = clazz || Model.getMetadata(obj);
54
- if (!(name in this.cache))
55
- throw new Error(sf("Provided class {0} is not a registered Model object", name));
56
- return new this.cache[name](obj);
57
- }
58
- }
59
- /**
60
- * @summary Bulk Registers Models
61
- * @description Useful when using bundlers that might not evaluate all the code at once
62
- *
63
- * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
64
- *
65
- * @memberOf module:decorator-validation.Model
66
- * @category Model
67
- */
68
- export function bulkModelRegister(...models) {
69
- models.forEach((m) => {
70
- const constructor = (m.constructor ? m.constructor : m);
71
- Model.register(constructor, m.name);
72
- });
73
- }
@@ -1,59 +0,0 @@
1
- import { Model } from "./Model";
2
- import { BuilderRegistry } from "../utils/registry";
3
- import { Constructor, ModelConstructor } from "./types";
4
- /**
5
- * @summary ModelRegistry Interface
6
- *
7
- * @interface ModelRegistry
8
- * @extends BuilderRegistry<Model>
9
- *
10
- * @category Model
11
- */
12
- export type ModelRegistry<T extends Model> = BuilderRegistry<T>;
13
- /**
14
- * @summary Util class to enable serialization and correct rebuilding
15
- *
16
- * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
17
- * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
18
- *
19
- * @class ModelRegistryManager
20
- * @implements ModelRegistry
21
- *
22
- * @category Model
23
- */
24
- export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
25
- private cache;
26
- private readonly testFunction;
27
- constructor(testFunction?: (obj: Record<string, any>) => boolean);
28
- /**
29
- * @summary register new Models
30
- * @param {any} constructor
31
- * @param {string} [name] when not defined, the name of the constructor will be used
32
- */
33
- register(constructor: ModelConstructor<T>, name?: string): void;
34
- /**
35
- * @summary Gets a registered Model {@link ModelConstructor}
36
- * @param {string} name
37
- */
38
- get(name: string): ModelConstructor<T> | undefined;
39
- /**
40
- * @param {Record<string, any>} obj
41
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
42
- *
43
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
44
- */
45
- build(obj?: Record<string, any>, clazz?: string): T;
46
- }
47
- /**
48
- * @summary Bulk Registers Models
49
- * @description Useful when using bundlers that might not evaluate all the code at once
50
- *
51
- * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
52
- *
53
- * @memberOf module:decorator-validation.Model
54
- * @category Model
55
- */
56
- export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
57
- name: string;
58
- constructor: Constructor<T>;
59
- })[]): void;
@@ -1,78 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelRegistryManager = void 0;
4
- exports.bulkModelRegister = bulkModelRegister;
5
- const Model_1 = require("./Model");
6
- const strings_1 = require("../utils/strings");
7
- const utils_1 = require("./utils");
8
- /**
9
- * @summary Util class to enable serialization and correct rebuilding
10
- *
11
- * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
12
- * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
13
- *
14
- * @class ModelRegistryManager
15
- * @implements ModelRegistry
16
- *
17
- * @category Model
18
- */
19
- class ModelRegistryManager {
20
- constructor(testFunction = utils_1.isModel) {
21
- this.cache = {};
22
- this.testFunction = testFunction;
23
- }
24
- /**
25
- * @summary register new Models
26
- * @param {any} constructor
27
- * @param {string} [name] when not defined, the name of the constructor will be used
28
- */
29
- register(constructor, name) {
30
- if (typeof constructor !== "function")
31
- throw new Error("Model registering failed. Missing Class name or constructor");
32
- name = name || constructor.name;
33
- this.cache[name] = constructor;
34
- }
35
- /**
36
- * @summary Gets a registered Model {@link ModelConstructor}
37
- * @param {string} name
38
- */
39
- get(name) {
40
- try {
41
- return this.cache[name];
42
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
- }
44
- catch (e) {
45
- return undefined;
46
- }
47
- }
48
- /**
49
- * @param {Record<string, any>} obj
50
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
51
- *
52
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
53
- */
54
- build(obj = {}, clazz) {
55
- if (!clazz && !this.testFunction(obj))
56
- throw new Error("Provided obj is not a Model object");
57
- const name = clazz || Model_1.Model.getMetadata(obj);
58
- if (!(name in this.cache))
59
- throw new Error((0, strings_1.sf)("Provided class {0} is not a registered Model object", name));
60
- return new this.cache[name](obj);
61
- }
62
- }
63
- exports.ModelRegistryManager = ModelRegistryManager;
64
- /**
65
- * @summary Bulk Registers Models
66
- * @description Useful when using bundlers that might not evaluate all the code at once
67
- *
68
- * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
69
- *
70
- * @memberOf module:decorator-validation.Model
71
- * @category Model
72
- */
73
- function bulkModelRegister(...models) {
74
- models.forEach((m) => {
75
- const constructor = (m.constructor ? m.constructor : m);
76
- Model_1.Model.register(constructor, m.name);
77
- });
78
- }