knight-validation 2.0.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/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, TypeOfValues } 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';
@@ -1,9 +0,0 @@
1
- import Misfit from './Misfit';
2
- export default abstract class Constraint {
3
- name: string;
4
- abstract validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
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;
9
- }
@@ -1,17 +0,0 @@
1
- export default class Misfit {
2
- constraint: string;
3
- field: string;
4
- fields: string[];
5
- values?: any;
6
- message?: string;
7
- constructor(constraint?: string, field?: string | string[], values?: any);
8
- setMessage(message: string | undefined): this;
9
- isSingleField(): boolean;
10
- isMultipleField(): boolean;
11
- fieldsEqual(fields: string[]): boolean;
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;
17
- }
@@ -1,5 +0,0 @@
1
- import Misfit from './Misfit';
2
- export default class MisfitsError extends Error {
3
- misfits: Misfit[];
4
- constructor(misfits: Misfit | Misfit[]);
5
- }
@@ -1,38 +0,0 @@
1
- import Constraint from './Constraint';
2
- import Misfit from './Misfit';
3
- export interface ValidatorOptions {
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
- }
14
- export default class Validator {
15
- options?: ValidatorOptions;
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[];
31
- constraint?: Constraint;
32
- validator?: Validator;
33
- condition?: (object: any) => Promise<boolean>;
34
- constructor(field: string | string[], constraintOrValidator: Constraint | Validator, condition?: (object: any) => Promise<boolean>);
35
- validateConstraint(value: any, object: any): Promise<Misfit | undefined>;
36
- validateValidator(value: any): Promise<Misfit[]>;
37
- }
38
- export {};
@@ -1,5 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class Absent extends Constraint {
4
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
5
- }
@@ -1,9 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class Enum extends Constraint {
4
- values: any[];
5
- constructor(values: any[]);
6
- constructor(...values: any[]);
7
- constructor(values: object);
8
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
9
- }
@@ -1,7 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class Exists extends Constraint {
4
- doesExist: (value: any, obj: any) => Promise<boolean>;
5
- constructor(doesExist: (obj: any, field: string | string[]) => Promise<boolean>);
6
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
7
- }
@@ -1,7 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class Max extends Constraint {
4
- max: number;
5
- constructor(max: number);
6
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
7
- }
@@ -1,84 +0,0 @@
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
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
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
- Object.defineProperty(exports, "__esModule", { value: true });
52
- var Constraint_1 = require("../Constraint");
53
- var Misfit_1 = require("../Misfit");
54
- var Max = /** @class */ (function (_super) {
55
- __extends(Max, _super);
56
- function Max(max) {
57
- var _this = _super.call(this) || this;
58
- _this.max = max;
59
- return _this;
60
- }
61
- Max.prototype.validate = function (obj, field) {
62
- return __awaiter(this, void 0, void 0, function () {
63
- var _this = this;
64
- return __generator(this, function (_a) {
65
- return [2 /*return*/, this.defaultValidation(obj, field, function (value) { return __awaiter(_this, void 0, void 0, function () {
66
- return __generator(this, function (_a) {
67
- if (typeof value == 'number') {
68
- return [2 /*return*/, value > this.max ? new Misfit_1.default : undefined];
69
- }
70
- if (typeof value == 'string') {
71
- return [2 /*return*/, value.length > this.max ? new Misfit_1.default : undefined];
72
- }
73
- if (value instanceof Array) {
74
- return [2 /*return*/, value.length > this.max ? new Misfit_1.default : undefined];
75
- }
76
- return [2 /*return*/];
77
- });
78
- }); })];
79
- });
80
- });
81
- };
82
- return Max;
83
- }(Constraint_1.default));
84
- exports.default = Max;
@@ -1,7 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class QuickConstraint extends Constraint {
4
- validateFn: (obj: any, field: string | string[]) => Promise<Misfit | undefined>;
5
- constructor(name: string, validate: (obj: any, field: string | string[]) => Promise<Misfit | undefined>);
6
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
7
- }
@@ -1,70 +0,0 @@
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
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
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
- Object.defineProperty(exports, "__esModule", { value: true });
52
- var Constraint_1 = require("../Constraint");
53
- var QuickConstraint = /** @class */ (function (_super) {
54
- __extends(QuickConstraint, _super);
55
- function QuickConstraint(name, validate) {
56
- var _this = _super.call(this) || this;
57
- _this.name = name;
58
- _this.validateFn = validate;
59
- return _this;
60
- }
61
- QuickConstraint.prototype.validate = function (obj, field) {
62
- return __awaiter(this, void 0, void 0, function () {
63
- return __generator(this, function (_a) {
64
- return [2 /*return*/, this.validateFn(obj, field)];
65
- });
66
- });
67
- };
68
- return QuickConstraint;
69
- }(Constraint_1.default));
70
- exports.default = QuickConstraint;
@@ -1,5 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class Required extends Constraint {
4
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
5
- }
@@ -1,12 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class TypeOf extends Constraint {
4
- valueType: string | (new (...params: any[]) => any);
5
- constructor(valueType: string);
6
- constructor(valueType: (new (...params: any[]) => any));
7
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
8
- }
9
- export interface TypeOfValues {
10
- expectedType: string;
11
- actualType: string;
12
- }
@@ -1,7 +0,0 @@
1
- import Constraint from '../Constraint';
2
- import Misfit from '../Misfit';
3
- export default class Unique extends Constraint {
4
- isUnique: (value: any, obj: any) => Promise<boolean>;
5
- constructor(isUnique: (obj: any, field: string | string[]) => Promise<boolean>);
6
- validate(obj: any, field: string | string[]): Promise<Misfit | undefined>;
7
- }
@@ -1 +0,0 @@
1
- export default function fieldsEqual(a1?: string[], a2?: string[]): boolean;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function fieldsEqual(a1, a2) {
4
- if (!a1 || !a2) {
5
- return false;
6
- }
7
- if (a1.length != a2.length) {
8
- return false;
9
- }
10
- for (var i = 0; i < a1.length; i++) {
11
- if (a2.indexOf(a1[i]) == -1) {
12
- return false;
13
- }
14
- }
15
- return true;
16
- }
17
- exports.default = fieldsEqual;