@decaf-ts/decorator-validation 1.5.5 → 1.5.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/dist/decorator-validation.js +1 -1
- package/dist/esm/decorator-validation.js +1 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -3
- package/lib/esm/model/Model.js +0 -2
- package/lib/esm/model/ModelErrorDefinition.js +0 -2
- package/lib/esm/model/Registry.js +0 -2
- package/lib/esm/model/constants.js +0 -2
- package/lib/esm/model/construction.js +0 -2
- package/lib/esm/model/decorators.js +0 -2
- package/lib/esm/model/index.js +0 -2
- package/lib/esm/model/types.js +0 -2
- package/lib/esm/model/utils.js +0 -2
- package/lib/esm/model/validation.js +0 -2
- package/lib/esm/utils/constants.js +0 -2
- package/lib/esm/utils/dates.js +0 -2
- package/lib/esm/utils/decorators.js +0 -2
- package/lib/esm/utils/hashing.js +0 -2
- package/lib/esm/utils/index.js +0 -2
- package/lib/esm/utils/registry.js +0 -2
- package/lib/esm/utils/serialization.js +0 -2
- package/lib/esm/utils/strings.js +0 -2
- package/lib/esm/validation/Validation.js +0 -2
- package/lib/esm/validation/Validators/DateValidator.js +0 -2
- package/lib/esm/validation/Validators/EmailValidator.js +0 -2
- package/lib/esm/validation/Validators/ListValidator.js +0 -2
- package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -2
- package/lib/esm/validation/Validators/MaxValidator.js +0 -2
- package/lib/esm/validation/Validators/MinLengthValidator.js +0 -2
- package/lib/esm/validation/Validators/MinValidator.js +0 -2
- package/lib/esm/validation/Validators/PasswordValidator.js +0 -2
- package/lib/esm/validation/Validators/PatternValidator.js +0 -2
- package/lib/esm/validation/Validators/RequiredValidator.js +0 -2
- package/lib/esm/validation/Validators/StepValidator.js +0 -2
- package/lib/esm/validation/Validators/TypeValidator.js +0 -2
- package/lib/esm/validation/Validators/URLValidator.js +0 -2
- package/lib/esm/validation/Validators/Validator.js +0 -2
- package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -2
- package/lib/esm/validation/Validators/constants.js +0 -2
- package/lib/esm/validation/Validators/decorators.js +0 -2
- package/lib/esm/validation/Validators/index.js +0 -2
- package/lib/esm/validation/Validators/types.js +0 -2
- package/lib/esm/validation/decorators.js +0 -2
- package/lib/esm/validation/index.js +0 -2
- package/lib/esm/validation/types.js +0 -2
- package/lib/index.d.ts +1 -1
- package/lib/index.js +59 -0
- package/lib/model/Model.js +300 -0
- package/lib/model/ModelErrorDefinition.js +56 -0
- package/lib/model/Registry.js +78 -0
- package/lib/model/constants.js +72 -0
- package/lib/model/construction.js +68 -0
- package/lib/model/decorators.js +67 -0
- package/lib/{validation/index.cjs → model/index.js} +9 -6
- package/lib/model/types.js +2 -0
- package/lib/model/utils.js +29 -0
- package/lib/model/validation.js +138 -0
- package/lib/utils/constants.js +30 -0
- package/lib/utils/dates.js +251 -0
- package/lib/utils/decorators.js +22 -0
- package/lib/utils/hashing.js +106 -0
- package/lib/utils/index.js +23 -0
- package/lib/utils/registry.js +2 -0
- package/lib/utils/serialization.js +93 -0
- package/lib/utils/strings.js +35 -0
- package/lib/validation/Validation.js +75 -0
- package/lib/validation/Validators/DateValidator.js +57 -0
- package/lib/validation/Validators/EmailValidator.js +53 -0
- package/lib/validation/Validators/ListValidator.js +70 -0
- package/lib/validation/Validators/MaxLengthValidator.js +55 -0
- package/lib/validation/Validators/MaxValidator.js +60 -0
- package/lib/validation/Validators/MinLengthValidator.js +55 -0
- package/lib/validation/Validators/MinValidator.js +60 -0
- package/lib/validation/Validators/PasswordValidator.js +53 -0
- package/lib/validation/Validators/PatternValidator.js +73 -0
- package/lib/validation/Validators/RequiredValidator.js +60 -0
- package/lib/validation/Validators/StepValidator.js +55 -0
- package/lib/validation/Validators/TypeValidator.js +63 -0
- package/lib/validation/Validators/URLValidator.js +52 -0
- package/lib/validation/Validators/Validator.js +51 -0
- package/lib/validation/Validators/ValidatorRegistry.js +85 -0
- package/lib/validation/Validators/constants.js +137 -0
- package/lib/validation/Validators/decorators.js +28 -0
- package/lib/validation/Validators/index.js +69 -0
- package/lib/validation/Validators/types.js +2 -0
- package/lib/validation/decorators.js +304 -0
- package/lib/validation/index.js +20 -0
- package/lib/validation/types.js +2 -0
- package/package.json +1 -1
- package/lib/index.cjs +0 -61
- package/lib/model/Model.cjs +0 -302
- package/lib/model/ModelErrorDefinition.cjs +0 -58
- package/lib/model/Registry.cjs +0 -80
- package/lib/model/constants.cjs +0 -74
- package/lib/model/construction.cjs +0 -70
- package/lib/model/decorators.cjs +0 -69
- package/lib/model/index.cjs +0 -27
- package/lib/model/types.cjs +0 -4
- package/lib/model/utils.cjs +0 -31
- package/lib/model/validation.cjs +0 -140
- package/lib/utils/constants.cjs +0 -32
- package/lib/utils/dates.cjs +0 -253
- package/lib/utils/decorators.cjs +0 -24
- package/lib/utils/hashing.cjs +0 -108
- package/lib/utils/index.cjs +0 -25
- package/lib/utils/registry.cjs +0 -4
- package/lib/utils/serialization.cjs +0 -95
- package/lib/utils/strings.cjs +0 -37
- package/lib/validation/Validation.cjs +0 -77
- package/lib/validation/Validators/DateValidator.cjs +0 -59
- package/lib/validation/Validators/EmailValidator.cjs +0 -55
- package/lib/validation/Validators/ListValidator.cjs +0 -72
- package/lib/validation/Validators/MaxLengthValidator.cjs +0 -57
- package/lib/validation/Validators/MaxValidator.cjs +0 -62
- package/lib/validation/Validators/MinLengthValidator.cjs +0 -57
- package/lib/validation/Validators/MinValidator.cjs +0 -62
- package/lib/validation/Validators/PasswordValidator.cjs +0 -55
- package/lib/validation/Validators/PatternValidator.cjs +0 -75
- package/lib/validation/Validators/RequiredValidator.cjs +0 -62
- package/lib/validation/Validators/StepValidator.cjs +0 -57
- package/lib/validation/Validators/TypeValidator.cjs +0 -65
- package/lib/validation/Validators/URLValidator.cjs +0 -54
- package/lib/validation/Validators/Validator.cjs +0 -53
- package/lib/validation/Validators/ValidatorRegistry.cjs +0 -87
- package/lib/validation/Validators/constants.cjs +0 -139
- package/lib/validation/Validators/decorators.cjs +0 -30
- package/lib/validation/Validators/index.cjs +0 -71
- package/lib/validation/Validators/types.d.ts +0 -88
- package/lib/validation/decorators.cjs +0 -306
- package/lib/validation/types.cjs +0 -4
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./constants"), exports);
|
|
18
|
+
__exportStar(require("./dates"), exports);
|
|
19
|
+
__exportStar(require("./decorators"), exports);
|
|
20
|
+
__exportStar(require("./hashing"), exports);
|
|
21
|
+
__exportStar(require("./registry"), exports);
|
|
22
|
+
__exportStar(require("./serialization"), exports);
|
|
23
|
+
__exportStar(require("./strings"), exports);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Serialization = exports.DefaultSerializationMethod = exports.JSONSerializer = void 0;
|
|
4
|
+
const Model_1 = require("../model/Model");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
/**
|
|
7
|
+
* @summary Concrete implementation of a {@link Serializer} in JSON format
|
|
8
|
+
* @description JS's native JSON.stringify (used here) is not deterministic
|
|
9
|
+
* and therefore should not be used for hashing purposes
|
|
10
|
+
*
|
|
11
|
+
* To keep dependencies low, we will not implement this, but we recommend
|
|
12
|
+
* implementing a similar {@link JSONSerializer} using 'deterministic-json' libraries
|
|
13
|
+
*
|
|
14
|
+
* @class JSONSerializer
|
|
15
|
+
* @implements Serializer
|
|
16
|
+
*
|
|
17
|
+
* @category Serialization
|
|
18
|
+
*/
|
|
19
|
+
class JSONSerializer {
|
|
20
|
+
/**
|
|
21
|
+
* @summary prepares the model for serialization
|
|
22
|
+
* @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
|
|
23
|
+
* so the object can be recognized upon deserialization
|
|
24
|
+
*
|
|
25
|
+
* @param {T} model
|
|
26
|
+
* @protected
|
|
27
|
+
*/
|
|
28
|
+
preSerialize(model) {
|
|
29
|
+
// TODO: nested preserialization (so increase performance when deserializing)
|
|
30
|
+
const toSerialize = Object.assign({}, model);
|
|
31
|
+
const metadata = Model_1.Model.getMetadata(model);
|
|
32
|
+
toSerialize[constants_1.ModelKeys.ANCHOR] = metadata || model.constructor.name;
|
|
33
|
+
return toSerialize;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @summary Rebuilds a model from a serialization
|
|
37
|
+
* @param {string} str
|
|
38
|
+
*
|
|
39
|
+
* @throws {Error} If it fails to parse the string, or to build the model
|
|
40
|
+
*/
|
|
41
|
+
deserialize(str) {
|
|
42
|
+
const deserialization = JSON.parse(str);
|
|
43
|
+
const className = deserialization[constants_1.ModelKeys.ANCHOR];
|
|
44
|
+
if (!className)
|
|
45
|
+
throw new Error("Could not find class reference in serialized model");
|
|
46
|
+
const model = Model_1.Model.build(deserialization, className);
|
|
47
|
+
return model;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @summary Serializes a model
|
|
51
|
+
* @param {T} model
|
|
52
|
+
*
|
|
53
|
+
* @throws {Error} if fails to serialize
|
|
54
|
+
*/
|
|
55
|
+
serialize(model) {
|
|
56
|
+
return JSON.stringify(this.preSerialize(model));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.JSONSerializer = JSONSerializer;
|
|
60
|
+
exports.DefaultSerializationMethod = "json";
|
|
61
|
+
class Serialization {
|
|
62
|
+
static { this.current = exports.DefaultSerializationMethod; }
|
|
63
|
+
static { this.cache = {
|
|
64
|
+
json: new JSONSerializer(),
|
|
65
|
+
}; }
|
|
66
|
+
constructor() { }
|
|
67
|
+
static get(key) {
|
|
68
|
+
if (key in this.cache)
|
|
69
|
+
return this.cache[key];
|
|
70
|
+
throw new Error(`No serialization method registered under ${key}`);
|
|
71
|
+
}
|
|
72
|
+
static register(key, func, setDefault = false) {
|
|
73
|
+
if (key in this.cache)
|
|
74
|
+
throw new Error(`Serialization method ${key} already registered`);
|
|
75
|
+
this.cache[key] = new func();
|
|
76
|
+
if (setDefault)
|
|
77
|
+
this.current = key;
|
|
78
|
+
}
|
|
79
|
+
static serialize(obj, method, ...args) {
|
|
80
|
+
if (!method)
|
|
81
|
+
return this.get(this.current).serialize(obj, ...args);
|
|
82
|
+
return this.get(method).serialize(obj, ...args);
|
|
83
|
+
}
|
|
84
|
+
static deserialize(obj, method, ...args) {
|
|
85
|
+
if (!method)
|
|
86
|
+
return this.get(this.current).deserialize(obj, ...args);
|
|
87
|
+
return this.get(method).deserialize(obj, ...args);
|
|
88
|
+
}
|
|
89
|
+
static setDefault(method) {
|
|
90
|
+
this.current = this.get(method);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.Serialization = Serialization;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sf = void 0;
|
|
4
|
+
exports.stringFormat = stringFormat;
|
|
5
|
+
/**
|
|
6
|
+
* @summary Util function to provide string format functionality similar to C#'s string.format
|
|
7
|
+
*
|
|
8
|
+
* @param {string} string
|
|
9
|
+
* @param {Array<string | number>} [args] replacements made by order of appearance (replacement0 wil replace {0} and so on)
|
|
10
|
+
* @return {string} formatted string
|
|
11
|
+
*
|
|
12
|
+
* @function stringFormat
|
|
13
|
+
* @memberOf module:decorator-validation.Utils.Format
|
|
14
|
+
* @category Format
|
|
15
|
+
*/
|
|
16
|
+
function stringFormat(string, ...args) {
|
|
17
|
+
return string.replace(/{(\d+)}/g, function (match, number) {
|
|
18
|
+
return typeof args[number] !== "undefined"
|
|
19
|
+
? args[number].toString()
|
|
20
|
+
: "undefined";
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @summary Util function to provide string format functionality similar to C#'s string.format
|
|
25
|
+
* @description alias for {@link stringFormat}
|
|
26
|
+
*
|
|
27
|
+
* @param {string} string
|
|
28
|
+
* @param {string} args replacements made by order of appearance (replacement0 wil replace {0} and so on)
|
|
29
|
+
* @return {string} formatted string
|
|
30
|
+
*
|
|
31
|
+
* @function sf
|
|
32
|
+
* @memberOf module:decorator-validation.Utils.Format
|
|
33
|
+
* @category Format
|
|
34
|
+
*/
|
|
35
|
+
exports.sf = stringFormat;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Validation = void 0;
|
|
4
|
+
const ValidatorRegistry_1 = require("./Validators/ValidatorRegistry");
|
|
5
|
+
const constants_1 = require("./Validators/constants");
|
|
6
|
+
/**
|
|
7
|
+
* @summary Static class acting as a namespace for the Validation
|
|
8
|
+
*
|
|
9
|
+
* @class Validation
|
|
10
|
+
* @static
|
|
11
|
+
*
|
|
12
|
+
* @category Validation
|
|
13
|
+
*/
|
|
14
|
+
class Validation {
|
|
15
|
+
static { this.actingValidatorRegistry = undefined; }
|
|
16
|
+
constructor() { }
|
|
17
|
+
/**
|
|
18
|
+
* @summary Defines the acting ValidatorRegistry
|
|
19
|
+
*
|
|
20
|
+
* @param {IValidatorRegistry} validatorRegistry the new implementation of the validator Registry
|
|
21
|
+
* @param {function(Validator): Validator} [migrationHandler] the method to map the validator if required;
|
|
22
|
+
*/
|
|
23
|
+
static setRegistry(validatorRegistry, migrationHandler) {
|
|
24
|
+
if (migrationHandler && Validation.actingValidatorRegistry)
|
|
25
|
+
Validation.actingValidatorRegistry.getKeys().forEach((k) => {
|
|
26
|
+
const validator = validatorRegistry.get(k);
|
|
27
|
+
if (validator)
|
|
28
|
+
validatorRegistry.register(migrationHandler(validator));
|
|
29
|
+
});
|
|
30
|
+
Validation.actingValidatorRegistry = validatorRegistry;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @summary Returns the current ValidatorRegistry
|
|
34
|
+
*
|
|
35
|
+
* @return IValidatorRegistry, defaults to {@link ValidatorRegistry}
|
|
36
|
+
*/
|
|
37
|
+
static getRegistry() {
|
|
38
|
+
if (!Validation.actingValidatorRegistry)
|
|
39
|
+
Validation.actingValidatorRegistry = new ValidatorRegistry_1.ValidatorRegistry();
|
|
40
|
+
return Validation.actingValidatorRegistry;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @summary Retrieves a validator
|
|
44
|
+
*
|
|
45
|
+
* @param {string} validatorKey one of the {@link ValidationKeys}
|
|
46
|
+
* @return {Validator | undefined} the registered Validator or undefined if there is nono matching the provided key
|
|
47
|
+
*/
|
|
48
|
+
static get(validatorKey) {
|
|
49
|
+
return Validation.getRegistry().get(validatorKey);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @summary Registers the provided validators onto the registry
|
|
53
|
+
*
|
|
54
|
+
* @param {T[] | ValidatorDefinition[]} validator
|
|
55
|
+
*/
|
|
56
|
+
static register(...validator) {
|
|
57
|
+
return Validation.getRegistry().register(...validator);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @summary Builds the key to store as Metadata under Reflections
|
|
61
|
+
* @description concatenates {@link ValidationKeys#REFLECT} with the provided key
|
|
62
|
+
*
|
|
63
|
+
* @param {string} key
|
|
64
|
+
*/
|
|
65
|
+
static key(key) {
|
|
66
|
+
return constants_1.ValidationKeys.REFLECT + key;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @summary Returns all registered validation keys
|
|
70
|
+
*/
|
|
71
|
+
static keys() {
|
|
72
|
+
return this.getRegistry().getKeys();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.Validation = Validation;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DateValidator = void 0;
|
|
13
|
+
const Validator_1 = require("./Validator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary Date Validator
|
|
18
|
+
*
|
|
19
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#DATE}
|
|
20
|
+
*
|
|
21
|
+
* @class DateValidator
|
|
22
|
+
* @extends Validator
|
|
23
|
+
*
|
|
24
|
+
* @category Validators
|
|
25
|
+
*/
|
|
26
|
+
let DateValidator = class DateValidator extends Validator_1.Validator {
|
|
27
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.DATE) {
|
|
28
|
+
super(message, Number.name, Date.name, String.name);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @summary Validates a model
|
|
32
|
+
*
|
|
33
|
+
* @param {Date | string} value
|
|
34
|
+
* @param {DateValidatorOptions} [options]
|
|
35
|
+
*
|
|
36
|
+
* @return {string | undefined}
|
|
37
|
+
*
|
|
38
|
+
* @override
|
|
39
|
+
*
|
|
40
|
+
* @see Validator#hasErrors
|
|
41
|
+
*/
|
|
42
|
+
hasErrors(value, options = {}) {
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
return;
|
|
45
|
+
if (typeof value === "string")
|
|
46
|
+
value = new Date(value);
|
|
47
|
+
if (Number.isNaN(value.getDate())) {
|
|
48
|
+
const { message = "" } = options;
|
|
49
|
+
return this.getMessage(message || this.message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
exports.DateValidator = DateValidator;
|
|
54
|
+
exports.DateValidator = DateValidator = __decorate([
|
|
55
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.DATE),
|
|
56
|
+
__metadata("design:paramtypes", [String])
|
|
57
|
+
], DateValidator);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EmailValidator = void 0;
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
const PatternValidator_1 = require("./PatternValidator");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary Email Validator
|
|
18
|
+
*
|
|
19
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#EMAIL}
|
|
20
|
+
*
|
|
21
|
+
* @class EmailValidator
|
|
22
|
+
* @extends PatternValidator
|
|
23
|
+
*
|
|
24
|
+
* @category Validators
|
|
25
|
+
*/
|
|
26
|
+
let EmailValidator = class EmailValidator extends PatternValidator_1.PatternValidator {
|
|
27
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.EMAIL) {
|
|
28
|
+
super(message);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @summary Validates a model
|
|
32
|
+
*
|
|
33
|
+
* @param {string} value
|
|
34
|
+
* @param {PatternValidatorOptions} [options]
|
|
35
|
+
*
|
|
36
|
+
* @return {string | undefined}
|
|
37
|
+
*
|
|
38
|
+
* @override
|
|
39
|
+
*
|
|
40
|
+
* @see Validator#hasErrors
|
|
41
|
+
*/
|
|
42
|
+
hasErrors(value, options = {}) {
|
|
43
|
+
return super.hasErrors(value, {
|
|
44
|
+
...options,
|
|
45
|
+
pattern: options?.pattern || constants_1.DEFAULT_PATTERNS.EMAIL,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
exports.EmailValidator = EmailValidator;
|
|
50
|
+
exports.EmailValidator = EmailValidator = __decorate([
|
|
51
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.EMAIL),
|
|
52
|
+
__metadata("design:paramtypes", [String])
|
|
53
|
+
], EmailValidator);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ListValidator = void 0;
|
|
13
|
+
const Validator_1 = require("./Validator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary List Validator
|
|
18
|
+
*
|
|
19
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#LIST}
|
|
20
|
+
*
|
|
21
|
+
* @class ListValidator
|
|
22
|
+
* @extends Validator
|
|
23
|
+
*
|
|
24
|
+
* @category Validators
|
|
25
|
+
*/
|
|
26
|
+
let ListValidator = class ListValidator extends Validator_1.Validator {
|
|
27
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.LIST) {
|
|
28
|
+
super(message, Array.name, Set.name);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @summary Validates a model
|
|
32
|
+
*
|
|
33
|
+
* @param {any[] | Set<any>} value
|
|
34
|
+
* @param {ListValidatorOptions} options
|
|
35
|
+
*
|
|
36
|
+
* @return {string | undefined}
|
|
37
|
+
*
|
|
38
|
+
* @override
|
|
39
|
+
*
|
|
40
|
+
* @see Validator#hasErrors
|
|
41
|
+
*/
|
|
42
|
+
hasErrors(value, options) {
|
|
43
|
+
if (!value || (Array.isArray(value) ? !value.length : !value.size))
|
|
44
|
+
return;
|
|
45
|
+
const clazz = Array.isArray(options.clazz)
|
|
46
|
+
? options.clazz
|
|
47
|
+
: [options.clazz];
|
|
48
|
+
let val, isValid = true;
|
|
49
|
+
for (let i = 0; i < (Array.isArray(value) ? value.length : value.size); i++) {
|
|
50
|
+
val = value[i];
|
|
51
|
+
switch (typeof val) {
|
|
52
|
+
case "object":
|
|
53
|
+
case "function":
|
|
54
|
+
isValid = clazz.includes(val.constructor?.name);
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
isValid = clazz.some((c) => typeof val === c.toLowerCase());
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return isValid
|
|
62
|
+
? undefined
|
|
63
|
+
: this.getMessage(options.message || this.message, clazz);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
exports.ListValidator = ListValidator;
|
|
67
|
+
exports.ListValidator = ListValidator = __decorate([
|
|
68
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.LIST),
|
|
69
|
+
__metadata("design:paramtypes", [String])
|
|
70
|
+
], ListValidator);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MaxLengthValidator = void 0;
|
|
13
|
+
const Validator_1 = require("./Validator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary Maximum Length Validator
|
|
18
|
+
* @description Validates strings and Arrays on their maximum length
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MAX_LENGTH}
|
|
21
|
+
*
|
|
22
|
+
* @class MinLengthValidator
|
|
23
|
+
* @extends Validator
|
|
24
|
+
*
|
|
25
|
+
* @category Validators
|
|
26
|
+
*/
|
|
27
|
+
let MaxLengthValidator = class MaxLengthValidator extends Validator_1.Validator {
|
|
28
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MAX_LENGTH) {
|
|
29
|
+
super(message, String.name, Array.name);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @summary Validates a model
|
|
33
|
+
*
|
|
34
|
+
* @param {string} value
|
|
35
|
+
* @param {MaxLengthValidatorOptions} options
|
|
36
|
+
*
|
|
37
|
+
* @return {string | undefined}
|
|
38
|
+
*
|
|
39
|
+
* @override
|
|
40
|
+
*
|
|
41
|
+
* @see Validator#hasErrors
|
|
42
|
+
*/
|
|
43
|
+
hasErrors(value, options) {
|
|
44
|
+
if (typeof value === "undefined")
|
|
45
|
+
return;
|
|
46
|
+
return value.length > options.maxLength
|
|
47
|
+
? this.getMessage(options.message || this.message, options.maxLength)
|
|
48
|
+
: undefined;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.MaxLengthValidator = MaxLengthValidator;
|
|
52
|
+
exports.MaxLengthValidator = MaxLengthValidator = __decorate([
|
|
53
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.MAX_LENGTH),
|
|
54
|
+
__metadata("design:paramtypes", [String])
|
|
55
|
+
], MaxLengthValidator);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MaxValidator = void 0;
|
|
13
|
+
const Validator_1 = require("./Validator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary Max Validator
|
|
18
|
+
*
|
|
19
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MAX}
|
|
20
|
+
*
|
|
21
|
+
* @class MaxValidator
|
|
22
|
+
* @extends Validator
|
|
23
|
+
*
|
|
24
|
+
* @category Validators
|
|
25
|
+
*/
|
|
26
|
+
let MaxValidator = class MaxValidator extends Validator_1.Validator {
|
|
27
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MAX) {
|
|
28
|
+
super(message, "number", "Date", "string");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @summary Validates a Model
|
|
32
|
+
*
|
|
33
|
+
* @param {string} value
|
|
34
|
+
* @param {MaxValidatorOptions} options
|
|
35
|
+
*
|
|
36
|
+
* @return {string | undefined}
|
|
37
|
+
*
|
|
38
|
+
* @override
|
|
39
|
+
*
|
|
40
|
+
* @see Validator#hasErrors
|
|
41
|
+
*/
|
|
42
|
+
hasErrors(value, options) {
|
|
43
|
+
if (typeof value === "undefined")
|
|
44
|
+
return;
|
|
45
|
+
let { max } = options;
|
|
46
|
+
if (value instanceof Date && !(max instanceof Date)) {
|
|
47
|
+
max = new Date(max);
|
|
48
|
+
if (Number.isNaN(max.getDate()))
|
|
49
|
+
throw new Error("Invalid Max param defined");
|
|
50
|
+
}
|
|
51
|
+
return value > max
|
|
52
|
+
? this.getMessage(options.message || this.message, max)
|
|
53
|
+
: undefined;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.MaxValidator = MaxValidator;
|
|
57
|
+
exports.MaxValidator = MaxValidator = __decorate([
|
|
58
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.MAX),
|
|
59
|
+
__metadata("design:paramtypes", [String])
|
|
60
|
+
], MaxValidator);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MinLengthValidator = void 0;
|
|
13
|
+
const Validator_1 = require("./Validator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary Minimum Length Validator
|
|
18
|
+
* @description Validates strings and Arrays on their minimum length
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MIN_LENGTH}
|
|
21
|
+
*
|
|
22
|
+
* @class MinLengthValidator
|
|
23
|
+
* @extends Validator
|
|
24
|
+
*
|
|
25
|
+
* @category Validators
|
|
26
|
+
*/
|
|
27
|
+
let MinLengthValidator = class MinLengthValidator extends Validator_1.Validator {
|
|
28
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MIN_LENGTH) {
|
|
29
|
+
super(message, String.name, Array.name);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param {string | Array} value
|
|
34
|
+
* @param {MinLengthValidatorOptions} options
|
|
35
|
+
*
|
|
36
|
+
* @return {string | undefined}
|
|
37
|
+
*
|
|
38
|
+
* @memberOf MinLengthValidator
|
|
39
|
+
* @override
|
|
40
|
+
*
|
|
41
|
+
* @see Validator#hasErrors
|
|
42
|
+
*/
|
|
43
|
+
hasErrors(value, options) {
|
|
44
|
+
if (typeof value === "undefined")
|
|
45
|
+
return;
|
|
46
|
+
return value.length < options.minLength
|
|
47
|
+
? this.getMessage(options.message || this.message, options.minLength)
|
|
48
|
+
: undefined;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.MinLengthValidator = MinLengthValidator;
|
|
52
|
+
exports.MinLengthValidator = MinLengthValidator = __decorate([
|
|
53
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.MIN_LENGTH),
|
|
54
|
+
__metadata("design:paramtypes", [String])
|
|
55
|
+
], MinLengthValidator);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MinValidator = void 0;
|
|
13
|
+
const Validator_1 = require("./Validator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const decorators_1 = require("./decorators");
|
|
16
|
+
/**
|
|
17
|
+
* @summary Min Validator
|
|
18
|
+
*
|
|
19
|
+
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MIN}
|
|
20
|
+
*
|
|
21
|
+
* @class MinValidator
|
|
22
|
+
* @extends Validator
|
|
23
|
+
*
|
|
24
|
+
* @category Validators
|
|
25
|
+
*/
|
|
26
|
+
let MinValidator = class MinValidator extends Validator_1.Validator {
|
|
27
|
+
constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MIN) {
|
|
28
|
+
super(message, "number", "Date", "string");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @summary Validates Model
|
|
32
|
+
*
|
|
33
|
+
* @param {string} value
|
|
34
|
+
* @param {MaxValidatorOptions} options
|
|
35
|
+
*
|
|
36
|
+
* @return {string | undefined}
|
|
37
|
+
*
|
|
38
|
+
* @override
|
|
39
|
+
*
|
|
40
|
+
* @see Validator#hasErrors
|
|
41
|
+
*/
|
|
42
|
+
hasErrors(value, options) {
|
|
43
|
+
if (typeof value === "undefined")
|
|
44
|
+
return;
|
|
45
|
+
let { min } = options;
|
|
46
|
+
if (value instanceof Date && !(min instanceof Date)) {
|
|
47
|
+
min = new Date(min);
|
|
48
|
+
if (Number.isNaN(min.getDate()))
|
|
49
|
+
throw new Error("Invalid Min param defined");
|
|
50
|
+
}
|
|
51
|
+
return value < min
|
|
52
|
+
? this.getMessage(options.message || this.message, min)
|
|
53
|
+
: undefined;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.MinValidator = MinValidator;
|
|
57
|
+
exports.MinValidator = MinValidator = __decorate([
|
|
58
|
+
(0, decorators_1.validator)(constants_1.ValidationKeys.MIN),
|
|
59
|
+
__metadata("design:paramtypes", [String])
|
|
60
|
+
], MinValidator);
|