28 lines
No EOL
1.4 KiB
TypeScript
28 lines
No EOL
1.4 KiB
TypeScript
//#region src/core/validator.d.ts
|
|
/**
|
|
* Type guards for a plugin's `validate` field. A validator is a plain
|
|
* type-guard function, so a plugin can also use a hand-written one.
|
|
*/
|
|
type Validator<T> = (value: unknown) => value is T;
|
|
declare const string: Validator<string>;
|
|
declare const number: Validator<number>;
|
|
declare const boolean: Validator<boolean>;
|
|
declare const bigint: Validator<bigint>;
|
|
/** Matches an instance of the given constructor. */
|
|
declare function instanceOf<T>(ctor: abstract new (...args: never[]) => T): Validator<T>;
|
|
type Guarded<V> = V extends Validator<infer T> ? T : never;
|
|
/** Matches when any of the given validators match. */
|
|
declare function union<V extends Validator<unknown>[]>(...validators: V): Validator<Guarded<V[number]>>;
|
|
/** Matches the given validator or `undefined`. */
|
|
declare function optional<T>(validator: Validator<T>): Validator<T | undefined>;
|
|
/** Matches an array whose items each match `item`. */
|
|
declare function array<T>(item: Validator<T>): Validator<T[]>;
|
|
type Shape = Record<string, Validator<unknown>>;
|
|
type Infer<S extends Shape> = { [K in keyof S]: Guarded<S[K]>; };
|
|
/**
|
|
* Matches an object whose declared keys each pass their validator. Extra keys
|
|
* are not checked.
|
|
*/
|
|
declare function object<S extends Shape>(shape: S): Validator<Infer<S>>;
|
|
//#endregion
|
|
export { instanceOf as a, optional as c, boolean as i, string as l, array as n, number as o, bigint as r, object as s, Validator as t, union as u }; |