@typeofweb/schema
Version:
`@typeofweb/schema` is a lightweight and extensible library for data validation with full TypeScript support!
40 lines • 1.74 kB
TypeScript
import type { ValidationError } from './errors';
export declare type TypeOf<S extends SomeSchema<any>> = Pretty<S['__type']>;
export declare type Primitives = string | number | boolean;
export interface Schema<Type extends unknown> {
readonly __type: Type;
toString(): string;
}
declare type Left<L> = {
readonly _t: 'left';
readonly value: L;
};
declare type Right<R> = {
readonly _t: 'right';
readonly value: R;
};
export declare type Next<Output> = {
readonly _t: 'nextNotValid';
readonly value: Output;
} | {
readonly _t: 'nextValid';
readonly value: Output;
};
export declare type Either<R, L = ValidationError> = Left<L> | Right<R>;
export declare type SomeSchema<T> = Schema<T>;
export declare type TupleOf<T, Length extends number, Acc extends readonly unknown[] = readonly []> = Acc['length'] extends Length ? Acc : TupleOf<T, Length, readonly [T, ...Acc]>;
export declare type If<T, Condition, Y, N = never> = T extends Condition ? Y : N;
export declare type Pretty<X> = X extends Date ? X : X extends object | readonly unknown[] ? {
readonly [K in keyof X]: X[K];
} : X;
export declare type KeysOfType<T extends object, SelectedType> = {
readonly [key in keyof T]: SelectedType extends T[key] ? key : never;
}[keyof T];
declare type PlainObject = {
readonly [name: string]: any;
};
export declare type Optional<T extends object> = Partial<Pick<T, KeysOfType<T, undefined>>>;
export declare type Required<T extends object> = Omit<T, KeysOfType<T, undefined>>;
export declare type UndefinedToOptional<T> = T extends PlainObject ? {} extends T ? {} : T extends Date | readonly unknown[] ? T : Pretty<Required<T> & Optional<T>> : T;
export {};
//# sourceMappingURL=types.d.ts.map