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/README.md
CHANGED
|
@@ -1,355 +1,293 @@
|
|
|
1
|
-
#
|
|
1
|
+
# thesimplevalidation
|
|
2
2
|
|
|
3
|
-
The simple
|
|
3
|
+
The simple validation service
|
|
4
4
|
|
|
5
|
-
[](https://travis-ci.org/ripenko/thesimplevalidation)
|
|
8
6
|
|
|
9
7
|
## Installation
|
|
10
|
-
|
|
11
8
|
```
|
|
12
|
-
npm install --save
|
|
9
|
+
npm install --save thesimplevalidation
|
|
13
10
|
```
|
|
14
11
|
|
|
15
|
-
##
|
|
12
|
+
## Setup
|
|
13
|
+
There are paar steps to enable validation.
|
|
16
14
|
|
|
17
|
-
###
|
|
18
|
-
method `localize`
|
|
15
|
+
### 1. Create a model
|
|
19
16
|
```typescript
|
|
20
|
-
|
|
17
|
+
interface IModel {
|
|
18
|
+
title: string;
|
|
19
|
+
description: string;
|
|
20
|
+
}
|
|
21
|
+
```
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
### 2. Create or use state, or something similar where we store user model values
|
|
24
|
+
```typescript
|
|
25
|
+
this.model: IModel = {
|
|
26
|
+
title: "",
|
|
27
|
+
description: ""
|
|
27
28
|
};
|
|
28
|
-
|
|
29
|
-
const service = new LocalizationService({
|
|
30
|
-
importedLanguages: {
|
|
31
|
-
[LocalizationService.DEFAULT_CULTURE_NAME]: defaultLanguage
|
|
32
|
-
},
|
|
33
|
-
currentLanguage: defaultLanguage
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const localized: string = service.localize("Key2.Key21");
|
|
37
|
-
console.log(localized); // "Value 21"
|
|
38
|
-
|
|
39
29
|
```
|
|
40
30
|
|
|
41
|
-
###
|
|
42
|
-
The events bus is `eventservice`. See [https://github.com/ripenko/eventservice]
|
|
43
|
-
|
|
44
|
-
There are two localization files `default.jsonc` and `de-de.json`. To generate localization keys from `default.jsonc` file we can use webpack plugin [https://github.com/ripenko/localizationkeys-webpack].
|
|
45
|
-
|
|
46
|
-
The `Localization.ts` file in your app.
|
|
31
|
+
### 3. Create validation model, validation errors model
|
|
47
32
|
```typescript
|
|
48
|
-
import
|
|
49
|
-
import EventService from "eventservice";
|
|
33
|
+
import { ValidationModel } from "thesimplevalidation";
|
|
50
34
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
public static localize<T = string>(key: string, ...formatArgs: string[]): T {
|
|
56
|
-
if (Localization.service == null) initService();
|
|
57
|
-
return Localization.service.localize(key, formatArgs);
|
|
58
|
-
}
|
|
35
|
+
this.validation: ValidationModel<IModel> = {
|
|
36
|
+
title: false,
|
|
37
|
+
description: true
|
|
38
|
+
};
|
|
59
39
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
importedLanguages: {
|
|
65
|
-
[LocalizationService.DEFAULT_CULTURE_NAME]: defaultLanguage,
|
|
66
|
-
"de-de": germanLanguage
|
|
67
|
-
},
|
|
68
|
-
onLanguageChanged: (languageName: string, language: ILocalizationLanguage): Promise<void> => {
|
|
69
|
-
// we can notify subscribers that the current language has been changed.
|
|
70
|
-
// if we use momentjs then we can change locale by `moment.locale(languageName);`
|
|
71
|
-
// or numeraljs, etc...
|
|
72
|
-
return EventService.fire("Events.Localization.LanguageChanged", { name: languageName, langauge: language });
|
|
73
|
-
},
|
|
74
|
-
onLanguageImported: (languageName: string, language: ILocalizationLanguage): Promise<void> => {
|
|
75
|
-
// we can load language from api or some another place async to import it by `service.importLanguage` method
|
|
76
|
-
// when new language will be imported we can update UI by refreshing a list of available languages
|
|
77
|
-
return EventService.fire("Events.Localization.LanguageImported", { name: languageName, langauge: language });
|
|
78
|
-
},
|
|
79
|
-
onLocalizationMissing: (key: string, _languageName: string, _language: ILocalizationLanguage | null, _formatArgs: string[]): string => {
|
|
80
|
-
// When localization has been not found in current language we return just key.
|
|
81
|
-
// The testers could find easy such unlocalizated places.
|
|
82
|
-
// Or we can notify insights about missing values.
|
|
83
|
-
// Or we can try to get value from default language.
|
|
84
|
-
// Or write in the console, etc...
|
|
85
|
-
return key;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
40
|
+
this.validationErrors: ValidationErrorsModel<IModel> = {
|
|
41
|
+
title: [];
|
|
42
|
+
description: [];
|
|
43
|
+
};
|
|
89
44
|
|
|
90
|
-
|
|
45
|
+
this.allValidationErrors: string [] = [];
|
|
91
46
|
|
|
47
|
+
this.isValid: boolean = false;
|
|
92
48
|
```
|
|
93
49
|
|
|
94
|
-
### Return localized object instead of certain string
|
|
95
|
-
Method `localize`. By default return type of method `localize<T = string>` is string.
|
|
96
|
-
If we pass a localization key that point doesn't point to string, but it points to the parent nested object, then localized result will be object.
|
|
97
|
-
Typically usecases:
|
|
98
|
-
|
|
99
|
-
- dynamic key. if localization key is computed
|
|
100
|
-
- localized object should be passed to some component. (Example: Globalization, or localization of some Telerik control)
|
|
101
50
|
|
|
51
|
+
### 4. Creating Validation Scope
|
|
102
52
|
```typescript
|
|
103
|
-
import
|
|
53
|
+
import { ValidationScope } from "thesimplevalidation";
|
|
104
54
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
55
|
+
const scope = new ValidationScope<IModel>({
|
|
56
|
+
getModel: (): IModel => this.model
|
|
57
|
+
});
|
|
109
58
|
|
|
110
|
-
|
|
111
|
-
"Feature": {
|
|
112
|
-
"Title": "Some Title",
|
|
113
|
-
"Description": "Some Description"
|
|
114
|
-
}
|
|
115
|
-
};
|
|
59
|
+
```
|
|
116
60
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
});
|
|
61
|
+
### 5. Add validators to model property(ies)
|
|
62
|
+
There are some built-in validators like `RequiredValidator`, `MinValidator`, `MaxValidator`, `SimpleValidator`, `BooleanValidator`. You can create own by yourself. It is easy.
|
|
63
|
+
So. To add validators we use validation scope method `useValidators`.
|
|
64
|
+
```typescript
|
|
65
|
+
import { RequiredValidator } from "thesimplevalidation";
|
|
123
66
|
|
|
124
|
-
|
|
125
|
-
console.log(result); // -> { Title: "Some Title", Description: "Some Description" }
|
|
67
|
+
this.scope.useValidators("title", new RequiredValidator());
|
|
126
68
|
|
|
127
69
|
```
|
|
128
70
|
|
|
129
|
-
###
|
|
130
|
-
method `changeLanguage`
|
|
71
|
+
### 6. Do validation
|
|
131
72
|
```typescript
|
|
132
|
-
|
|
73
|
+
const result: IValidationResult<IModel> = await this.scope.isValid();
|
|
74
|
+
console.log(result.isValid, result);
|
|
75
|
+
// -> false, { isValid: false, properties: { title: { isValid: false, errors: [] }, description: { isValid: true, errors: [] } }, errors: [] }
|
|
76
|
+
```
|
|
133
77
|
|
|
134
|
-
|
|
135
|
-
"Key1": "Value 1",
|
|
136
|
-
"Key2": {
|
|
137
|
-
"Key21": "Value 21"
|
|
138
|
-
}
|
|
139
|
-
};
|
|
78
|
+
## Usage
|
|
140
79
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
80
|
+
### ValidationScope
|
|
81
|
+
This class is main point to define validation. Possible to define more than one scope in the form. As you wish.
|
|
82
|
+
```typescript
|
|
83
|
+
const scope = new ValidationScope<IModel>({
|
|
84
|
+
getModel: (): IModel => this.model
|
|
85
|
+
onValidationChanged: async (result: IValidationResult<TModel>): Promise<void> => {
|
|
86
|
+
const properties: Array<keyof TModel> = keys(result.properties) as Array<keyof TModel>;
|
|
87
|
+
const validation: ValidationModel<TModel> = {} as ValidationModel<TModel>;
|
|
88
|
+
const validationErrors: ValidationErrorsModel<TModel> = {} as ValidationErrorsModel<TModel>;
|
|
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;
|
|
145
101
|
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const service = new LocalizationService({
|
|
149
|
-
importedLanguages: {
|
|
150
|
-
[LocalizationService.DEFAULT_CULTURE_NAME]: defaultLanguage,
|
|
151
|
-
"de-de": germanLanguage
|
|
152
|
-
},
|
|
153
|
-
currentLanguage: defaultLanguage
|
|
154
102
|
});
|
|
155
|
-
|
|
156
|
-
await service.changeLanguage("de-DE"); // language name case will be lowered => de-de
|
|
157
|
-
|
|
158
|
-
const localized: string = service.localize("Key2.Key21");
|
|
159
|
-
console.log(localized); // "Wert 21"
|
|
160
|
-
|
|
161
103
|
```
|
|
104
|
+
`getModel` is invoked on validation by method `isValid`.
|
|
105
|
+
`onValidationChanged` Optional. To handle validation result. It is invoked before `isValid` method is completed.
|
|
162
106
|
|
|
163
|
-
###
|
|
164
|
-
|
|
107
|
+
### `isValid`
|
|
108
|
+
Validates the whole model.
|
|
165
109
|
```typescript
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
"Key1": "Value 1",
|
|
170
|
-
"Key2": {
|
|
171
|
-
"Key21": "Value 21"
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const germanLanguage = {
|
|
176
|
-
"Key1": "Wert 1",
|
|
177
|
-
"Key2": {
|
|
178
|
-
"Key21": "Wert 21"
|
|
179
|
-
}
|
|
180
|
-
}
|
|
110
|
+
const result: IValidationResult<IModel> = await this.scope.isValid();
|
|
111
|
+
console.log(result.isValid); // -> false
|
|
112
|
+
```
|
|
181
113
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
onChangeLanguage: async (languageName: string, language: ILocalizationLanguage | null): Promise<void> => {
|
|
189
|
-
console.info(languageName, language);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
114
|
+
#### `isPropertyValid`
|
|
115
|
+
Validates only a specific property.
|
|
116
|
+
```typescript
|
|
117
|
+
const propertyResult: IValidationPropertyResult = await this.scope.isPropertyValid("description");
|
|
118
|
+
console.log(propertyResult.isValid); // -> false
|
|
119
|
+
```
|
|
192
120
|
|
|
193
|
-
|
|
194
|
-
|
|
121
|
+
#### `useValidators`
|
|
122
|
+
Use this method to set validators for property.
|
|
123
|
+
```typescript
|
|
124
|
+
this.scope.useValidators(
|
|
125
|
+
"title", // property
|
|
126
|
+
new RequiredValidator(),
|
|
127
|
+
new MaxValidator({ maxValue: () => 10 }),
|
|
128
|
+
// add more validators if you need
|
|
129
|
+
);
|
|
130
|
+
```
|
|
195
131
|
|
|
132
|
+
#### `useOriginal`
|
|
133
|
+
You have built-in ability to check if the property has been changed or not. `isDirty`.
|
|
134
|
+
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
|
+
`useOriginal` resets saved cloned copy of model.
|
|
136
|
+
```typescript
|
|
137
|
+
this.scope.useOriginal(newModel);
|
|
196
138
|
```
|
|
197
139
|
|
|
198
|
-
|
|
199
|
-
method
|
|
140
|
+
#### `isPropertyDirty`
|
|
141
|
+
The method of the validation scope allows to check if property has been changed (dirty).
|
|
200
142
|
```typescript
|
|
201
|
-
|
|
143
|
+
this.model.title = "New Title";
|
|
144
|
+
let isTitleDirty: boolean = this.scope.isPropertyDirty("title");
|
|
145
|
+
console.log(isTitleDirty); // -> true
|
|
202
146
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
};
|
|
147
|
+
this.scope.useOriginal(this.model);
|
|
148
|
+
isTitleDirty = this.scope.isPropertyDirty("title");
|
|
149
|
+
console.log(isTitleDirty); // -> false
|
|
150
|
+
```
|
|
209
151
|
|
|
210
|
-
|
|
211
|
-
__cultureName__: "de-de",
|
|
212
|
-
"Key1": "Wert 1",
|
|
213
|
-
"Key2": {
|
|
214
|
-
"Key21": "Wert 21"
|
|
215
|
-
}
|
|
216
|
-
}
|
|
152
|
+
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`.
|
|
217
153
|
|
|
218
|
-
const service = new LocalizationService({
|
|
219
|
-
importedLanguages: {
|
|
220
|
-
[LocalizationService.DEFAULT_CULTURE_NAME]: defaultLanguage
|
|
221
|
-
},
|
|
222
|
-
currentLanguage: defaultLanguage
|
|
223
|
-
});
|
|
224
154
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
await service.importLanguage(germanLanguage);
|
|
155
|
+
#### `getOriginalProperty`
|
|
156
|
+
Gets property from saved cloned model.
|
|
157
|
+
```typescript
|
|
158
|
+
const originalTitle = this.scope.getOriginalProperty("title");
|
|
159
|
+
```
|
|
231
160
|
|
|
232
|
-
|
|
233
|
-
|
|
161
|
+
#### `isDirty`
|
|
162
|
+
Checks if model or model properties have been changed (dirty).
|
|
234
163
|
|
|
164
|
+
Checks the whole model
|
|
165
|
+
```typescript
|
|
166
|
+
this.scope.isDirty();
|
|
167
|
+
```
|
|
168
|
+
or checks only `title` property
|
|
169
|
+
```typescript
|
|
170
|
+
this.scope.isDirty("title");
|
|
171
|
+
```
|
|
172
|
+
or checks multiple properties
|
|
173
|
+
```typescript
|
|
174
|
+
this.scope.isDirty("title", "description");
|
|
235
175
|
```
|
|
236
176
|
|
|
237
|
-
###
|
|
238
|
-
|
|
177
|
+
### Validators
|
|
178
|
+
You can create own validator using the followed template:
|
|
239
179
|
```typescript
|
|
240
|
-
import
|
|
180
|
+
import { IValidationPropertyResult, IValidatorSetup, Validator } from "thesimplevalidation";
|
|
181
|
+
|
|
182
|
+
// if you need to pass extra data you can put here.
|
|
183
|
+
export interface IMyValidatorSetup<TModel, K extends keyof TModel> extends IValidatorSetup<TModel, K> {
|
|
184
|
+
extraParam: () => number;
|
|
185
|
+
}
|
|
241
186
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
"Key21": "Value 21"
|
|
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);
|
|
246
190
|
}
|
|
247
|
-
};
|
|
248
191
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
+
};
|
|
253
201
|
}
|
|
254
202
|
}
|
|
255
203
|
|
|
256
|
-
|
|
257
|
-
importedLanguages: {
|
|
258
|
-
[LocalizationService.DEFAULT_CULTURE_NAME]: defaultLanguage
|
|
259
|
-
},
|
|
260
|
-
currentLanguage: defaultLanguage
|
|
261
|
-
});
|
|
204
|
+
```
|
|
262
205
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
// The method doesn't change the current language. See `changeLanguage`.
|
|
266
|
-
await service.importLanguage(germanLanguage, "de-DE");
|
|
206
|
+
#### `IMyValidatorSetup<TModel, K>`
|
|
207
|
+
The base options of validators.
|
|
267
208
|
|
|
268
|
-
|
|
269
|
-
|
|
209
|
+
`isDisabled?: (value: TModel[K]) => boolean;` is used to disable validator based on some logic-function.
|
|
210
|
+
Could be nice if validation logic is depended on some values, logic, etc...
|
|
270
211
|
|
|
271
|
-
|
|
212
|
+
`getErrors?: (value: TModel[K]) => string[];` is used to define error messages.
|
|
272
213
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
```typescript
|
|
276
|
-
import LocalizationService from "localizationservice";
|
|
214
|
+
#### RequiredValidator
|
|
215
|
+
Validates property against `""`, `null`, `undefined`, `0`, array.`length`.
|
|
277
216
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
"Key2": {
|
|
281
|
-
"Key21": "Value 21"
|
|
282
|
-
}
|
|
283
|
-
};
|
|
217
|
+
#### MinValidator
|
|
218
|
+
Validates the minimum value of property.
|
|
284
219
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
"Key21": "Wert 21"
|
|
289
|
-
}
|
|
290
|
-
}
|
|
220
|
+
Property value is a string then the validator checks the string length.
|
|
221
|
+
If an array then the validator checks the array length.
|
|
222
|
+
If a number then the validator validate against min value.
|
|
291
223
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
},
|
|
296
|
-
currentLanguage: defaultLanguage
|
|
224
|
+
```typescript
|
|
225
|
+
new MinValidator({
|
|
226
|
+
minValue: () => 10
|
|
297
227
|
});
|
|
228
|
+
```
|
|
298
229
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
await service.importLanguage(germanLanguage);
|
|
302
|
-
|
|
303
|
-
// To apply updated language you have to refresh current language
|
|
304
|
-
await service.changeLanguage();
|
|
230
|
+
#### MaxValidator
|
|
231
|
+
Validates the maximum value of property.
|
|
305
232
|
|
|
306
|
-
|
|
307
|
-
|
|
233
|
+
Property value is a string then the validator checks the string length.
|
|
234
|
+
If an array then the validator checks the array length.
|
|
235
|
+
If a number then the validator validate against max value.
|
|
308
236
|
|
|
237
|
+
```typescript
|
|
238
|
+
new MaxValidator({
|
|
239
|
+
maxValue: () => 255
|
|
240
|
+
});
|
|
309
241
|
```
|
|
310
242
|
|
|
311
|
-
|
|
312
|
-
|
|
243
|
+
#### BooleanValidator
|
|
244
|
+
Validates if the model boolean property value is the same if some boolean value
|
|
313
245
|
```typescript
|
|
314
|
-
|
|
246
|
+
new MaxValidator({
|
|
247
|
+
target: () => true
|
|
248
|
+
});
|
|
249
|
+
```
|
|
315
250
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
251
|
+
#### SimpleValidator
|
|
252
|
+
The simpliest validator that option `getValidation` boolean value will be used as isValid of validation. The model property value is not used.
|
|
253
|
+
See implementation:
|
|
254
|
+
```typescript
|
|
255
|
+
export interface ISimpleValidatorSetup<TModel, K extends keyof TModel> extends IValidatorSetup<TModel, K> {
|
|
256
|
+
getValidation: () => boolean;
|
|
257
|
+
}
|
|
322
258
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
259
|
+
export class SimpleValidator<TModel, K extends keyof TModel> extends Validator<TModel, K, ISimpleValidatorSetup<TModel, K>> {
|
|
260
|
+
constructor(setup: ISimpleValidatorSetup<TModel, K>) {
|
|
261
|
+
super(setup);
|
|
326
262
|
}
|
|
327
|
-
});
|
|
328
263
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
await service.importLanguage(defaultLanguage);
|
|
332
|
-
// -> "" { Key1: "Value 1", Key2: { Key21: "Value 21" } }
|
|
264
|
+
public async isValidInternal(_value: TModel[K]): Promise<IValidationPropertyResult> {
|
|
265
|
+
const isValid: boolean = this.setup.getValidation();
|
|
333
266
|
|
|
267
|
+
return {
|
|
268
|
+
isValid: isValid,
|
|
269
|
+
errors: []
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
334
273
|
```
|
|
335
274
|
|
|
336
|
-
|
|
337
|
-
constructor option `onLocalizationMissing` or property `onLocalizationMissing`.
|
|
275
|
+
Example:
|
|
338
276
|
```typescript
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const service = new LocalizationService({
|
|
342
|
-
// name will be `LocalizationService.DEFAULT_CULTURE_NAME`, language is `{}`
|
|
343
|
-
onLocalizationMissing: (key: string, name: string, language: ILocalizationLanguage): string => {
|
|
344
|
-
return `Missed '${key}' for default language`;
|
|
345
|
-
}
|
|
277
|
+
new SimpleValidator({
|
|
278
|
+
getValidation: () => true
|
|
346
279
|
});
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
280
|
+
// -> isValid = true
|
|
281
|
+
```
|
|
282
|
+
or
|
|
283
|
+
```typescript
|
|
284
|
+
new SimpleValidator({
|
|
285
|
+
getValidation: () => false
|
|
286
|
+
});
|
|
287
|
+
// -> isValid = false
|
|
351
288
|
```
|
|
352
289
|
|
|
290
|
+
|
|
353
291
|
## Credits
|
|
354
292
|
[Alexey Ripenko](http://ripenko.ru/), [GitHub](https://github.com/ripenko/)
|
|
355
293
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./src";
|
|
1
|
+
export * from "./src";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./src"), exports);
|
|
7
18
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { ValidationScope } from "./validation-scope";
|
|
2
|
-
export { ValidationErrorsModel } from "./validation-errors-model";
|
|
3
|
-
export { ValidationModel } from "./validation-model";
|
|
4
|
-
export { IValidationPropertyResult } from "./validation-property-result";
|
|
5
|
-
export { IValidationResult } from "./validation-result";
|
|
6
|
-
export { Validator } from "./validator";
|
|
7
|
-
export { IValidatorSetup } from "./validator-setup";
|
|
8
|
-
export * from "./validators";
|
|
1
|
+
export { ValidationScope } from "./validation-scope";
|
|
2
|
+
export { ValidationErrorsModel } from "./validation-errors-model";
|
|
3
|
+
export { ValidationModel } from "./validation-model";
|
|
4
|
+
export { IValidationPropertyResult } from "./validation-property-result";
|
|
5
|
+
export { IValidationResult } from "./validation-result";
|
|
6
|
+
export { Validator } from "./validator";
|
|
7
|
+
export { IValidatorSetup } from "./validator-setup";
|
|
8
|
+
export * from "./validators";
|
package/dist/src/index.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Validator = exports.ValidationScope = void 0;
|
|
18
|
+
var validation_scope_1 = require("./validation-scope");
|
|
19
|
+
Object.defineProperty(exports, "ValidationScope", { enumerable: true, get: function () { return validation_scope_1.ValidationScope; } });
|
|
20
|
+
var validator_1 = require("./validator");
|
|
21
|
+
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return validator_1.Validator; } });
|
|
22
|
+
__exportStar(require("./validators"), exports);
|
|
11
23
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,uDAAqD;AAA5C,mHAAA,eAAe,OAAA;AAKxB,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAGlB,+CAA6B"}
|