obj-schema-validator
Version:
Lightweight and simple validator for objects and DTOs, designed for TypeScript and Node.js projects.
53 lines (49 loc) • 1.15 kB
text/typescript
export type SchemaType =
| "string"
| "number"
| "boolean"
| "float"
| "email"
| "uuid"
| "Date";
export type SchemaOptions = {
toTitleCase?: boolean,
toUpperCase?: boolean,
toLowerCase?: boolean,
isEmail?: boolean,
includes?: Array<any>,
checkPattern?: RegExp,
isLength?: number,
minLength?: number,
maxLength?: number,
minimunValue?: number,
maximunValue?: number,
}
export type OptionsForType<T extends SchemaType> =
T extends "string"
? {
toTitleCase?: boolean;
toUpperCase?: boolean;
toLowerCase?: boolean;
isEmail?: boolean;
includes?: any[];
checkPattern?: RegExp;
isLength?: number;
minLength?: number;
maxLength?: number;
}
: T extends "number" | "float" ? {
minimunValue?: number;
maximunValue?: number;
} : {};
export interface Schema {
[key: string]: {
required: boolean,
type: SchemaType,
value?: any,
options?: SchemaOptions,
};
}
export type SchemaValues<T extends Schema> = {
[k in keyof T]: T[k]["value"]
}