knight-validation 3.0.0 → 3.1.0
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/lib/constraints/Absent.d.ts +1 -0
- package/lib/lib/constraints/Absent.js +4 -0
- package/lib/lib/constraints/Enum.d.ts +2 -2
- package/lib/lib/constraints/Enum.js +21 -10
- package/lib/lib/constraints/Exists.d.ts +1 -1
- package/lib/lib/constraints/Exists.js +2 -1
- package/lib/lib/constraints/Required.d.ts +1 -0
- package/lib/lib/constraints/Required.js +4 -0
- package/lib/lib/constraints/TypeOf.d.ts +2 -2
- package/lib/lib/constraints/TypeOf.js +14 -3
- package/lib/lib/constraints/Unique.d.ts +1 -1
- package/lib/lib/constraints/Unique.js +2 -1
- package/package.json +1 -1
|
@@ -4,5 +4,6 @@ export interface AbsentMisfitValues extends ConstraintMisfitValues {
|
|
|
4
4
|
actual: any;
|
|
5
5
|
}
|
|
6
6
|
export declare class Absent extends Constraint<any, AbsentMisfitValues> {
|
|
7
|
+
constructor(constraints?: Partial<Absent>);
|
|
7
8
|
validate(value: any): Promise<Misfit<AbsentMisfitValues> | null>;
|
|
8
9
|
}
|
|
@@ -13,6 +13,10 @@ exports.Absent = void 0;
|
|
|
13
13
|
const knight_misfit_1 = require("knight-misfit");
|
|
14
14
|
const Constraint_1 = require("../Constraint");
|
|
15
15
|
class Absent extends Constraint_1.Constraint {
|
|
16
|
+
constructor(constraints) {
|
|
17
|
+
super();
|
|
18
|
+
Object.assign(this, constraints);
|
|
19
|
+
}
|
|
16
20
|
validate(value) {
|
|
17
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
22
|
return value !== undefined ? new knight_misfit_1.Misfit(this.name, undefined, { actual: value }) : null;
|
|
@@ -6,8 +6,8 @@ export interface EnumMisfitValues extends ConstraintMisfitValues {
|
|
|
6
6
|
}
|
|
7
7
|
export declare class Enum extends Constraint<any, EnumMisfitValues> {
|
|
8
8
|
values: any[];
|
|
9
|
-
constructor(values: any[]);
|
|
9
|
+
constructor(values: any[], constraints?: Partial<Constraint>);
|
|
10
10
|
constructor(...values: any[]);
|
|
11
|
-
constructor(values: object);
|
|
11
|
+
constructor(values: object, constraints?: Partial<Constraint>);
|
|
12
12
|
validate(value: any): Promise<Misfit<EnumMisfitValues> | null>;
|
|
13
13
|
}
|
|
@@ -13,22 +13,33 @@ exports.Enum = void 0;
|
|
|
13
13
|
const knight_misfit_1 = require("knight-misfit");
|
|
14
14
|
const Constraint_1 = require("../Constraint");
|
|
15
15
|
class Enum extends Constraint_1.Constraint {
|
|
16
|
-
constructor(...
|
|
16
|
+
constructor(...args) {
|
|
17
17
|
super();
|
|
18
18
|
this.values = [];
|
|
19
|
-
for (let
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
for (let i = 0; i < args.length; i++) {
|
|
20
|
+
let arg = args[i];
|
|
21
|
+
if (i == 0) {
|
|
22
|
+
if (Array.isArray(arg)) {
|
|
23
|
+
this.values.push(...arg);
|
|
24
|
+
}
|
|
25
|
+
else if (typeof arg == 'object') {
|
|
26
|
+
for (let key in arg) {
|
|
27
|
+
if (isNaN(parseInt(key, 10))) {
|
|
28
|
+
this.values.push(arg[key]);
|
|
29
|
+
}
|
|
27
30
|
}
|
|
28
31
|
}
|
|
32
|
+
else {
|
|
33
|
+
this.values.push(arg);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (i == args.length - 1) {
|
|
37
|
+
if (typeof arg == 'object') {
|
|
38
|
+
Object.assign(this, arg);
|
|
39
|
+
}
|
|
29
40
|
}
|
|
30
41
|
else {
|
|
31
|
-
this.values.push(
|
|
42
|
+
this.values.push(arg);
|
|
32
43
|
}
|
|
33
44
|
}
|
|
34
45
|
}
|
|
@@ -5,6 +5,6 @@ export interface ExistsMisfitValues extends ConstraintMisfitValues {
|
|
|
5
5
|
}
|
|
6
6
|
export declare class Exists<T> extends Constraint<T, ExistsMisfitValues> {
|
|
7
7
|
doesExist: (value: T) => Promise<boolean>;
|
|
8
|
-
constructor(doesExist: (value: T) => Promise<boolean>);
|
|
8
|
+
constructor(doesExist: (value: T) => Promise<boolean>, constraints?: Partial<Constraint>);
|
|
9
9
|
validate(value: T): Promise<Misfit<ExistsMisfitValues> | null>;
|
|
10
10
|
}
|
|
@@ -13,8 +13,9 @@ exports.Exists = void 0;
|
|
|
13
13
|
const knight_misfit_1 = require("knight-misfit");
|
|
14
14
|
const Constraint_1 = require("../Constraint");
|
|
15
15
|
class Exists extends Constraint_1.Constraint {
|
|
16
|
-
constructor(doesExist) {
|
|
16
|
+
constructor(doesExist, constraints) {
|
|
17
17
|
super();
|
|
18
|
+
Object.assign(this, constraints);
|
|
18
19
|
this.doesExist = doesExist;
|
|
19
20
|
}
|
|
20
21
|
validate(value) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Misfit } from 'knight-misfit';
|
|
2
2
|
import { Constraint, ConstraintMisfitValues } from '../Constraint';
|
|
3
3
|
export declare class Required extends Constraint {
|
|
4
|
+
constructor(constraints?: Partial<Required>);
|
|
4
5
|
validate(value: any): Promise<Misfit<ConstraintMisfitValues> | null>;
|
|
5
6
|
}
|
|
@@ -13,6 +13,10 @@ exports.Required = void 0;
|
|
|
13
13
|
const knight_misfit_1 = require("knight-misfit");
|
|
14
14
|
const Constraint_1 = require("../Constraint");
|
|
15
15
|
class Required extends Constraint_1.Constraint {
|
|
16
|
+
constructor(constraints) {
|
|
17
|
+
super();
|
|
18
|
+
Object.assign(this, constraints);
|
|
19
|
+
}
|
|
16
20
|
validate(value) {
|
|
17
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
22
|
return value === undefined ? new knight_misfit_1.Misfit(this.name) : null;
|
|
@@ -5,7 +5,7 @@ export interface TypeOfMisfitValues extends ConstraintMisfitValues {
|
|
|
5
5
|
actual: string | null;
|
|
6
6
|
}
|
|
7
7
|
export declare class TypeOf extends Constraint<any, TypeOfMisfitValues> {
|
|
8
|
-
|
|
9
|
-
constructor(...valueTypes: (string | null | (new (...params: any[]) => any))[]);
|
|
8
|
+
types: (string | null | (new (...params: any[]) => any))[];
|
|
9
|
+
constructor(...valueTypes: (string | null | (new (...params: any[]) => any) | Partial<Constraint>)[]);
|
|
10
10
|
validate(value: any): Promise<Misfit<TypeOfMisfitValues> | null>;
|
|
11
11
|
}
|
|
@@ -15,14 +15,25 @@ const Constraint_1 = require("../Constraint");
|
|
|
15
15
|
class TypeOf extends Constraint_1.Constraint {
|
|
16
16
|
constructor(...valueTypes) {
|
|
17
17
|
super();
|
|
18
|
-
this.
|
|
18
|
+
this.types = [];
|
|
19
|
+
for (let i = 0; i < valueTypes.length; i++) {
|
|
20
|
+
let valueType = valueTypes[i];
|
|
21
|
+
if (i == valueTypes.length - 1 &&
|
|
22
|
+
typeof valueType == 'object' &&
|
|
23
|
+
valueType !== null) {
|
|
24
|
+
Object.assign(this, valueType);
|
|
25
|
+
}
|
|
26
|
+
else if (!(typeof valueType == 'object') || valueType === null) {
|
|
27
|
+
this.types.push(valueType);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
19
30
|
}
|
|
20
31
|
validate(value) {
|
|
21
32
|
return __awaiter(this, void 0, void 0, function* () {
|
|
22
33
|
if (value === undefined) {
|
|
23
34
|
return null;
|
|
24
35
|
}
|
|
25
|
-
for (let valueType of this.
|
|
36
|
+
for (let valueType of this.types) {
|
|
26
37
|
if (typeof valueType == 'string') {
|
|
27
38
|
if (typeof value === valueType) {
|
|
28
39
|
return null;
|
|
@@ -51,7 +62,7 @@ class TypeOf extends Constraint_1.Constraint {
|
|
|
51
62
|
actual: actualType,
|
|
52
63
|
types: []
|
|
53
64
|
});
|
|
54
|
-
for (let valueType of this.
|
|
65
|
+
for (let valueType of this.types) {
|
|
55
66
|
if (typeof valueType == 'string') {
|
|
56
67
|
misfit.values.types.push(valueType);
|
|
57
68
|
}
|
|
@@ -5,6 +5,6 @@ export interface UniqueMisfitValues extends ConstraintMisfitValues {
|
|
|
5
5
|
}
|
|
6
6
|
export declare class Unique<T> extends Constraint<T, UniqueMisfitValues> {
|
|
7
7
|
isUnique: (value: T) => Promise<boolean>;
|
|
8
|
-
constructor(isUnique: (value: T) => Promise<boolean>);
|
|
8
|
+
constructor(isUnique: (value: T) => Promise<boolean>, constraints?: Partial<Constraint>);
|
|
9
9
|
validate(value: T): Promise<Misfit<UniqueMisfitValues> | null>;
|
|
10
10
|
}
|
|
@@ -13,8 +13,9 @@ exports.Unique = void 0;
|
|
|
13
13
|
const knight_misfit_1 = require("knight-misfit");
|
|
14
14
|
const Constraint_1 = require("../Constraint");
|
|
15
15
|
class Unique extends Constraint_1.Constraint {
|
|
16
|
-
constructor(isUnique) {
|
|
16
|
+
constructor(isUnique, constraints) {
|
|
17
17
|
super();
|
|
18
|
+
Object.assign(this, constraints);
|
|
18
19
|
this.isUnique = isUnique;
|
|
19
20
|
}
|
|
20
21
|
validate(value) {
|