thesimplevalidation 1.1.2 → 2.0.0
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/README.md +158 -140
- package/dist/Validation.d.ts +16 -0
- package/dist/Validation.js +99 -0
- package/dist/Validation.js.map +1 -0
- package/{src/validation-errors-model.ts → dist/ValidationErrorsModel.d.ts} +3 -3
- package/dist/ValidationErrorsModel.js +2 -0
- package/dist/ValidationErrorsModel.js.map +1 -0
- package/{src/validation-model.ts → dist/ValidationModel.d.ts} +3 -3
- package/dist/ValidationModel.js +2 -0
- package/dist/ValidationModel.js.map +1 -0
- package/dist/ValidationPropertyResult.d.ts +4 -0
- package/dist/ValidationPropertyResult.js +2 -0
- package/dist/ValidationPropertyResult.js.map +1 -0
- package/dist/ValidationResult.d.ts +8 -0
- package/dist/ValidationResult.js +2 -0
- package/dist/ValidationResult.js.map +1 -0
- package/dist/ValidationScope.d.ts +15 -0
- package/dist/ValidationScope.js +95 -0
- package/dist/ValidationScope.js.map +1 -0
- package/dist/Validator.d.ts +10 -0
- package/dist/Validator.js +28 -0
- package/dist/Validator.js.map +1 -0
- package/{src/validator-setup.ts → dist/ValidatorSetup.d.ts} +4 -4
- package/dist/ValidatorSetup.js +2 -0
- package/dist/ValidatorSetup.js.map +1 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +5 -6
- package/dist/index.js.map +1 -1
- package/dist/validators/boolean/BooleanValidator.d.ts +7 -0
- package/dist/validators/boolean/BooleanValidator.js +18 -0
- package/dist/validators/boolean/BooleanValidator.js.map +1 -0
- package/dist/validators/boolean/BooleanValidatorSetup.d.ts +4 -0
- package/dist/validators/boolean/BooleanValidatorSetup.js +2 -0
- package/dist/validators/boolean/BooleanValidatorSetup.js.map +1 -0
- package/dist/validators/index.d.ts +9 -0
- package/dist/validators/index.js +6 -0
- package/dist/validators/index.js.map +1 -0
- package/dist/validators/max/MaxValidator.d.ts +7 -0
- package/dist/validators/max/MaxValidator.js +26 -0
- package/dist/validators/max/MaxValidator.js.map +1 -0
- package/dist/validators/max/MaxValidatorSetup.d.ts +4 -0
- package/dist/validators/max/MaxValidatorSetup.js +2 -0
- package/dist/validators/max/MaxValidatorSetup.js.map +1 -0
- package/dist/validators/min/MinValidator.d.ts +7 -0
- package/dist/validators/min/MinValidator.js +27 -0
- package/dist/validators/min/MinValidator.js.map +1 -0
- package/dist/validators/min/MinValidatorSetup.d.ts +4 -0
- package/dist/validators/min/MinValidatorSetup.js +2 -0
- package/dist/validators/min/MinValidatorSetup.js.map +1 -0
- package/dist/validators/required/RequiredValidator.d.ts +7 -0
- package/dist/validators/required/RequiredValidator.js +21 -0
- package/dist/validators/required/RequiredValidator.js.map +1 -0
- package/dist/validators/simple/SimpleValidator.d.ts +7 -0
- package/dist/validators/simple/SimpleValidator.js +14 -0
- package/dist/validators/simple/SimpleValidator.js.map +1 -0
- package/dist/validators/simple/SimpleValidatorSetup.d.ts +4 -0
- package/dist/validators/simple/SimpleValidatorSetup.js +2 -0
- package/dist/validators/simple/SimpleValidatorSetup.js.map +1 -0
- package/package.json +13 -16
- package/.editorconfig +0 -11
- package/.travis.yml +0 -7
- package/.yo-rc.json +0 -9
- package/dist/src/index.d.ts +0 -8
- package/dist/src/index.js +0 -11
- package/dist/src/index.js.map +0 -1
- package/dist/src/validation-errors-model.d.ts +0 -3
- package/dist/src/validation-errors-model.js +0 -3
- package/dist/src/validation-errors-model.js.map +0 -1
- package/dist/src/validation-model.d.ts +0 -3
- package/dist/src/validation-model.js +0 -3
- package/dist/src/validation-model.js.map +0 -1
- package/dist/src/validation-property-result.d.ts +0 -4
- package/dist/src/validation-property-result.js +0 -3
- package/dist/src/validation-property-result.js.map +0 -1
- package/dist/src/validation-result.d.ts +0 -8
- package/dist/src/validation-result.js +0 -3
- package/dist/src/validation-result.js.map +0 -1
- package/dist/src/validation-scope.d.ts +0 -19
- package/dist/src/validation-scope.js +0 -197
- package/dist/src/validation-scope.js.map +0 -1
- package/dist/src/validator-setup.d.ts +0 -4
- package/dist/src/validator-setup.js +0 -3
- package/dist/src/validator-setup.js.map +0 -1
- package/dist/src/validator.d.ts +0 -8
- package/dist/src/validator.js +0 -68
- package/dist/src/validator.js.map +0 -1
- package/dist/src/validators/boolean/index.d.ts +0 -10
- package/dist/src/validators/boolean/index.js +0 -77
- package/dist/src/validators/boolean/index.js.map +0 -1
- package/dist/src/validators/index.d.ts +0 -5
- package/dist/src/validators/index.js +0 -13
- package/dist/src/validators/index.js.map +0 -1
- package/dist/src/validators/max/index.d.ts +0 -11
- package/dist/src/validators/max/index.js +0 -86
- package/dist/src/validators/max/index.js.map +0 -1
- package/dist/src/validators/min/index.d.ts +0 -11
- package/dist/src/validators/min/index.js +0 -87
- package/dist/src/validators/min/index.js.map +0 -1
- package/dist/src/validators/required/index.d.ts +0 -7
- package/dist/src/validators/required/index.js +0 -81
- package/dist/src/validators/required/index.js.map +0 -1
- package/dist/src/validators/simple/index.d.ts +0 -10
- package/dist/src/validators/simple/index.js +0 -73
- package/dist/src/validators/simple/index.js.map +0 -1
- package/index.ts +0 -1
- package/jest.config.js +0 -192
- package/src/index.ts +0 -9
- package/src/validation-property-result.ts +0 -5
- package/src/validation-result.ts +0 -11
- package/src/validation-scope.ts +0 -118
- package/src/validator.ts +0 -25
- package/src/validators/boolean/index.ts +0 -27
- package/src/validators/index.ts +0 -5
- package/src/validators/max/__tests__/index.test.ts +0 -92
- package/src/validators/max/index.ts +0 -34
- package/src/validators/min/__tests__/index.test.ts +0 -92
- package/src/validators/min/index.ts +0 -35
- package/src/validators/required/__tests__/index.test.ts +0 -229
- package/src/validators/required/index.ts +0 -24
- package/src/validators/simple/index.ts +0 -22
- package/thesimplevalidation.code-workspace +0 -10
- package/tsconfig.json +0 -23
- package/tslint.json +0 -18
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { IValidationPropertyResult } from "../../validation-property-result";
|
|
2
|
-
import { IValidatorSetup } from "../../validator-setup";
|
|
3
|
-
import { Validator } from "../../validator";
|
|
4
|
-
|
|
5
|
-
export interface IBooleanValidatorSetup<TModel, K extends keyof TModel> extends IValidatorSetup<TModel, K> {
|
|
6
|
-
target: () => boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class BooleanValidator<TModel, K extends keyof TModel> extends Validator<TModel, K, IBooleanValidatorSetup<TModel, K>> {
|
|
10
|
-
constructor(setup: IBooleanValidatorSetup<TModel, K>) {
|
|
11
|
-
super(setup);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public async isValidInternal(value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
15
|
-
let isValid: boolean = true;
|
|
16
|
-
const targetValue: boolean = this.setup.target();
|
|
17
|
-
|
|
18
|
-
if (value != null && typeof value === "boolean") {
|
|
19
|
-
isValid = targetValue === value;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
isValid: isValid,
|
|
24
|
-
errors: []
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
}
|
package/src/validators/index.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { SimpleValidator, ISimpleValidatorSetup } from "./simple";
|
|
2
|
-
export { BooleanValidator, IBooleanValidatorSetup } from "./boolean";
|
|
3
|
-
export { RequiredValidator } from "./required";
|
|
4
|
-
export { MinValidator, IMinValidatorSetup } from "./min";
|
|
5
|
-
export { MaxValidator, IMaxValidatorSetup } from "./max";
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { MaxValidator } from "..";
|
|
2
|
-
|
|
3
|
-
describe("[validation].[MaxValidator] checking", () => {
|
|
4
|
-
test("[validation].[MaxValidator]: null", async () => {
|
|
5
|
-
const validator = new MaxValidator<{ value: string | null }, "value">({
|
|
6
|
-
maxValue: () => 1,
|
|
7
|
-
});
|
|
8
|
-
const result = await validator.isValidInternal(null);
|
|
9
|
-
expect(result.isValid).toEqual(true);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("[validation].[MaxValidator]: function", async () => {
|
|
13
|
-
const validator = new MaxValidator<{ value: () => void; }, "value">({
|
|
14
|
-
maxValue: () => 1
|
|
15
|
-
});
|
|
16
|
-
const result = await validator.isValidInternal(() => { return; });
|
|
17
|
-
expect(result.isValid).toEqual(true);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("[validation].[MaxValidator]: Empty String. Max=1", async () => {
|
|
21
|
-
const validator = new MaxValidator<{ value: string; }, "value">({
|
|
22
|
-
maxValue: () => 1
|
|
23
|
-
});
|
|
24
|
-
const result = await validator.isValidInternal("");
|
|
25
|
-
expect(result.isValid).toEqual(true);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("[validation].[MaxValidator]: Empty String. Max=0", async () => {
|
|
29
|
-
const validator = new MaxValidator<{ value: string; }, "value">({
|
|
30
|
-
maxValue: () => 0
|
|
31
|
-
});
|
|
32
|
-
const result = await validator.isValidInternal("");
|
|
33
|
-
expect(result.isValid).toEqual(true);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("[validation].[MaxValidator]: Empty Array. Max=1", async () => {
|
|
37
|
-
const validator = new MaxValidator<{ value: string[]; }, "value">({
|
|
38
|
-
maxValue: () => 1
|
|
39
|
-
});
|
|
40
|
-
const result = await validator.isValidInternal([]);
|
|
41
|
-
expect(result.isValid).toEqual(true);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("[validation].[MaxValidator]: Empty Array. Max=0", async () => {
|
|
45
|
-
const validator = new MaxValidator<{ value: string[]; }, "value">({
|
|
46
|
-
maxValue: () => 0
|
|
47
|
-
});
|
|
48
|
-
const result = await validator.isValidInternal([]);
|
|
49
|
-
expect(result.isValid).toEqual(true);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("[validation].[MaxValidator]: 0. Max=0", async () => {
|
|
53
|
-
const validator = new MaxValidator<{ value: number; }, "value">({
|
|
54
|
-
maxValue: () => 0
|
|
55
|
-
});
|
|
56
|
-
const result = await validator.isValidInternal(0);
|
|
57
|
-
expect(result.isValid).toEqual(true);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("[validation].[MaxValidator]: 0. Max=1", async () => {
|
|
61
|
-
const validator = new MaxValidator<{ value: number; }, "value">({
|
|
62
|
-
maxValue: () => 1
|
|
63
|
-
});
|
|
64
|
-
const result = await validator.isValidInternal(0);
|
|
65
|
-
expect(result.isValid).toEqual(true);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test("[validation].[MaxValidator]: 1. Max=1", async () => {
|
|
69
|
-
const validator = new MaxValidator<{ value: number; }, "value">({
|
|
70
|
-
maxValue: () => 1
|
|
71
|
-
});
|
|
72
|
-
const result = await validator.isValidInternal(0);
|
|
73
|
-
expect(result.isValid).toEqual(true);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("[validation].[MaxValidator]: 2. Max=1", async () => {
|
|
77
|
-
const validator = new MaxValidator<{ value: number; }, "value">({
|
|
78
|
-
maxValue: () => 1
|
|
79
|
-
});
|
|
80
|
-
const result = await validator.isValidInternal(2);
|
|
81
|
-
expect(result.isValid).toEqual(false);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("[validation].[MaxValidator]: Disabled", async () => {
|
|
85
|
-
const validator = new MaxValidator<{ value: number; }, "value">({
|
|
86
|
-
maxValue: () => 1,
|
|
87
|
-
isDisabled: () => true
|
|
88
|
-
});
|
|
89
|
-
const result = await validator.isValid(2, { value: 2 }, "value");
|
|
90
|
-
expect(result.isValid).toEqual(true);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { IValidatorSetup } from "../../validator-setup";
|
|
2
|
-
import { IValidationPropertyResult } from "../../validation-property-result";
|
|
3
|
-
import { Validator } from "../../validator";
|
|
4
|
-
|
|
5
|
-
export interface IMaxValidatorSetup<TModel, K extends keyof TModel> extends IValidatorSetup<TModel, K> {
|
|
6
|
-
maxValue: () => number;
|
|
7
|
-
converter?: (val: TModel[K]) => unknown;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class MaxValidator<TModel, K extends keyof TModel> extends Validator<TModel, K, IMaxValidatorSetup<TModel, K>> {
|
|
11
|
-
constructor(setup: IMaxValidatorSetup<TModel, K>) {
|
|
12
|
-
super(setup);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public async isValidInternal(value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
16
|
-
let isValid: boolean = true;
|
|
17
|
-
const val: unknown = this.setup.converter ? this.setup.converter(value) : value;
|
|
18
|
-
if (val != null) {
|
|
19
|
-
const maxValue: number = this.setup.maxValue();
|
|
20
|
-
if (Array.isArray(val)) {
|
|
21
|
-
isValid = val.length <= maxValue;
|
|
22
|
-
} else if (typeof val === "string") {
|
|
23
|
-
isValid = val.length <= maxValue;
|
|
24
|
-
} else if (typeof val === "number") {
|
|
25
|
-
isValid = val <= maxValue;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
isValid: isValid,
|
|
31
|
-
errors: []
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { MinValidator } from "..";
|
|
2
|
-
|
|
3
|
-
describe("[validation].[MinValidator] checking", () => {
|
|
4
|
-
test("[validation].[MinValidator]: null", async () => {
|
|
5
|
-
const validator = new MinValidator<{ value: string | null; }, "value">({
|
|
6
|
-
minValue: () => 1
|
|
7
|
-
});
|
|
8
|
-
const result = await validator.isValidInternal(null);
|
|
9
|
-
expect(result.isValid).toEqual(true);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("[validation].[MinValidator]: function", async () => {
|
|
13
|
-
const validator = new MinValidator<{ value: () => void; }, "value">({
|
|
14
|
-
minValue: () => 1
|
|
15
|
-
});
|
|
16
|
-
const result = await validator.isValidInternal(() => { return; });
|
|
17
|
-
expect(result.isValid).toEqual(true);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("[validation].[MinValidator]: Empty String. Min=1", async () => {
|
|
21
|
-
const validator = new MinValidator<{ value: string; }, "value">({
|
|
22
|
-
minValue: () => 1
|
|
23
|
-
});
|
|
24
|
-
const result = await validator.isValidInternal("");
|
|
25
|
-
expect(result.isValid).toEqual(false);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("[validation].[MinValidator]: Empty String. Min=0", async () => {
|
|
29
|
-
const validator = new MinValidator<{ value: string; }, "value">({
|
|
30
|
-
minValue: () => 0
|
|
31
|
-
});
|
|
32
|
-
const result = await validator.isValidInternal("");
|
|
33
|
-
expect(result.isValid).toEqual(true);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("[validation].[MinValidator]: Empty Array. Min=1", async () => {
|
|
37
|
-
const validator = new MinValidator<{ value: string[]; }, "value">({
|
|
38
|
-
minValue: () => 1
|
|
39
|
-
});
|
|
40
|
-
const result = await validator.isValidInternal([]);
|
|
41
|
-
expect(result.isValid).toEqual(false);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("[validation].[MinValidator]: Empty Array. Min=0", async () => {
|
|
45
|
-
const validator = new MinValidator<{ value: string[]; }, "value">({
|
|
46
|
-
minValue: () => 0
|
|
47
|
-
});
|
|
48
|
-
const result = await validator.isValidInternal([]);
|
|
49
|
-
expect(result.isValid).toEqual(true);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("[validation].[MinValidator]: 0. Min=0", async () => {
|
|
53
|
-
const validator = new MinValidator<{ value: number; }, "value">({
|
|
54
|
-
minValue: () => 0
|
|
55
|
-
});
|
|
56
|
-
const result = await validator.isValidInternal(0);
|
|
57
|
-
expect(result.isValid).toEqual(true);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("[validation].[MinValidator]: 0. Min=1", async () => {
|
|
61
|
-
const validator = new MinValidator<{ value: number; }, "value">({
|
|
62
|
-
minValue: () => 1
|
|
63
|
-
});
|
|
64
|
-
const result = await validator.isValidInternal(0);
|
|
65
|
-
expect(result.isValid).toEqual(false);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test("[validation].[MinValidator]: 1. Min=1", async () => {
|
|
69
|
-
const validator = new MinValidator<{ value: number; }, "value">({
|
|
70
|
-
minValue: () => 1
|
|
71
|
-
});
|
|
72
|
-
const result = await validator.isValidInternal(0);
|
|
73
|
-
expect(result.isValid).toEqual(false);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("[validation].[MinValidator]: 8. Min=9", async () => {
|
|
77
|
-
const validator = new MinValidator<{ value: number; }, "value">({
|
|
78
|
-
minValue: () => 9
|
|
79
|
-
});
|
|
80
|
-
const result = await validator.isValidInternal(8);
|
|
81
|
-
expect(result.isValid).toEqual(false);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("[validation].[MinValidator]: Disabled", async () => {
|
|
85
|
-
const validator = new MinValidator<{ value: number; }, "value">({
|
|
86
|
-
minValue: () => 9,
|
|
87
|
-
isDisabled: () => true
|
|
88
|
-
});
|
|
89
|
-
const result = await validator.isValid(8, { value: 8 }, "value");
|
|
90
|
-
expect(result.isValid).toEqual(true);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { IValidationPropertyResult } from "../../validation-property-result";
|
|
2
|
-
import { IValidatorSetup } from "../../validator-setup";
|
|
3
|
-
import { Validator } from "../../validator";
|
|
4
|
-
|
|
5
|
-
export interface IMinValidatorSetup<TModel, K extends keyof TModel> extends IValidatorSetup<TModel, K> {
|
|
6
|
-
minValue: () => number;
|
|
7
|
-
converter?: (val: TModel[K]) => unknown;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class MinValidator<TModel, K extends keyof TModel> extends Validator<TModel, K, IMinValidatorSetup<TModel, K>> {
|
|
11
|
-
constructor(setup: IMinValidatorSetup<TModel, K>) {
|
|
12
|
-
super(setup);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public async isValidInternal(value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
16
|
-
let isValid: boolean = true;
|
|
17
|
-
const val: unknown = this.setup.converter ? this.setup.converter(value) : value;
|
|
18
|
-
if (val != null) {
|
|
19
|
-
const minValue: number = this.setup.minValue();
|
|
20
|
-
// tslint:disable-next-line:prefer-conditional-expression
|
|
21
|
-
if (Array.isArray(val)) {
|
|
22
|
-
isValid = val.length >= minValue;
|
|
23
|
-
} else if (typeof val === "string") {
|
|
24
|
-
isValid = val.length >= minValue;
|
|
25
|
-
} else if (typeof val === "number") {
|
|
26
|
-
isValid = val >= minValue;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
isValid: isValid,
|
|
32
|
-
errors: []
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import { Validator } from "../../../validator";
|
|
2
|
-
import { RequiredValidator } from "..";
|
|
3
|
-
|
|
4
|
-
// String check
|
|
5
|
-
test("[validation].[RequiredValidator]: check null string", async () => {
|
|
6
|
-
interface IModelType {
|
|
7
|
-
val: string | null;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const model: IModelType = {
|
|
11
|
-
val: null
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
15
|
-
getErrors: () => ["Error.Message"]
|
|
16
|
-
});
|
|
17
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
18
|
-
.resolves
|
|
19
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("[validation].[RequiredValidator]: check empty string", async () => {
|
|
23
|
-
interface IModelType {
|
|
24
|
-
val: string | null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const model: IModelType = {
|
|
28
|
-
val: ""
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
32
|
-
getErrors: () => ["Error.Message"]
|
|
33
|
-
});
|
|
34
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
35
|
-
.resolves
|
|
36
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("[validation].[RequiredValidator]: check non-empty string", async () => {
|
|
40
|
-
interface IModelType {
|
|
41
|
-
val: string | null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const model: IModelType = {
|
|
45
|
-
val: "HelloWorld"
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
49
|
-
getErrors: () => ["Error.Message"]
|
|
50
|
-
});
|
|
51
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
52
|
-
.resolves
|
|
53
|
-
.toEqual({ errors: [], isValid: true });
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// Number check
|
|
57
|
-
test("[validation].[RequiredValidator]: check null number", async () => {
|
|
58
|
-
interface IModelType {
|
|
59
|
-
val: number | null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const model: IModelType = {
|
|
63
|
-
val: null
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
67
|
-
getErrors: () => ["Error.Message"]
|
|
68
|
-
});
|
|
69
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
70
|
-
.resolves
|
|
71
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("[validation].[RequiredValidator]: check 0 number", async () => {
|
|
75
|
-
interface IModelType {
|
|
76
|
-
val: number | null;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const model: IModelType = {
|
|
80
|
-
val: 0
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
84
|
-
getErrors: () => ["Error.Message"]
|
|
85
|
-
});
|
|
86
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
87
|
-
.resolves
|
|
88
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("[validation].[RequiredValidator]: check > 0 number", async () => {
|
|
92
|
-
interface IModelType {
|
|
93
|
-
val: number | null;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const model: IModelType = {
|
|
97
|
-
val: 0.1
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
101
|
-
getErrors: () => ["Error.Message"]
|
|
102
|
-
});
|
|
103
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
104
|
-
.resolves
|
|
105
|
-
.toEqual({ errors: [], isValid: true });
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test("[validation].[RequiredValidator]: check < 0 number", async () => {
|
|
109
|
-
interface IModelType {
|
|
110
|
-
val: number | null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const model: IModelType = {
|
|
114
|
-
val: -0.1
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
118
|
-
getErrors: () => ["Error.Message"]
|
|
119
|
-
});
|
|
120
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
121
|
-
.resolves
|
|
122
|
-
.toEqual({ errors: [], isValid: true });
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// Array check
|
|
126
|
-
test("[validation].[RequiredValidator]: check null array", async () => {
|
|
127
|
-
interface IModelType {
|
|
128
|
-
val: string[] | null;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const model: IModelType = {
|
|
132
|
-
val: null
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
136
|
-
getErrors: () => ["Error.Message"]
|
|
137
|
-
});
|
|
138
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
139
|
-
.resolves
|
|
140
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test("[validation].[RequiredValidator]: check empty array", async () => {
|
|
144
|
-
interface IModelType {
|
|
145
|
-
val: string[] | null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const model: IModelType = {
|
|
149
|
-
val: []
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
153
|
-
getErrors: () => ["Error.Message"]
|
|
154
|
-
});
|
|
155
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
156
|
-
.resolves
|
|
157
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
test("[validation].[RequiredValidator]: check non-empty array", async () => {
|
|
161
|
-
interface IModelType {
|
|
162
|
-
val: string[] | null;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const model: IModelType = {
|
|
166
|
-
val: ["HelloWorld"]
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
170
|
-
getErrors: () => ["Error.Message"]
|
|
171
|
-
});
|
|
172
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
173
|
-
.resolves
|
|
174
|
-
.toEqual({ errors: [], isValid: true });
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
// Object check
|
|
178
|
-
test("[validation].[RequiredValidator]: check null object", async () => {
|
|
179
|
-
interface IModelType {
|
|
180
|
-
val: {} | null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const model: IModelType = {
|
|
184
|
-
val: null
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
188
|
-
getErrors: () => ["Error.Message"]
|
|
189
|
-
});
|
|
190
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
191
|
-
.resolves
|
|
192
|
-
.toEqual({ errors: ["Error.Message"], isValid: false });
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
test("[validation].[RequiredValidator]: check non-null object", async () => {
|
|
196
|
-
interface IModelType {
|
|
197
|
-
val: {} | null;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const model: IModelType = {
|
|
201
|
-
val: {}
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
205
|
-
getErrors: () => ["Error.Message"]
|
|
206
|
-
});
|
|
207
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
208
|
-
.resolves
|
|
209
|
-
.toEqual({ errors: [], isValid: true });
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
// Disabling
|
|
213
|
-
test("[validation].[RequiredValidator]: disabling check", async () => {
|
|
214
|
-
interface IModelType {
|
|
215
|
-
val: {} | null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const model: IModelType = {
|
|
219
|
-
val: null
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
const validator: Validator<IModelType, "val"> = new RequiredValidator({
|
|
223
|
-
getErrors: () => ["Error.Message"],
|
|
224
|
-
isDisabled: () => true
|
|
225
|
-
});
|
|
226
|
-
await expect(validator.isValid(model.val, model, "val"))
|
|
227
|
-
.resolves
|
|
228
|
-
.toEqual({ errors: [], isValid: true });
|
|
229
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { IValidationPropertyResult } from "../../validation-property-result";
|
|
2
|
-
import { IValidatorSetup } from "../../validator-setup";
|
|
3
|
-
import { Validator } from "../../validator";
|
|
4
|
-
|
|
5
|
-
export class RequiredValidator<TModel, K extends keyof TModel> extends Validator<TModel, K> {
|
|
6
|
-
constructor(setup: IValidatorSetup<TModel, K> = {}) {
|
|
7
|
-
super(setup);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public async isValidInternal(value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
11
|
-
let isValid: boolean = true;
|
|
12
|
-
// tslint:disable-next-line:prefer-conditional-expression
|
|
13
|
-
if (Array.isArray(value)) {
|
|
14
|
-
isValid = value.length > 0;
|
|
15
|
-
} else {
|
|
16
|
-
isValid = !!value;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
isValid: isValid,
|
|
21
|
-
errors: []
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { IValidationPropertyResult } from "../../validation-property-result";
|
|
2
|
-
import { IValidatorSetup } from "../../validator-setup";
|
|
3
|
-
import { Validator } from "../../validator";
|
|
4
|
-
|
|
5
|
-
export interface ISimpleValidatorSetup<TModel, K extends keyof TModel> extends IValidatorSetup<TModel, K> {
|
|
6
|
-
getValidation: () => boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class SimpleValidator<TModel, K extends keyof TModel> extends Validator<TModel, K, ISimpleValidatorSetup<TModel, K>> {
|
|
10
|
-
constructor(setup: ISimpleValidatorSetup<TModel, K>) {
|
|
11
|
-
super(setup);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public async isValidInternal(_value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
15
|
-
const isValid: boolean = this.setup.getValidation();
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
isValid: isValid,
|
|
19
|
-
errors: []
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"outDir": "./dist/",
|
|
4
|
-
"sourceMap": true,
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"noImplicitAny": true,
|
|
7
|
-
"module": "commonjs",
|
|
8
|
-
"moduleResolution": "node",
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"noUnusedLocals": true,
|
|
11
|
-
"noImplicitReturns": true,
|
|
12
|
-
"traceResolution": false,
|
|
13
|
-
"allowSyntheticDefaultImports": true,
|
|
14
|
-
"target": "es5",
|
|
15
|
-
"lib": [
|
|
16
|
-
"dom",
|
|
17
|
-
"es2015"
|
|
18
|
-
]
|
|
19
|
-
},
|
|
20
|
-
"include": [
|
|
21
|
-
"./index.ts"
|
|
22
|
-
]
|
|
23
|
-
}
|
package/tslint.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"defaultSeverity": "error",
|
|
3
|
-
"extends": [
|
|
4
|
-
"tslint:recommended"
|
|
5
|
-
],
|
|
6
|
-
"jsRules": {},
|
|
7
|
-
"rules": {
|
|
8
|
-
"curly": [true, "ignore-same-line"],
|
|
9
|
-
"object-literal-shorthand": [true, "never"],
|
|
10
|
-
"trailing-comma": false,
|
|
11
|
-
"object-literal-sort-keys": false,
|
|
12
|
-
"max-line-length": false,
|
|
13
|
-
"no-console": false,
|
|
14
|
-
"arrow-parens": false,
|
|
15
|
-
"ordered-imports": false
|
|
16
|
-
},
|
|
17
|
-
"rulesDirectory": []
|
|
18
|
-
}
|