UNPKG

3.69 kBTypeScriptView Raw
1import { BasePredicate, testSymbol } from './base-predicate';
2import { Main } from '..';
3/**
4Function executed when the provided validation fails.
5
6@param value - The tested value.
7@param label - Label of the tested value.
8
9@returns {string} - The actual error message.
10*/
11export declare type ValidatorMessageBuilder<T> = (value: T, label?: string) => string;
12/**
13@hidden
14*/
15export interface Validator<T> {
16 message(value: T, label?: string, result?: any): string;
17 validator(value: T): unknown;
18 /**
19 Provide custom message used by `not` operator.
20
21 When absent, the return value of `message()` is used and 'not' is inserted after the first 'to', e.g. `Expected 'smth' to be empty` -> `Expected 'smth' to not be empty`.
22 */
23 negatedMessage?(value: T, label: string): string;
24}
25/**
26@hidden
27*/
28export interface PredicateOptions {
29 optional?: boolean;
30}
31/**
32@hidden
33*/
34export interface Context<T = unknown> extends PredicateOptions {
35 validators: Array<Validator<T>>;
36}
37/**
38@hidden
39*/
40export declare const validatorSymbol: unique symbol;
41export declare type CustomValidator<T> = (value: T) => {
42 /**
43 Should be `true` if the validation is correct.
44 */
45 validator: boolean;
46 /**
47 The error message which should be shown if the `validator` is `false`. Or a error function which returns the error message and accepts the label as first argument.
48 */
49 message: string | ((label: string) => string);
50};
51/**
52@hidden
53*/
54export 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 | Function, 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 //=> ArgumentError: Expected string, to be have a minimum length of 5, got `🌈`
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