@vennersys/type-check
Version:
Library for checking js object property types
97 lines (94 loc) • 3.07 kB
TypeScript
// Generated by dts-bundle-generator v9.5.1
export type DataValidations = {
validEmail: () => boolean;
validUkPostcode: () => boolean;
validPhoneNumber: () => boolean;
};
export declare function checkDataType<T>(value: T): DataValidations;
export declare function checkJsType<T>(value: T): string;
export declare function checkTsType<T>(value: T): string;
export type TypeAnalysis = {
jsType: string;
tsType: string;
dataType: DataValidations;
};
export declare function checkType<T>(value: T): TypeAnalysis;
export declare enum SupportedType {
STRING = "string",
NUMBER = "number",
BOOLEAN = "boolean",
DATE = "date",
OBJECT = "object",
ARRAY = "array",
FUNCTION = "function",
SYMBOL = "symbol",
UNDEFINED = "undefined",
NULL = "null",
REGEXP = "regexp",
MAP = "map",
SET = "set"
}
export declare const supportedTypes: SupportedType[];
export type ValidationResult = {
valid: boolean;
validationMessages: string[];
};
export type FieldValidator<T> = {
validate: (value: T, model: Record<string, unknown>) => ValidationResult;
getRules: () => {
type: string;
params?: Record<string, unknown>;
message: string;
}[];
};
export type ValidationSchema = Record<string, FieldValidator<unknown>>;
export declare enum RuleType {
REQUIRED = "required",
MIN_LENGTH = "minLength",
MAX_LENGTH = "maxLength",
MIN_VALUE = "minValue",
MAX_VALUE = "maxValue",
CUSTOM = "custom",
IS_EMAIL = "isEmail",
IS_POSTCODE = "isPostcode",
IS_PHONE_NUMBER = "isPhoneNumber"
}
declare class ValidatorBase<T> {
rules: {
type: string;
params?: Record<string, unknown>;
message: string;
validationFunction?: (value: T, model: Record<string, unknown>) => boolean;
}[];
evaluateRule(rule: {
type: string;
params?: Record<string, unknown>;
message: string;
validationFunction?: (value: T, model: Record<string, unknown>) => boolean;
}, value: T, model: Record<string, unknown>): {
valid: boolean;
validationMessages: string[];
};
validate(value: T, model: Record<string, unknown>): ValidationResult;
getRules(): {
type: string;
params?: Record<string, unknown>;
message: string;
validationFunction?: (value: T, model: Record<string, unknown>) => boolean;
}[];
}
export declare class Validator<T> extends ValidatorBase<T> {
required(message: string): this;
minLength(length: number, message: string): this;
maxLength(length: number, message: string): this;
minValue(size: number, message: string): this;
maxValue(size: number, message: string): this;
isEmail(message: string): this;
isPostcode(message: string): this;
isPhoneNumber(message: string): this;
custom(validationFunction: (value: T, model: Record<string, unknown>) => boolean, message: string): this;
}
export declare function typeField<T>(typeName?: string): Validator<T>;
export declare function validateSchema(schema: ValidationSchema, model: Record<string, unknown>): Record<string, ValidationResult>;
export declare function evaluateSchema(schema: Record<string, Validator<unknown>>, model: Record<string, unknown>): Record<string, unknown>;
export {};