knight-validation 2.0.0 → 2.0.3
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 +31 -31
- package/lib/index.d.ts +12 -13
- package/lib/index.js +26 -26
- package/lib/lib/Constraint.d.ts +3 -7
- package/lib/lib/Constraint.js +5 -121
- package/lib/lib/Misfit.d.ts +8 -14
- package/lib/lib/Misfit.js +24 -44
- package/lib/lib/MisfitsError.d.ts +2 -2
- package/lib/lib/MisfitsError.js +7 -22
- package/lib/lib/QuickConstraint.d.ts +8 -0
- package/lib/lib/QuickConstraint.js +26 -0
- package/lib/lib/Validator.d.ts +16 -31
- package/lib/lib/Validator.js +104 -403
- package/lib/lib/constraints/Absent.d.ts +7 -4
- package/lib/lib/constraints/Absent.js +10 -75
- package/lib/lib/constraints/Bounds.d.ts +18 -0
- package/lib/lib/constraints/Bounds.js +50 -0
- package/lib/lib/constraints/Enum.d.ts +8 -4
- package/lib/lib/constraints/Enum.js +27 -76
- package/lib/lib/constraints/Exists.d.ts +9 -6
- package/lib/lib/constraints/Exists.js +19 -67
- package/lib/lib/constraints/Length.d.ts +16 -0
- package/lib/lib/constraints/Length.js +46 -0
- package/lib/lib/constraints/Required.d.ts +4 -4
- package/lib/lib/constraints/Required.js +10 -75
- package/lib/lib/constraints/TypeOf.d.ts +9 -10
- package/lib/lib/constraints/TypeOf.js +57 -81
- package/lib/lib/constraints/Unique.d.ts +9 -6
- package/lib/lib/constraints/Unique.js +19 -68
- package/package.json +7 -7
- package/lib/lib/constraints/Max.d.ts +0 -7
- package/lib/lib/constraints/Max.js +0 -84
- package/lib/lib/constraints/QuickConstraint.d.ts +0 -7
- package/lib/lib/constraints/QuickConstraint.js +0 -70
- package/lib/lib/fieldsEqual.d.ts +0 -1
- package/lib/lib/fieldsEqual.js +0 -17
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
## Overview
|
|
8
8
|
|
|
9
|
-
### Constraints for single
|
|
9
|
+
### Constraints for single properties
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
12
|
import { Required, TypeOf, Unique, Validator } from 'knight-validation'
|
|
@@ -25,7 +25,7 @@ class UserValidator extends Validator {
|
|
|
25
25
|
}
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
### Constraints for multiple
|
|
28
|
+
### Constraints for multiple properties
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
31
|
import { Validator } from 'knight-validation'
|
|
@@ -41,12 +41,12 @@ class UserValidator extends Validator {
|
|
|
41
41
|
### Available constraints
|
|
42
42
|
|
|
43
43
|
```typescript
|
|
44
|
-
// Check if a
|
|
44
|
+
// Check if a property is absent
|
|
45
45
|
new Absent
|
|
46
46
|
|
|
47
|
-
// Check a
|
|
47
|
+
// Check a property's value equals one of the three given strings
|
|
48
48
|
new Enum('Moni', 'Lisa', 'Anna')
|
|
49
|
-
// Check if a
|
|
49
|
+
// Check if a property's value equals one of the three given numbers
|
|
50
50
|
new Enum(1, 3, 7)
|
|
51
51
|
|
|
52
52
|
new Exists(async (user: User) => {
|
|
@@ -54,12 +54,12 @@ new Exists(async (user: User) => {
|
|
|
54
54
|
// Return true if your exists condition is met
|
|
55
55
|
})
|
|
56
56
|
|
|
57
|
-
// Check if a
|
|
57
|
+
// Check if a property is there
|
|
58
58
|
new Required
|
|
59
59
|
|
|
60
|
-
// Check if a
|
|
60
|
+
// Check if a property's value is of the given JavaScript type
|
|
61
61
|
new TypeOf('number')
|
|
62
|
-
// Check if a
|
|
62
|
+
// Check if a property's value is an instance if the given class
|
|
63
63
|
new TypeOf(Date)
|
|
64
64
|
|
|
65
65
|
new Unique(async (user: User) => {
|
|
@@ -70,7 +70,7 @@ new Unique(async (user: User) => {
|
|
|
70
70
|
|
|
71
71
|
### Validation
|
|
72
72
|
|
|
73
|
-
Validating an object returns an array of misfits which is empty if there are not any. The validator goes through all constraints for one
|
|
73
|
+
Validating an object returns an array of misfits which is empty if there are not any. The validator goes through all constraints for one property and stops the validation on the first misfit. Afterwards it goes on to the next property.
|
|
74
74
|
|
|
75
75
|
```typescript
|
|
76
76
|
let user = new User
|
|
@@ -86,19 +86,19 @@ A misfit contains the following informations by default.
|
|
|
86
86
|
|
|
87
87
|
```typescript
|
|
88
88
|
// The name of the misfit which defaults to the name of the constraint which was not met
|
|
89
|
-
misfit.
|
|
89
|
+
misfit.constraint == 'Required'
|
|
90
90
|
|
|
91
|
-
// The
|
|
92
|
-
misfit.
|
|
91
|
+
// The property where the misfit occured
|
|
92
|
+
misfit.property == 'email'
|
|
93
93
|
|
|
94
|
-
// The
|
|
95
|
-
misfit.
|
|
94
|
+
// The properties where the misfit occured
|
|
95
|
+
misfit.properties == ['firstName', 'lastName']
|
|
96
96
|
|
|
97
97
|
// It contains any information that is useful about why checking the constraint resulted in a misfit (Optional)
|
|
98
|
-
misfit.
|
|
98
|
+
misfit.value
|
|
99
99
|
|
|
100
100
|
// A message (Optional)
|
|
101
|
-
misift.message == 'The
|
|
101
|
+
misift.message == 'The property email is required.'
|
|
102
102
|
```
|
|
103
103
|
|
|
104
104
|
#### Check only what is there
|
|
@@ -111,7 +111,7 @@ user.email = undefined
|
|
|
111
111
|
|
|
112
112
|
let misfits = validator.validate(user, { checkOnlyWhatIsThere: true })
|
|
113
113
|
|
|
114
|
-
misfits.length == 0 // There are no misfits even though the email
|
|
114
|
+
misfits.length == 0 // There are no misfits even though the email property is required
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
### Constraints that are only checked if a condition is met
|
|
@@ -132,11 +132,11 @@ class UserValidator extends Validator {
|
|
|
132
132
|
#### Exclude rules
|
|
133
133
|
|
|
134
134
|
```typescript
|
|
135
|
-
// Exclude all constraints regarding the email
|
|
135
|
+
// Exclude all constraints regarding the email property
|
|
136
136
|
let misfits = validator.validate(user, { exclude: ['email'] })
|
|
137
137
|
|
|
138
|
-
// Exclude only the required constraint of the email
|
|
139
|
-
let misfits = validator.validate(user, { exclude: [{
|
|
138
|
+
// Exclude only the required constraint of the email property
|
|
139
|
+
let misfits = validator.validate(user, { exclude: [{ properties: 'email', constraint: 'Required' }] })
|
|
140
140
|
```
|
|
141
141
|
|
|
142
142
|
### Anonymous custom constraints
|
|
@@ -150,7 +150,7 @@ validator.add(['firstName', 'lastName'], 'Different', async (user: User) => {
|
|
|
150
150
|
if (user.firstName == user.lastName) {
|
|
151
151
|
let misfit = new Misfit
|
|
152
152
|
|
|
153
|
-
// You can skip setting a name and the
|
|
153
|
+
// You can skip setting a name and the property(s) for the misfit. These will be set automatically.
|
|
154
154
|
|
|
155
155
|
// We give it some information here on what went wrong. What you put in here depends on your needs.
|
|
156
156
|
misfit.constraints = {
|
|
@@ -173,32 +173,32 @@ import { Constraint } from 'knight-validation'
|
|
|
173
173
|
class YourConstraint extends Constraint {
|
|
174
174
|
|
|
175
175
|
// Override the abstract method validate
|
|
176
|
-
async validate(obj: any,
|
|
176
|
+
async validate(obj: any, properties: string|string[]): Promise<Misfit|undefined> {
|
|
177
177
|
|
|
178
|
-
// At first you want to check if the
|
|
179
|
-
if (this.
|
|
178
|
+
// At first you want to check if the properties are absent because in case of absense you do not want to validate because a property may be optional.
|
|
179
|
+
if (this.arePropertyAbsent(obj, properties)) {
|
|
180
180
|
return
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
// Next you need to check if the
|
|
184
|
-
if (typeof
|
|
185
|
-
// In case of a single
|
|
183
|
+
// Next you need to check if the properties were single or a nultiple ones. Maybe you just implement on of the two possibilities.
|
|
184
|
+
if (typeof properties == 'string') {
|
|
185
|
+
// In case of a single property
|
|
186
186
|
}
|
|
187
187
|
else {
|
|
188
|
-
// In case of multiple
|
|
188
|
+
// In case of multiple properties
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
192
|
```
|
|
193
193
|
|
|
194
|
-
Another possibility is to use the `defaultValidation` method. It will do the check for absence and will implement the validation of combined
|
|
194
|
+
Another possibility is to use the `defaultValidation` method. It will do the check for absence and will implement the validation of combined properties.
|
|
195
195
|
|
|
196
196
|
```typescript
|
|
197
197
|
import { Constraint } from 'knight-validation'
|
|
198
198
|
|
|
199
199
|
class YourConstraint extends Constraint {
|
|
200
|
-
async validate(obj: any,
|
|
201
|
-
return this.defaultValidation(obj,
|
|
200
|
+
async validate(obj: any, properties: string|string[]): Promise<Misfit|undefined> {
|
|
201
|
+
return this.defaultValidation(obj, properties, async (value: any) => {
|
|
202
202
|
if (value == 1) { // Validate the value here
|
|
203
203
|
return new Misfit
|
|
204
204
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export { default as Validator, ValidatorOptions } from './lib/Validator';
|
|
1
|
+
export * from './lib/constraints/Absent';
|
|
2
|
+
export * from './lib/constraints/Bounds';
|
|
3
|
+
export * from './lib/constraints/Enum';
|
|
4
|
+
export * from './lib/constraints/Exists';
|
|
5
|
+
export * from './lib/constraints/Length';
|
|
6
|
+
export * from './lib/constraints/Required';
|
|
7
|
+
export * from './lib/constraints/TypeOf';
|
|
8
|
+
export * from './lib/constraints/Unique';
|
|
9
|
+
export * from './lib/Misfit';
|
|
10
|
+
export * from './lib/MisfitsError';
|
|
11
|
+
export * from './lib/QuickConstraint';
|
|
12
|
+
export * from './lib/Validator';
|
package/lib/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var Required_1 = require("./lib/constraints/Required");
|
|
16
|
-
Object.defineProperty(exports, "Required", { enumerable: true, get: function () { return Required_1.default; } });
|
|
17
|
-
var TypeOf_1 = require("./lib/constraints/TypeOf");
|
|
18
|
-
Object.defineProperty(exports, "TypeOf", { enumerable: true, get: function () { return TypeOf_1.default; } });
|
|
19
|
-
var Unique_1 = require("./lib/constraints/Unique");
|
|
20
|
-
Object.defineProperty(exports, "Unique", { enumerable: true, get: function () { return Unique_1.default; } });
|
|
21
|
-
var fieldsEqual_1 = require("./lib/fieldsEqual");
|
|
22
|
-
Object.defineProperty(exports, "fieldsEqual", { enumerable: true, get: function () { return fieldsEqual_1.default; } });
|
|
23
|
-
var Misfit_1 = require("./lib/Misfit");
|
|
24
|
-
Object.defineProperty(exports, "Misfit", { enumerable: true, get: function () { return Misfit_1.default; } });
|
|
25
|
-
var MisfitsError_1 = require("./lib/MisfitsError");
|
|
26
|
-
Object.defineProperty(exports, "MisfitsError", { enumerable: true, get: function () { return MisfitsError_1.default; } });
|
|
27
|
-
var Validator_1 = require("./lib/Validator");
|
|
28
|
-
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return Validator_1.default; } });
|
|
17
|
+
__exportStar(require("./lib/constraints/Absent"), exports);
|
|
18
|
+
__exportStar(require("./lib/constraints/Bounds"), exports);
|
|
19
|
+
__exportStar(require("./lib/constraints/Enum"), exports);
|
|
20
|
+
__exportStar(require("./lib/constraints/Exists"), exports);
|
|
21
|
+
__exportStar(require("./lib/constraints/Length"), exports);
|
|
22
|
+
__exportStar(require("./lib/constraints/Required"), exports);
|
|
23
|
+
__exportStar(require("./lib/constraints/TypeOf"), exports);
|
|
24
|
+
__exportStar(require("./lib/constraints/Unique"), exports);
|
|
25
|
+
__exportStar(require("./lib/Misfit"), exports);
|
|
26
|
+
__exportStar(require("./lib/MisfitsError"), exports);
|
|
27
|
+
__exportStar(require("./lib/QuickConstraint"), exports);
|
|
28
|
+
__exportStar(require("./lib/Validator"), exports);
|
package/lib/lib/Constraint.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import Misfit from './Misfit';
|
|
2
|
-
export
|
|
1
|
+
import { Misfit } from './Misfit';
|
|
2
|
+
export declare abstract class Constraint<T = any, MisfitValuesType = any> {
|
|
3
3
|
name: string;
|
|
4
|
-
abstract validate(
|
|
5
|
-
protected defaultValidation(obj: any, field: string | string[], validateValue: (value: any) => Promise<Misfit | undefined>): Promise<Misfit | undefined>;
|
|
6
|
-
validateValue(value: any): Promise<Misfit | undefined>;
|
|
7
|
-
isFieldAbsent(obj: any, field: string | string[]): boolean;
|
|
8
|
-
static absent(value: any): boolean;
|
|
4
|
+
abstract validate(value: T): Promise<Misfit<MisfitValuesType> | null>;
|
|
9
5
|
}
|
package/lib/lib/Constraint.js
CHANGED
|
@@ -1,125 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
|
|
40
|
-
|
|
3
|
+
exports.Constraint = void 0;
|
|
4
|
+
class Constraint {
|
|
5
|
+
constructor() {
|
|
41
6
|
this.name = this.constructor.name;
|
|
42
7
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
var value, _i, field_1, fld, value, misfit;
|
|
46
|
-
return __generator(this, function (_a) {
|
|
47
|
-
switch (_a.label) {
|
|
48
|
-
case 0:
|
|
49
|
-
if (this.isFieldAbsent(obj, field)) {
|
|
50
|
-
return [2 /*return*/];
|
|
51
|
-
}
|
|
52
|
-
if (!(typeof field == 'string')) return [3 /*break*/, 1];
|
|
53
|
-
value = obj[field];
|
|
54
|
-
return [2 /*return*/, validateValue(value)];
|
|
55
|
-
case 1:
|
|
56
|
-
if (!(field instanceof Array)) return [3 /*break*/, 6];
|
|
57
|
-
_i = 0, field_1 = field;
|
|
58
|
-
_a.label = 2;
|
|
59
|
-
case 2:
|
|
60
|
-
if (!(_i < field_1.length)) return [3 /*break*/, 5];
|
|
61
|
-
fld = field_1[_i];
|
|
62
|
-
value = obj[fld];
|
|
63
|
-
return [4 /*yield*/, validateValue(value)];
|
|
64
|
-
case 3:
|
|
65
|
-
misfit = _a.sent();
|
|
66
|
-
if (misfit) {
|
|
67
|
-
return [2 /*return*/, misfit];
|
|
68
|
-
}
|
|
69
|
-
_a.label = 4;
|
|
70
|
-
case 4:
|
|
71
|
-
_i++;
|
|
72
|
-
return [3 /*break*/, 2];
|
|
73
|
-
case 5: return [3 /*break*/, 7];
|
|
74
|
-
case 6: throw new Error('Parameter field was neither of type string nor instance of Array');
|
|
75
|
-
case 7: return [2 /*return*/];
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
Constraint.prototype.validateValue = function (value) {
|
|
81
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
-
var misfit;
|
|
83
|
-
return __generator(this, function (_a) {
|
|
84
|
-
switch (_a.label) {
|
|
85
|
-
case 0: return [4 /*yield*/, this.validate({ value: value }, 'value')];
|
|
86
|
-
case 1:
|
|
87
|
-
misfit = _a.sent();
|
|
88
|
-
if (misfit) {
|
|
89
|
-
return [2 /*return*/, misfit];
|
|
90
|
-
}
|
|
91
|
-
return [2 /*return*/];
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
Constraint.prototype.isFieldAbsent = function (obj, field) {
|
|
97
|
-
if (typeof field == 'string') {
|
|
98
|
-
var value = obj[field];
|
|
99
|
-
if (Constraint.absent(value)) {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
else if (field instanceof Array) {
|
|
104
|
-
for (var _i = 0, field_2 = field; _i < field_2.length; _i++) {
|
|
105
|
-
var fld = field_2[_i];
|
|
106
|
-
var value = obj[fld];
|
|
107
|
-
if (Constraint.absent(value)) {
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
throw new Error('Parameter field was neither of type string nor instance of Array');
|
|
114
|
-
}
|
|
115
|
-
return false;
|
|
116
|
-
};
|
|
117
|
-
Constraint.absent = function (value) {
|
|
118
|
-
return value === undefined ||
|
|
119
|
-
value === null ||
|
|
120
|
-
value === '' ||
|
|
121
|
-
typeof value === 'number' && isNaN(value);
|
|
122
|
-
};
|
|
123
|
-
return Constraint;
|
|
124
|
-
}());
|
|
125
|
-
exports.default = Constraint;
|
|
8
|
+
}
|
|
9
|
+
exports.Constraint = Constraint;
|
package/lib/lib/Misfit.d.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare class Misfit<ValuesType = any> {
|
|
2
2
|
constraint: string;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
values?: any;
|
|
3
|
+
properties: string[];
|
|
4
|
+
values?: ValuesType;
|
|
6
5
|
message?: string;
|
|
7
|
-
constructor(constraint?: string,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
static required(field: string, message?: string): Misfit;
|
|
13
|
-
static absent(field: string, message?: string): Misfit;
|
|
14
|
-
static unique(field: string, message?: string): Misfit;
|
|
15
|
-
static exists(field: string, message?: string): Misfit;
|
|
16
|
-
static typeOf(field: string, message?: string): Misfit;
|
|
6
|
+
constructor(constraint?: string, values?: ValuesType);
|
|
7
|
+
setProperties(property: string | string[]): void;
|
|
8
|
+
addPrefix(prefix: string): void;
|
|
9
|
+
isSinglePropery(): boolean;
|
|
10
|
+
isMultipleProperties(): boolean;
|
|
17
11
|
}
|
package/lib/lib/Misfit.js
CHANGED
|
@@ -1,50 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var Unique_1 = require("./constraints/Unique");
|
|
8
|
-
var fieldsEqual_1 = require("./fieldsEqual");
|
|
9
|
-
var Misfit = /** @class */ (function () {
|
|
10
|
-
function Misfit(constraint, field, values) {
|
|
3
|
+
exports.Misfit = void 0;
|
|
4
|
+
class Misfit {
|
|
5
|
+
constructor(constraint, values) {
|
|
6
|
+
this.properties = [];
|
|
11
7
|
this.constraint = constraint;
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
this.values = values;
|
|
9
|
+
}
|
|
10
|
+
setProperties(property) {
|
|
11
|
+
if (typeof property == 'string') {
|
|
12
|
+
this.properties = [property];
|
|
14
13
|
}
|
|
15
|
-
else if (
|
|
16
|
-
this.
|
|
14
|
+
else if (property instanceof Array) {
|
|
15
|
+
this.properties = property;
|
|
17
16
|
}
|
|
18
|
-
this.values = values;
|
|
19
17
|
}
|
|
20
|
-
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
Misfit.required = function (field, message) {
|
|
34
|
-
return new Misfit(Required_1.default.name, field).setMessage(message);
|
|
35
|
-
};
|
|
36
|
-
Misfit.absent = function (field, message) {
|
|
37
|
-
return new Misfit(Absent_1.default.name, field).setMessage(message);
|
|
38
|
-
};
|
|
39
|
-
Misfit.unique = function (field, message) {
|
|
40
|
-
return new Misfit(Unique_1.default.name, field).setMessage(message);
|
|
41
|
-
};
|
|
42
|
-
Misfit.exists = function (field, message) {
|
|
43
|
-
return new Misfit(Exists_1.default.name, field).setMessage(message);
|
|
44
|
-
};
|
|
45
|
-
Misfit.typeOf = function (field, message) {
|
|
46
|
-
return new Misfit(TypeOf_1.default.name, field).setMessage(message);
|
|
47
|
-
};
|
|
48
|
-
return Misfit;
|
|
49
|
-
}());
|
|
50
|
-
exports.default = Misfit;
|
|
18
|
+
addPrefix(prefix) {
|
|
19
|
+
for (let i = 0; i < this.properties.length; i++) {
|
|
20
|
+
this.properties[i] = prefix + this.properties[i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
isSinglePropery() {
|
|
24
|
+
return this.properties && this.properties.length == 1;
|
|
25
|
+
}
|
|
26
|
+
isMultipleProperties() {
|
|
27
|
+
return this.properties && this.properties.length > 1;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Misfit = Misfit;
|
package/lib/lib/MisfitsError.js
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return _this;
|
|
3
|
+
exports.MisfitsError = void 0;
|
|
4
|
+
class MisfitsError extends Error {
|
|
5
|
+
constructor(misfits) {
|
|
6
|
+
super('Misfits occured ' + JSON.stringify(misfits));
|
|
7
|
+
this.misfits = misfits instanceof Array ? misfits : [misfits];
|
|
22
8
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
exports.default = MisfitsError;
|
|
9
|
+
}
|
|
10
|
+
exports.MisfitsError = MisfitsError;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Constraint } from './Constraint';
|
|
2
|
+
import { Misfit } from './Misfit';
|
|
3
|
+
export declare class QuickConstraint<T> extends Constraint<T, any> {
|
|
4
|
+
name: string;
|
|
5
|
+
validateFn: (obj: T) => Promise<Misfit | null>;
|
|
6
|
+
constructor(name: string, validateFn: (obj: T) => Promise<Misfit | null>);
|
|
7
|
+
validate(obj: T): Promise<Misfit | null>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QuickConstraint = void 0;
|
|
13
|
+
const Constraint_1 = require("./Constraint");
|
|
14
|
+
class QuickConstraint extends Constraint_1.Constraint {
|
|
15
|
+
constructor(name, validateFn) {
|
|
16
|
+
super();
|
|
17
|
+
this.name = name;
|
|
18
|
+
this.validateFn = validateFn;
|
|
19
|
+
}
|
|
20
|
+
validate(obj) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
return this.validateFn(obj);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.QuickConstraint = QuickConstraint;
|
package/lib/lib/Validator.d.ts
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
|
-
import Constraint from './Constraint';
|
|
2
|
-
import Misfit from './Misfit';
|
|
1
|
+
import { Constraint } from './Constraint';
|
|
2
|
+
import { Misfit } from './Misfit';
|
|
3
3
|
export interface ValidatorOptions {
|
|
4
4
|
checkOnlyWhatIsThere?: boolean;
|
|
5
|
-
include?: (string | string[] | {
|
|
6
|
-
field: string | string[];
|
|
7
|
-
constraint?: string | string[];
|
|
8
|
-
})[];
|
|
9
|
-
exclude?: (string | string[] | {
|
|
10
|
-
field: string | string[];
|
|
11
|
-
constraint?: string | string[];
|
|
12
|
-
})[];
|
|
13
5
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
fieldConstraints: FieldConstraint[];
|
|
17
|
-
constructor(options?: ValidatorOptions);
|
|
18
|
-
add(field: string | string[], constraint: Constraint, condition?: (object: any) => Promise<boolean>): void;
|
|
19
|
-
add(field: string | string[], constraintName: string, validate: (object: any, field: string | string[]) => Promise<Misfit | undefined>, condition?: (object: any) => Promise<boolean>): void;
|
|
20
|
-
add(field: string | string[], validator: Validator, condition?: (object: any) => Promise<boolean>): void;
|
|
21
|
-
add(validator: Validator): void;
|
|
22
|
-
get fields(): (string | string[])[];
|
|
23
|
-
get singleFields(): string[];
|
|
24
|
-
get combinedFields(): string[][];
|
|
25
|
-
constraints(field: string | string[]): FieldConstraint[];
|
|
26
|
-
validate(object: any, options?: ValidatorOptions): Promise<Misfit[]>;
|
|
27
|
-
}
|
|
28
|
-
declare class FieldConstraint {
|
|
29
|
-
field?: string;
|
|
30
|
-
fields?: string[];
|
|
6
|
+
interface ValidatorEntry {
|
|
7
|
+
properties: string[];
|
|
31
8
|
constraint?: Constraint;
|
|
32
|
-
validator?: Validator
|
|
9
|
+
validator?: Validator<any>;
|
|
33
10
|
condition?: (object: any) => Promise<boolean>;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
11
|
+
}
|
|
12
|
+
export declare class Validator<T> {
|
|
13
|
+
options?: ValidatorOptions;
|
|
14
|
+
entries: ValidatorEntry[];
|
|
15
|
+
constructor(options?: ValidatorOptions);
|
|
16
|
+
add(properties: string, constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
|
|
17
|
+
add(properties: string, constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
18
|
+
add(properties: string[], constraintName: string, validate: (object: T) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
19
|
+
add(property: string, validator: Validator<any>, condition?: (object: T) => Promise<boolean>): void;
|
|
20
|
+
add(validator: Validator<any>): void;
|
|
21
|
+
validate(object: T, options?: ValidatorOptions): Promise<Misfit[]>;
|
|
37
22
|
}
|
|
38
23
|
export {};
|