knight-validation 2.0.1 → 2.0.2

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 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,5 @@
1
+ import { Misfit } from './Misfit';
2
+ export declare abstract class Constraint<T = any, MisfitValuesType = any> {
3
+ name: string;
4
+ abstract validate(value: T): Promise<Misfit<MisfitValuesType> | null>;
5
+ }
@@ -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,5 @@
1
+ import { Misfit } from './Misfit';
2
+ export declare class MisfitsError extends Error {
3
+ misfits: Misfit[];
4
+ constructor(misfits: Misfit | Misfit[]);
5
+ }
@@ -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 {};
@@ -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,5 @@
1
+ import { Constraint } from '../Constraint';
2
+ import { Misfit } from '../Misfit';
3
+ export declare class Required extends Constraint<any, void> {
4
+ validate(value: any): Promise<Misfit<void> | null>;
5
+ }
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knight-validation",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A validation lib",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",