perfect-validator
Version:
A TypeScript-based validation library that supports both static and dynamic validation with serializable models.
87 lines (86 loc) • 3.28 kB
TypeScript
export declare namespace PerfectValidator {
type ValidationType = 'S' | 'N' | 'B' | 'L' | 'M' | 'EMAIL' | 'URL' | 'DATE' | 'PHONE' | 'REGEX';
const ValidationTypes: Record<string, ValidationType>;
const DataTypeDescriptions: Record<ValidationType, string>;
interface ValidationDependency {
field: string;
condition: (value: any) => boolean;
validate: (value: any, dependentValue: any, fullData?: any) => boolean;
message: string;
optional?: boolean;
isRequired?: boolean;
}
interface IValidationTypeParams {
type: string;
description: string;
params: {
name: string;
type: string;
description: string;
required?: boolean;
}[];
}
interface ValidationRule {
type?: ValidationType;
optional?: boolean;
default?: any;
message?: string;
minLength?: number;
maxLength?: number;
pattern?: string;
min?: number;
max?: number;
integer?: boolean;
decimal?: boolean;
decimals?: number;
items?: ValidationRule | string;
fields?: {
[key: string]: ValidationRule | string;
};
values?: any[];
dependsOn?: ValidationDependency | ValidationDependency[];
validate?: (value: any) => boolean;
}
interface ValidationModel {
[key: string]: ValidationRule | string;
}
interface ValidationError {
field: string;
message: string;
}
interface ModelValidationResponse {
isValid: boolean;
errors: string[] | null;
}
type ValidationResponse<T> = {
isValid: true;
data: T;
} | {
isValid: false;
errors: ValidationError[];
} | null;
interface ModelVersion {
version: number;
model: string;
createdAt: Date;
}
interface IModelStorage {
storeModelVersion(modelName: string, model: string, version: number, collection?: string): Promise<void>;
getLatestModelVersion(modelName: string, collection?: string): Promise<ModelVersion | null>;
getModelVersion(modelName: string, version: number, collection?: string): Promise<ModelVersion | null>;
listModelVersions(modelName: string, collection?: string): Promise<number[]>;
insertModel(modelName: string, model: ValidationModel, collection?: string): Promise<void>;
updateModel(modelName: string, model: ValidationModel, collection?: string): Promise<void>;
getModel(modelName: string, collection?: string): Promise<string | null>;
}
interface IModelCache {
setModel(modelName: string, serializedModel: string): Promise<void>;
getModel(modelName: string): Promise<string | null>;
invalidateModel(modelName: string): Promise<void>;
setModelWithVersion(modelName: string, serializedModel: string, version?: number): Promise<void>;
}
}
export declare function isValidationError<T>(response: PerfectValidator.ValidationResponse<T> | null): response is {
isValid: false;
errors: PerfectValidator.ValidationError[];
};