knight-validation 1.1.0 → 2.0.1
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.js +26 -26
- package/lib/lib/Constraint.js +5 -121
- package/lib/lib/Misfit.js +25 -45
- package/lib/lib/MisfitsError.js +7 -22
- package/lib/lib/QuickConstraint.js +26 -0
- package/lib/lib/Validator.js +91 -404
- package/lib/lib/constraints/Absent.js +10 -75
- package/lib/lib/constraints/Bounds.js +50 -0
- package/lib/lib/constraints/Enum.js +36 -73
- package/lib/lib/constraints/Exists.js +19 -67
- package/lib/lib/constraints/Length.js +46 -0
- package/lib/lib/constraints/Required.js +10 -75
- package/lib/lib/constraints/TypeOf.js +57 -77
- package/lib/lib/constraints/Unique.js +19 -68
- package/package.json +7 -7
- package/lib/index.d.ts +0 -13
- package/lib/lib/Constraint.d.ts +0 -9
- package/lib/lib/Misfit.d.ts +0 -17
- package/lib/lib/MisfitsError.d.ts +0 -5
- package/lib/lib/Validator.d.ts +0 -38
- package/lib/lib/constraints/Absent.d.ts +0 -5
- package/lib/lib/constraints/Enum.d.ts +0 -8
- package/lib/lib/constraints/Exists.d.ts +0 -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/constraints/Required.d.ts +0 -5
- package/lib/lib/constraints/TypeOf.d.ts +0 -10
- package/lib/lib/constraints/Unique.d.ts +0 -7
- package/lib/lib/fieldsEqual.d.ts +0 -1
- package/lib/lib/fieldsEqual.js +0 -17
|
@@ -1,17 +1,4 @@
|
|
|
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
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -21,67 +8,43 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
21
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
9
|
});
|
|
23
10
|
};
|
|
24
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
25
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
26
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
27
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
28
|
-
function step(op) {
|
|
29
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
30
|
-
while (_) try {
|
|
31
|
-
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;
|
|
32
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
33
|
-
switch (op[0]) {
|
|
34
|
-
case 0: case 1: t = op; break;
|
|
35
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
36
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
37
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
38
|
-
default:
|
|
39
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
40
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
41
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
42
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
43
|
-
if (t[2]) _.ops.pop();
|
|
44
|
-
_.trys.pop(); continue;
|
|
45
|
-
}
|
|
46
|
-
op = body.call(thisArg, _);
|
|
47
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
48
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
12
|
+
exports.Enum = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class Enum extends Constraint_1.Constraint {
|
|
16
|
+
constructor(...values) {
|
|
17
|
+
super();
|
|
18
|
+
this.values = [];
|
|
19
|
+
for (let value of values) {
|
|
20
|
+
if (value instanceof Array) {
|
|
21
|
+
this.values.push(...value);
|
|
22
|
+
}
|
|
23
|
+
else if (typeof value == 'object') {
|
|
24
|
+
for (let key in value) {
|
|
25
|
+
if (isNaN(parseInt(key, 10))) {
|
|
26
|
+
this.values.push(value[key]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.values.push(value);
|
|
32
|
+
}
|
|
67
33
|
}
|
|
68
|
-
return _this;
|
|
69
34
|
}
|
|
70
|
-
|
|
71
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
});
|
|
35
|
+
validate(value) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
if (value === undefined) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
if (this.values.indexOf(value) == -1) {
|
|
41
|
+
return new Misfit_1.Misfit(this.name, {
|
|
42
|
+
actual: value,
|
|
43
|
+
values: this.values
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
83
47
|
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
exports.default = Enum;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.Enum = Enum;
|
|
@@ -1,17 +1,4 @@
|
|
|
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
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -21,61 +8,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
21
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
9
|
});
|
|
23
10
|
};
|
|
24
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
25
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
26
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
27
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
28
|
-
function step(op) {
|
|
29
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
30
|
-
while (_) try {
|
|
31
|
-
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;
|
|
32
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
33
|
-
switch (op[0]) {
|
|
34
|
-
case 0: case 1: t = op; break;
|
|
35
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
36
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
37
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
38
|
-
default:
|
|
39
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
40
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
41
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
42
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
43
|
-
if (t[2]) _.ops.pop();
|
|
44
|
-
_.trys.pop(); continue;
|
|
45
|
-
}
|
|
46
|
-
op = body.call(thisArg, _);
|
|
47
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
48
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return _this;
|
|
12
|
+
exports.Exists = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class Exists extends Constraint_1.Constraint {
|
|
16
|
+
constructor(doesExist) {
|
|
17
|
+
super();
|
|
18
|
+
this.doesExist = doesExist;
|
|
60
19
|
}
|
|
61
|
-
|
|
62
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
return [4 /*yield*/, this.doesExist(obj, field)];
|
|
70
|
-
case 1:
|
|
71
|
-
if (!(_a.sent())) {
|
|
72
|
-
return [2 /*return*/, new Misfit_1.default];
|
|
73
|
-
}
|
|
74
|
-
return [2 /*return*/];
|
|
20
|
+
validate(value) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (value !== undefined) {
|
|
23
|
+
if (!(yield this.doesExist(value))) {
|
|
24
|
+
return new Misfit_1.Misfit(this.name, {
|
|
25
|
+
notExistingValue: value
|
|
26
|
+
});
|
|
75
27
|
}
|
|
76
|
-
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
77
30
|
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
exports.default = Exists;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.Exists = Exists;
|
|
@@ -0,0 +1,46 @@
|
|
|
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.Length = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class Length extends Constraint_1.Constraint {
|
|
16
|
+
constructor(constraints) {
|
|
17
|
+
super();
|
|
18
|
+
Object.assign(this, constraints);
|
|
19
|
+
}
|
|
20
|
+
validate(value) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (typeof value != 'string' && !(value instanceof Array)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (this.min != undefined && value.length < this.min) {
|
|
26
|
+
return this._createMisfit(value.length);
|
|
27
|
+
}
|
|
28
|
+
else if (this.max != undefined && value.length > this.max) {
|
|
29
|
+
return this._createMisfit(value.length);
|
|
30
|
+
}
|
|
31
|
+
else if (this.exact != undefined && value.length != this.exact) {
|
|
32
|
+
return this._createMisfit(value.length);
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
_createMisfit(actual) {
|
|
38
|
+
return new Misfit_1.Misfit(this.name, {
|
|
39
|
+
actual: actual,
|
|
40
|
+
exact: this.exact,
|
|
41
|
+
max: this.max,
|
|
42
|
+
min: this.min
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.Length = Length;
|
|
@@ -1,17 +1,4 @@
|
|
|
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
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -21,67 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
21
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
9
|
});
|
|
23
10
|
};
|
|
24
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
25
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
26
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
27
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
28
|
-
function step(op) {
|
|
29
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
30
|
-
while (_) try {
|
|
31
|
-
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;
|
|
32
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
33
|
-
switch (op[0]) {
|
|
34
|
-
case 0: case 1: t = op; break;
|
|
35
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
36
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
37
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
38
|
-
default:
|
|
39
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
40
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
41
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
42
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
43
|
-
if (t[2]) _.ops.pop();
|
|
44
|
-
_.trys.pop(); continue;
|
|
45
|
-
}
|
|
46
|
-
op = body.call(thisArg, _);
|
|
47
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
48
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
Required.prototype.validate = function (obj, field) {
|
|
60
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
61
|
-
var value, _i, field_1, fld, value;
|
|
62
|
-
return __generator(this, function (_a) {
|
|
63
|
-
if (typeof field == 'string') {
|
|
64
|
-
value = obj[field];
|
|
65
|
-
if (Constraint_1.default.absent(value)) {
|
|
66
|
-
return [2 /*return*/, new Misfit_1.default];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
else if (field instanceof Array) {
|
|
70
|
-
for (_i = 0, field_1 = field; _i < field_1.length; _i++) {
|
|
71
|
-
fld = field_1[_i];
|
|
72
|
-
value = obj[fld];
|
|
73
|
-
if (Constraint_1.default.absent(value)) {
|
|
74
|
-
return [2 /*return*/, new Misfit_1.default];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
throw new Error('Parameter field was neither of type string nor instance of Array');
|
|
80
|
-
}
|
|
81
|
-
return [2 /*return*/];
|
|
82
|
-
});
|
|
12
|
+
exports.Required = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class Required extends Constraint_1.Constraint {
|
|
16
|
+
validate(value) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
return value === undefined ? new Misfit_1.Misfit(this.name) : null;
|
|
83
19
|
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
exports.default = Required;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Required = Required;
|
|
@@ -1,17 +1,4 @@
|
|
|
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
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -21,70 +8,63 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
21
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
9
|
});
|
|
23
10
|
};
|
|
24
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
25
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
26
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
27
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
28
|
-
function step(op) {
|
|
29
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
30
|
-
while (_) try {
|
|
31
|
-
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;
|
|
32
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
33
|
-
switch (op[0]) {
|
|
34
|
-
case 0: case 1: t = op; break;
|
|
35
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
36
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
37
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
38
|
-
default:
|
|
39
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
40
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
41
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
42
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
43
|
-
if (t[2]) _.ops.pop();
|
|
44
|
-
_.trys.pop(); continue;
|
|
45
|
-
}
|
|
46
|
-
op = body.call(thisArg, _);
|
|
47
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
48
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return _this;
|
|
12
|
+
exports.TypeOf = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class TypeOf extends Constraint_1.Constraint {
|
|
16
|
+
constructor(...valueTypes) {
|
|
17
|
+
super();
|
|
18
|
+
this.valueTypes = valueTypes;
|
|
60
19
|
}
|
|
61
|
-
|
|
62
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
20
|
+
validate(value) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (value === undefined) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
for (let valueType of this.valueTypes) {
|
|
26
|
+
if (typeof valueType == 'string') {
|
|
27
|
+
if (typeof value === valueType) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (valueType === null) {
|
|
32
|
+
if (value === null) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (value instanceof valueType) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let actualType;
|
|
41
|
+
if (typeof value == 'object' && value !== null) {
|
|
42
|
+
actualType = value.constructor.name;
|
|
43
|
+
}
|
|
44
|
+
else if (value === null) {
|
|
45
|
+
actualType = null;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
actualType = typeof value;
|
|
49
|
+
}
|
|
50
|
+
let misfit = new Misfit_1.Misfit(this.name);
|
|
51
|
+
misfit.values = {
|
|
52
|
+
actual: actualType,
|
|
53
|
+
types: []
|
|
54
|
+
};
|
|
55
|
+
for (let valueType of this.valueTypes) {
|
|
56
|
+
if (typeof valueType == 'string') {
|
|
57
|
+
misfit.values.types.push(valueType);
|
|
58
|
+
}
|
|
59
|
+
else if (valueType === null) {
|
|
60
|
+
misfit.values.types.push(null);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
misfit.values.types.push(valueType.name);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return misfit;
|
|
86
67
|
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
exports.default = TypeOf;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.TypeOf = TypeOf;
|
|
@@ -1,17 +1,4 @@
|
|
|
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
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -21,61 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
21
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
9
|
});
|
|
23
10
|
};
|
|
24
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
25
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
26
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
27
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
28
|
-
function step(op) {
|
|
29
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
30
|
-
while (_) try {
|
|
31
|
-
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;
|
|
32
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
33
|
-
switch (op[0]) {
|
|
34
|
-
case 0: case 1: t = op; break;
|
|
35
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
36
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
37
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
38
|
-
default:
|
|
39
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
40
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
41
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
42
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
43
|
-
if (t[2]) _.ops.pop();
|
|
44
|
-
_.trys.pop(); continue;
|
|
45
|
-
}
|
|
46
|
-
op = body.call(thisArg, _);
|
|
47
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
48
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return _this;
|
|
12
|
+
exports.Unique = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class Unique extends Constraint_1.Constraint {
|
|
16
|
+
constructor(isUnique) {
|
|
17
|
+
super();
|
|
18
|
+
this.isUnique = isUnique;
|
|
60
19
|
}
|
|
61
|
-
|
|
62
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
case 1:
|
|
71
|
-
if (!(_a.sent())) {
|
|
72
|
-
return [2 /*return*/, new Misfit_1.default(this.name)];
|
|
73
|
-
}
|
|
74
|
-
return [2 /*return*/];
|
|
75
|
-
}
|
|
76
|
-
});
|
|
20
|
+
validate(value) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (value === undefined) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (!(yield this.isUnique(value))) {
|
|
26
|
+
return new Misfit_1.Misfit(this.name, { notUniqueValue: value });
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
77
29
|
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
exports.default = Unique;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.Unique = Unique;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "knight-validation",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "A validation lib",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"lib/**/*"
|
|
9
9
|
],
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@types/chai": "^4.
|
|
12
|
-
"@types/mocha": "^
|
|
11
|
+
"@types/chai": "^4.3.3",
|
|
12
|
+
"@types/mocha": "^9.1.1",
|
|
13
13
|
"@types/node": "^13.5.0",
|
|
14
|
-
"chai": "^4.
|
|
15
|
-
"mocha": "^
|
|
16
|
-
"ts-node": "^
|
|
17
|
-
"typescript": "^
|
|
14
|
+
"chai": "^4.3.6",
|
|
15
|
+
"mocha": "^10.0.0",
|
|
16
|
+
"ts-node": "^10.9.1",
|
|
17
|
+
"typescript": "^4.8.2"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"test": "mocha -r ts-node/register \"test/**/*.test.ts\"",
|
package/lib/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export { default as Constraint } from './lib/Constraint';
|
|
2
|
-
export { default as Absent } from './lib/constraints/Absent';
|
|
3
|
-
export { default as Enum } from './lib/constraints/Enum';
|
|
4
|
-
export { default as Exists } from './lib/constraints/Exists';
|
|
5
|
-
export { default as Max } from './lib/constraints/Max';
|
|
6
|
-
export { default as QuickConstraint } from './lib/constraints/QuickConstraint';
|
|
7
|
-
export { default as Required } from './lib/constraints/Required';
|
|
8
|
-
export { default as TypeOf, TypeOfConstraints } from './lib/constraints/TypeOf';
|
|
9
|
-
export { default as Unique } from './lib/constraints/Unique';
|
|
10
|
-
export { default as fieldsEqual } from './lib/fieldsEqual';
|
|
11
|
-
export { default as Misfit } from './lib/Misfit';
|
|
12
|
-
export { default as MisfitsError } from './lib/MisfitsError';
|
|
13
|
-
export { default as Validator, ValidatorOptions } from './lib/Validator';
|