@dolanske/v-valid 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +521 -0
- package/dist/v-valid.d.ts +458 -0
- package/dist/v-valid.js +508 -0
- package/package.json +53 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { ComputedRef } from 'vue-demi';
|
|
2
|
+
import { Ref } from 'vue-demi';
|
|
3
|
+
import { UnwrapRef } from 'vue-demi';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @rule Value must pass every provided check
|
|
7
|
+
* @param rules Single or multiple validation rules
|
|
8
|
+
*/
|
|
9
|
+
export declare function $and(...rules: ValidationRule[]): ValidationRule;
|
|
10
|
+
|
|
11
|
+
export declare namespace $and {
|
|
12
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @rule Value must fail the check or list of all provided checks
|
|
17
|
+
* @param rules Single or multiple validation rules
|
|
18
|
+
*/
|
|
19
|
+
export declare function $not(...rules: ValidationRule[]): ValidationRule;
|
|
20
|
+
|
|
21
|
+
export declare namespace $not {
|
|
22
|
+
var skip: ValidationRule;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @rule Value must pass at least one of the provided checks
|
|
27
|
+
* @param rules Single or multiple validation rules
|
|
28
|
+
*/
|
|
29
|
+
export declare function $or(...rules: ValidationRule[]): ValidationRule;
|
|
30
|
+
|
|
31
|
+
export declare namespace $or {
|
|
32
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Shorthand for calling `rule(...args).validate(value)`
|
|
37
|
+
*
|
|
38
|
+
* @Rule Execute validation against a provided value outside of the validation
|
|
39
|
+
* scope. This helper can be also used within `validateIf` and `validateIfNot`
|
|
40
|
+
*
|
|
41
|
+
* @param rule Validation rule
|
|
42
|
+
* @param value Value to validate
|
|
43
|
+
* @returns Wether the provided value passes the provided check
|
|
44
|
+
*/
|
|
45
|
+
export declare function $test(rule: ValidationRule, value: any): Promise<boolean> | boolean;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @Rule Perform validation if provided condition is met
|
|
49
|
+
* @param condition Boolean or function returning boolean
|
|
50
|
+
* @param rule Validation rule
|
|
51
|
+
*/
|
|
52
|
+
export declare function $validateIf(condition: boolean | (() => boolean) | Ref<boolean> | Promise<boolean>, rule: ValidationRule): ValidationRule | Promise<ValidationRule>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @Rule Perform validation if provided condition is not met
|
|
56
|
+
* @param condition Boolean or function returning boolean
|
|
57
|
+
* @param rule Validation rule
|
|
58
|
+
*/
|
|
59
|
+
export declare function $validateIfNot(condition: boolean | (() => boolean) | Ref<boolean> | Promise<boolean>, rule: ValidationRule): ValidationRule | Promise<ValidationRule>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* Helper method which adds custom message to any validation.
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* minLength: withLabel("Too short!", minLength(10))
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param message Custom message to display in the returned error
|
|
70
|
+
* @param validator Validation rule
|
|
71
|
+
*/
|
|
72
|
+
export declare function $withLabel(message: string | Label, validator: ValidationRule): ValidationRule;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @Rule Checks if value is between the provided range
|
|
76
|
+
* @param min Minimum value
|
|
77
|
+
* @param max Maximum value
|
|
78
|
+
*/
|
|
79
|
+
export declare function between(min: number | Date | Ref<number | Date>, max: number | Date | Ref<number | Date>): {
|
|
80
|
+
__skip: boolean;
|
|
81
|
+
name: string;
|
|
82
|
+
validate: (value: any) => boolean;
|
|
83
|
+
label: () => string;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export declare namespace between {
|
|
87
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @Rule Checks wether string input contains certain words or characters
|
|
92
|
+
* @param toInclude Word(s) or character(s) to check for
|
|
93
|
+
* @param exact Should it split sentences into words or check against the entire string
|
|
94
|
+
*/
|
|
95
|
+
export declare function contains(toInclude: string | string[], exact?: boolean): ValidationRule;
|
|
96
|
+
|
|
97
|
+
export declare namespace contains {
|
|
98
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Simple rule definition. This method takes in a method wihout params and returns a simple rule.
|
|
103
|
+
* This rule is 'as is' and does not take in any parameters nor can it be called.
|
|
104
|
+
*/
|
|
105
|
+
export declare function createRule(rule: (value: any) => boolean | Promise<boolean>, label?: DefLabel, name?: string): ValidationRuleObject;
|
|
106
|
+
|
|
107
|
+
export declare function createRuleArg<P = RuleParams>(rule: (value: any, params: P) => boolean | Promise<boolean>, label?: DefParamLabel<P>, name?: string): {
|
|
108
|
+
(params: P): ValidationRule;
|
|
109
|
+
skip: (..._args: any[]) => ValidationRule;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
declare type DeepKeys<T> = DropInitDot<_DeepKeys<FixArr<T>>>;
|
|
113
|
+
|
|
114
|
+
declare type _DeepKeys<T> = T extends object ? ({
|
|
115
|
+
[K in (string | number) & keyof T]: `${(`.${K}` | (`${K}` extends `${number}` ? `[${K}]` : never))}${'' | _DeepKeys<FixArr<T[K]>>}`;
|
|
116
|
+
}[(string | number) & keyof T]) : never;
|
|
117
|
+
|
|
118
|
+
declare type DefLabel = string | ((value: any) => string);
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* This method produces rules which require parameter object. This allows user to
|
|
122
|
+
* create more complex rules
|
|
123
|
+
*/
|
|
124
|
+
declare type DefParamLabel<P = undefined> = string | ((value: any, params: P) => string);
|
|
125
|
+
|
|
126
|
+
declare type DropInitDot<T> = T extends `.${infer U}` ? U : T;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @Rule Input must be a valid email address
|
|
130
|
+
*/
|
|
131
|
+
export declare const email: ValidationRuleObject;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @Rule Checks wether a string ends with the provided value
|
|
135
|
+
* @param str The checked value
|
|
136
|
+
* @param position At which character index from the end should the matching begin
|
|
137
|
+
*/
|
|
138
|
+
export declare function endsWith(str: string | Ref<string>, position?: number): {
|
|
139
|
+
__skip: boolean;
|
|
140
|
+
name: string;
|
|
141
|
+
validate: (value: any) => boolean;
|
|
142
|
+
label: (value: any) => string;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export declare namespace endsWith {
|
|
146
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
declare type FixArr<T> = T extends readonly any[] ? Omit<T, Exclude<keyof any[], number>> : T;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @Rule Checks wether an input contains any special characters
|
|
153
|
+
*/
|
|
154
|
+
export declare const hasSpecialChars: ValidationRuleObject;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @Rule Checkes wether value is an Array
|
|
158
|
+
* @param value Input value
|
|
159
|
+
*/
|
|
160
|
+
export declare const isArr: Type['arr'];
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @Rule Checkes wether value is a valid Date object
|
|
164
|
+
* @param value Input value
|
|
165
|
+
*/
|
|
166
|
+
export declare const isDate: Type['date'];
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @Rule Checkes wether value is a Map
|
|
170
|
+
* @param value Input value
|
|
171
|
+
*/
|
|
172
|
+
export declare const isMap: Type['map'];
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @Rule Checkes wether value is a number
|
|
176
|
+
* @param value Input value
|
|
177
|
+
*/
|
|
178
|
+
export declare const isNum: Type['num'];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @Rule Checkes wether value is an Object
|
|
182
|
+
* @param value Input value
|
|
183
|
+
*/
|
|
184
|
+
export declare const isObj: Type['obj'];
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @Rule Checkes wether value is a Set
|
|
188
|
+
* @param value Input value
|
|
189
|
+
*/
|
|
190
|
+
export declare const isSet: Type['set'];
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @Rule Checkes wether value is a string
|
|
194
|
+
* @param value Input value
|
|
195
|
+
*/
|
|
196
|
+
export declare const isStr: Type['str'];
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @Rule Checkes wether value is a Symbol
|
|
200
|
+
* @param value Input value
|
|
201
|
+
*/
|
|
202
|
+
export declare const isSymbol: Type['symbol'];
|
|
203
|
+
|
|
204
|
+
declare type Label = (value: any, args?: Record<string, unknown>) => string;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @Rule Input must pass the provided regex test
|
|
208
|
+
* @param regex Regex validation rule
|
|
209
|
+
*/
|
|
210
|
+
export declare function match(regex: RegExp | string): ValidationRule;
|
|
211
|
+
|
|
212
|
+
export declare namespace match {
|
|
213
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @Rule Input must be equal or lesser than the provided amount
|
|
218
|
+
* @param max Maximum allowed length the input must satisfy
|
|
219
|
+
*/
|
|
220
|
+
export declare function maxLength(max: number | Ref<number>): ValidationRule;
|
|
221
|
+
|
|
222
|
+
export declare namespace maxLength {
|
|
223
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @Rule Input must be a string and excluding spaces must be equal or lesser to the provided max value
|
|
228
|
+
* @param max Maximum allowed length
|
|
229
|
+
*/
|
|
230
|
+
export declare function maxLenNoSpace(max: number | Ref<number>): ValidationRule;
|
|
231
|
+
|
|
232
|
+
export declare namespace maxLenNoSpace {
|
|
233
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @Rule Input must be a number or a date which satisfies the maximum provided value.
|
|
238
|
+
* @param max maximum allowed value
|
|
239
|
+
*/
|
|
240
|
+
export declare function maxValue(max: number | Date | Ref<number | Date>): ValidationRule;
|
|
241
|
+
|
|
242
|
+
export declare namespace maxValue {
|
|
243
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @Rule Input must be equal or greater than the provided amount
|
|
248
|
+
* @param min Minimum allowed length the input must satisfy
|
|
249
|
+
*/
|
|
250
|
+
export declare function minLength(min: number | Ref<number>): ValidationRule;
|
|
251
|
+
|
|
252
|
+
export declare namespace minLength {
|
|
253
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @Rule Input must be a string and excluding spaces must be equal or greater to the provided min value
|
|
258
|
+
* @param min Minimum allowed length
|
|
259
|
+
*/
|
|
260
|
+
export declare function minLenNoSpace(min: number | Ref<number>): ValidationRule;
|
|
261
|
+
|
|
262
|
+
export declare namespace minLenNoSpace {
|
|
263
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @Rule Input must be a number or a date which satisfies the minimum provided value.
|
|
268
|
+
* @param min Minimum allowed value
|
|
269
|
+
*/
|
|
270
|
+
export declare function minValue(min: number | Date | Ref<number | Date>): ValidationRule;
|
|
271
|
+
|
|
272
|
+
export declare namespace minValue {
|
|
273
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
declare type ReplacePrimitives<T, ReplaceWith> = T extends Record<string, unknown> ? {
|
|
277
|
+
[K in keyof T]: ReplacePrimitives<T[K], ReplaceWith>;
|
|
278
|
+
} : ReplaceWith;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* @Rule Input must not be empty, null or undefined.
|
|
282
|
+
* If input is number with value 0, it will return true as value was provided
|
|
283
|
+
*/
|
|
284
|
+
export declare const required: ValidationRuleObject;
|
|
285
|
+
|
|
286
|
+
declare type RuleParams = Record<string, any>;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* @Rule Input must match the provided `compared` value, either by value or by type & value
|
|
290
|
+
* @param compared The value we compare to the input
|
|
291
|
+
* @param lenient Wether to compare values as == or === (default ===)
|
|
292
|
+
*/
|
|
293
|
+
export declare function sameAs(compared: any | Ref<any>, lenient?: boolean): ValidationRule;
|
|
294
|
+
|
|
295
|
+
export declare namespace sameAs {
|
|
296
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @Rule Checks wether a string starts with the provided value
|
|
301
|
+
* @param str The checked value
|
|
302
|
+
* @param position At which character index should the matching begin
|
|
303
|
+
*/
|
|
304
|
+
export declare function startsWith(str: string | Ref<string>, position?: number): {
|
|
305
|
+
__skip: boolean;
|
|
306
|
+
name: string;
|
|
307
|
+
validate: (value: any) => boolean;
|
|
308
|
+
label: (value: any) => string;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
export declare namespace startsWith {
|
|
312
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
declare interface Type {
|
|
316
|
+
str: ValidationRuleObject;
|
|
317
|
+
num: ValidationRuleObject;
|
|
318
|
+
arr: ValidationRuleObject;
|
|
319
|
+
obj: ValidationRuleObject;
|
|
320
|
+
set: ValidationRuleObject;
|
|
321
|
+
map: ValidationRuleObject;
|
|
322
|
+
date: ValidationRuleObject;
|
|
323
|
+
symbol: ValidationRuleObject;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export declare const type: Type;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* @Rule Input must be a valid URL
|
|
330
|
+
*/
|
|
331
|
+
export declare const url: ValidationRuleObject;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
*
|
|
335
|
+
* @param form Reactive object which will be validated
|
|
336
|
+
* @param rules Object which includes rules appended to the keys which match the structure of the `form`
|
|
337
|
+
*
|
|
338
|
+
* Optionally, you can pass in an options object
|
|
339
|
+
* - `proactive`: Boolean - perform validation on every form change
|
|
340
|
+
* - `autoclear`: Boolean - reset changes on first input after validation
|
|
341
|
+
*/
|
|
342
|
+
export declare function useValidation<F extends Record<string, any>, R extends Partial<Record<keyof F, any>> | ComputedRef<Partial<Record<keyof F, any>>>>(form: F, rules: R, options?: ValidationOptions): {
|
|
343
|
+
reset: () => void;
|
|
344
|
+
validate: (...rulesToOnlyValidate: string[]) => Promise<ReplacePrimitives<F, ValidationError>>;
|
|
345
|
+
addError: (path: DeepKeys<F>, error: {
|
|
346
|
+
key: string;
|
|
347
|
+
message: string;
|
|
348
|
+
}) => void;
|
|
349
|
+
errors: Ref<UnwrapRef<ReplacePrimitives<F, ValidationError>>>;
|
|
350
|
+
anyError: Ref<boolean>;
|
|
351
|
+
pending: Ref<boolean>;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
export declare interface ValidationError {
|
|
355
|
+
id: string | null;
|
|
356
|
+
value: any;
|
|
357
|
+
invalid: boolean;
|
|
358
|
+
errors: Record<string, string>;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
declare interface ValidationOptions {
|
|
362
|
+
proactive?: boolean;
|
|
363
|
+
autoclear?: boolean;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
declare interface ValidationRule {
|
|
367
|
+
__skip: boolean;
|
|
368
|
+
validate: (arg: any) => Promise<boolean> | boolean;
|
|
369
|
+
label: Label;
|
|
370
|
+
name: string;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
declare interface ValidationRuleObject extends ValidationRule {
|
|
374
|
+
skip: () => void;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export { }
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
declare namespace $and {
|
|
381
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
declare namespace $or {
|
|
386
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
declare namespace $not {
|
|
391
|
+
var skip: ValidationRule;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
declare namespace minLength {
|
|
396
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
declare namespace sameAs {
|
|
401
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
declare namespace maxLength {
|
|
406
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
declare namespace match {
|
|
411
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
declare namespace between {
|
|
416
|
+
var skip: (..._args: any[]) => import("../types").ValidationRule;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
declare namespace minValue {
|
|
421
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
declare namespace maxLenNoSpace {
|
|
426
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
declare namespace maxValue {
|
|
431
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
declare namespace startsWith {
|
|
436
|
+
var skip: (..._args: any[]) => import("../../types").ValidationRule;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
declare namespace endsWith {
|
|
441
|
+
var skip: (..._args: any[]) => import("../../types").ValidationRule;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
declare namespace contains {
|
|
446
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
declare namespace minLenNoSpace {
|
|
451
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
declare namespace password {
|
|
456
|
+
var skip: (..._args: any[]) => ValidationRule;
|
|
457
|
+
}
|
|
458
|
+
|