thesimplevalidation 1.0.0 → 1.0.2
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 +207 -269
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- package/dist/src/index.d.ts +8 -8
- package/dist/src/index.js +22 -10
- package/dist/src/index.js.map +1 -1
- package/dist/src/validation-errors-model.d.ts +3 -3
- package/dist/src/validation-errors-model.js +2 -2
- package/dist/src/validation-model.d.ts +3 -3
- package/dist/src/validation-model.js +2 -2
- package/dist/src/validation-property-result.d.ts +4 -4
- package/dist/src/validation-property-result.js +2 -2
- package/dist/src/validation-result.d.ts +8 -8
- package/dist/src/validation-result.js +2 -2
- package/dist/src/validation-scope.d.ts +19 -17
- package/dist/src/validation-scope.js +199 -194
- package/dist/src/validation-scope.js.map +1 -1
- package/dist/src/validator-setup.d.ts +4 -4
- package/dist/src/validator-setup.js +2 -2
- package/dist/src/validator.d.ts +8 -8
- package/dist/src/validator.js +68 -67
- package/dist/src/validator.js.map +1 -1
- package/dist/src/validators/boolean/index.d.ts +10 -10
- package/dist/src/validators/boolean/index.js +79 -76
- package/dist/src/validators/boolean/index.js.map +1 -1
- package/dist/src/validators/index.d.ts +5 -5
- package/dist/src/validators/index.js +13 -12
- package/dist/src/validators/index.js.map +1 -1
- package/dist/src/validators/max/index.d.ts +10 -10
- package/dist/src/validators/max/index.js +87 -87
- package/dist/src/validators/max/index.js.map +1 -1
- package/dist/src/validators/min/index.d.ts +10 -10
- package/dist/src/validators/min/index.js +88 -87
- package/dist/src/validators/min/index.js.map +1 -1
- package/dist/src/validators/required/index.d.ts +7 -7
- package/dist/src/validators/required/index.js +83 -80
- package/dist/src/validators/required/index.js.map +1 -1
- package/dist/src/validators/simple/index.d.ts +10 -10
- package/dist/src/validators/simple/index.js +75 -72
- package/dist/src/validators/simple/index.js.map +1 -1
- package/package.json +16 -9
- package/src/validation-scope.ts +10 -6
- package/src/validators/max/index.ts +6 -9
- package/src/validators/min/index.ts +6 -8
- package/src/validators/simple/index.ts +1 -1
- /package/{simplevalidation.code-workspace → thesimplevalidation.code-workspace} +0 -0
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thesimplevalidation",
|
|
3
3
|
"description": "The simple validation",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc",
|
|
8
|
-
"lint": "tslint index.ts",
|
|
9
|
-
"pretest": "npm run lint",
|
|
10
8
|
"test": "jest"
|
|
11
9
|
},
|
|
12
10
|
"publishConfig": {
|
|
13
|
-
"registry": "
|
|
11
|
+
"registry": "https://registry.npmjs.org"
|
|
14
12
|
},
|
|
15
13
|
"author": "Alexey Ripenko <alexey@ripenko.ru>",
|
|
16
14
|
"license": "MIT",
|
|
@@ -26,11 +24,20 @@
|
|
|
26
24
|
"typescript",
|
|
27
25
|
"promise"
|
|
28
26
|
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"lodash.clonedeep": "4.5.0",
|
|
29
|
+
"lodash.get": "4.4.2",
|
|
30
|
+
"lodash.isequal": "4.5.0",
|
|
31
|
+
"lodash.keys": "4.2.0"
|
|
32
|
+
},
|
|
29
33
|
"devDependencies": {
|
|
30
|
-
"@types/
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
34
|
+
"@types/lodash.clonedeep": "4.5.7",
|
|
35
|
+
"@types/lodash.get": "4.4.7",
|
|
36
|
+
"@types/lodash.isequal": "4.5.6",
|
|
37
|
+
"@types/lodash.keys": "4.2.7",
|
|
38
|
+
"@types/jest": "29.5.1",
|
|
39
|
+
"typescript": "5.0.4",
|
|
40
|
+
"jest": "29.5.0",
|
|
41
|
+
"ts-jest": "29.1.0"
|
|
35
42
|
}
|
|
36
43
|
}
|
package/src/validation-scope.ts
CHANGED
|
@@ -14,8 +14,12 @@ export class ValidationScope<TModel extends {}> {
|
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
constructor(private
|
|
18
|
-
|
|
17
|
+
constructor(private setup: {
|
|
18
|
+
getModel: () => TModel;
|
|
19
|
+
onValidationChanged?: (result: IValidationResult<TModel>) => Promise<void>;
|
|
20
|
+
|
|
21
|
+
}) {
|
|
22
|
+
this.originalModel = cloneDeep(setup.getModel());
|
|
19
23
|
this.modelInfo = {} as any;
|
|
20
24
|
}
|
|
21
25
|
|
|
@@ -32,7 +36,7 @@ export class ValidationScope<TModel extends {}> {
|
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
public isPropertyDirty = (field: keyof TModel, key: string | null = null): boolean => {
|
|
35
|
-
const model: TModel = this.getModel();
|
|
39
|
+
const model: TModel = this.setup.getModel();
|
|
36
40
|
const originalField = this.originalModel[field];
|
|
37
41
|
const modelField = model[field];
|
|
38
42
|
if (Array.isArray(originalField) && Array.isArray(modelField)) {
|
|
@@ -71,7 +75,7 @@ export class ValidationScope<TModel extends {}> {
|
|
|
71
75
|
let isValid: boolean = true;
|
|
72
76
|
let errors: string[] = [];
|
|
73
77
|
|
|
74
|
-
const model: TModel = this.getModel();
|
|
78
|
+
const model: TModel = this.setup.getModel();
|
|
75
79
|
const value: TModel[K] = model[field];
|
|
76
80
|
|
|
77
81
|
for (const validator of this.modelInfo[field].validators) {
|
|
@@ -92,7 +96,7 @@ export class ValidationScope<TModel extends {}> {
|
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
public isValid = async (): Promise<IValidationResult<TModel>> => {
|
|
95
|
-
const properties: Array<keyof TModel> = keys(this.getModel()) as Array<keyof TModel>;
|
|
99
|
+
const properties: Array<keyof TModel> = keys(this.setup.getModel()) as Array<keyof TModel>;
|
|
96
100
|
const result: IValidationResult<TModel> = {
|
|
97
101
|
isValid: true,
|
|
98
102
|
errors: [],
|
|
@@ -108,7 +112,7 @@ export class ValidationScope<TModel extends {}> {
|
|
|
108
112
|
if (!propertyValidationResult.isValid) result.errors = [...result.errors, ...propertyValidationResult.errors];
|
|
109
113
|
}
|
|
110
114
|
|
|
111
|
-
await this.onValidationChanged(result);
|
|
115
|
+
if (this.setup.onValidationChanged != null) await this.setup.onValidationChanged(result);
|
|
112
116
|
|
|
113
117
|
return result;
|
|
114
118
|
}
|
|
@@ -15,15 +15,12 @@ export class MaxValidator<TModel, K extends keyof TModel> extends Validator<TMod
|
|
|
15
15
|
let isValid: boolean = true;
|
|
16
16
|
if (value != null) {
|
|
17
17
|
const maxValue: number = this.setup.maxValue();
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
} else if (typeof value === "number") {
|
|
25
|
-
isValid = value <= maxValue;
|
|
26
|
-
}
|
|
18
|
+
if (Array.isArray(value)) {
|
|
19
|
+
isValid = value.length <= maxValue;
|
|
20
|
+
} else if (typeof value === "string") {
|
|
21
|
+
isValid = value.length <= maxValue;
|
|
22
|
+
} else if (typeof value === "number") {
|
|
23
|
+
isValid = value <= maxValue;
|
|
27
24
|
}
|
|
28
25
|
}
|
|
29
26
|
|
|
@@ -16,14 +16,12 @@ export class MinValidator<TModel, K extends keyof TModel> extends Validator<TMod
|
|
|
16
16
|
if (value != null) {
|
|
17
17
|
const minValue: number = this.setup.minValue();
|
|
18
18
|
// tslint:disable-next-line:prefer-conditional-expression
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
isValid = value >= minValue;
|
|
26
|
-
}
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
isValid = value.length >= minValue;
|
|
21
|
+
} else if (typeof value === "string") {
|
|
22
|
+
isValid = value.length >= minValue;
|
|
23
|
+
} else if (typeof value === "number") {
|
|
24
|
+
isValid = value >= minValue;
|
|
27
25
|
}
|
|
28
26
|
}
|
|
29
27
|
|
|
@@ -11,7 +11,7 @@ export class SimpleValidator<TModel, K extends keyof TModel> extends Validator<T
|
|
|
11
11
|
super(setup);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
public async isValidInternal(
|
|
14
|
+
public async isValidInternal(_value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
15
15
|
const isValid: boolean = this.setup.getValidation();
|
|
16
16
|
|
|
17
17
|
return {
|
|
File without changes
|