thesimplevalidation 1.2.0 → 2.1.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/dist/ValidationErrorsModel.js +2 -0
- package/dist/ValidationErrorsModel.js.map +1 -0
- 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/dist/{src/validator-setup.d.ts → ValidatorSetup.d.ts} +2 -2
- 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 -17
- 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 +12 -12
- 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 -23
- package/dist/src/index.js.map +0 -1
- package/dist/src/validation-errors-model.js +0 -3
- package/dist/src/validation-errors-model.js.map +0 -1
- 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 -200
- package/dist/src/validation-scope.js.map +0 -1
- 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 -69
- 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 -80
- 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 -14
- package/dist/src/validators/index.js.map +0 -1
- package/dist/src/validators/max/index.d.ts +0 -10
- package/dist/src/validators/max/index.js +0 -88
- package/dist/src/validators/max/index.js.map +0 -1
- package/dist/src/validators/min/index.d.ts +0 -10
- package/dist/src/validators/min/index.js +0 -89
- 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 -84
- 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 -76
- 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-errors-model.ts +0 -3
- package/src/validation-model.ts +0 -3
- package/src/validation-property-result.ts +0 -5
- package/src/validation-result.ts +0 -11
- package/src/validation-scope.ts +0 -119
- package/src/validator-setup.ts +0 -4
- 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 -32
- package/src/validators/min/__tests__/index.test.ts +0 -92
- package/src/validators/min/index.ts +0 -33
- 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
- /package/dist/{src/validation-errors-model.d.ts → ValidationErrorsModel.d.ts} +0 -0
- /package/dist/{src/validation-model.d.ts → ValidationModel.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -2,208 +2,209 @@
|
|
|
2
2
|
|
|
3
3
|
The simple validation service
|
|
4
4
|
|
|
5
|
-
[](https://travis-ci.org/ripenko/thesimplevalidation)
|
|
6
|
-
|
|
7
5
|
## Installation
|
|
6
|
+
|
|
8
7
|
```
|
|
9
8
|
npm install --save thesimplevalidation
|
|
10
9
|
```
|
|
11
10
|
|
|
12
11
|
## Setup
|
|
13
|
-
There are paar steps to enable validation.
|
|
14
12
|
|
|
15
|
-
###
|
|
16
|
-
```typescript
|
|
17
|
-
interface IModel {
|
|
18
|
-
title: string;
|
|
19
|
-
description: string;
|
|
20
|
-
}
|
|
21
|
-
```
|
|
13
|
+
### create validation
|
|
22
14
|
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
this.model: IModel = {
|
|
26
|
-
title: "",
|
|
27
|
-
description: ""
|
|
28
|
-
};
|
|
29
|
-
```
|
|
15
|
+
Create a validation using model that is passed to contructor as original model. The original model will be used to identity dirty properties.
|
|
30
16
|
|
|
31
|
-
### 3. Create validation model, validation errors model
|
|
32
17
|
```typescript
|
|
33
|
-
import
|
|
34
|
-
|
|
35
|
-
this.validation: ValidationModel<IModel> = {
|
|
36
|
-
title: false,
|
|
37
|
-
description: true
|
|
38
|
-
};
|
|
18
|
+
import Validation from "thesimplevalidation";
|
|
39
19
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
this.allValidationErrors: string [] = [];
|
|
46
|
-
|
|
47
|
-
this.isValid: boolean = false;
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
### 4. Creating Validation Scope
|
|
52
|
-
```typescript
|
|
53
|
-
import { ValidationScope } from "thesimplevalidation";
|
|
54
|
-
|
|
55
|
-
const scope = new ValidationScope<IModel>({
|
|
56
|
-
getModel: (): IModel => this.model
|
|
20
|
+
const validation = new Validation({
|
|
21
|
+
title: "",
|
|
22
|
+
description: "",
|
|
57
23
|
});
|
|
58
|
-
|
|
59
24
|
```
|
|
60
25
|
|
|
61
|
-
###
|
|
26
|
+
### Add validators to model property(ies)
|
|
27
|
+
|
|
62
28
|
There are some built-in validators like `RequiredValidator`, `MinValidator`, `MaxValidator`, `SimpleValidator`, `BooleanValidator`. You can create own by yourself. It is easy.
|
|
63
29
|
So. To add validators we use validation scope method `useValidators`.
|
|
30
|
+
|
|
64
31
|
```typescript
|
|
65
32
|
import { RequiredValidator } from "thesimplevalidation";
|
|
66
33
|
|
|
67
|
-
this.
|
|
68
|
-
|
|
34
|
+
this.validation.useValidators("title", new RequiredValidator());
|
|
69
35
|
```
|
|
70
36
|
|
|
71
37
|
### 6. Do validation
|
|
38
|
+
|
|
72
39
|
```typescript
|
|
73
|
-
const result
|
|
74
|
-
|
|
40
|
+
const result = await this.scope.isValid({
|
|
41
|
+
title: "",
|
|
42
|
+
description: "",
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(result.isValid, result);
|
|
75
46
|
// -> false, { isValid: false, properties: { title: { isValid: false, errors: [] }, description: { isValid: true, errors: [] } }, errors: [] }
|
|
76
47
|
```
|
|
77
48
|
|
|
78
49
|
## Usage
|
|
79
50
|
|
|
80
|
-
###
|
|
81
|
-
|
|
51
|
+
### Validation
|
|
52
|
+
|
|
53
|
+
This class is main point to define validation. Possible to define more than one instance in the form. As you wish.
|
|
54
|
+
|
|
82
55
|
```typescript
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
for (const property of properties) {
|
|
91
|
-
if (!result.properties.hasOwnProperty(property)) continue;
|
|
92
|
-
|
|
93
|
-
validation[property] = result.properties[property].isValid;
|
|
94
|
-
validationErrors[property] = result.properties[property].errors;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
this.isValid = result.isValid;
|
|
98
|
-
this.validation = validation;
|
|
99
|
-
this.validationErrors = validationErrors;
|
|
100
|
-
this.allValidationErrors = result.errors;
|
|
101
|
-
}
|
|
102
|
-
});
|
|
56
|
+
const validation = new Validation<Model>({
|
|
57
|
+
firstName: "",
|
|
58
|
+
lastName: "",
|
|
59
|
+
})
|
|
60
|
+
.useValidators("firstName", new RequiredValidator())
|
|
61
|
+
.useValidators("lastName", new RequiredValidator());
|
|
103
62
|
```
|
|
104
|
-
|
|
105
|
-
|
|
63
|
+
|
|
64
|
+
The value that is passed to constructor is used as initial model or original model.
|
|
106
65
|
|
|
107
66
|
### `isValid`
|
|
67
|
+
|
|
108
68
|
Validates the whole model.
|
|
69
|
+
|
|
109
70
|
```typescript
|
|
110
|
-
const
|
|
71
|
+
const model = {
|
|
72
|
+
firstName: "Alexey",
|
|
73
|
+
lastName: "",
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const result: ValidationResult<Model> = await this.validation.isValid(model);
|
|
111
77
|
console.log(result.isValid); // -> false
|
|
112
78
|
```
|
|
113
79
|
|
|
114
80
|
#### `isPropertyValid`
|
|
81
|
+
|
|
115
82
|
Validates only a specific property.
|
|
83
|
+
|
|
116
84
|
```typescript
|
|
117
|
-
const propertyResult:
|
|
85
|
+
const propertyResult: ValidationPropertyResult =
|
|
86
|
+
await this.validation.isPropertyValid(model, "firstName");
|
|
118
87
|
console.log(propertyResult.isValid); // -> false
|
|
119
88
|
```
|
|
120
89
|
|
|
121
90
|
#### `useValidators`
|
|
91
|
+
|
|
122
92
|
Use this method to set validators for property.
|
|
93
|
+
|
|
123
94
|
```typescript
|
|
124
|
-
this.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
95
|
+
this.validation.useValidators(
|
|
96
|
+
"title", // property
|
|
97
|
+
new RequiredValidator(),
|
|
98
|
+
new MaxValidator({ maxValue: () => 10 })
|
|
99
|
+
// add more validators if you need
|
|
129
100
|
);
|
|
130
101
|
```
|
|
131
102
|
|
|
132
103
|
#### `useOriginal`
|
|
133
|
-
|
|
104
|
+
|
|
105
|
+
You have built-in ability to check if the property has been changed or not. `isDirty`.
|
|
134
106
|
To do it scope save a deep cloned copy of the model on init. When we invoke `isDirty`, then `isDirty` method compares value to saved copy.
|
|
135
107
|
`useOriginal` resets saved cloned copy of model.
|
|
108
|
+
|
|
136
109
|
```typescript
|
|
137
|
-
this.
|
|
110
|
+
this.validation.useOriginal({
|
|
111
|
+
firstName: "Alexey",
|
|
112
|
+
lastName: "Ripenko",
|
|
113
|
+
});
|
|
138
114
|
```
|
|
139
115
|
|
|
140
116
|
#### `isPropertyDirty`
|
|
117
|
+
|
|
141
118
|
The method of the validation scope allows to check if property has been changed (dirty).
|
|
119
|
+
|
|
142
120
|
```typescript
|
|
143
|
-
this.model.
|
|
144
|
-
let
|
|
145
|
-
|
|
121
|
+
this.model.firstName = "Mike";
|
|
122
|
+
let isFirstNameDirty: boolean = this.validation.isPropertyDirty(
|
|
123
|
+
model,
|
|
124
|
+
"firstName"
|
|
125
|
+
);
|
|
126
|
+
console.log(isFirstNameDirty); // -> true
|
|
146
127
|
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
console.log(
|
|
128
|
+
this.validation.useOriginal(this.model);
|
|
129
|
+
isFirstNameDirty = this.validation.isPropertyDirty(model, "firstName");
|
|
130
|
+
console.log(isFirstNameDirty); // -> false
|
|
150
131
|
```
|
|
151
132
|
|
|
152
133
|
This method has optional `key` parameter. It uses to use nested value if property is object with nested object-properties. Example `someProperty.someNestedProperty.key`, `arrayProperty.key`.
|
|
153
134
|
|
|
154
|
-
|
|
155
135
|
#### `getOriginalProperty`
|
|
136
|
+
|
|
156
137
|
Gets property from saved cloned model.
|
|
138
|
+
|
|
157
139
|
```typescript
|
|
158
|
-
const
|
|
140
|
+
const originalFirstName = this.validation.getOriginalProperty("firstName");
|
|
159
141
|
```
|
|
160
142
|
|
|
161
143
|
#### `isDirty`
|
|
144
|
+
|
|
162
145
|
Checks if model or model properties have been changed (dirty).
|
|
163
146
|
|
|
164
147
|
Checks the whole model
|
|
148
|
+
|
|
165
149
|
```typescript
|
|
166
|
-
this.
|
|
150
|
+
this.validation.isDirty(model);
|
|
167
151
|
```
|
|
168
|
-
|
|
152
|
+
|
|
153
|
+
or checks only `firstName` property
|
|
154
|
+
|
|
169
155
|
```typescript
|
|
170
|
-
this.
|
|
156
|
+
this.validation.isDirty(model, "firstName");
|
|
171
157
|
```
|
|
158
|
+
|
|
172
159
|
or checks multiple properties
|
|
160
|
+
|
|
173
161
|
```typescript
|
|
174
|
-
this.scope.isDirty("
|
|
162
|
+
this.scope.isDirty(model, "firstName", "lastName");
|
|
175
163
|
```
|
|
176
164
|
|
|
177
165
|
### Validators
|
|
166
|
+
|
|
178
167
|
You can create own validator using the followed template:
|
|
168
|
+
|
|
179
169
|
```typescript
|
|
180
|
-
import {
|
|
170
|
+
import {
|
|
171
|
+
ValidationPropertyResult,
|
|
172
|
+
ValidatorSetup,
|
|
173
|
+
Validator,
|
|
174
|
+
} from "thesimplevalidation";
|
|
181
175
|
|
|
182
176
|
// if you need to pass extra data you can put here.
|
|
183
|
-
export
|
|
184
|
-
|
|
177
|
+
export type MyValidatorSetup<TModel, K extends keyof TModel> = ValidatorSetup<
|
|
178
|
+
TModel,
|
|
179
|
+
K
|
|
180
|
+
> & { extraParam: () => number };
|
|
181
|
+
|
|
182
|
+
export class MyValidator<TModel, K extends keyof TModel> extends Validator<
|
|
183
|
+
TModel,
|
|
184
|
+
K,
|
|
185
|
+
MyValidatorSetup<TModel, K>
|
|
186
|
+
> {
|
|
187
|
+
constructor(setup: MyValidatorSetup<TModel, K>) {
|
|
188
|
+
super(setup);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public async isValidInternal(
|
|
192
|
+
_value: TModel[K]
|
|
193
|
+
): Promise<ValidationPropertyResult> {
|
|
194
|
+
let isValid: boolean = true;
|
|
195
|
+
|
|
196
|
+
//... add validation core here to set isValid
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
isValid: isValid,
|
|
200
|
+
errors: [], // You can add errors here, or use `setup.getErrors` method
|
|
201
|
+
};
|
|
202
|
+
}
|
|
185
203
|
}
|
|
186
|
-
|
|
187
|
-
export class MyValidator<TModel, K extends keyof TModel> extends Validator<TModel, K, IMyValidatorSetup<TModel, K>> {
|
|
188
|
-
constructor(setup: IMyValidatorSetup<TModel, K>) {
|
|
189
|
-
super(setup);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
public async isValidInternal(_value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
193
|
-
let isValid: boolean = true;
|
|
194
|
-
|
|
195
|
-
//... add validation core here to set isValid
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
isValid: isValid,
|
|
199
|
-
errors: [] // You can add errors here, or use `setup.getErrors` method
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
-
#### `
|
|
206
|
+
#### `MyValidatorSetup<TModel, K>`
|
|
207
|
+
|
|
207
208
|
The base options of validators.
|
|
208
209
|
|
|
209
210
|
`isDisabled?: (value: TModel[K]) => boolean;` is used to disable validator based on some logic-function.
|
|
@@ -212,84 +213,101 @@ Could be nice if validation logic is depended on some values, logic, etc...
|
|
|
212
213
|
`getErrors?: (value: TModel[K]) => string[];` is used to define error messages.
|
|
213
214
|
|
|
214
215
|
#### RequiredValidator
|
|
216
|
+
|
|
215
217
|
Validates property against `""`, `null`, `undefined`, `0`, array.`length`.
|
|
216
218
|
|
|
217
219
|
#### MinValidator
|
|
218
|
-
Validates the minimum value of property.
|
|
219
220
|
|
|
220
|
-
|
|
221
|
+
Validates the minimum value of property.
|
|
222
|
+
|
|
223
|
+
Property value is a string then the validator checks the string length.
|
|
221
224
|
If an array then the validator checks the array length.
|
|
222
225
|
If a number then the validator validate against min value.
|
|
223
226
|
|
|
224
227
|
```typescript
|
|
225
228
|
new MinValidator({
|
|
226
|
-
|
|
229
|
+
minValue: () => 10,
|
|
227
230
|
});
|
|
228
231
|
```
|
|
229
232
|
|
|
230
233
|
#### MaxValidator
|
|
231
|
-
Validates the maximum value of property.
|
|
232
234
|
|
|
233
|
-
|
|
235
|
+
Validates the maximum value of property.
|
|
236
|
+
|
|
237
|
+
Property value is a string then the validator checks the string length.
|
|
234
238
|
If an array then the validator checks the array length.
|
|
235
239
|
If a number then the validator validate against max value.
|
|
236
240
|
|
|
237
241
|
```typescript
|
|
238
242
|
new MaxValidator({
|
|
239
|
-
|
|
243
|
+
maxValue: () => 255,
|
|
240
244
|
});
|
|
241
245
|
```
|
|
242
246
|
|
|
243
247
|
#### BooleanValidator
|
|
248
|
+
|
|
244
249
|
Validates if the model boolean property value is the same if some boolean value
|
|
250
|
+
|
|
245
251
|
```typescript
|
|
246
252
|
new MaxValidator({
|
|
247
|
-
|
|
253
|
+
target: () => true,
|
|
248
254
|
});
|
|
249
255
|
```
|
|
250
256
|
|
|
251
257
|
#### SimpleValidator
|
|
258
|
+
|
|
252
259
|
The simpliest validator that option `getValidation` boolean value will be used as isValid of validation. The model property value is not used.
|
|
253
260
|
See implementation:
|
|
261
|
+
|
|
254
262
|
```typescript
|
|
255
|
-
export interface
|
|
256
|
-
|
|
263
|
+
export interface SimpleValidatorSetup<TModel, K extends keyof TModel>
|
|
264
|
+
extends ValidatorSetup<TModel, K> {
|
|
265
|
+
getValidation: () => boolean;
|
|
257
266
|
}
|
|
258
267
|
|
|
259
|
-
export class SimpleValidator<TModel, K extends keyof TModel> extends Validator<
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
268
|
+
export class SimpleValidator<TModel, K extends keyof TModel> extends Validator<
|
|
269
|
+
TModel,
|
|
270
|
+
K,
|
|
271
|
+
SimpleValidatorSetup<TModel, K>
|
|
272
|
+
> {
|
|
273
|
+
constructor(setup: SimpleValidatorSetup<TModel, K>) {
|
|
274
|
+
super(setup);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
public async isValidInternal(
|
|
278
|
+
_value: TModel[K]
|
|
279
|
+
): Promise<ValidationPropertyResult> {
|
|
280
|
+
const isValid: boolean = this.setup.getValidation();
|
|
281
|
+
|
|
282
|
+
return {
|
|
283
|
+
isValid: isValid,
|
|
284
|
+
errors: [],
|
|
285
|
+
};
|
|
286
|
+
}
|
|
272
287
|
}
|
|
273
288
|
```
|
|
274
289
|
|
|
275
290
|
Example:
|
|
291
|
+
|
|
276
292
|
```typescript
|
|
277
293
|
new SimpleValidator({
|
|
278
|
-
|
|
294
|
+
getValidation: () => true,
|
|
279
295
|
});
|
|
280
296
|
// -> isValid = true
|
|
281
297
|
```
|
|
298
|
+
|
|
282
299
|
or
|
|
300
|
+
|
|
283
301
|
```typescript
|
|
284
302
|
new SimpleValidator({
|
|
285
|
-
|
|
303
|
+
getValidation: () => false,
|
|
286
304
|
});
|
|
287
305
|
// -> isValid = false
|
|
288
306
|
```
|
|
289
307
|
|
|
290
|
-
|
|
291
308
|
## Credits
|
|
292
|
-
|
|
309
|
+
|
|
310
|
+
[Alexey Ripenko](https://www.linkedin.com/in/ripenko/), [GitHub](https://github.com/ripenko/)
|
|
293
311
|
|
|
294
312
|
## License
|
|
295
313
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ValidationPropertyResult } from "./ValidationPropertyResult";
|
|
2
|
+
import { ValidationResult } from "./ValidationResult";
|
|
3
|
+
import { Validator } from "./Validator";
|
|
4
|
+
export declare class Validation<TModel extends {}> {
|
|
5
|
+
private originalModel;
|
|
6
|
+
private modelInfo;
|
|
7
|
+
constructor(originalModel: TModel);
|
|
8
|
+
useOriginal: (model: TModel) => Validation<TModel>;
|
|
9
|
+
useNoValidators: () => Validation<TModel>;
|
|
10
|
+
useValidators: <K extends keyof TModel>(key: K, ...validators: Array<Validator<TModel, K>>) => Validation<TModel>;
|
|
11
|
+
isPropertyDirty: (model: TModel, field: keyof TModel, key?: string | null) => boolean;
|
|
12
|
+
getOriginalProperty: <K extends keyof TModel>(field: K) => TModel[K];
|
|
13
|
+
isDirty: (model: TModel, ...fields: Array<keyof TModel>) => boolean;
|
|
14
|
+
isPropertyValid: <K extends keyof TModel>(model: TModel, field: K) => Promise<ValidationPropertyResult>;
|
|
15
|
+
isValid: (model: TModel, ...fields: Array<keyof TModel>) => Promise<ValidationResult<TModel>>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import get from "lodash.get";
|
|
2
|
+
import isEqual from "lodash.isequal";
|
|
3
|
+
export class Validation {
|
|
4
|
+
constructor(originalModel) {
|
|
5
|
+
this.useOriginal = (model) => {
|
|
6
|
+
this.originalModel = structuredClone(model);
|
|
7
|
+
return this;
|
|
8
|
+
};
|
|
9
|
+
this.useNoValidators = () => {
|
|
10
|
+
this.modelInfo = {};
|
|
11
|
+
return this;
|
|
12
|
+
};
|
|
13
|
+
this.useValidators = (key, ...validators) => {
|
|
14
|
+
this.modelInfo[key] = {
|
|
15
|
+
validators: validators,
|
|
16
|
+
};
|
|
17
|
+
return this;
|
|
18
|
+
};
|
|
19
|
+
this.isPropertyDirty = (model, field, key = null) => {
|
|
20
|
+
const originalField = this.originalModel[field];
|
|
21
|
+
const modelField = model[field];
|
|
22
|
+
if (Array.isArray(originalField) && Array.isArray(modelField)) {
|
|
23
|
+
if (originalField.length !== modelField.length)
|
|
24
|
+
return true;
|
|
25
|
+
for (let index = 0; index < originalField.length; index++) {
|
|
26
|
+
if (originalField[index] === modelField[index])
|
|
27
|
+
continue;
|
|
28
|
+
if (key == null)
|
|
29
|
+
return true;
|
|
30
|
+
if (get(originalField[index], key) !== get(modelField[index], key))
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (key == null)
|
|
36
|
+
return !isEqual(this.originalModel[field], model[field]);
|
|
37
|
+
return get(this.originalModel[field], key) !== get(model[field], key);
|
|
38
|
+
};
|
|
39
|
+
this.getOriginalProperty = (field) => {
|
|
40
|
+
return this.originalModel[field];
|
|
41
|
+
};
|
|
42
|
+
this.isDirty = (model, ...fields) => {
|
|
43
|
+
for (const property in this.originalModel) {
|
|
44
|
+
if (!this.originalModel.hasOwnProperty(property))
|
|
45
|
+
continue;
|
|
46
|
+
if (fields && fields.length > 0) {
|
|
47
|
+
if (!fields.some((x) => x === property))
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (this.isPropertyDirty(model, property))
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
this.isPropertyValid = async (model, field) => {
|
|
56
|
+
if (this.modelInfo[field] &&
|
|
57
|
+
this.modelInfo[field].validators &&
|
|
58
|
+
this.modelInfo[field].validators) {
|
|
59
|
+
let isValid = true;
|
|
60
|
+
let errors = [];
|
|
61
|
+
const value = model[field];
|
|
62
|
+
for (const validator of this.modelInfo[field].validators) {
|
|
63
|
+
const validatorResult = await validator.isValid(value, model, field);
|
|
64
|
+
isValid = isValid && validatorResult.isValid;
|
|
65
|
+
if (!validatorResult.isValid)
|
|
66
|
+
errors = [...errors, ...validatorResult.errors];
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
isValid: isValid,
|
|
70
|
+
errors: errors,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
isValid: true,
|
|
75
|
+
errors: [],
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
this.isValid = async (model, ...fields) => {
|
|
79
|
+
model = model ?? {};
|
|
80
|
+
const properties = (fields == null || fields.length === 0 ? Object.keys(model) : fields);
|
|
81
|
+
const result = {
|
|
82
|
+
isValid: true,
|
|
83
|
+
errors: [],
|
|
84
|
+
properties: {},
|
|
85
|
+
};
|
|
86
|
+
for (const property of properties) {
|
|
87
|
+
const propertyValidationResult = await this.isPropertyValid(model, property);
|
|
88
|
+
result.isValid = result.isValid && propertyValidationResult.isValid;
|
|
89
|
+
result.properties[property] = propertyValidationResult;
|
|
90
|
+
if (!propertyValidationResult.isValid)
|
|
91
|
+
result.errors = [...result.errors, ...propertyValidationResult.errors];
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
};
|
|
95
|
+
this.originalModel = structuredClone(originalModel);
|
|
96
|
+
this.modelInfo = {};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=Validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Validation.js","sourceRoot":"","sources":["../src/Validation.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,YAAY,CAAC;AAC7B,OAAO,OAAO,MAAM,gBAAgB,CAAC;AAKrC,MAAM,OAAO,UAAU;IAQrB,YAAY,aAAqB;QAK1B,gBAAW,GAAG,CAAC,KAAa,EAAsB,EAAE;YACzD,IAAI,CAAC,aAAa,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;YAC5C,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QAEK,oBAAe,GAAG,GAAuB,EAAE;YAChD,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QAEK,kBAAa,GAAG,CACrB,GAAM,EACN,GAAG,UAAuC,EACtB,EAAE;YACtB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG;gBACpB,UAAU,EAAE,UAAU;aACvB,CAAC;YACF,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;QAEK,oBAAe,GAAG,CACvB,KAAa,EACb,KAAmB,EACnB,MAAqB,IAAI,EAChB,EAAE;YACX,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAChD,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;YAChC,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC9D,IAAI,aAAa,CAAC,MAAM,KAAK,UAAU,CAAC,MAAM;oBAAE,OAAO,IAAI,CAAC;gBAC5D,KAAK,IAAI,KAAK,GAAG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MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAC7C,CAAC;YAEzB,MAAM,MAAM,GAA6B;gBACvC,OAAO,EAAE,IAAI;gBACb,MAAM,EAAE,EAAE;gBACV,UAAU,EAAE,EAEX;aAC0B,CAAC;YAE9B,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;gBAClC,MAAM,wBAAwB,GAC5B,MAAM,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;gBAC9C,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,wBAAwB,CAAC,OAAO,CAAC;gBACpE,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,GAAG,wBAAwB,CAAC;gBACvD,IAAI,CAAC,wBAAwB,CAAC,OAAO;oBACnC,MAAM,CAAC,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAAC;YAC3E,CAAC;YAED,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;QA/HA,IAAI,CAAC,aAAa,GAAG,eAAe,CAAC,aAAa,CAAC,CAAC;QACpD,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACtB,CAAC;CA8HF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationErrorsModel.js","sourceRoot":"","sources":["../src/ValidationErrorsModel.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationModel.js","sourceRoot":"","sources":["../src/ValidationModel.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationPropertyResult.js","sourceRoot":"","sources":["../src/ValidationPropertyResult.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationResult.js","sourceRoot":"","sources":["../src/ValidationResult.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValidationPropertyResult } from "./ValidationPropertyResult";
|
|
2
|
+
import { ValidationResult } from "./ValidationResult";
|
|
3
|
+
import { Validator } from "./Validator";
|
|
4
|
+
export declare class ValidationScope<TModel extends {}> {
|
|
5
|
+
private originalModel;
|
|
6
|
+
private modelInfo;
|
|
7
|
+
constructor(originalModel: TModel);
|
|
8
|
+
useOriginal: (model: TModel) => ValidationScope<TModel>;
|
|
9
|
+
useValidators: <K extends keyof TModel>(key: K, ...validators: Array<Validator<TModel, K>>) => ValidationScope<TModel>;
|
|
10
|
+
isPropertyDirty: (model: TModel, field: keyof TModel, key?: string | null) => boolean;
|
|
11
|
+
getOriginalProperty: <K extends keyof TModel>(field: K) => TModel[K];
|
|
12
|
+
isDirty: (model: TModel, ...fields: Array<keyof TModel>) => boolean;
|
|
13
|
+
isPropertyValid: <K extends keyof TModel>(model: TModel, field: K) => Promise<ValidationPropertyResult>;
|
|
14
|
+
isValid: (model: TModel, ...fields: Array<keyof TModel>) => Promise<ValidationResult<TModel>>;
|
|
15
|
+
}
|