knight-validation 2.0.4 → 2.0.6
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 +62 -52
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/lib/Validator.d.ts +7 -7
- package/lib/lib/Validator.js +2 -2
- package/lib/lib/constraints/Exists.d.ts +4 -4
- package/lib/lib/constraints/Unique.d.ts +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,7 +33,14 @@ import { Validator } from 'knight-validation'
|
|
|
33
33
|
class UserValidator extends Validator {
|
|
34
34
|
constructor(userDb: UserDb) {
|
|
35
35
|
super()
|
|
36
|
-
|
|
36
|
+
|
|
37
|
+
this.add(['firstName', 'lastName'], 'Different', async (user: User) => {
|
|
38
|
+
if (user.firstName == user.lastName) {
|
|
39
|
+
// You do not need to set the constraint name nor the properties on the misfit.
|
|
40
|
+
// Those will be set automatically.
|
|
41
|
+
return new Misfit
|
|
42
|
+
}
|
|
43
|
+
})
|
|
37
44
|
}
|
|
38
45
|
}
|
|
39
46
|
```
|
|
@@ -44,6 +51,11 @@ class UserValidator extends Validator {
|
|
|
44
51
|
// Check if a property is absent
|
|
45
52
|
new Absent
|
|
46
53
|
|
|
54
|
+
// Check if a number is greater and/or lesser than a given number
|
|
55
|
+
new Bounds({ greaterThan: 0, lesserThan: 10 })
|
|
56
|
+
// Check if a number is greater and/or lesser than or equal a given number
|
|
57
|
+
new Bounds({ greaterThanEqual: 0, lesserThanqEqual: 10 })
|
|
58
|
+
|
|
47
59
|
// Check a property's value equals one of the three given strings
|
|
48
60
|
new Enum('Moni', 'Lisa', 'Anna')
|
|
49
61
|
// Check if a property's value equals one of the three given numbers
|
|
@@ -54,6 +66,11 @@ new Exists(async (user: User) => {
|
|
|
54
66
|
// Return true if your exists condition is met
|
|
55
67
|
})
|
|
56
68
|
|
|
69
|
+
// Check if the length of a string has a minmum and/or maximum length
|
|
70
|
+
new Length({ min: 5, max: 10})
|
|
71
|
+
// Check if the length of a string has exactly a specific length
|
|
72
|
+
new Length({ exact: 5 })
|
|
73
|
+
|
|
57
74
|
// Check if a property is there
|
|
58
75
|
new Required
|
|
59
76
|
|
|
@@ -85,19 +102,16 @@ misfits.length == 1
|
|
|
85
102
|
A misfit contains the following informations by default.
|
|
86
103
|
|
|
87
104
|
```typescript
|
|
88
|
-
// The name of the
|
|
105
|
+
// The name of the constraint that resulted in the misfit
|
|
89
106
|
misfit.constraint == 'Required'
|
|
90
107
|
|
|
91
|
-
// The
|
|
92
|
-
misfit.property == 'email'
|
|
93
|
-
|
|
94
|
-
// The properties where the misfit occured
|
|
95
|
-
misfit.properties == ['firstName', 'lastName']
|
|
108
|
+
// The involved properties
|
|
109
|
+
misfit.property == [ 'email' ]
|
|
96
110
|
|
|
97
|
-
//
|
|
98
|
-
misfit.
|
|
111
|
+
// Contextual values specific to the misfit at hand (Optional)
|
|
112
|
+
misfit.values
|
|
99
113
|
|
|
100
|
-
// A message (Optional)
|
|
114
|
+
// A misfit message (Optional)
|
|
101
115
|
misift.message == 'The property email is required.'
|
|
102
116
|
```
|
|
103
117
|
|
|
@@ -129,40 +143,46 @@ class UserValidator extends Validator {
|
|
|
129
143
|
}
|
|
130
144
|
```
|
|
131
145
|
|
|
132
|
-
#### Exclude rules
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
// Exclude all constraints regarding the email property
|
|
136
|
-
let misfits = validator.validate(user, { exclude: ['email'] })
|
|
137
|
-
|
|
138
|
-
// Exclude only the required constraint of the email property
|
|
139
|
-
let misfits = validator.validate(user, { exclude: [{ properties: 'email', constraint: 'Required' }] })
|
|
140
|
-
```
|
|
141
|
-
|
|
142
146
|
### Anonymous custom constraints
|
|
143
147
|
|
|
144
|
-
You can
|
|
148
|
+
You can create custom constraints on the fly without having to create a dedicated constraint class.
|
|
145
149
|
|
|
146
150
|
```typescript
|
|
147
151
|
let validator = new Validator
|
|
148
152
|
|
|
153
|
+
// Second parameter is the name of the contstraint
|
|
154
|
+
// Third parameter is the validation function which receives the value of the property
|
|
155
|
+
validator.add('email', 'OnlyGermanMails', async (email: string) {
|
|
156
|
+
if (! email.endsWith('.de')) {
|
|
157
|
+
|
|
158
|
+
// You do not need to set the constraint name nor the property on the misfit.
|
|
159
|
+
// Those will be set automatically.
|
|
160
|
+
return new Misfit
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
// Second parameter is the name of the contstraint
|
|
165
|
+
// Third parameter is the validation function which receives the complete object owning both properties
|
|
149
166
|
validator.add(['firstName', 'lastName'], 'Different', async (user: User) => {
|
|
150
167
|
if (user.firstName == user.lastName) {
|
|
151
168
|
let misfit = new Misfit
|
|
152
169
|
|
|
153
|
-
// You
|
|
170
|
+
// You do not need to set the constraint name nor the properties on the misfit.
|
|
171
|
+
// Those will be set automatically.
|
|
154
172
|
|
|
155
|
-
//
|
|
156
|
-
misfit.
|
|
173
|
+
// You can provide some contextual information
|
|
174
|
+
misfit.values = {
|
|
157
175
|
firstName: user.firstName,
|
|
158
176
|
lastName: user.lastName
|
|
159
177
|
}
|
|
160
178
|
|
|
161
|
-
return
|
|
179
|
+
return misfit
|
|
162
180
|
}
|
|
163
181
|
})
|
|
164
182
|
```
|
|
165
183
|
|
|
184
|
+
In the case the misfit involves exactly one property, its corresponding validation function receives the value of that one property as a parameter. In the case the misfit involves more than one property, the corresponding validation function receives the whole object that is being validated, so that all of the involved properties can be accessed.
|
|
185
|
+
|
|
166
186
|
### Custom constraints as classes
|
|
167
187
|
|
|
168
188
|
If you want to reuse a constraint over and over again, create a new class for it.
|
|
@@ -170,39 +190,29 @@ If you want to reuse a constraint over and over again, create a new class for it
|
|
|
170
190
|
```typescript
|
|
171
191
|
import { Constraint } from 'knight-validation'
|
|
172
192
|
|
|
173
|
-
|
|
193
|
+
export interface DifferentMisfitValues {
|
|
194
|
+
firstName: string
|
|
195
|
+
lastName: string
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export class Different extends Constraint<User, DifferentMisfitValues> {
|
|
174
199
|
|
|
175
200
|
// Override the abstract method validate
|
|
176
|
-
|
|
201
|
+
validate(user: User): Promise<Misfit<DifferentMisfitValues>|null> {
|
|
202
|
+
if (user.firstName == user.lastName) {
|
|
203
|
+
let misfit = new Misfit<DifferentMisfitValues>
|
|
177
204
|
|
|
178
|
-
//
|
|
179
|
-
|
|
180
|
-
|
|
205
|
+
// You do not need to set the constraint name nor the properties on the misfit.
|
|
206
|
+
// Those will be set automatically.
|
|
207
|
+
|
|
208
|
+
misfit.values = {
|
|
209
|
+
firstName: user.firstName,
|
|
210
|
+
lastName: user.lastName
|
|
181
211
|
}
|
|
182
212
|
|
|
183
|
-
|
|
184
|
-
if (typeof properties == 'string') {
|
|
185
|
-
// In case of a single property
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
// In case of multiple properties
|
|
189
|
-
}
|
|
213
|
+
return misfit
|
|
190
214
|
}
|
|
191
215
|
}
|
|
192
216
|
```
|
|
193
217
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
```typescript
|
|
197
|
-
import { Constraint } from 'knight-validation'
|
|
198
|
-
|
|
199
|
-
class YourConstraint extends Constraint {
|
|
200
|
-
async validate(obj: any, properties: string|string[]): Promise<Misfit|undefined> {
|
|
201
|
-
return this.defaultValidation(obj, properties, async (value: any) => {
|
|
202
|
-
if (value == 1) { // Validate the value here
|
|
203
|
-
return new Misfit
|
|
204
|
-
}
|
|
205
|
-
})
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
```
|
|
218
|
+
You need to pay attention to the parameter of the validate method. In the case your constraint is assigned to a single property, that parameter will have the value of the mentioned property. In the case your constraint is assigned to an array of properties, that parameter will have the complete object as its value which ought to contain the mentioned properties.
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./lib/Constraint"), exports);
|
|
17
18
|
__exportStar(require("./lib/constraints/Absent"), exports);
|
|
18
19
|
__exportStar(require("./lib/constraints/Bounds"), exports);
|
|
19
20
|
__exportStar(require("./lib/constraints/Enum"), exports);
|
package/lib/lib/Validator.d.ts
CHANGED
|
@@ -3,18 +3,18 @@ import { Misfit } from './Misfit';
|
|
|
3
3
|
export interface ValidatorOptions {
|
|
4
4
|
checkOnlyWhatIsThere?: boolean;
|
|
5
5
|
}
|
|
6
|
-
interface ValidatorEntry {
|
|
6
|
+
interface ValidatorEntry<T = any> {
|
|
7
7
|
properties: string[];
|
|
8
8
|
constraint?: Constraint;
|
|
9
|
-
validator?: Validator
|
|
10
|
-
condition?: (object:
|
|
9
|
+
validator?: Validator;
|
|
10
|
+
condition?: (object: T) => Promise<boolean>;
|
|
11
11
|
}
|
|
12
|
-
export declare class Validator<T> {
|
|
12
|
+
export declare class Validator<T = any> {
|
|
13
13
|
options?: ValidatorOptions;
|
|
14
|
-
entries: ValidatorEntry[];
|
|
14
|
+
entries: ValidatorEntry<T>[];
|
|
15
15
|
constructor(options?: ValidatorOptions);
|
|
16
|
-
add(
|
|
17
|
-
add(
|
|
16
|
+
add(property: string, constraint: Constraint<T>, condition?: (object: T) => Promise<boolean>): void;
|
|
17
|
+
add(property: string, constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
18
18
|
add(properties: string[], constraintName: string, validate: (object: T) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
19
19
|
add(property: string, validator: Validator<any>, condition?: (object: T) => Promise<boolean>): void;
|
|
20
20
|
add(validator: Validator<any>): void;
|
package/lib/lib/Validator.js
CHANGED
|
@@ -43,7 +43,7 @@ class Validator {
|
|
|
43
43
|
condition = args.length > 2 ? args[2] : undefined;
|
|
44
44
|
}
|
|
45
45
|
else {
|
|
46
|
-
throw new Error('
|
|
46
|
+
throw new Error('Invalid parameters');
|
|
47
47
|
}
|
|
48
48
|
this.entries.push({
|
|
49
49
|
properties: properties,
|
|
@@ -98,7 +98,7 @@ class Validator {
|
|
|
98
98
|
if (misfit.constraint === undefined) {
|
|
99
99
|
misfit.constraint = entry.constraint.name;
|
|
100
100
|
}
|
|
101
|
-
misfit.properties = entry.properties;
|
|
101
|
+
misfit.properties = entry.properties.slice();
|
|
102
102
|
misfittingProperties.push(...entry.properties);
|
|
103
103
|
misfits.push(misfit);
|
|
104
104
|
}
|
|
@@ -3,8 +3,8 @@ import { Misfit } from '../Misfit';
|
|
|
3
3
|
export interface ExistsMisfitValues {
|
|
4
4
|
notExistingValue: any;
|
|
5
5
|
}
|
|
6
|
-
export declare class Exists extends Constraint<
|
|
7
|
-
doesExist: (value:
|
|
8
|
-
constructor(doesExist: (value:
|
|
9
|
-
validate(value:
|
|
6
|
+
export declare class Exists<T> extends Constraint<T, ExistsMisfitValues> {
|
|
7
|
+
doesExist: (value: T) => Promise<boolean>;
|
|
8
|
+
constructor(doesExist: (value: T) => Promise<boolean>);
|
|
9
|
+
validate(value: T): Promise<Misfit<ExistsMisfitValues> | null>;
|
|
10
10
|
}
|
|
@@ -3,8 +3,8 @@ import { Misfit } from '../Misfit';
|
|
|
3
3
|
export interface UniqueMisfitValues {
|
|
4
4
|
notUniqueValue: any;
|
|
5
5
|
}
|
|
6
|
-
export declare class Unique extends Constraint<
|
|
7
|
-
isUnique: (value:
|
|
8
|
-
constructor(isUnique: (value:
|
|
9
|
-
validate(value:
|
|
6
|
+
export declare class Unique<T> extends Constraint<T, UniqueMisfitValues> {
|
|
7
|
+
isUnique: (value: T) => Promise<boolean>;
|
|
8
|
+
constructor(isUnique: (value: T) => Promise<boolean>);
|
|
9
|
+
validate(value: T): Promise<Misfit<UniqueMisfitValues> | null>;
|
|
10
10
|
}
|