-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
types.ts
93 lines (76 loc) · 2.34 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Vue, { VNode } from 'vue';
import { ValidationProvider } from './components/Provider';
export type ProviderInstance = InstanceType<typeof ValidationProvider>;
export interface ValidationResult {
valid: boolean;
errors: string[];
failedRules: Record<string, string>;
}
export type VueValidationContext = Vue & {
$_veeObserver?: VeeObserver;
};
export interface ValidationMessageGenerator {
(field: string, params?: Record<string, any>): string;
}
export type ValidationMessageTemplate = string | ValidationMessageGenerator;
export interface ValidationRuleResult {
data?: Record<string, any>;
valid: boolean;
required?: boolean;
}
export type ValidationRuleFunction = (
value: any,
params: any[] | Record<string, any>
) => boolean | ValidationRuleResult | Promise<boolean | ValidationRuleResult>;
export interface RuleParamConfig {
name: string;
isTarget?: boolean;
default?: any;
cast?(value: any): any;
}
export type RuleParamSchema = string | RuleParamConfig;
export interface ValidationRuleSchema {
validate?: ValidationRuleFunction;
params?: RuleParamSchema[];
message?: ValidationMessageTemplate;
lazy?: boolean;
computesRequired?: boolean;
castValue?(value: any): any;
}
export type ValidationRule = ValidationRuleFunction | ValidationRuleSchema;
export type StringOrNumber = string | number;
// Extracts explicit keys of an interface without index signature
// https://stackoverflow.com/questions/51465182/typescript-remove-index-signature-using-mapped-types
export type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
} extends { [_ in keyof T]: infer U }
? U
: never;
export interface ValidationFlags {
untouched: boolean;
touched: boolean;
dirty: boolean;
pristine: boolean;
valid: boolean;
invalid: boolean;
validated: boolean;
pending: boolean;
required: boolean;
changed: boolean;
[x: string]: boolean | undefined;
}
export interface VeeObserver {
refs: Record<string, ProviderInstance>;
subscribe(provider: any, type?: 'provider' | 'observer'): void;
unsubscribe(provider: any, type?: 'provider' | 'observer'): void;
}
export interface InactiveRefCache {
errors: string[];
flags: ValidationFlags;
failedRules: Record<string, string>;
}
export type VNodeWithVeeContext = VNode & {
context: Vue & {
$_veeObserver?: VeeObserver;
};
};