Skip to content

Commit a059201

Browse files
committed
feat
1 parent d745181 commit a059201

File tree

129 files changed

+2621
-1387
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+2621
-1387
lines changed

src/container.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
21
/**
32
* Container options.
43
*/
54
export interface UseContainerOptions {
6-
75
/**
86
* If set to true, then default container will be used in the case if given container haven't returned anything.
97
*/
@@ -13,17 +11,20 @@ export interface UseContainerOptions {
1311
* If set to true, then default container will be used in the case if given container thrown an exception.
1412
*/
1513
fallbackOnErrors?: boolean;
16-
1714
}
1815

1916
/**
2017
* Container to be used by this library for inversion control. If container was not implicitly set then by default
2118
* container simply creates a new instance of the given class.
2219
*/
23-
const defaultContainer: { get<T>(someClass: { new (...args: any[]): T }|Function): T } = new (class {
24-
private instances: { type: Function, object: any }[] = [];
20+
const defaultContainer: {
21+
get<T>(someClass: { new (...args: any[]): T } | Function): T;
22+
} = new (class {
23+
private instances: { type: Function; object: any }[] = [];
2524
get<T>(someClass: { new (...args: any[]): T }): T {
26-
let instance = this.instances.find(instance => instance.type === someClass);
25+
let instance = this.instances.find(
26+
(instance) => instance.type === someClass
27+
);
2728
if (!instance) {
2829
instance = { type: someClass, object: new someClass() };
2930
this.instances.push(instance);
@@ -33,30 +34,35 @@ const defaultContainer: { get<T>(someClass: { new (...args: any[]): T }|Function
3334
}
3435
})();
3536

36-
let userContainer: { get<T>(someClass: { new (...args: any[]): T }|Function): T };
37+
let userContainer: {
38+
get<T>(someClass: { new (...args: any[]): T } | Function): T;
39+
};
3740
let userContainerOptions: UseContainerOptions;
3841

3942
/**
4043
* Sets container to be used by this library.
4144
*/
42-
export function useContainer(iocContainer: { get(someClass: any): any }, options?: UseContainerOptions) {
45+
export function useContainer(
46+
iocContainer: { get(someClass: any): any },
47+
options?: UseContainerOptions
48+
) {
4349
userContainer = iocContainer;
4450
userContainerOptions = options;
4551
}
4652

4753
/**
4854
* Gets the IOC container used by this library.
4955
*/
50-
export function getFromContainer<T>(someClass: { new (...args: any[]): T }|Function): T {
56+
export function getFromContainer<T>(
57+
someClass: { new (...args: any[]): T } | Function
58+
): T {
5159
if (userContainer) {
5260
try {
5361
const instance = userContainer.get(someClass);
54-
if (instance)
55-
return instance;
62+
if (instance) return instance;
5663

5764
if (!userContainerOptions || !userContainerOptions.fallback)
5865
return instance;
59-
6066
} catch (error) {
6167
if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)
6268
throw error;

src/decorator/ValidationOptions.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { ValidationArguments } from "../validation/ValidationArguments";
1+
import { ValidationArguments } from "../validation/ValidationArguments.ts";
22

33
/**
44
* Options used to pass to validation decorators.
55
*/
66
export interface ValidationOptions {
7-
87
/**
98
* Specifies if validated value is an array and each of its items must be validated.
109
*/
@@ -32,14 +31,15 @@ export interface ValidationOptions {
3231
context?: any;
3332
}
3433

35-
3634
export function isValidationOptions(val: any): val is ValidationOptions {
3735
if (!val) {
3836
return false;
3937
}
40-
return "each" in val
41-
|| "message" in val
42-
|| "groups" in val
43-
|| "always" in val
44-
|| "context" in val;
38+
return (
39+
"each" in val ||
40+
"message" in val ||
41+
"groups" in val ||
42+
"always" in val ||
43+
"context" in val
44+
);
4545
}

src/decorator/array/ArrayContains.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { buildMessage, ValidateBy } from "../common/ValidateBy";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { buildMessage, ValidateBy } from "../common/ValidateBy.ts";
33

44
export const ARRAY_CONTAINS = "arrayContains";
55

@@ -8,28 +8,33 @@ export const ARRAY_CONTAINS = "arrayContains";
88
* If null or undefined is given then this function returns false.
99
*/
1010
export function arrayContains(array: unknown, values: any[]) {
11-
if (!(array instanceof Array))
12-
return false;
11+
if (!(array instanceof Array)) return false;
1312

14-
return values.every(value => array.indexOf(value) !== -1);
13+
return values.every((value) => array.indexOf(value) !== -1);
1514
}
1615

1716
/**
1817
* Checks if array contains all values from the given array of values.
1918
* If null or undefined is given then this function returns false.
2019
*/
21-
export function ArrayContains(values: any[], validationOptions?: ValidationOptions): PropertyDecorator {
20+
export function ArrayContains(
21+
values: any[],
22+
validationOptions?: ValidationOptions
23+
): PropertyDecorator {
2224
return ValidateBy(
2325
{
2426
name: ARRAY_CONTAINS,
2527
constraints: [values],
2628
validator: {
27-
validate: (value, args) => arrayContains(value, args.constraints[0]),
29+
validate: (value, args) =>
30+
arrayContains(value, args?.constraints[0]),
2831
defaultMessage: buildMessage(
29-
(eachPrefix) => eachPrefix + "$property must contain $constraint1 values",
32+
(eachPrefix) =>
33+
eachPrefix +
34+
"$property must contain $constraint1 values",
3035
validationOptions
31-
)
32-
}
36+
),
37+
},
3338
},
3439
validationOptions
3540
);

src/decorator/array/ArrayMaxSize.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { buildMessage, ValidateBy } from "../common/ValidateBy";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { buildMessage, ValidateBy } from "../common/ValidateBy.ts";
33

44
export const ARRAY_MAX_SIZE = "arrayMaxSize";
55

@@ -15,18 +15,24 @@ export function arrayMaxSize(array: unknown, max: number) {
1515
* Checks if array's length is as maximal this number.
1616
* If null or undefined is given then this function returns false.
1717
*/
18-
export function ArrayMaxSize(max: number, validationOptions?: ValidationOptions): PropertyDecorator {
18+
export function ArrayMaxSize(
19+
max: number,
20+
validationOptions?: ValidationOptions
21+
): PropertyDecorator {
1922
return ValidateBy(
2023
{
2124
name: ARRAY_MAX_SIZE,
2225
constraints: [max],
2326
validator: {
24-
validate: (value, args) => arrayMaxSize(value, args.constraints[0]),
27+
validate: (value, args) =>
28+
arrayMaxSize(value, args?.constraints[0]),
2529
defaultMessage: buildMessage(
26-
(eachPrefix) => eachPrefix + "$property must contain not more than $constraint1 elements",
30+
(eachPrefix) =>
31+
eachPrefix +
32+
"$property must contain not more than $constraint1 elements",
2733
validationOptions
28-
)
29-
}
34+
),
35+
},
3036
},
3137
validationOptions
3238
);

src/decorator/array/ArrayMinSize.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { buildMessage, ValidateBy } from "../common/ValidateBy";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { buildMessage, ValidateBy } from "../common/ValidateBy.ts";
33

44
export const ARRAY_MIN_SIZE = "arrayMinSize";
55

@@ -15,18 +15,24 @@ export function arrayMinSize(array: unknown, min: number) {
1515
* Checks if array's length is as minimal this number.
1616
* If null or undefined is given then this function returns false.
1717
*/
18-
export function ArrayMinSize(min: number, validationOptions?: ValidationOptions): PropertyDecorator {
18+
export function ArrayMinSize(
19+
min: number,
20+
validationOptions?: ValidationOptions
21+
): PropertyDecorator {
1922
return ValidateBy(
2023
{
2124
name: ARRAY_MIN_SIZE,
2225
constraints: [min],
2326
validator: {
24-
validate: (value, args) => arrayMinSize(value, args.constraints[0]),
27+
validate: (value, args) =>
28+
arrayMinSize(value, args?.constraints[0]),
2529
defaultMessage: buildMessage(
26-
(eachPrefix) => eachPrefix + "$property must contain at least $constraint1 elements",
30+
(eachPrefix) =>
31+
eachPrefix +
32+
"$property must contain at least $constraint1 elements",
2733
validationOptions
28-
)
29-
}
34+
),
35+
},
3036
},
3137
validationOptions
3238
);

src/decorator/array/ArrayNotContains.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { buildMessage, ValidateBy } from "../common/ValidateBy";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { buildMessage, ValidateBy } from "../common/ValidateBy.ts";
33

44
export const ARRAY_NOT_CONTAINS = "arrayNotContains";
55

@@ -8,28 +8,33 @@ export const ARRAY_NOT_CONTAINS = "arrayNotContains";
88
* If null or undefined is given then this function returns false.
99
*/
1010
export function arrayNotContains(array: unknown, values: any[]) {
11-
if (!(array instanceof Array))
12-
return false;
11+
if (!(array instanceof Array)) return false;
1312

14-
return values.every(value => array.indexOf(value) === -1);
13+
return values.every((value) => array.indexOf(value) === -1);
1514
}
1615

1716
/**
1817
* Checks if array does not contain any of the given values.
1918
* If null or undefined is given then this function returns false.
2019
*/
21-
export function ArrayNotContains(values: any[], validationOptions?: ValidationOptions): PropertyDecorator {
20+
export function ArrayNotContains(
21+
values: any[],
22+
validationOptions?: ValidationOptions
23+
): PropertyDecorator {
2224
return ValidateBy(
2325
{
2426
name: ARRAY_NOT_CONTAINS,
2527
constraints: [values],
2628
validator: {
27-
validate: (value, args) => arrayNotContains(value, args.constraints[0]),
29+
validate: (value, args) =>
30+
arrayNotContains(value, args?.constraints[0]),
2831
defaultMessage: buildMessage(
29-
(eachPrefix) => eachPrefix + "$property should not contain $constraint1 values",
32+
(eachPrefix) =>
33+
eachPrefix +
34+
"$property should not contain $constraint1 values",
3035
validationOptions
31-
)
32-
}
36+
),
37+
},
3338
},
3439
validationOptions
3540
);

src/decorator/array/ArrayNotEmpty.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { buildMessage, ValidateBy } from "../common/ValidateBy";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { buildMessage, ValidateBy } from "../common/ValidateBy.ts";
33

44
export const ARRAY_NOT_EMPTY = "arrayNotEmpty";
55

@@ -15,17 +15,20 @@ export function arrayNotEmpty(array: unknown) {
1515
* Checks if given array is not empty.
1616
* If null or undefined is given then this function returns false.
1717
*/
18-
export function ArrayNotEmpty(validationOptions?: ValidationOptions): PropertyDecorator {
18+
export function ArrayNotEmpty(
19+
validationOptions?: ValidationOptions
20+
): PropertyDecorator {
1921
return ValidateBy(
2022
{
2123
name: ARRAY_NOT_EMPTY,
2224
validator: {
2325
validate: (value, args) => arrayNotEmpty(value),
2426
defaultMessage: buildMessage(
25-
(eachPrefix) => eachPrefix + "$property should not be empty",
27+
(eachPrefix) =>
28+
eachPrefix + "$property should not be empty",
2629
validationOptions
27-
)
28-
}
30+
),
31+
},
2932
},
3033
validationOptions
3134
);

src/decorator/array/ArrayUnique.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { buildMessage, ValidateBy } from "../common/ValidateBy";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { buildMessage, ValidateBy } from "../common/ValidateBy.ts";
33

44
export const ARRAY_UNIQUE = "arrayUnique";
55

@@ -8,8 +8,7 @@ export const ARRAY_UNIQUE = "arrayUnique";
88
* If null or undefined is given then this function returns false.
99
*/
1010
export function arrayUnique(array: unknown) {
11-
if (!(array instanceof Array))
12-
return false;
11+
if (!(array instanceof Array)) return false;
1312

1413
const uniqueItems = array.filter((a, b, c) => c.indexOf(a) === b);
1514
return array.length === uniqueItems.length;
@@ -19,17 +18,20 @@ export function arrayUnique(array: unknown) {
1918
* Checks if all array's values are unique. Comparison for objects is reference-based.
2019
* If null or undefined is given then this function returns false.
2120
*/
22-
export function ArrayUnique(validationOptions?: ValidationOptions): PropertyDecorator {
21+
export function ArrayUnique(
22+
validationOptions?: ValidationOptions
23+
): PropertyDecorator {
2324
return ValidateBy(
2425
{
2526
name: ARRAY_UNIQUE,
2627
validator: {
2728
validate: (value, args) => arrayUnique(value),
2829
defaultMessage: buildMessage(
29-
(eachPrefix) => eachPrefix + "All $property's elements must be unique",
30+
(eachPrefix) =>
31+
eachPrefix + "All $property's elements must be unique",
3032
validationOptions
31-
)
32-
}
33+
),
34+
},
3335
},
3436
validationOptions
3537
);

src/decorator/common/Allow.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
import { ValidationOptions } from "../ValidationOptions";
2-
import { ValidationMetadataArgs } from "../../metadata/ValidationMetadataArgs";
3-
import { ValidationTypes } from "../../validation/ValidationTypes";
4-
import { ValidationMetadata } from "../../metadata/ValidationMetadata";
5-
import { getMetadataStorage } from "../../metadata/MetadataStorage";
1+
import { ValidationOptions } from "../ValidationOptions.ts";
2+
import { ValidationMetadataArgs } from "../../metadata/ValidationMetadataArgs.ts";
3+
import { ValidationTypes } from "../../validation/ValidationTypes.ts";
4+
import { ValidationMetadata } from "../../metadata/ValidationMetadata.ts";
5+
import { getMetadataStorage } from "../../metadata/MetadataStorage.ts";
66

77
/**
88
* If object has both allowed and not allowed properties a validation error will be thrown.
99
*/
10-
export function Allow(validationOptions?: ValidationOptions): PropertyDecorator {
11-
return function (object: Object, propertyName: string) {
10+
export function Allow(
11+
validationOptions?: ValidationOptions
12+
): PropertyDecorator {
13+
return function (object, propertyName) {
1214
const args: ValidationMetadataArgs = {
1315
type: ValidationTypes.WHITELIST,
1416
target: object.constructor,
15-
propertyName: propertyName,
16-
validationOptions: validationOptions
17+
propertyName: propertyName as string,
18+
validationOptions: validationOptions,
1719
};
18-
getMetadataStorage().addValidationMetadata(new ValidationMetadata(args));
20+
getMetadataStorage().addValidationMetadata(
21+
new ValidationMetadata(args)
22+
);
1923
};
2024
}

0 commit comments

Comments
 (0)