knight-validation 2.0.1 → 2.0.4
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 +12 -0
- package/lib/lib/Constraint.d.ts +5 -0
- package/lib/lib/DotNotification.d.ts +6 -0
- package/lib/lib/DotNotification.js +35 -0
- package/lib/lib/Misfit.d.ts +11 -0
- package/lib/lib/MisfitsError.d.ts +5 -0
- package/lib/lib/QuickConstraint.d.ts +8 -0
- package/lib/lib/Validator.d.ts +23 -0
- package/lib/lib/Validator.js +28 -10
- package/lib/lib/constraints/Absent.d.ts +8 -0
- package/lib/lib/constraints/Bounds.d.ts +18 -0
- package/lib/lib/constraints/Enum.d.ts +13 -0
- package/lib/lib/constraints/Exists.d.ts +10 -0
- package/lib/lib/constraints/Length.d.ts +16 -0
- package/lib/lib/constraints/Required.d.ts +5 -0
- package/lib/lib/constraints/TypeOf.d.ts +11 -0
- package/lib/lib/constraints/Unique.d.ts +10 -0
- package/package.json +1 -1
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DotNotification = void 0;
|
|
4
|
+
class DotNotification {
|
|
5
|
+
constructor(dotNotification) {
|
|
6
|
+
this.properties = dotNotification.split('.');
|
|
7
|
+
}
|
|
8
|
+
exists(obj) {
|
|
9
|
+
let exists = false;
|
|
10
|
+
for (let property of this.properties) {
|
|
11
|
+
if (typeof obj == 'object' && obj !== null && obj[property] !== undefined) {
|
|
12
|
+
exists = true;
|
|
13
|
+
obj = obj[property];
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
exists = false;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return exists;
|
|
21
|
+
}
|
|
22
|
+
get(obj) {
|
|
23
|
+
for (let property of this.properties) {
|
|
24
|
+
if (typeof obj == 'object' && obj !== null) {
|
|
25
|
+
obj = obj[property];
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
obj = undefined;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return obj;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.DotNotification = DotNotification;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class Misfit<ValuesType = any> {
|
|
2
|
+
constraint: string;
|
|
3
|
+
properties: string[];
|
|
4
|
+
values?: ValuesType;
|
|
5
|
+
message?: string;
|
|
6
|
+
constructor(constraint?: string, values?: ValuesType);
|
|
7
|
+
setProperties(property: string | string[]): void;
|
|
8
|
+
addPrefix(prefix: string): void;
|
|
9
|
+
isSinglePropery(): boolean;
|
|
10
|
+
isMultipleProperties(): boolean;
|
|
11
|
+
}
|
|
@@ -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,23 @@
|
|
|
1
|
+
import { Constraint } from './Constraint';
|
|
2
|
+
import { Misfit } from './Misfit';
|
|
3
|
+
export interface ValidatorOptions {
|
|
4
|
+
checkOnlyWhatIsThere?: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface ValidatorEntry {
|
|
7
|
+
properties: string[];
|
|
8
|
+
constraint?: Constraint;
|
|
9
|
+
validator?: Validator<any>;
|
|
10
|
+
condition?: (object: any) => Promise<boolean>;
|
|
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[]>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
package/lib/lib/Validator.js
CHANGED
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Validator = void 0;
|
|
13
13
|
const Constraint_1 = require("./Constraint");
|
|
14
|
+
const DotNotification_1 = require("./DotNotification");
|
|
14
15
|
const QuickConstraint_1 = require("./QuickConstraint");
|
|
15
16
|
class Validator {
|
|
16
17
|
constructor(options) {
|
|
@@ -73,7 +74,8 @@ class Validator {
|
|
|
73
74
|
}
|
|
74
75
|
let atLeastOnePropertyExists = false;
|
|
75
76
|
for (let property of entry.properties) {
|
|
76
|
-
|
|
77
|
+
let dotNotification = new DotNotification_1.DotNotification(property);
|
|
78
|
+
if (dotNotification.exists(object)) {
|
|
77
79
|
atLeastOnePropertyExists = true;
|
|
78
80
|
break;
|
|
79
81
|
}
|
|
@@ -85,7 +87,8 @@ class Validator {
|
|
|
85
87
|
let misfit;
|
|
86
88
|
if (entry.properties.length == 1) {
|
|
87
89
|
let property = entry.properties[0];
|
|
88
|
-
let
|
|
90
|
+
let dotNotification = new DotNotification_1.DotNotification(property);
|
|
91
|
+
let value = dotNotification.get(object);
|
|
89
92
|
misfit = yield entry.constraint.validate(value);
|
|
90
93
|
}
|
|
91
94
|
else {
|
|
@@ -105,17 +108,32 @@ class Validator {
|
|
|
105
108
|
throw new Error('Using another validator only works for one property');
|
|
106
109
|
}
|
|
107
110
|
let property = entry.properties[0];
|
|
108
|
-
let
|
|
109
|
-
|
|
111
|
+
let dotNotification = new DotNotification_1.DotNotification(property);
|
|
112
|
+
let value = dotNotification.get(object);
|
|
113
|
+
if (value === undefined) {
|
|
110
114
|
continue;
|
|
111
115
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
+
if (value instanceof Array) {
|
|
117
|
+
for (let i = 0; i < value.length; i++) {
|
|
118
|
+
let subMisfits = yield entry.validator.validate(value[i], options);
|
|
119
|
+
if (subMisfits.length > 0) {
|
|
120
|
+
for (let misfit of subMisfits) {
|
|
121
|
+
misfit.addPrefix(`${property}[${i}].`);
|
|
122
|
+
}
|
|
123
|
+
misfittingProperties.push(...entry.properties);
|
|
124
|
+
misfits.push(...subMisfits);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
let subMisfits = yield entry.validator.validate(value, options);
|
|
130
|
+
if (subMisfits.length > 0) {
|
|
131
|
+
for (let misfit of subMisfits) {
|
|
132
|
+
misfit.addPrefix(property + '.');
|
|
133
|
+
}
|
|
134
|
+
misfittingProperties.push(...entry.properties);
|
|
135
|
+
misfits.push(...subMisfits);
|
|
116
136
|
}
|
|
117
|
-
misfittingProperties.push(...entry.properties);
|
|
118
|
-
misfits.push(...subMisfits);
|
|
119
137
|
}
|
|
120
138
|
}
|
|
121
139
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface AbsentMisfitValues {
|
|
4
|
+
actual: any;
|
|
5
|
+
}
|
|
6
|
+
export declare class Absent extends Constraint<any, AbsentMisfitValues> {
|
|
7
|
+
validate(value: any): Promise<Misfit<AbsentMisfitValues> | null>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface BoundsMisfitValues {
|
|
4
|
+
actual: any;
|
|
5
|
+
greaterThan?: number;
|
|
6
|
+
greaterThanEqual?: number;
|
|
7
|
+
lesserThan?: number;
|
|
8
|
+
lesserThanEqual?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class Bounds extends Constraint<any, BoundsMisfitValues> {
|
|
11
|
+
greaterThan?: number;
|
|
12
|
+
greaterThanEqual?: number;
|
|
13
|
+
lesserThan?: number;
|
|
14
|
+
lesserThanEqual?: number;
|
|
15
|
+
constructor(constraints: Partial<Bounds>);
|
|
16
|
+
validate(value: any): Promise<Misfit<BoundsMisfitValues> | null>;
|
|
17
|
+
private _createMisfit;
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface EnumMisfitValues {
|
|
4
|
+
actual: any;
|
|
5
|
+
values: any[];
|
|
6
|
+
}
|
|
7
|
+
export declare class Enum extends Constraint<any, EnumMisfitValues> {
|
|
8
|
+
values: any[];
|
|
9
|
+
constructor(values: any[]);
|
|
10
|
+
constructor(...values: any[]);
|
|
11
|
+
constructor(values: object);
|
|
12
|
+
validate(value: any): Promise<Misfit<EnumMisfitValues> | null>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface ExistsMisfitValues {
|
|
4
|
+
notExistingValue: any;
|
|
5
|
+
}
|
|
6
|
+
export declare class Exists extends Constraint<any, ExistsMisfitValues> {
|
|
7
|
+
doesExist: (value: any) => Promise<boolean>;
|
|
8
|
+
constructor(doesExist: (value: any) => Promise<boolean>);
|
|
9
|
+
validate(value: any): Promise<Misfit<ExistsMisfitValues> | null>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface LengthMisfitValues {
|
|
4
|
+
actual: any;
|
|
5
|
+
min?: number;
|
|
6
|
+
max?: number;
|
|
7
|
+
exact?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class Length extends Constraint<any, LengthMisfitValues> {
|
|
10
|
+
min?: number;
|
|
11
|
+
max?: number;
|
|
12
|
+
exact?: number;
|
|
13
|
+
constructor(constraints: Partial<Length>);
|
|
14
|
+
validate(value: any): Promise<Misfit<LengthMisfitValues> | null>;
|
|
15
|
+
private _createMisfit;
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface TypeOfMisfitValues {
|
|
4
|
+
types: (string | null)[];
|
|
5
|
+
actual: string | null;
|
|
6
|
+
}
|
|
7
|
+
export declare class TypeOf extends Constraint<any, TypeOfMisfitValues> {
|
|
8
|
+
valueTypes: (string | null | (new (...params: any[]) => any))[];
|
|
9
|
+
constructor(...valueTypes: (string | null | (new (...params: any[]) => any))[]);
|
|
10
|
+
validate(value: any): Promise<Misfit<TypeOfMisfitValues> | null>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface UniqueMisfitValues {
|
|
4
|
+
notUniqueValue: any;
|
|
5
|
+
}
|
|
6
|
+
export declare class Unique extends Constraint<any, UniqueMisfitValues> {
|
|
7
|
+
isUnique: (value: any) => Promise<boolean>;
|
|
8
|
+
constructor(isUnique: (value: any) => Promise<boolean>);
|
|
9
|
+
validate(value: any): Promise<Misfit<UniqueMisfitValues> | null>;
|
|
10
|
+
}
|