UNPKG

2.97 kBTypeScriptView Raw
1import { type BasePredicate } from './predicates/base-predicate.js';
2import { type Modifiers } from './modifiers.js';
3import { type Predicates } from './predicates.js';
4/**
5@hidden
6*/
7export type Main = <T>(value: T, label: string | Function, predicate: BasePredicate<T>, idLabel?: boolean) => void;
8/**
9Retrieve the type from the given predicate.
10
11@example
12```
13import ow, {Infer} from 'ow';
14
15const userPredicate = ow.object.exactShape({
16 name: ow.string
17});
18
19type User = Infer<typeof userPredicate>;
20```
21*/
22export type Infer<P> = P extends BasePredicate<infer T> ? T : never;
23export type Ow = {
24 /**
25 Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
26
27 @param value - Value to test.
28 @param predicate - Predicate to test against.
29 */
30 <T>(value: unknown, predicate: BasePredicate<T>): asserts value is T;
31 /**
32 Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails.
33
34 @param value - Value to test.
35 @param label - Label which should be used in error messages.
36 @param predicate - Predicate to test against.
37 */
38 <T>(value: unknown, label: string, predicate: BasePredicate<T>): asserts value is T;
39 /**
40 Returns `true` if the value matches the predicate, otherwise returns `false`.
41
42 @param value - Value to test.
43 @param predicate - Predicate to test against.
44 */
45 isValid: <T>(value: unknown, predicate: BasePredicate<T>) => value is T;
46 /**
47 Create a reusable validator.
48
49 @param predicate - Predicate used in the validator function.
50 */
51 create: (<T>(predicate: BasePredicate<T>) => ReusableValidator<T>) & (<T>(label: string, predicate: BasePredicate<T>) => ReusableValidator<T>);
52} & Modifiers & Predicates;
53/**
54A reusable validator.
55*/
56export type ReusableValidator<T> = {
57 /**
58 Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
59
60 @param value - Value to test.
61 @param label - Override the label which should be used in error messages.
62 */
63 (value: unknown | T, label?: string): void;
64};
65/**
66Turn a `ReusableValidator` into one with a type assertion.
67
68@example
69```
70const checkUsername = ow.create(ow.string.minLength(3));
71const checkUsername_: AssertingValidator<typeof checkUsername> = checkUsername;
72```
73
74@example
75```
76const predicate = ow.string.minLength(3);
77const checkUsername: AssertingValidator<typeof predicate> = ow.create(predicate);
78```
79*/
80export type AssertingValidator<T> = T extends ReusableValidator<infer R> ? (value: unknown, label?: string) => asserts value is R : T extends BasePredicate<infer R> ? (value: unknown, label?: string) => asserts value is R : never;
81declare const _ow: Ow;
82export default _ow;
83export * from './predicates.js';
84export { ArgumentError } from './argument-error.js';
85export { Predicate } from './predicates/predicate.js';
86export type { BasePredicate } from './predicates/base-predicate.js';