@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.
@@ -0,0 +1,508 @@
1
+ import { ref as L, reactive as Q, watch as R, isRef as E, unref as i } from "vue-demi";
2
+ function y(e) {
3
+ return e.trim().replace(" ", ".");
4
+ }
5
+ async function z(e, t, n = "") {
6
+ for (const s in e) {
7
+ const f = `${n} ${s}`.trim();
8
+ m(e[s]) && !(e[s] instanceof Date) ? await z(e[s], t, f) : await t(s, e[s], f);
9
+ }
10
+ }
11
+ function a(e) {
12
+ return e == null;
13
+ }
14
+ function q(e) {
15
+ return typeof e == "boolean";
16
+ }
17
+ function A(e) {
18
+ return typeof e == "function" && e instanceof Function;
19
+ }
20
+ function k(e) {
21
+ return typeof e == "number" || e instanceof Number;
22
+ }
23
+ function h(e) {
24
+ return e instanceof Date;
25
+ }
26
+ function O(e) {
27
+ return Array.isArray(e);
28
+ }
29
+ function X(e) {
30
+ return typeof e == "string" || e instanceof String;
31
+ }
32
+ function Y(e) {
33
+ return a(e) ? !0 : "length" in e ? e.length <= 0 : "size" in e ? e.size <= 0 : m(e) ? Object.keys(e).length <= 0 : !0;
34
+ }
35
+ function $(e) {
36
+ return e instanceof Map;
37
+ }
38
+ function m(e) {
39
+ return typeof e == "object" && !O(e) && e !== null && !A(e);
40
+ }
41
+ function V(e) {
42
+ return e instanceof Set;
43
+ }
44
+ function Z(e) {
45
+ return e instanceof Symbol;
46
+ }
47
+ function p(e, t, n) {
48
+ if (a(e) || !m(e))
49
+ return;
50
+ t = y(t.trim());
51
+ const s = t.split(".");
52
+ let f = e;
53
+ const u = s.length;
54
+ if (u > 1)
55
+ for (let o = 0; o < u; o++) {
56
+ const l = s[o];
57
+ if (l.length === 0)
58
+ continue;
59
+ const d = Reflect.get(f, l);
60
+ if (m(d))
61
+ f = d;
62
+ else if (o === u - 1) {
63
+ Reflect.set(f, l, n);
64
+ break;
65
+ } else
66
+ d === void 0 && (Reflect.set(f, l, {}), f = Reflect.get(f, l));
67
+ }
68
+ else
69
+ Reflect.set(f, s[0], n);
70
+ }
71
+ function I(e, t) {
72
+ if (a(e) || !m(e))
73
+ return;
74
+ t = y(t.trim());
75
+ const n = t.split(".");
76
+ let s = e;
77
+ for (const f of n)
78
+ f.length !== 0 && (s = Reflect.get(s, f));
79
+ return s;
80
+ }
81
+ function P() {
82
+ return { id: null, value: null, invalid: !1, errors: {} };
83
+ }
84
+ function Le(e, t, n = {}) {
85
+ const { proactive: s = !1, autoclear: f = !1 } = n, u = L(!1), o = L(!1), l = L({}), d = Q({
86
+ didValidate: !1
87
+ });
88
+ f ? R(e, () => {
89
+ d.didValidate && _();
90
+ }, { deep: !0 }) : s && (R(e, () => w(), { deep: !0 }), E(t) && R(t, () => w(), { deep: !0 })), _();
91
+ function W() {
92
+ u.value = !1, o.value = !1, z(e, (b, g, D) => {
93
+ p(l.value, y(D), P());
94
+ });
95
+ }
96
+ function _() {
97
+ d.didValidate = !1, W();
98
+ }
99
+ async function w(...b) {
100
+ return _(), o.value = !0, new Promise(async (g, D) => {
101
+ await z(e, async (U, S, c) => {
102
+ c = y(c), p(l.value, c, P());
103
+ const F = i(t), T = I(F, c);
104
+ if (!T)
105
+ return Promise.resolve();
106
+ for (const [j, B] of Object.entries(T)) {
107
+ if (b.length > 0 && !b.includes(j))
108
+ continue;
109
+ const { label: K, validate: G, __skip: H } = B;
110
+ if (H)
111
+ continue;
112
+ const J = await G(S);
113
+ p(l.value, `${c}.id`, U), p(l.value, `${c}.value`, S), J || (u.value = !0, p(l.value, `${c}.invalid`, !0), p(l.value, `${c}.errors.${j}`, K(S)));
114
+ }
115
+ return Promise.resolve();
116
+ }), o.value = !1, d.didValidate = !0, u.value ? D(l.value) : g(l.value);
117
+ });
118
+ }
119
+ function C(b, g) {
120
+ p(l.value, `${b}.errors.${g.key}`, g.message), p(l.value, `${b}.invalid`, !0);
121
+ }
122
+ return {
123
+ reset: _,
124
+ validate: w,
125
+ addError: C,
126
+ errors: l,
127
+ anyError: u,
128
+ pending: o
129
+ };
130
+ }
131
+ const r = function(...e) {
132
+ return {
133
+ name: "skipped-rule",
134
+ __skip: !0,
135
+ validate: () => !0,
136
+ label: () => "test"
137
+ };
138
+ };
139
+ function N(e, t, n) {
140
+ return new Promise(async (s) => {
141
+ const f = [];
142
+ for (const u of t) {
143
+ if (!u.validate)
144
+ throw new Error("Rule is missing a validator function");
145
+ const o = await u.validate(e);
146
+ f.push(o);
147
+ }
148
+ switch (n) {
149
+ case "every": {
150
+ s(f.every((u) => u));
151
+ break;
152
+ }
153
+ case "some": {
154
+ s(f.some((u) => u));
155
+ break;
156
+ }
157
+ case "none": {
158
+ s(!f.every((u) => u));
159
+ break;
160
+ }
161
+ }
162
+ });
163
+ }
164
+ const v = "Value does not satisfy the validation rule.";
165
+ function Re(e, t, n) {
166
+ return {
167
+ skip: r,
168
+ name: n ?? "custom-object-rule",
169
+ __skip: !1,
170
+ validate: (s) => e(s),
171
+ label: (s) => a(t) ? v : typeof t == "string" ? t : t(s)
172
+ };
173
+ }
174
+ function ze(e, t, n) {
175
+ const s = (f) => ({
176
+ // the value from validate is the actual value we are testing against
177
+ // injected during validation
178
+ __skip: !1,
179
+ name: n ?? "custom-param-rule",
180
+ validate: (u) => e(u, f),
181
+ label: (u) => a(t) ? v : typeof t == "string" ? t : t(u, f)
182
+ });
183
+ return s.skip = r, s;
184
+ }
185
+ function Ee(e, t) {
186
+ const { validate: n, __skip: s, name: f } = t;
187
+ if (a(n))
188
+ throw new Error("[withLabel] Missing validator function");
189
+ return {
190
+ name: f,
191
+ __skip: s,
192
+ validate: n,
193
+ label: (u) => typeof e == "string" ? e : e(u)
194
+ };
195
+ }
196
+ function Ae(e, t) {
197
+ return q(e) ? e ? t : r() : E(e) ? e.value ? t : r() : A(e) ? e() ? t : r() : e.then((n) => n ? t : r());
198
+ }
199
+ function Oe(e, t) {
200
+ return q(e) ? e ? r() : t : E(e) ? e.value ? r() : t : A(e) ? e() ? r() : t : e.then((n) => n ? r() : t);
201
+ }
202
+ function x(...e) {
203
+ return {
204
+ name: "and-validator",
205
+ __skip: !1,
206
+ validate: (t) => N(t, e, "every"),
207
+ label() {
208
+ return "Value must pass all the required checks";
209
+ }
210
+ };
211
+ }
212
+ x.skip = r;
213
+ function ee(...e) {
214
+ return {
215
+ name: "or-validator",
216
+ __skip: !1,
217
+ validate: (t) => N(t, e, "some"),
218
+ label() {
219
+ return "Value must pass some of the checks";
220
+ }
221
+ };
222
+ }
223
+ ee.skip = r;
224
+ function te(...e) {
225
+ return {
226
+ name: "not-validator",
227
+ __skip: !1,
228
+ validate: (t) => N(t, e, "none"),
229
+ label() {
230
+ return "Value must pass none of the checks";
231
+ }
232
+ };
233
+ }
234
+ te.skip = r();
235
+ function Ne(e, t) {
236
+ return e.validate(t);
237
+ }
238
+ const Te = {
239
+ __skip: !1,
240
+ name: "required",
241
+ skip: r,
242
+ validate: (e) => a(e) || e === "null" || e === "undefined" ? !1 : typeof e == "string" ? e.length > 0 : typeof e == "number" ? !0 : !Y(e),
243
+ /* c8 ignore next 3 */
244
+ label: () => "Value is required"
245
+ };
246
+ function ne(e) {
247
+ return {
248
+ name: "minLength",
249
+ __skip: !1,
250
+ validate: (t) => a(t) ? !1 : (e = i(e), V(t) || $(t) ? t.size >= e : m(t) ? Object.keys(t).length >= e : t != null && t.length ? t.length >= e : !1),
251
+ /* c8 ignore next 3 */
252
+ label: () => `Value must be greater or equal to ${i(e)}`
253
+ };
254
+ }
255
+ ne.skip = r;
256
+ function re(e) {
257
+ return {
258
+ name: "maxLength",
259
+ __skip: !1,
260
+ validate: (t) => a(t) ? !1 : (e = i(e), V(t) || $(t) ? t.size <= e : m(t) ? Object.keys(t).length <= e : t.length <= e),
261
+ /* c8 ignore next 3 */
262
+ label: () => `Value must be lesser or equal to ${i(e)}`
263
+ };
264
+ }
265
+ re.skip = r;
266
+ const se = /^([\w!#$%&'*+/=?^`{|}~-]+(?:\.[\w!#$%&'*+/=?^`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)$/i, ie = /[^a-z0-9]/i, M = /\S/g, je = {
267
+ __skip: !1,
268
+ name: "email",
269
+ skip: r,
270
+ validate: (e) => a(e) ? !1 : se.test(e),
271
+ /* c8 ignore next 3 */
272
+ label: () => "Value must be in a valid email format"
273
+ };
274
+ function ae(e, t = !1) {
275
+ return {
276
+ name: "sameAs",
277
+ __skip: !1,
278
+ validate: (n) => (e = i(e), t ? i(n) == i(e) : i(n) === i(e)),
279
+ /* c8 ignore next 3 */
280
+ label: () => "Values must be matching"
281
+ };
282
+ }
283
+ ae.skip = r;
284
+ function fe(e) {
285
+ const t = typeof e == "string" ? new RegExp(e) : e;
286
+ return {
287
+ name: "match",
288
+ __skip: !1,
289
+ validate: (n) => a(n) ? !1 : t.test(n),
290
+ /* c8 ignore next 3 */
291
+ label: () => "Value must match the provided rule."
292
+ };
293
+ }
294
+ fe.skip = r;
295
+ const Pe = {
296
+ __skip: !1,
297
+ name: "url",
298
+ skip: r,
299
+ validate: (e) => {
300
+ if (a(e))
301
+ return !1;
302
+ try {
303
+ return new URL(e) instanceof URL;
304
+ } catch {
305
+ return !1;
306
+ }
307
+ },
308
+ label: () => "Value must be a valid URL"
309
+ }, ue = {
310
+ __skip: !1,
311
+ name: "str",
312
+ skip: r,
313
+ validate: (e) => a(e) ? !1 : X(e),
314
+ label: ({ value: e }) => `Value <${typeof e}> must be a string`
315
+ }, le = {
316
+ name: "num",
317
+ __skip: !1,
318
+ skip: r,
319
+ validate: (e) => a(e) ? !1 : k(e),
320
+ label: ({ value: e }) => `Value <${typeof e}> must be a number`
321
+ }, oe = {
322
+ name: "arr",
323
+ __skip: !1,
324
+ skip: r,
325
+ validate: (e) => a(e) ? !1 : O(e),
326
+ label: ({ value: e }) => `Value <${typeof e}> must be an Array`
327
+ }, ce = {
328
+ name: "obj",
329
+ __skip: !1,
330
+ skip: r,
331
+ validate: (e) => a(e) || O(e) || V(e) || $(e) ? !1 : m(e),
332
+ label: ({ value: e }) => `Value <${typeof e}> must be an Object`
333
+ }, pe = {
334
+ name: "set",
335
+ __skip: !1,
336
+ skip: r,
337
+ validate: (e) => a(e) ? !1 : V(e),
338
+ label: ({ value: e }) => `Value <${typeof e}> must be a Set`
339
+ }, me = {
340
+ name: "map",
341
+ __skip: !1,
342
+ skip: r,
343
+ validate: (e) => a(e) ? !1 : $(e),
344
+ label: ({ value: e }) => `Value <${typeof e}> must be a Map`
345
+ }, de = {
346
+ name: "date",
347
+ __skip: !1,
348
+ skip: r,
349
+ validate: (e) => {
350
+ if (a(e) || e == "Invalid Date")
351
+ return !1;
352
+ if (h(e) && e.getTime())
353
+ return !0;
354
+ const t = new Date(e);
355
+ return h(t);
356
+ },
357
+ label: ({ value: e }) => `Value <${typeof e}> must be a valid Date object`
358
+ }, be = {
359
+ name: "symbol",
360
+ __skip: !1,
361
+ skip: r,
362
+ validate: (e) => a(e) ? !1 : Z(e),
363
+ label: ({ value: e }) => `Value <${typeof e}> must be a Symbol`
364
+ }, ge = {
365
+ str: ue,
366
+ num: le,
367
+ arr: oe,
368
+ obj: ce,
369
+ set: pe,
370
+ map: me,
371
+ date: de,
372
+ symbol: be
373
+ };
374
+ function ke(e, t) {
375
+ return {
376
+ __skip: !1,
377
+ name: "between",
378
+ validate: (n) => a(n) ? !1 : (e = i(e), t = i(t), ge.date.validate(n) && (e = e instanceof Date ? e.getTime() : new Date(e).getTime(), t = t instanceof Date ? t.getTime() : new Date(t).getTime(), n = n instanceof Date ? n.getTime() : new Date(n).getTime()), n >= e && n <= t),
379
+ label: () => `Value must be between ${i(e)} and ${i(t)}`
380
+ };
381
+ }
382
+ ke.skip = r;
383
+ function _e(e) {
384
+ return {
385
+ name: "minValue",
386
+ __skip: !1,
387
+ validate: (t) => a(t) ? !1 : (e = i(e), k(t) && k(e) ? t >= e : h(t) ? new Date(t) >= new Date(e) : !1),
388
+ /* c8 ignore next 3 */
389
+ label: () => `Minimum allowed value is ${i(e)}`
390
+ };
391
+ }
392
+ _e.skip = r;
393
+ function ye(e) {
394
+ return {
395
+ name: "maxValue",
396
+ __skip: !1,
397
+ validate: (t) => a(t) ? !1 : (e = i(e), k(t) && k(e) ? t <= e : h(t) ? new Date(t) <= new Date(e) : !1),
398
+ /* c8 ignore next 3 */
399
+ label: () => `Value must be shorter or equal to ${i(e)}`
400
+ };
401
+ }
402
+ ye.skip = r;
403
+ function he(e) {
404
+ return {
405
+ __skip: !1,
406
+ name: "maxLenNoSpace",
407
+ validate: (t) => {
408
+ if (a(t) || typeof t != "string")
409
+ return !1;
410
+ e = i(e);
411
+ const n = t.match(M);
412
+ return n ? n.length <= e : !1;
413
+ },
414
+ label: (t) => typeof t != "string" ? "Value must be a string and " : `Value must have maximal lenght of ${i(e)} excluding spaces`
415
+ };
416
+ }
417
+ he.skip = r;
418
+ function $e(e) {
419
+ return {
420
+ name: "minLenNoSpace",
421
+ __skip: !1,
422
+ validate: (t) => {
423
+ if (a(t) || typeof t != "string")
424
+ return !1;
425
+ e = i(e);
426
+ const n = t.match(M);
427
+ return n ? n.length >= e : !1;
428
+ },
429
+ /* c8 ignore next 3 */
430
+ label: () => `Value must have minimal lenght of ${i(e)} excluding spaces`
431
+ };
432
+ }
433
+ $e.skip = r;
434
+ const qe = {
435
+ __skip: !1,
436
+ name: "hasSpecialChars",
437
+ skip: r,
438
+ validate: (e) => a(e) ? !1 : !ie.test(e),
439
+ label: (e) => typeof e != "string" ? "Value must be a string and contain no special characters" : "Value must not contain any special characters"
440
+ };
441
+ function Ve(e, t = !1) {
442
+ return {
443
+ __skip: !1,
444
+ name: "contains",
445
+ validate: (n) => a(n) || typeof n != "string" ? !1 : (typeof e == "string" && (e = t ? [e] : e.trim().split(/\s+/)), e.every((s) => n.toLowerCase().includes(s.toLowerCase()))),
446
+ label: (n) => {
447
+ e = i(e);
448
+ const s = typeof e == "string" ? e : e.join(", ");
449
+ return typeof n != "string" ? `Value must be a string and contain <${s}>` : `Value must contain <${s}>`;
450
+ }
451
+ };
452
+ }
453
+ Ve.skip = r;
454
+ function we(e, t) {
455
+ return {
456
+ __skip: !1,
457
+ name: "startsWith",
458
+ validate: (n) => (e = i(e), !n || typeof n != "string" ? !1 : n.startsWith(e, t)),
459
+ label: (n) => typeof n != "string" ? `Value must be a string and start with '${i(e)}'` : `Value must start with '${i(e)}'`
460
+ };
461
+ }
462
+ we.skip = r;
463
+ function De(e, t) {
464
+ return {
465
+ __skip: !1,
466
+ name: "endsWith",
467
+ validate: (n) => (e = i(e), !n || typeof n != "string" ? !1 : n.endsWith(e, t)),
468
+ label: (n) => typeof n != "string" ? `Value must be a string and end with '${i(e)}'` : `Value must end with '${i(e)}'`
469
+ };
470
+ }
471
+ De.skip = r;
472
+ export {
473
+ x as $and,
474
+ te as $not,
475
+ ee as $or,
476
+ Ne as $test,
477
+ Ae as $validateIf,
478
+ Oe as $validateIfNot,
479
+ Ee as $withLabel,
480
+ ke as between,
481
+ Ve as contains,
482
+ Re as createRule,
483
+ ze as createRuleArg,
484
+ je as email,
485
+ De as endsWith,
486
+ qe as hasSpecialChars,
487
+ oe as isArr,
488
+ de as isDate,
489
+ me as isMap,
490
+ le as isNum,
491
+ ce as isObj,
492
+ pe as isSet,
493
+ ue as isStr,
494
+ be as isSymbol,
495
+ fe as match,
496
+ he as maxLenNoSpace,
497
+ re as maxLength,
498
+ ye as maxValue,
499
+ $e as minLenNoSpace,
500
+ ne as minLength,
501
+ _e as minValue,
502
+ Te as required,
503
+ ae as sameAs,
504
+ we as startsWith,
505
+ ge as type,
506
+ Pe as url,
507
+ Le as useValidation
508
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@dolanske/v-valid",
3
+ "type": "module",
4
+ "version": "2.1.0",
5
+ "private": false,
6
+ "description": "Model based form validation made for vue 3. Inspired by vuelidate with personal take on the API and implementation",
7
+ "author": "dolanske",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/dolanske/v-valid.git"
12
+ },
13
+ "keywords": [
14
+ "form-validation",
15
+ "vue3",
16
+ "form",
17
+ "validation"
18
+ ],
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/v-valid.js"
22
+ }
23
+ },
24
+ "main": "./dist/v-valid.js",
25
+ "module": "./dist/v-valid.js",
26
+ "types": "./dist/v-valid.d.ts",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "scripts": {
31
+ "test": "vitest watch",
32
+ "coverage": "vitest run --coverage",
33
+ "build": "vite build",
34
+ "lint": "eslint .",
35
+ "lint:fix": "eslint . --fix"
36
+ },
37
+ "peerDependencies": {
38
+ "vue": "latest"
39
+ },
40
+ "dependencies": {
41
+ "vue-demi": "^0.14.8"
42
+ },
43
+ "devDependencies": {
44
+ "@antfu/eslint-config": "^2.21.1",
45
+ "@vitest/coverage-c8": "^0.22.1",
46
+ "eslint": "^9.5.0",
47
+ "eslint-plugin-format": "^0.1.2",
48
+ "typescript": "^5.4.5",
49
+ "vite-plugin-dts": "^3.9.1",
50
+ "vitest": "^1.6.0",
51
+ "vue": "latest"
52
+ }
53
+ }