UNPKG

@piiano/forms

Version:
35 lines (34 loc) 2.53 kB
export type Infer<V extends Validator<unknown>> = V extends Validator<infer U> ? U : never; export type Validator<Type> = { parse(value: unknown): value is Type; optional(): Validator<Type | undefined>; enum<Value extends Type>(...values: Value[]): Validator<Value>; }; export type OneOf<Types extends unknown[]> = Types[number]; type Validators<Types extends unknown[]> = { [K in keyof Types]: Validator<Types[K]>; }; export declare const literal: <Value extends string | boolean | number>(literalValue: Value) => Validator<Value>; export declare const string: () => Validator<string>; export declare const number: () => Validator<number>; export declare const boolean: () => Validator<boolean>; export declare const unknown: () => Validator<unknown>; export declare const array: <Type>(validator: Validator<Type>) => Validator<Type[]>; export type Simplify<T> = { [KeyType in keyof T]: T[KeyType]; } & {}; type ObjectSchema = { [key: string]: Validator<unknown>; }; export declare const object: <Schema extends ObjectSchema>(schema: Schema) => Validator<{ [K in keyof Schema as K extends unknown ? Infer<Schema[K]> extends {} ? K : never : never]: Infer<Schema[K]>; } & { [K_1 in keyof Schema as K_1 extends unknown ? Infer<Schema[K_1]> extends {} ? never : K_1 : never]?: Infer<Schema[K_1]> | undefined; } extends infer T ? { [KeyType in keyof T]: ({ [K in keyof Schema as K extends unknown ? Infer<Schema[K]> extends {} ? K : never : never]: Infer<Schema[K]>; } & { [K_1 in keyof Schema as K_1 extends unknown ? Infer<Schema[K_1]> extends {} ? never : K_1 : never]?: Infer<Schema[K_1]> | undefined; })[KeyType]; } : never>; type RecordType<Key extends string | number, Value, R = Record<Key, Value>> = Simplify<{ [K in keyof R as K extends unknown ? (R[K] extends {} ? K : never) : never]: R[K]; } & { [K in keyof R as K extends unknown ? (R[K] extends {} ? never : K) : never]?: R[K]; }>; export declare const record: <Key extends string, Value>(keyValidator: Validator<Key>, valueValidator: Validator<Value>) => Validator<RecordType<Key, Value>>; export declare const or: <Type1, Type2>(validator1: Validator<Type1>, validator2: Validator<Type2>) => Validator<Type1 | Type2>; export declare const and: <Type1, Type2>(validator1: Validator<Type1>, validator2: Validator<Type2>) => Validator<Type1 & Type2>; export declare const oneOf: <Types extends unknown[]>(...validators: Validators<Types>) => Validator<OneOf<Types>>; export declare const optional: () => Validator<undefined>; export {};