@decaf-ts/decorator-validation 1.0.7
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 +21 -0
- package/README.md +302 -0
- package/dist/decorator-validation.bundle.min.js +2 -0
- package/dist/decorator-validation.bundle.min.js.LICENSE.txt +14 -0
- package/dist/esm/decorator-validation.bundle.min.esm.js +2 -0
- package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +14 -0
- package/lib/esm/index.d.ts +61 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/model/Model.d.ts +160 -0
- package/lib/esm/model/Model.js +1 -0
- package/lib/esm/model/ModelErrorDefinition.d.ts +22 -0
- package/lib/esm/model/ModelErrorDefinition.js +3 -0
- package/lib/esm/model/Registry.d.ts +61 -0
- package/lib/esm/model/Registry.js +1 -0
- package/lib/esm/model/constants.d.ts +40 -0
- package/lib/esm/model/constants.js +1 -0
- package/lib/esm/model/construction.d.ts +45 -0
- package/lib/esm/model/construction.js +1 -0
- package/lib/esm/model/decorators.d.ts +33 -0
- package/lib/esm/model/decorators.js +1 -0
- package/lib/esm/model/index.d.ts +7 -0
- package/lib/esm/model/index.js +1 -0
- package/lib/esm/model/types.d.ts +67 -0
- package/lib/esm/model/types.js +0 -0
- package/lib/esm/utils/constants.d.ts +22 -0
- package/lib/esm/utils/constants.js +1 -0
- package/lib/esm/utils/general.d.ts +193 -0
- package/lib/esm/utils/general.js +1 -0
- package/lib/esm/utils/index.d.ts +4 -0
- package/lib/esm/utils/index.js +1 -0
- package/lib/esm/utils/registry.d.ts +68 -0
- package/lib/esm/utils/registry.js +0 -0
- package/lib/esm/utils/serialization.d.ts +65 -0
- package/lib/esm/utils/serialization.js +1 -0
- package/lib/esm/validation/Validation.d.ts +39 -0
- package/lib/esm/validation/Validation.js +1 -0
- package/lib/esm/validation/Validators/DateValidator.d.ts +29 -0
- package/lib/esm/validation/Validators/DateValidator.js +1 -0
- package/lib/esm/validation/Validators/EmailValidator.d.ts +29 -0
- package/lib/esm/validation/Validators/EmailValidator.js +1 -0
- package/lib/esm/validation/Validators/ListValidator.d.ts +29 -0
- package/lib/esm/validation/Validators/ListValidator.js +1 -0
- package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +30 -0
- package/lib/esm/validation/Validators/MaxLengthValidator.js +1 -0
- package/lib/esm/validation/Validators/MaxValidator.d.ts +29 -0
- package/lib/esm/validation/Validators/MaxValidator.js +1 -0
- package/lib/esm/validation/Validators/MinLengthValidator.d.ts +30 -0
- package/lib/esm/validation/Validators/MinLengthValidator.js +1 -0
- package/lib/esm/validation/Validators/MinValidator.d.ts +29 -0
- package/lib/esm/validation/Validators/MinValidator.js +1 -0
- package/lib/esm/validation/Validators/PasswordValidator.d.ts +31 -0
- package/lib/esm/validation/Validators/PasswordValidator.js +1 -0
- package/lib/esm/validation/Validators/PatternValidator.d.ts +38 -0
- package/lib/esm/validation/Validators/PatternValidator.js +1 -0
- package/lib/esm/validation/Validators/RequiredValidator.d.ts +28 -0
- package/lib/esm/validation/Validators/RequiredValidator.js +1 -0
- package/lib/esm/validation/Validators/StepValidator.d.ts +29 -0
- package/lib/esm/validation/Validators/StepValidator.js +1 -0
- package/lib/esm/validation/Validators/TypeValidator.d.ts +28 -0
- package/lib/esm/validation/Validators/TypeValidator.js +1 -0
- package/lib/esm/validation/Validators/URLValidator.d.ts +28 -0
- package/lib/esm/validation/Validators/URLValidator.js +1 -0
- package/lib/esm/validation/Validators/Validator.d.ts +42 -0
- package/lib/esm/validation/Validators/Validator.js +1 -0
- package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +47 -0
- package/lib/esm/validation/Validators/ValidatorRegistry.js +1 -0
- package/lib/esm/validation/Validators/constants.d.ts +91 -0
- package/lib/esm/validation/Validators/constants.js +1 -0
- package/lib/esm/validation/Validators/index.d.ts +50 -0
- package/lib/esm/validation/Validators/index.js +1 -0
- package/lib/esm/validation/Validators/types.d.ts +51 -0
- package/lib/esm/validation/Validators/types.js +0 -0
- package/lib/esm/validation/decorators.d.ts +263 -0
- package/lib/esm/validation/decorators.js +1 -0
- package/lib/esm/validation/index.d.ts +13 -0
- package/lib/esm/validation/index.js +1 -0
- package/lib/esm/validation/types.d.ts +53 -0
- package/lib/esm/validation/types.js +0 -0
- package/lib/index.cjs +1 -0
- package/lib/index.d.ts +61 -0
- package/lib/model/Model.cjs +1 -0
- package/lib/model/Model.d.ts +160 -0
- package/lib/model/ModelErrorDefinition.cjs +3 -0
- package/lib/model/ModelErrorDefinition.d.ts +22 -0
- package/lib/model/Registry.cjs +1 -0
- package/lib/model/Registry.d.ts +61 -0
- package/lib/model/constants.cjs +1 -0
- package/lib/model/constants.d.ts +40 -0
- package/lib/model/construction.cjs +1 -0
- package/lib/model/construction.d.ts +45 -0
- package/lib/model/decorators.cjs +1 -0
- package/lib/model/decorators.d.ts +33 -0
- package/lib/model/index.cjs +1 -0
- package/lib/model/index.d.ts +7 -0
- package/lib/model/types.cjs +1 -0
- package/lib/model/types.d.ts +67 -0
- package/lib/utils/constants.cjs +1 -0
- package/lib/utils/constants.d.ts +22 -0
- package/lib/utils/general.cjs +1 -0
- package/lib/utils/general.d.ts +193 -0
- package/lib/utils/index.cjs +1 -0
- package/lib/utils/index.d.ts +4 -0
- package/lib/utils/registry.cjs +1 -0
- package/lib/utils/registry.d.ts +68 -0
- package/lib/utils/serialization.cjs +1 -0
- package/lib/utils/serialization.d.ts +65 -0
- package/lib/validation/Validation.cjs +1 -0
- package/lib/validation/Validation.d.ts +39 -0
- package/lib/validation/Validators/DateValidator.cjs +1 -0
- package/lib/validation/Validators/DateValidator.d.ts +29 -0
- package/lib/validation/Validators/EmailValidator.cjs +1 -0
- package/lib/validation/Validators/EmailValidator.d.ts +29 -0
- package/lib/validation/Validators/ListValidator.cjs +1 -0
- package/lib/validation/Validators/ListValidator.d.ts +29 -0
- package/lib/validation/Validators/MaxLengthValidator.cjs +1 -0
- package/lib/validation/Validators/MaxLengthValidator.d.ts +30 -0
- package/lib/validation/Validators/MaxValidator.cjs +1 -0
- package/lib/validation/Validators/MaxValidator.d.ts +29 -0
- package/lib/validation/Validators/MinLengthValidator.cjs +1 -0
- package/lib/validation/Validators/MinLengthValidator.d.ts +30 -0
- package/lib/validation/Validators/MinValidator.cjs +1 -0
- package/lib/validation/Validators/MinValidator.d.ts +29 -0
- package/lib/validation/Validators/PasswordValidator.cjs +1 -0
- package/lib/validation/Validators/PasswordValidator.d.ts +31 -0
- package/lib/validation/Validators/PatternValidator.cjs +1 -0
- package/lib/validation/Validators/PatternValidator.d.ts +38 -0
- package/lib/validation/Validators/RequiredValidator.cjs +1 -0
- package/lib/validation/Validators/RequiredValidator.d.ts +28 -0
- package/lib/validation/Validators/StepValidator.cjs +1 -0
- package/lib/validation/Validators/StepValidator.d.ts +29 -0
- package/lib/validation/Validators/TypeValidator.cjs +1 -0
- package/lib/validation/Validators/TypeValidator.d.ts +28 -0
- package/lib/validation/Validators/URLValidator.cjs +1 -0
- package/lib/validation/Validators/URLValidator.d.ts +28 -0
- package/lib/validation/Validators/Validator.cjs +1 -0
- package/lib/validation/Validators/Validator.d.ts +42 -0
- package/lib/validation/Validators/ValidatorRegistry.cjs +1 -0
- package/lib/validation/Validators/ValidatorRegistry.d.ts +47 -0
- package/lib/validation/Validators/constants.cjs +1 -0
- package/lib/validation/Validators/constants.d.ts +91 -0
- package/lib/validation/Validators/index.cjs +1 -0
- package/lib/validation/Validators/index.d.ts +50 -0
- package/lib/validation/Validators/types.cjs +1 -0
- package/lib/validation/Validators/types.d.ts +51 -0
- package/lib/validation/decorators.cjs +1 -0
- package/lib/validation/decorators.d.ts +263 -0
- package/lib/validation/index.cjs +1 -0
- package/lib/validation/index.d.ts +13 -0
- package/lib/validation/types.cjs +1 -0
- package/lib/validation/types.d.ts +53 -0
- package/package.json +96 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module decorator-validation
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @summary Defines the Model object
|
|
6
|
+
* @namespace Model
|
|
7
|
+
* @memberOf module:decorator-validation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @summary Holds all the supported decorators
|
|
11
|
+
* @namespace Decorators
|
|
12
|
+
* @memberOf module:decorator-validation
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* @namespace Validation
|
|
16
|
+
* @memberOf module:decorator-validation.Decorators
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* @namespace Model
|
|
20
|
+
* @memberOf module:decorator-validation.Decorators
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* @namespace Construction
|
|
24
|
+
* @memberOf module:decorator-validation
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* @namespace Validation
|
|
28
|
+
* @memberOf module:decorator-validation
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* @namespace Validators
|
|
32
|
+
* @memberOf module:decorator-validation.Validation
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* @namespace Utils
|
|
36
|
+
* @memberOf module:decorator-validation
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* @namespace Dates
|
|
40
|
+
* @memberOf module:decorator-validation.Utils
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* @namespace Equality
|
|
44
|
+
* @memberOf module:decorator-validation.Utils
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* @namespace Hashing
|
|
48
|
+
* @memberOf module:decorator-validation.Utils
|
|
49
|
+
*/
|
|
50
|
+
/**
|
|
51
|
+
* @namespace Serialization
|
|
52
|
+
* @memberOf module:decorator-validation.Utils
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* @namespace Format
|
|
56
|
+
* @memberOf module:decorator-validation.Utils
|
|
57
|
+
*/
|
|
58
|
+
export * from "./utils";
|
|
59
|
+
export * from "./validation";
|
|
60
|
+
export * from "./model";
|
|
61
|
+
export declare const VERSION = "1.0.7";
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./utils";export*from"./validation";export*from"./model";const VERSION="1.0.7";export{VERSION};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { BuilderRegistry, HashingFunction, Serializer } from "../utils";
|
|
2
|
+
import { ModelErrorDefinition } from "./ModelErrorDefinition";
|
|
3
|
+
import { ModelArg, ModelConstructor, Serializable, Validatable } from "./types";
|
|
4
|
+
import { ModelBuilderFunction } from "./construction";
|
|
5
|
+
/**
|
|
6
|
+
* @summary Analyses the decorations of the properties and validates the obj according to them
|
|
7
|
+
*
|
|
8
|
+
* @typedef T extends Model
|
|
9
|
+
* @prop {T} obj Model object to validate
|
|
10
|
+
* @prop {string[]} [propsToIgnore] object properties to ignore in the validation
|
|
11
|
+
*
|
|
12
|
+
* @function validate
|
|
13
|
+
* @memberOf module:decorator-validation.Validation
|
|
14
|
+
* @category Validation
|
|
15
|
+
*/
|
|
16
|
+
export declare function validate<T extends Model>(obj: T, ...propsToIgnore: string[]): ModelErrorDefinition | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* @summary Abstract class representing a Validatable Model object
|
|
19
|
+
* @description Meant to be used as a base class for all Model classes
|
|
20
|
+
*
|
|
21
|
+
* Model objects must:
|
|
22
|
+
* - Have all their properties defined as optional;
|
|
23
|
+
* - Have all their properties initialized eg:
|
|
24
|
+
*
|
|
25
|
+
* <pre>
|
|
26
|
+
* class ClassName {
|
|
27
|
+
* propertyName?: PropertyType = undefined;
|
|
28
|
+
* }
|
|
29
|
+
* </pre>
|
|
30
|
+
*
|
|
31
|
+
* @param {Model | {}} model base object from which to populate properties from
|
|
32
|
+
*
|
|
33
|
+
* @class Model
|
|
34
|
+
* @abstract
|
|
35
|
+
* @implements Validatable
|
|
36
|
+
* @implements Serializable
|
|
37
|
+
*
|
|
38
|
+
* @category Model
|
|
39
|
+
*/
|
|
40
|
+
export declare abstract class Model implements Validatable, Serializable {
|
|
41
|
+
[indexer: string]: any;
|
|
42
|
+
protected constructor(model?: ModelArg<Model>);
|
|
43
|
+
/**
|
|
44
|
+
* @summary Validates the object according to its decorated properties
|
|
45
|
+
*
|
|
46
|
+
* @param {any[]} [exceptions] properties in the object to be ignored for he validation. Marked as 'any' to allow for extension but expects strings
|
|
47
|
+
*/
|
|
48
|
+
hasErrors(...exceptions: any[]): ModelErrorDefinition | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* @summary Compare object equality recursively
|
|
51
|
+
* @param {any} obj object to compare to
|
|
52
|
+
* @param {string} [exceptions] property names to be excluded from the comparison
|
|
53
|
+
*/
|
|
54
|
+
equals(obj: any, ...exceptions: string[]): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* @summary Returns the serialized model according to the currently defined {@link Serializer}
|
|
57
|
+
*/
|
|
58
|
+
serialize(): string;
|
|
59
|
+
/**
|
|
60
|
+
* @summary Override the implementation for js's 'toString()' which sucks...
|
|
61
|
+
* @override
|
|
62
|
+
*/
|
|
63
|
+
toString(): string;
|
|
64
|
+
/**
|
|
65
|
+
* @summary Defines a default implementation for object hash. Relies on a very basic implementation based on Java's string hash;
|
|
66
|
+
*/
|
|
67
|
+
toHash(): string;
|
|
68
|
+
/**
|
|
69
|
+
* @summary Deserializes a Model
|
|
70
|
+
* @param {string} str
|
|
71
|
+
*
|
|
72
|
+
* @throws {Error} If it fails to parse the string, or if it fails to build the model
|
|
73
|
+
*/
|
|
74
|
+
static deserialize(str: string): any;
|
|
75
|
+
/**
|
|
76
|
+
* @summary Serializes a Model
|
|
77
|
+
* @param {Model} model
|
|
78
|
+
*/
|
|
79
|
+
static serialize(model: any): string;
|
|
80
|
+
static hash(obj: any): string;
|
|
81
|
+
/**
|
|
82
|
+
* @summary Wrapper around {@link constructFromObject}
|
|
83
|
+
* @param {T} self
|
|
84
|
+
* @param {T | Record<string, any>} obj
|
|
85
|
+
*/
|
|
86
|
+
static fromObject<T extends Model>(self: T, obj?: T | Record<string, any>): T;
|
|
87
|
+
/**
|
|
88
|
+
* @summary Wrapper around {@link constructFromModel}
|
|
89
|
+
* @param {T} self
|
|
90
|
+
* @param {T | Record<string, any>} obj
|
|
91
|
+
*/
|
|
92
|
+
static fromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
|
|
93
|
+
/**
|
|
94
|
+
* @summary Sets the Global {@link ModelBuilderFunction}
|
|
95
|
+
* @param {ModelBuilderFunction} [builder]
|
|
96
|
+
*/
|
|
97
|
+
static setBuilder(builder?: ModelBuilderFunction): void;
|
|
98
|
+
/**
|
|
99
|
+
* @summary Retrieves the current global {@link ModelBuilderFunction}
|
|
100
|
+
*/
|
|
101
|
+
static getBuilder(): ModelBuilderFunction | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the current {@link ModelRegistryManager}
|
|
104
|
+
*
|
|
105
|
+
* @return ModelRegistry, defaults to {@link ModelRegistryManager}
|
|
106
|
+
*/
|
|
107
|
+
private static getRegistry;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the current actingModelRegistry
|
|
110
|
+
*
|
|
111
|
+
* @param {BuilderRegistry} modelRegistry the new implementation of Registry
|
|
112
|
+
*/
|
|
113
|
+
static setRegistry(modelRegistry: BuilderRegistry<any>): void;
|
|
114
|
+
/**
|
|
115
|
+
* @summary register new Models
|
|
116
|
+
* @param {any} constructor
|
|
117
|
+
* @param {string} [name] when not defined, the name of the constructor will be used
|
|
118
|
+
*
|
|
119
|
+
* @see ModelRegistry
|
|
120
|
+
*/
|
|
121
|
+
static register<T extends Model>(constructor: ModelConstructor<T>, name?: string): void;
|
|
122
|
+
/**
|
|
123
|
+
* @summary Gets a registered Model {@link ModelConstructor}
|
|
124
|
+
* @param {string} name
|
|
125
|
+
*
|
|
126
|
+
* @see ModelRegistry
|
|
127
|
+
*/
|
|
128
|
+
static get<T extends Model>(name: string): ModelConstructor<T> | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* @param {Record<string, any>} obj
|
|
131
|
+
* @param {string} [clazz] when provided, it will attempt to find the matching constructor
|
|
132
|
+
*
|
|
133
|
+
* @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
|
|
134
|
+
*
|
|
135
|
+
* @see ModelRegistry
|
|
136
|
+
*/
|
|
137
|
+
static build<T extends Model>(obj?: Record<string, any>, clazz?: string): T;
|
|
138
|
+
/**
|
|
139
|
+
* @summary Sets the {@link Serializer}
|
|
140
|
+
*
|
|
141
|
+
* @param {Serializer} ser
|
|
142
|
+
*/
|
|
143
|
+
static setSerializer(ser: Serializer<any>): void;
|
|
144
|
+
/**
|
|
145
|
+
* @summary Retrieves the current defined {@link Serializer}
|
|
146
|
+
*
|
|
147
|
+
*/
|
|
148
|
+
private static getSerializer;
|
|
149
|
+
/**
|
|
150
|
+
* @summary Sets the {@link HashingFunction}
|
|
151
|
+
*
|
|
152
|
+
* @param {HashingFunction} hasher
|
|
153
|
+
*/
|
|
154
|
+
static setHashingFunction(hasher: HashingFunction): void;
|
|
155
|
+
/**
|
|
156
|
+
* @summary Retrieves the current defined {@link HashingFunction}
|
|
157
|
+
*
|
|
158
|
+
*/
|
|
159
|
+
private static getHashingFunction;
|
|
160
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{DEFAULT_ERROR_MESSAGES,ValidationKeys}from"../validation";import{getPropertyDecorators,hashObj,isEqual,JSONSerializer,stringFormat}from"../utils";import{ModelErrorDefinition}from"./ModelErrorDefinition";import{ReservedModels}from"./constants";import{ModelKeys}from"../utils/constants";import{constructFromModel,constructFromObject}from"./construction";import{ModelRegistryManager}from"./Registry";import{Validation}from"../validation/Validation";function validate(a,...e){var t=[];for(const i in a)a.hasOwnProperty(i)&&-1===e.indexOf(i)&&t.push(getPropertyDecorators(ValidationKeys.REFLECT,a,i));var r=t.reduce((e,r)=>{const{prop:i,decorators:o}=r;if(o&&o.length){const s=o[0];o.find(e=>e.key===ValidationKeys.TYPE||!!e.props.types?.find(e=>e===s.props.name))&&o.shift();let t=o.reduce((e,t)=>{var r=Validation.get(t.key);return r&&(r=r.hasErrors(a[i.toString()],...t.key===ModelKeys.TYPE?[t.props]:Object.values(t.props)))&&((e=e||{})[t.key]=r),e},void 0);(t=t||Object.keys(a).filter(e=>!t||!t[e]).reduce((e,r)=>{let i;const o=getPropertyDecorators(ValidationKeys.REFLECT,a,r).decorators;var t=o.filter(e=>-1!==[ModelKeys.TYPE,ValidationKeys.TYPE].indexOf(e.key));if(t&&t.length){t=t.pop(),t=t.props.name?[t.props.name]:Array.isArray(t.props.customTypes)?t.props.customTypes:[t.props.customTypes];const s=Object.values(ReservedModels).map(e=>e.toLowerCase());t.forEach(e=>{if(-1===s.indexOf(e.toLowerCase()))switch(e){case"Array":case"Set":var t;o.length&&o.find(e=>e.key===ValidationKeys.LIST)&&(t="Array"===e?a[r].find(e=>e.hasErrors()):a[r].values().find(e=>e.hasErrors()))&&(i=stringFormat(DEFAULT_ERROR_MESSAGES.LIST_INSIDE,t.toString()));break;default:try{a[r]&&(i=a[r].hasErrors())}catch(e){console.warn(stringFormat("Model should be validatable but its not"))}}}),i&&((e=e||{})[r]=i)}return e},void 0))&&((e=e||{})[r.prop.toString()]=t)}return e},void 0);return r?new ModelErrorDefinition(r):void 0}let modelBuilderFunction,actingModelRegistry,serializer,hashingFunction;class Model{constructor(e){}hasErrors(...e){return validate(this,...e)}equals(e,...t){return isEqual(this,e,...t)}serialize(){return Model.serialize(this)}toString(){return this.constructor.name+": "+JSON.stringify(this,void 0,2)}toHash(){return Model.getHashingFunction()(this).toString()}static deserialize(e){return Model.getSerializer().deserialize(e)}static serialize(e){return Model.getSerializer().serialize(e)}static hash(e){return Model.getHashingFunction()(e)}static fromObject(e,t){return constructFromObject(e,t)}static fromModel(e,t){return constructFromModel(e,t)}static setBuilder(e){modelBuilderFunction=e}static getBuilder(){return modelBuilderFunction}static getRegistry(){return actingModelRegistry=actingModelRegistry||new ModelRegistryManager}static setRegistry(e){actingModelRegistry=e}static register(e,t){return Model.getRegistry().register(e,t)}static get(e){return Model.getRegistry().get(e)}static build(e={},t){return Model.getRegistry().build(e,t)}static setSerializer(e){serializer=e}static getSerializer(){return serializer=serializer||new JSONSerializer}static setHashingFunction(e){hashingFunction=e}static getHashingFunction(){return hashingFunction=hashingFunction||hashObj}}export{validate,Model};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Errors, ModelErrors } from "../validation";
|
|
2
|
+
/**
|
|
3
|
+
* @summary Helper Class to hold the error results
|
|
4
|
+
* @description holds error results in an 'indexable' manner
|
|
5
|
+
* while still providing the same result on toString
|
|
6
|
+
*
|
|
7
|
+
* @param {ModelErrors} errors
|
|
8
|
+
*
|
|
9
|
+
* @class ModelErrorDefinition
|
|
10
|
+
*
|
|
11
|
+
* @category Model
|
|
12
|
+
*/
|
|
13
|
+
export declare class ModelErrorDefinition {
|
|
14
|
+
[indexer: string]: Record<string, Errors> | (() => Errors);
|
|
15
|
+
constructor(errors: ModelErrors);
|
|
16
|
+
/**
|
|
17
|
+
* @summary Outputs the class to a nice readable string
|
|
18
|
+
*
|
|
19
|
+
* @override
|
|
20
|
+
*/
|
|
21
|
+
toString(): string;
|
|
22
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
class ModelErrorDefinition{constructor(e){for(const r in e)e.hasOwnProperty(r)&&e[r]&&Object.defineProperty(this,r,{enumerable:!0,configurable:!1,value:e[r],writable:!1})}toString(){const o=this;return Object.keys(o).filter(e=>o.hasOwnProperty(e)&&"function"!=typeof o[e]).reduce((e,t)=>{var r=Object.keys(o[t]).reduce((e,r)=>(e?e+=`
|
|
2
|
+
`+o[t][r]:e=o[t][r],e),void 0);return r&&(r=t+" - "+r,e?e+=`
|
|
3
|
+
`+r:e=r),e},"")}}export{ModelErrorDefinition};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Model } from "./Model";
|
|
2
|
+
import { BuilderRegistry } from "../utils";
|
|
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 interface ModelRegistry<T extends Model> extends BuilderRegistry<T> {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @summary Util class to enable serialization and correct rebuilding
|
|
16
|
+
*
|
|
17
|
+
* @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
|
|
18
|
+
* @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
|
|
19
|
+
*
|
|
20
|
+
* @class ModelRegistryManager
|
|
21
|
+
* @implements ModelRegistry
|
|
22
|
+
*
|
|
23
|
+
* @category Model
|
|
24
|
+
*/
|
|
25
|
+
export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
|
|
26
|
+
private cache;
|
|
27
|
+
private readonly testFunction;
|
|
28
|
+
private readonly anchorKey;
|
|
29
|
+
constructor(anchorKey?: string, testFunction?: (obj: Record<string, any>) => boolean);
|
|
30
|
+
/**
|
|
31
|
+
* @summary register new Models
|
|
32
|
+
* @param {any} constructor
|
|
33
|
+
* @param {string} [name] when not defined, the name of the constructor will be used
|
|
34
|
+
*/
|
|
35
|
+
register(constructor: ModelConstructor<T>, name?: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* @summary Gets a registered Model {@link ModelConstructor}
|
|
38
|
+
* @param {string} name
|
|
39
|
+
*/
|
|
40
|
+
get(name: string): ModelConstructor<T> | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* @param {Record<string, any>} obj
|
|
43
|
+
* @param {string} [clazz] when provided, it will attempt to find the matching constructor
|
|
44
|
+
*
|
|
45
|
+
* @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
|
|
46
|
+
*/
|
|
47
|
+
build(obj?: Record<string, any>, clazz?: string): T;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @summary Bulk Registers Models
|
|
51
|
+
* @description Useful when using bundlers that might not evaluate all of the code at once
|
|
52
|
+
*
|
|
53
|
+
* @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
|
|
54
|
+
*
|
|
55
|
+
* @memberOf module:decorator-validation.Model
|
|
56
|
+
* @category Model
|
|
57
|
+
*/
|
|
58
|
+
export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
|
|
59
|
+
name: string;
|
|
60
|
+
constructor: Constructor<T>;
|
|
61
|
+
})[]): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Model}from"./Model";import{isModel,stringFormat}from"../utils";import{ModelKeys}from"../utils/constants";class ModelRegistryManager{constructor(e=ModelKeys.ANCHOR,t=isModel){this.cache={},this.testFunction=t,this.anchorKey=e}register(e,t){if("function"!=typeof e)throw new Error("Model registering failed. Missing Class name or constructor");t=t||e.name,this.cache[t]=e}get(e){try{return this.cache[e]}catch(e){}}build(e={},t){if(!t&&!this.testFunction(e))throw new Error("Provided obj is not a Model object");t=t||e[this.anchorKey].class;if(t in this.cache)return new this.cache[t](e);throw new Error(stringFormat("Provided class {0} is not a registered Model object",t))}}function bulkModelRegister(...e){e.forEach(e=>{var t=e.constructor||e;Model.register(t,e.name)})}export{ModelRegistryManager,bulkModelRegister};
|
|
@@ -0,0 +1,40 @@
|
|
|
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.Construction
|
|
11
|
+
* @category Model
|
|
12
|
+
*/
|
|
13
|
+
export declare enum Primitives {
|
|
14
|
+
STRING = "string",
|
|
15
|
+
NUMBER = "number",
|
|
16
|
+
BOOLEAN = "boolean",
|
|
17
|
+
BIGINT = "bigint"
|
|
18
|
+
}
|
|
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.Construction
|
|
31
|
+
* @category Construction
|
|
32
|
+
*/
|
|
33
|
+
export declare enum ReservedModels {
|
|
34
|
+
STRING = "string",
|
|
35
|
+
OBJECT = "object",
|
|
36
|
+
NUMBER = "number",
|
|
37
|
+
BOOLEAN = "boolean",
|
|
38
|
+
BIGINT = "bigint",
|
|
39
|
+
DATE = "date"
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var Primitives,ReservedModels;!function(e){e.STRING="string",e.NUMBER="number",e.BOOLEAN="boolean",e.BIGINT="bigint"}(Primitives=Primitives||{}),function(e){e.STRING="string",e.OBJECT="object",e.NUMBER="number",e.BOOLEAN="boolean",e.BIGINT="bigint",e.DATE="date"}(ReservedModels=ReservedModels||{});export{Primitives,ReservedModels};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Model } from "./Model";
|
|
2
|
+
/**
|
|
3
|
+
* @summary Repopulates the Object properties with the ones from the new object
|
|
4
|
+
* @description Iterates all common properties of obj (if existing) and self, and copies them onto self
|
|
5
|
+
*
|
|
6
|
+
* @param {T} self
|
|
7
|
+
* @param {T | Record<string, any>} [obj]
|
|
8
|
+
*
|
|
9
|
+
* @function constructFromObject
|
|
10
|
+
* @memberOf module:decorator-validation.Construction
|
|
11
|
+
* @category Construction
|
|
12
|
+
*/
|
|
13
|
+
export declare function constructFromObject<T extends Model>(self: T, obj?: T | Record<string, any>): T;
|
|
14
|
+
/**
|
|
15
|
+
* @summary Repopulates the instance with the ones from the new Model Object
|
|
16
|
+
* @description Iterates all common properties of obj (if existing) and self, and copies them onto self.
|
|
17
|
+
* Is aware of nested Model Objects and rebuilds them also.
|
|
18
|
+
* When List properties are decorated with {@link list}, they list items will also be rebuilt
|
|
19
|
+
*
|
|
20
|
+
* @param {T} self
|
|
21
|
+
* @param {T | Record<string, any>} [obj]
|
|
22
|
+
*
|
|
23
|
+
* @function constructFromModel
|
|
24
|
+
* @memberOf module:decorator-validation.Construction
|
|
25
|
+
* @category Construction
|
|
26
|
+
*/
|
|
27
|
+
export declare function constructFromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
|
|
28
|
+
/**
|
|
29
|
+
* @summary Helper Function to override constructors
|
|
30
|
+
*
|
|
31
|
+
* @param {Function} constructor
|
|
32
|
+
* @param {any[]} [args]
|
|
33
|
+
* @return {T} the new instance
|
|
34
|
+
*
|
|
35
|
+
* @function construct
|
|
36
|
+
* @memberOf module:decorator-validation.Construction
|
|
37
|
+
* @category Construction
|
|
38
|
+
*/
|
|
39
|
+
export declare function construct<T extends Model>(constructor: any, ...args: any[]): T;
|
|
40
|
+
/**
|
|
41
|
+
* @summary Typo of a Model builder function
|
|
42
|
+
* @memberOf module:decorator-validation.Construction
|
|
43
|
+
* @category Construction
|
|
44
|
+
*/
|
|
45
|
+
export type ModelBuilderFunction = <T extends Model>(self: T, obj?: T | Record<string, any>) => T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Model}from"./Model";import{getPropertyDecorators,isModel,stringFormat}from"../utils";import{ValidationKeys}from"../validation";import{ReservedModels}from"./constants";import{ModelKeys}from"../utils/constants";function constructFromObject(o,r){if(r)for(const t in r)r.hasOwnProperty(t)&&(o.hasOwnProperty(t)||o.prototype&&o.prototype.hasOwnProperty(t))&&(o[t]=r[t]);return o}function constructFromModel(s,o){if(o)for(const n in o)if(o.hasOwnProperty(n)&&(s.hasOwnProperty(n)||s.prototype&&s.prototype.hasOwnProperty(n))){if(s[n]=o[n],"object"!=typeof s[n])continue;if(isModel(s[n])){try{s[n]=Model.build(s[n])}catch(o){console.log(o)}continue}const p=getPropertyDecorators(ValidationKeys.REFLECT,s,n).decorators;if(!(r=p.filter(o=>-1!==[ModelKeys.TYPE,ValidationKeys.TYPE].indexOf(o.key)))||!r.length)throw new Error(stringFormat("failed to find decorators for property {0}",n));var r=(r=r.pop()).props.name?[r.props.name]:Array.isArray(r.props.customTypes)?r.props.customTypes:[r.props.customTypes];const a=Object.values(ReservedModels).map(o=>o.toLowerCase());r.forEach(o=>{if(-1===a.indexOf(o.toLowerCase()))try{switch(o){case"Array":case"Set":if(p.length){const t=p.find(o=>o.key===ValidationKeys.LIST);if(t&&("Array"===o&&(s[n]=s[n].map(o=>Model.build(o,t.props.class))),"Set"===o)){var r=new Set;for(const e of s[n])r.add(Model.build(e,t.props.class));s[n]=r}}break;default:s[n]&&(s[n]=Model.build(s[n],o))}}catch(o){console.log(o)}})}return s}function construct(r,...o){var t=(...o)=>new r(...o);return t.prototype=r.prototype,t(...o)}export{constructFromObject,constructFromModel,construct};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @summary Builds the key to store as Metadata under Reflections
|
|
3
|
+
* @description concatenates {@link ModelKeys#REFLECT} with the provided key
|
|
4
|
+
* @param {string} str
|
|
5
|
+
*
|
|
6
|
+
* @function getModelKey
|
|
7
|
+
* @memberOf module:decorator-validation.Model
|
|
8
|
+
* @category Utilities
|
|
9
|
+
*/
|
|
10
|
+
export declare const getModelKey: (str: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* @summary defines the tpe os an InstanceCallback function
|
|
13
|
+
* @memberOf module:decorator-validation.Construction
|
|
14
|
+
* @category Construction
|
|
15
|
+
*/
|
|
16
|
+
export type InstanceCallback = (instance: any, ...args: any[]) => void;
|
|
17
|
+
/**
|
|
18
|
+
* @summary Defines a class as a Model class
|
|
19
|
+
* @description
|
|
20
|
+
* - Registers the class under the model registry so it can be easily rebuilt;
|
|
21
|
+
* - Overrides the class constructor;
|
|
22
|
+
* - Runs the global {@link ModelBuilderFunction} if defined;
|
|
23
|
+
* - Runs the optional {@link InstanceCallback} if provided;
|
|
24
|
+
* - Defines an {@link ModelKeys#ANCHOR} property for serialization and model rebuilding purposes;
|
|
25
|
+
*
|
|
26
|
+
* @prop {Record<string, any>} [props] additional properties to store as metadata
|
|
27
|
+
* @prop {InstanceCallback} [instanceCallback] optional callback that will be call with the instance upon instantiation. defaults to undefined
|
|
28
|
+
*
|
|
29
|
+
* @function model
|
|
30
|
+
* @memberOf module:decorator-validation.Decorators.Model
|
|
31
|
+
* @category Decorators
|
|
32
|
+
*/
|
|
33
|
+
export declare function model(props?: Record<string, any>, instanceCallback?: InstanceCallback): (original: any) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{construct}from"./construction";import{ModelKeys}from"../utils/constants";import{Model}from"./Model";const getModelKey=e=>ModelKeys.REFLECT+e;function model(n,l){return r=>{function e(...e){var t=construct(r,...e),o=((o=Model.getBuilder())&&o(t,e.length?e[0]:void 0),Object.assign({},{class:r.name}));return Object.defineProperty(t,ModelKeys.ANCHOR,{writable:!1,enumerable:!1,configurable:!1,value:o}),Reflect.defineMetadata(getModelKey(ModelKeys.MODEL),Object.assign(o,n||{}),t.constructor),l&&l(t,...e),t}return e.prototype=r.prototype,Object.defineProperty(e,"name",{writable:!1,enumerable:!0,configurable:!1,value:r.prototype.constructor.name}),Model.register(e),e}}export{getModelKey,model};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./construction";export*from"./Registry";export*from"./decorators";export*from"./ModelErrorDefinition";export*from"./types";export*from"./Model";export*from"./constants";
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ModelErrorDefinition } from "./ModelErrorDefinition";
|
|
2
|
+
import { Model } from "./Model";
|
|
3
|
+
/**
|
|
4
|
+
* @summary Definition of a Model Constructor Argument
|
|
5
|
+
*
|
|
6
|
+
* @memberOf module:decorator-validation.Model
|
|
7
|
+
* @category Model
|
|
8
|
+
*
|
|
9
|
+
* @see ModelConstructor
|
|
10
|
+
*/
|
|
11
|
+
export type ModelArg<T> = T | Record<string, any>;
|
|
12
|
+
/**
|
|
13
|
+
* @summary Definition of a Class Constructor
|
|
14
|
+
* @description Generic type for Constructor functions
|
|
15
|
+
*
|
|
16
|
+
* @typedef Constructor
|
|
17
|
+
*
|
|
18
|
+
* @param {any[]} [args]
|
|
19
|
+
* @memberOf module:decorator-validation.Model
|
|
20
|
+
* @category Model
|
|
21
|
+
*/
|
|
22
|
+
export type Constructor<T> = {
|
|
23
|
+
new (...args: any[]): T;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* @summary Definition of a Model Constructor
|
|
27
|
+
* @description Generic type for all Model Constructor functions
|
|
28
|
+
*
|
|
29
|
+
* @typedef ModelConstructor
|
|
30
|
+
*
|
|
31
|
+
* @param {ModelArg<T>} [model]
|
|
32
|
+
* @param {any[]} [args]
|
|
33
|
+
* @memberOf module:decorator-validation.Model
|
|
34
|
+
* @category Construction
|
|
35
|
+
*/
|
|
36
|
+
export type ModelConstructor<T extends Model> = {
|
|
37
|
+
new (model?: ModelArg<T>, ...args: any[]): T;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* @summary Defines the Validation API for validatable models
|
|
41
|
+
* @interface Validatable
|
|
42
|
+
*
|
|
43
|
+
* @category Validation
|
|
44
|
+
*/
|
|
45
|
+
export interface Validatable {
|
|
46
|
+
/**
|
|
47
|
+
* @summary Validates the model and returns the {@link ModelErrorDefinition} if any
|
|
48
|
+
* @param {any} [args]
|
|
49
|
+
*
|
|
50
|
+
* @method
|
|
51
|
+
*/
|
|
52
|
+
hasErrors(...args: any[]): ModelErrorDefinition | undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @summary Serializable interface
|
|
56
|
+
*
|
|
57
|
+
* @interface Serializable
|
|
58
|
+
*
|
|
59
|
+
* @category Serialization
|
|
60
|
+
*/
|
|
61
|
+
export interface Serializable {
|
|
62
|
+
/**
|
|
63
|
+
* @summary serializes the model
|
|
64
|
+
* @method
|
|
65
|
+
*/
|
|
66
|
+
serialize(): string;
|
|
67
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
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 = "model.definition.",
|
|
17
|
+
TYPE = "design:type",
|
|
18
|
+
PARAMS = "design:paramtypes",
|
|
19
|
+
RETURN = "design:returntype",
|
|
20
|
+
MODEL = "model",
|
|
21
|
+
ANCHOR = "__modelDefinition"
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var ModelKeys;!function(e){e.REFLECT="model.definition.",e.TYPE="design:type",e.PARAMS="design:paramtypes",e.RETURN="design:returntype",e.MODEL="model",e.ANCHOR="__modelDefinition"}(ModelKeys=ModelKeys||{});export{ModelKeys};
|