@my-devkit/core 1.0.121 → 1.0.122
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/.eslintrc.js +2 -4
- package/dist/date-helper.js +28 -20
- package/dist/date-helper.js.map +1 -1
- package/dist/decorators/cacheable.decorator.js.map +1 -1
- package/dist/enum-helper.js.map +1 -1
- package/dist/event.js +2 -1
- package/dist/event.js.map +1 -1
- package/dist/json-helper.js.map +1 -1
- package/dist/logger.js +4 -1
- package/dist/logger.js.map +1 -1
- package/dist/model.js.map +1 -1
- package/dist/retry.js.map +1 -1
- package/dist/serialize/deserialize.js +0 -1
- package/dist/serialize/deserialize.js.map +1 -1
- package/dist/serialize/index.js.map +1 -1
- package/dist/serialize/serializable.js.map +1 -1
- package/dist/serialize/serialize-helper.js +28 -3
- package/dist/serialize/serialize-helper.js.map +1 -1
- package/dist/serialize/type-helper.d.ts +1 -1
- package/dist/serialize/type-helper.js +24 -11
- package/dist/serialize/type-helper.js.map +1 -1
- package/dist/sleep.js.map +1 -1
- package/dist/validators/custom-validators/greater-than-date.js.map +1 -1
- package/dist/validators/custom-validators/is-empty-if.js +4 -2
- package/dist/validators/custom-validators/is-empty-if.js.map +1 -1
- package/dist/validators/custom-validators/is-not-empty-if.js.map +1 -1
- package/dist/validators/custom-validators/is-not-in-relative-to.js +3 -1
- package/dist/validators/custom-validators/is-not-in-relative-to.js.map +1 -1
- package/dist/validators/custom-validators/is-optional-if.js +1 -2
- package/dist/validators/custom-validators/is-optional-if.js.map +1 -1
- package/dist/validators/index.js.map +1 -1
- package/dist/validators/validate.js +17 -5
- package/dist/validators/validate.js.map +1 -1
- package/dist/validators/validation-error.js.map +1 -1
- package/package.json +2 -2
- package/src/date-helper.ts +91 -64
- package/src/decorators/cacheable.decorator.ts +29 -13
- package/src/enum-helper.ts +4 -2
- package/src/event.ts +2 -1
- package/src/json-helper.ts +3 -1
- package/src/logger.ts +9 -2
- package/src/model.ts +4 -1
- package/src/retry.ts +8 -2
- package/src/serialize/deserialize.ts +0 -1
- package/src/serialize/index.ts +7 -2
- package/src/serialize/serializable.ts +3 -2
- package/src/serialize/serialize-helper.ts +40 -14
- package/src/serialize/type-helper.ts +39 -15
- package/src/sleep.ts +1 -1
- package/src/validators/custom-validators/greater-than-date.ts +5 -1
- package/src/validators/custom-validators/is-empty-if.ts +10 -3
- package/src/validators/custom-validators/is-not-empty-if.ts +4 -1
- package/src/validators/custom-validators/is-not-in-relative-to.ts +13 -3
- package/src/validators/custom-validators/is-optional-if.ts +5 -3
- package/src/validators/index.ts +65 -11
- package/src/validators/validate.ts +42 -10
- package/src/validators/validation-error.ts +2 -3
- package/src/vendors/lodash.ts +0 -1
- package/tsconfig.json +1 -3
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
1
|
import { Deserialize, GenericDeserialize, Serialize, __TypeMap } from 'cerialize';
|
|
3
2
|
|
|
4
3
|
import { _get, _isObjectLike, _keys, _trimEnd } from '../vendors/lodash';
|
|
@@ -8,8 +7,7 @@ export class TypeHelper {
|
|
|
8
7
|
const serialized = Serialize(rawJson, classType);
|
|
9
8
|
const deserialized = GenericDeserialize(serialized, classType);
|
|
10
9
|
|
|
11
|
-
if (
|
|
12
|
-
deserialized['_type'] = classType.name;
|
|
10
|
+
if (new classType()['_type'] === classType.name) deserialized['_type'] = classType.name;
|
|
13
11
|
|
|
14
12
|
return deserialized;
|
|
15
13
|
}
|
|
@@ -21,7 +19,9 @@ export class TypeHelper {
|
|
|
21
19
|
const remaining = metaData.map(m => ({ parent: null, m }));
|
|
22
20
|
while (remaining.length > 0) {
|
|
23
21
|
const property = remaining.shift();
|
|
24
|
-
const path = property.parent
|
|
22
|
+
const path = property.parent
|
|
23
|
+
? `${property.parent}.${property.m.name}`
|
|
24
|
+
: property.m.name;
|
|
25
25
|
result.push({
|
|
26
26
|
parent: property.parent ? _trimEnd(property.parent, '[]') : null,
|
|
27
27
|
path,
|
|
@@ -33,7 +33,7 @@ export class TypeHelper {
|
|
|
33
33
|
|
|
34
34
|
if (property.m.type === TypeHelper.Type.Object) {
|
|
35
35
|
const parent = property.m.isArray ? `${path}[]` : path;
|
|
36
|
-
remaining.push(...property.m.objectMetadata.map(m => ({ parent, m })))
|
|
36
|
+
remaining.push(...property.m.objectMetadata.map(m => ({ parent, m })));
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
return result;
|
|
@@ -49,15 +49,24 @@ export class TypeHelper {
|
|
|
49
49
|
const metaDataType = TypeHelper.getMetaDataType(m.deserializedType);
|
|
50
50
|
const metadata: TypeHelper.MetaData = {
|
|
51
51
|
name: m.keyName,
|
|
52
|
-
serializedKey:
|
|
53
|
-
deserializedKey:
|
|
52
|
+
serializedKey: m.serializedKey ? m.serializedKey : m.keyName,
|
|
53
|
+
deserializedKey: m.deserializedKey ? m.deserializedKey : m.keyName,
|
|
54
54
|
type: metaDataType,
|
|
55
|
-
enumName:
|
|
55
|
+
enumName:
|
|
56
|
+
metaDataType === TypeHelper.Type.Enum
|
|
57
|
+
? this.getEnumName(m.deserializedType)
|
|
58
|
+
: null,
|
|
56
59
|
isArray: Array.isArray(instance[m.keyName]),
|
|
57
60
|
arrayLength: Array.isArray(instance[m.keyName]) ? instance[m.keyName].length : null,
|
|
58
61
|
objectName: metaDataType === TypeHelper.Type.Object ? m.deserializedType.name : [],
|
|
59
|
-
objectMetadata:
|
|
60
|
-
|
|
62
|
+
objectMetadata:
|
|
63
|
+
metaDataType === TypeHelper.Type.Object
|
|
64
|
+
? this.getMetaData(m.deserializedType)
|
|
65
|
+
: [],
|
|
66
|
+
enumValues:
|
|
67
|
+
metaDataType === TypeHelper.Type.Enum
|
|
68
|
+
? this.getEnumValues(m.deserializedType)
|
|
69
|
+
: []
|
|
61
70
|
};
|
|
62
71
|
return metadata;
|
|
63
72
|
});
|
|
@@ -132,21 +141,33 @@ export class TypeHelper {
|
|
|
132
141
|
}
|
|
133
142
|
|
|
134
143
|
private static isString(propType: any) {
|
|
135
|
-
if (
|
|
144
|
+
if (
|
|
145
|
+
propType &&
|
|
146
|
+
propType.Deserialize &&
|
|
147
|
+
propType.Deserialize.name.replace('deserialize', '') === 'String'
|
|
148
|
+
) {
|
|
136
149
|
return true;
|
|
137
150
|
}
|
|
138
151
|
return false;
|
|
139
152
|
}
|
|
140
153
|
|
|
141
154
|
private static isBoolean(propType: any) {
|
|
142
|
-
if (
|
|
155
|
+
if (
|
|
156
|
+
propType &&
|
|
157
|
+
propType.Deserialize &&
|
|
158
|
+
propType.Deserialize.name.replace('deserialize', '') === 'Boolean'
|
|
159
|
+
) {
|
|
143
160
|
return true;
|
|
144
161
|
}
|
|
145
162
|
return false;
|
|
146
163
|
}
|
|
147
164
|
|
|
148
165
|
private static isNumber(propType: any) {
|
|
149
|
-
if (
|
|
166
|
+
if (
|
|
167
|
+
propType &&
|
|
168
|
+
propType.Deserialize &&
|
|
169
|
+
propType.Deserialize.name.replace('deserialize', '') === 'Number'
|
|
170
|
+
) {
|
|
150
171
|
return true;
|
|
151
172
|
}
|
|
152
173
|
return false;
|
|
@@ -194,8 +215,11 @@ export namespace TypeHelper {
|
|
|
194
215
|
}
|
|
195
216
|
|
|
196
217
|
type NonFunctionPropertyNames<T> = {
|
|
197
|
-
[K in keyof T]: T[K] extends
|
|
218
|
+
[K in keyof T]: T[K] extends (...args: any[]) => any ? never : K;
|
|
198
219
|
}[keyof T];
|
|
199
220
|
|
|
200
|
-
export type ClassProperties<T> = Omit<
|
|
221
|
+
export type ClassProperties<T> = Omit<
|
|
222
|
+
Pick<T, NonFunctionPropertyNames<T>>,
|
|
223
|
+
'_path' | 'publishedAt' | 'publishedAt'
|
|
224
|
+
>;
|
|
201
225
|
}
|
package/src/sleep.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
|
|
2
2
|
|
|
3
|
-
export function GreaterThanDate(
|
|
3
|
+
export function GreaterThanDate(
|
|
4
|
+
property: string,
|
|
5
|
+
milliSecondsOffset?: number,
|
|
6
|
+
validationOptions?: ValidationOptions
|
|
7
|
+
) {
|
|
4
8
|
return function (object: Record<string, any>, propertyName: string): void {
|
|
5
9
|
registerDecorator({
|
|
6
10
|
name: 'greaterThanDate',
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
// eslint-disable-next-line no-restricted-imports
|
|
2
1
|
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
|
|
3
2
|
import { isNil } from 'lodash';
|
|
4
3
|
|
|
5
|
-
export function IsEmptyIf<T>(
|
|
4
|
+
export function IsEmptyIf<T>(
|
|
5
|
+
condition: (object: T) => boolean,
|
|
6
|
+
validationOptions?: ValidationOptions
|
|
7
|
+
) {
|
|
6
8
|
return function (object: Record<string, any>, propertyName: string): void {
|
|
7
9
|
registerDecorator({
|
|
8
10
|
name: 'isEmpty',
|
|
@@ -15,7 +17,12 @@ export function IsEmptyIf<T>(condition: (object: T) => boolean, validationOption
|
|
|
15
17
|
const [relatedCondition] = args.constraints;
|
|
16
18
|
const cond = relatedCondition(args.object);
|
|
17
19
|
|
|
18
|
-
if (
|
|
20
|
+
if (
|
|
21
|
+
cond &&
|
|
22
|
+
Array.isArray(value) &&
|
|
23
|
+
validationOptions &&
|
|
24
|
+
validationOptions.each
|
|
25
|
+
) {
|
|
19
26
|
return value.every(v => isNil(v));
|
|
20
27
|
} else if (cond) {
|
|
21
28
|
return isNil(value);
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
|
|
2
2
|
import { isNil } from 'lodash';
|
|
3
3
|
|
|
4
|
-
export function IsNotEmptyIf<T>(
|
|
4
|
+
export function IsNotEmptyIf<T>(
|
|
5
|
+
condition: (object: T) => boolean,
|
|
6
|
+
validationOptions?: ValidationOptions
|
|
7
|
+
) {
|
|
5
8
|
return function (object: Record<string, any>, propertyName: string): void {
|
|
6
9
|
registerDecorator({
|
|
7
10
|
name: 'isNotEmpty',
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
isNotIn,
|
|
3
|
+
registerDecorator,
|
|
4
|
+
ValidationArguments,
|
|
5
|
+
ValidationOptions
|
|
6
|
+
} from 'class-validator';
|
|
2
7
|
|
|
3
|
-
export function IsNotInRelativeTo<T, S>(
|
|
8
|
+
export function IsNotInRelativeTo<T, S>(
|
|
9
|
+
excludedValuesCallback: (object: T) => S[],
|
|
10
|
+
validationOptions?: ValidationOptions
|
|
11
|
+
) {
|
|
4
12
|
return function (object: Record<string, any>, propertyName: string): void {
|
|
5
13
|
registerDecorator({
|
|
6
14
|
name: 'isNotInRelativeTo',
|
|
7
15
|
target: object.constructor,
|
|
8
16
|
propertyName: propertyName,
|
|
9
|
-
constraints: [
|
|
17
|
+
constraints: [
|
|
18
|
+
{ name: 'isNotInRelativeTo', relativeValueCallback: excludedValuesCallback }
|
|
19
|
+
],
|
|
10
20
|
options: validationOptions,
|
|
11
21
|
validator: {
|
|
12
22
|
validate(value: any, args: ValidationArguments) {
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
// eslint-disable-next-line no-restricted-imports
|
|
2
1
|
import { ValidateIf, ValidationOptions } from 'class-validator';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Checks if value is missing and if so, ignores all validators.
|
|
6
5
|
**/
|
|
7
|
-
export function IsOptionalIf<T>(
|
|
6
|
+
export function IsOptionalIf<T>(
|
|
7
|
+
condition: (object: T, value: any) => boolean,
|
|
8
|
+
validationOptions?: ValidationOptions
|
|
9
|
+
) {
|
|
8
10
|
return ValidateIf((object: T, value: any) => {
|
|
9
|
-
const valueIsNil =
|
|
11
|
+
const valueIsNil = value === '' || value === null || value === undefined;
|
|
10
12
|
return !valueIsNil || !condition(object, value);
|
|
11
13
|
}, validationOptions);
|
|
12
14
|
}
|
package/src/validators/index.ts
CHANGED
|
@@ -1,21 +1,75 @@
|
|
|
1
1
|
export {
|
|
2
2
|
// array validation methods
|
|
3
|
-
ArrayContains,
|
|
3
|
+
ArrayContains,
|
|
4
|
+
ArrayMaxSize,
|
|
5
|
+
ArrayMinSize,
|
|
6
|
+
ArrayNotContains,
|
|
7
|
+
ArrayNotEmpty,
|
|
8
|
+
ArrayUnique,
|
|
4
9
|
// string validation methods
|
|
5
|
-
Contains,
|
|
10
|
+
Contains,
|
|
11
|
+
Equals,
|
|
12
|
+
IsAlpha,
|
|
13
|
+
IsAlphanumeric,
|
|
6
14
|
// type validation methods
|
|
7
|
-
IsArray,
|
|
15
|
+
IsArray,
|
|
16
|
+
IsAscii,
|
|
17
|
+
IsBase64,
|
|
18
|
+
IsBoolean,
|
|
8
19
|
// string-type validation methods
|
|
9
|
-
IsBooleanString,
|
|
10
|
-
|
|
20
|
+
IsBooleanString,
|
|
21
|
+
IsByteLength,
|
|
22
|
+
IsCreditCard,
|
|
23
|
+
IsCurrency,
|
|
24
|
+
IsDate,
|
|
25
|
+
IsDefined,
|
|
11
26
|
// number validation methods
|
|
12
|
-
IsDivisibleBy,
|
|
13
|
-
|
|
27
|
+
IsDivisibleBy,
|
|
28
|
+
IsEmail,
|
|
29
|
+
IsEmpty,
|
|
30
|
+
IsEnum,
|
|
31
|
+
IsFQDN,
|
|
32
|
+
IsFullWidth,
|
|
33
|
+
IsHalfWidth,
|
|
34
|
+
IsHexColor,
|
|
35
|
+
IsHexadecimal,
|
|
36
|
+
IsIP,
|
|
37
|
+
IsISBN,
|
|
38
|
+
IsISIN,
|
|
39
|
+
IsISO8601,
|
|
40
|
+
IsIn,
|
|
41
|
+
IsInt,
|
|
42
|
+
IsJSON,
|
|
43
|
+
IsLowercase,
|
|
44
|
+
IsMilitaryTime,
|
|
45
|
+
IsMobilePhone,
|
|
46
|
+
IsMongoId,
|
|
47
|
+
IsMultibyte,
|
|
48
|
+
IsNegative,
|
|
49
|
+
IsNotEmpty,
|
|
50
|
+
IsNotIn,
|
|
51
|
+
IsNumber,
|
|
52
|
+
IsNumberString,
|
|
53
|
+
IsOptional,
|
|
54
|
+
IsPositive,
|
|
55
|
+
IsString,
|
|
56
|
+
IsSurrogatePair,
|
|
57
|
+
IsUUID,
|
|
58
|
+
IsUppercase,
|
|
59
|
+
IsUrl,
|
|
60
|
+
IsVariableWidth,
|
|
61
|
+
Length,
|
|
62
|
+
Matches,
|
|
63
|
+
Max,
|
|
64
|
+
MaxDate,
|
|
65
|
+
MaxLength,
|
|
66
|
+
Min,
|
|
14
67
|
// date validation methods
|
|
15
|
-
MinDate,
|
|
68
|
+
MinDate,
|
|
69
|
+
MinLength,
|
|
70
|
+
NotContains,
|
|
71
|
+
NotEquals,
|
|
72
|
+
ValidateNested
|
|
16
73
|
} from 'class-validator';
|
|
17
74
|
export * from './custom-validators';
|
|
18
75
|
export { validate } from './validate';
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
@@ -89,43 +89,75 @@ const validationPriorities = [
|
|
|
89
89
|
'pascalCase'
|
|
90
90
|
];
|
|
91
91
|
|
|
92
|
-
function recursiveGetErrors(
|
|
93
|
-
|
|
92
|
+
function recursiveGetErrors(
|
|
93
|
+
validationErrors: ClassValidatorValidationError[],
|
|
94
|
+
errors: ValidationError[] = [],
|
|
95
|
+
propertyName = '',
|
|
96
|
+
previousValueWasArray = false
|
|
97
|
+
): ValidationError[] {
|
|
94
98
|
for (const validationError of validationErrors) {
|
|
95
99
|
if (!validationError) {
|
|
96
100
|
return null;
|
|
97
101
|
}
|
|
98
102
|
|
|
99
103
|
if (validationError.constraints) {
|
|
100
|
-
const containsNestedValidation =
|
|
104
|
+
const containsNestedValidation =
|
|
105
|
+
Object.keys(validationError.constraints).some(v => v === 'nestedValidation') &&
|
|
106
|
+
validationError.children.length > 0;
|
|
101
107
|
const validations = Object.keys(validationError.constraints);
|
|
102
108
|
|
|
103
|
-
const constraintName = validationPriorities.find(cm =>
|
|
109
|
+
const constraintName = validationPriorities.find(cm =>
|
|
110
|
+
validations.some(cs => cs.split('-')[0] === cm)
|
|
111
|
+
);
|
|
104
112
|
|
|
105
113
|
let domainErrorCode: string;
|
|
106
114
|
let i = 0;
|
|
107
115
|
while (!domainErrorCode) {
|
|
108
|
-
domainErrorCode = validations.find(
|
|
116
|
+
domainErrorCode = validations.find(
|
|
117
|
+
vp => vp.split('-')[0] === validationPriorities[i]
|
|
118
|
+
);
|
|
109
119
|
i++;
|
|
110
120
|
}
|
|
111
121
|
|
|
112
|
-
if (
|
|
122
|
+
if (
|
|
123
|
+
validationError.contexts &&
|
|
124
|
+
validationError.contexts[domainErrorCode] &&
|
|
125
|
+
validationError.contexts[domainErrorCode].domainErrorCodes
|
|
126
|
+
) {
|
|
113
127
|
domainErrorCode = validationError.contexts[domainErrorCode].domainErrorCodes;
|
|
114
128
|
}
|
|
115
129
|
|
|
116
|
-
const domainErrorProperty = propertyName
|
|
130
|
+
const domainErrorProperty = propertyName
|
|
131
|
+
? containsNestedValidation
|
|
132
|
+
? propertyName
|
|
133
|
+
: `${propertyName}.${validationError.property}`
|
|
134
|
+
: validationError.property;
|
|
117
135
|
|
|
118
|
-
errors.push(
|
|
136
|
+
errors.push(
|
|
137
|
+
new ValidationError(
|
|
138
|
+
domainErrorProperty,
|
|
139
|
+
validationError.value,
|
|
140
|
+
constraintName,
|
|
141
|
+
null
|
|
142
|
+
)
|
|
143
|
+
);
|
|
119
144
|
}
|
|
120
145
|
|
|
121
146
|
if (validationError.children && validationError.children.length) {
|
|
122
147
|
let childName = validationError.property;
|
|
123
148
|
if (propertyName) {
|
|
124
|
-
childName = previousValueWasArray
|
|
149
|
+
childName = previousValueWasArray
|
|
150
|
+
? `${propertyName}[${validationError.property}]`
|
|
151
|
+
: `${propertyName}.${validationError.property}`;
|
|
125
152
|
}
|
|
126
153
|
|
|
127
154
|
errors = recursiveGetErrors(
|
|
128
|
-
validationError.children.filter(ve =>
|
|
155
|
+
validationError.children.filter(ve =>
|
|
156
|
+
ve.constraints
|
|
157
|
+
? !Object.keys(ve.constraints).includes('nestedValidation') ||
|
|
158
|
+
ve.children.length === 0
|
|
159
|
+
: true
|
|
160
|
+
),
|
|
129
161
|
errors,
|
|
130
162
|
childName,
|
|
131
163
|
Array.isArray(validationError.value)
|
|
@@ -5,9 +5,8 @@ export class ValidationError {
|
|
|
5
5
|
public readonly property: string,
|
|
6
6
|
public readonly value: string,
|
|
7
7
|
public readonly type: string,
|
|
8
|
-
public readonly message: string | ((args: ValidationArguments) => string)
|
|
9
|
-
|
|
10
|
-
}
|
|
8
|
+
public readonly message: string | ((args: ValidationArguments) => string)
|
|
9
|
+
) {}
|
|
11
10
|
|
|
12
11
|
public getErrorMessage(): string {
|
|
13
12
|
if (this.message && typeof this.message === 'string') {
|
package/src/vendors/lodash.ts
CHANGED