1 | import type { Main } from '../index.js';
|
2 | import { testSymbol, type BasePredicate } from './base-predicate.js';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export type ValidatorMessageBuilder<T> = (value: T, label?: string) => string;
|
12 |
|
13 |
|
14 |
|
15 | export type Validator<T> = {
|
16 | message(value: T, label?: string, result?: any): string;
|
17 | validator(value: T): unknown;
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | negatedMessage?(value: T, label: string): string;
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 | export type PredicateOptions = {
|
29 | optional?: boolean;
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 | export type Context<T = unknown> = {
|
35 | validators: Array<Validator<T>>;
|
36 | } & PredicateOptions;
|
37 |
|
38 |
|
39 |
|
40 | export declare const validatorSymbol: unique symbol;
|
41 | export type CustomValidator<T> = (value: T) => {
|
42 | |
43 |
|
44 |
|
45 | validator: boolean;
|
46 | |
47 |
|
48 |
|
49 | message: string | ((label: string) => string);
|
50 | };
|
51 | /**
|
52 | @hidden
|
53 | */
|
54 | export declare class Predicate<T = unknown> implements BasePredicate<T> {
|
55 | private readonly type;
|
56 | private readonly options;
|
57 | private readonly context;
|
58 | constructor(type: string, options?: PredicateOptions);
|
59 | /**
|
60 | @hidden
|
61 | */
|
62 | [testSymbol](value: T, main: Main, label: string | (() => string), idLabel: boolean): asserts value is T;
|
63 | /**
|
64 | @hidden
|
65 | */
|
66 | get [validatorSymbol](): Array<Validator<T>>;
|
67 | /**
|
68 | Invert the following validators.
|
69 | */
|
70 | get not(): this;
|
71 | /**
|
72 | Test if the value matches a custom validation function. The validation function should return an object containing a `validator` and `message`. If the `validator` is `false`, the validation fails and the `message` will be used as error message. If the `message` is a function, the function is invoked with the `label` as argument to let you further customize the error message.
|
73 |
|
74 | @param customValidator - Custom validation function.
|
75 | */
|
76 | validate(customValidator: CustomValidator<T>): this;
|
77 | /**
|
78 | Test if the value matches a custom validation function. The validation function should return `true` if the value passes the function. If the function either returns `false` or a string, the function fails and the string will be used as error message.
|
79 |
|
80 | @param validator - Validation function.
|
81 | */
|
82 | is(validator: (value: T) => boolean | string): this;
|
83 | /**
|
84 | Provide a new error message to be thrown when the validation fails.
|
85 |
|
86 | @param newMessage - Either a string containing the new message or a function returning the new message.
|
87 |
|
88 | @example
|
89 | ```
|
90 | ow('🌈', 'unicorn', ow.string.equals('🦄').message('Expected unicorn, got rainbow'));
|
91 | //=> ArgumentError: Expected unicorn, got rainbow
|
92 | ```
|
93 |
|
94 | @example
|
95 | ```
|
96 | ow('🌈', ow.string.minLength(5).message((value, label) => `Expected ${label}, to have a minimum length of 5, got \`${value}\``));
|
97 |
|
98 | ```
|
99 | */
|
100 | message(newMessage: string | ValidatorMessageBuilder<T>): this;
|
101 | /**
|
102 | Register a new validator.
|
103 |
|
104 | @param validator - Validator to register.
|
105 | */
|
106 | addValidator(validator: Validator<T>): this;
|
107 | }
|
108 |
|
\ | No newline at end of file |