@regle/core 0.0.5 → 0.0.7
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/dist/index.cjs +361 -223
- package/dist/index.d.cts +430 -70
- package/dist/index.d.ts +430 -70
- package/dist/index.js +306 -178
- package/package.json +21 -19
package/dist/index.js
CHANGED
|
@@ -18,29 +18,47 @@ function createReactiveParams(params) {
|
|
|
18
18
|
return toRef(() => param);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
function getFunctionParametersLength(func) {
|
|
22
|
+
const funcStr = func.toString();
|
|
23
|
+
const params = funcStr.slice(funcStr.indexOf("(") + 1, funcStr.indexOf(")")).split(",").map((param) => param.trim());
|
|
24
|
+
const defaults = params.filter((param) => param.includes("="));
|
|
25
|
+
return defaults.length + func.length;
|
|
26
|
+
}
|
|
21
27
|
|
|
22
28
|
// src/core/createRule/defineRuleProcessors.ts
|
|
23
29
|
function defineRuleProcessors(definition, ...params) {
|
|
24
30
|
const { message, validator, active, ...properties } = definition;
|
|
25
31
|
const isAsync = validator.constructor.name === "AsyncFunction";
|
|
26
|
-
const
|
|
27
|
-
|
|
32
|
+
const defaultProcessors = {
|
|
33
|
+
validator(value, ...args) {
|
|
34
|
+
return definition.validator(value, ...unwrapRuleParameters(args.length ? args : params));
|
|
35
|
+
},
|
|
36
|
+
message(value, metadata) {
|
|
28
37
|
if (typeof definition.message === "function") {
|
|
29
|
-
return definition.message(value,
|
|
38
|
+
return definition.message(value, {
|
|
39
|
+
...metadata,
|
|
40
|
+
$params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
|
|
41
|
+
});
|
|
30
42
|
} else {
|
|
31
43
|
return definition.message;
|
|
32
44
|
}
|
|
33
45
|
},
|
|
34
|
-
|
|
35
|
-
return definition.validator(value, ...args.length ? args : params);
|
|
36
|
-
},
|
|
37
|
-
active(value, ...args) {
|
|
46
|
+
active(value, metadata) {
|
|
38
47
|
if (typeof definition.active === "function") {
|
|
39
|
-
return definition.active(value,
|
|
48
|
+
return definition.active(value, {
|
|
49
|
+
...metadata,
|
|
50
|
+
$params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
|
|
51
|
+
});
|
|
40
52
|
} else {
|
|
41
53
|
return definition.active ?? true;
|
|
42
54
|
}
|
|
43
55
|
},
|
|
56
|
+
exec(value) {
|
|
57
|
+
return definition.validator(value, ...unwrapRuleParameters(params));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const processors = {
|
|
61
|
+
...defaultProcessors,
|
|
44
62
|
...properties,
|
|
45
63
|
...{
|
|
46
64
|
_validator: definition.validator,
|
|
@@ -61,7 +79,7 @@ function createRule(definition) {
|
|
|
61
79
|
let fakeParams = [];
|
|
62
80
|
const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
|
|
63
81
|
const isAsync = definition.validator.constructor.name === "AsyncFunction";
|
|
64
|
-
if (definition.validator
|
|
82
|
+
if (getFunctionParametersLength(definition.validator) > 1) {
|
|
65
83
|
const ruleFactory = function(...params) {
|
|
66
84
|
return defineRuleProcessors(definition, ...params);
|
|
67
85
|
};
|
|
@@ -69,9 +87,10 @@ function createRule(definition) {
|
|
|
69
87
|
ruleFactory.message = staticProcessors.message;
|
|
70
88
|
ruleFactory.active = staticProcessors.active;
|
|
71
89
|
ruleFactory.type = staticProcessors.type;
|
|
72
|
-
ruleFactory.
|
|
73
|
-
ruleFactory.
|
|
74
|
-
ruleFactory.
|
|
90
|
+
ruleFactory.exec = staticProcessors.exec;
|
|
91
|
+
ruleFactory._validator = staticProcessors.validator;
|
|
92
|
+
ruleFactory._message = staticProcessors.message;
|
|
93
|
+
ruleFactory._active = staticProcessors.active;
|
|
75
94
|
ruleFactory._type = definition.type;
|
|
76
95
|
ruleFactory._patched = false;
|
|
77
96
|
ruleFactory._async = isAsync;
|
|
@@ -82,12 +101,132 @@ function createRule(definition) {
|
|
|
82
101
|
}
|
|
83
102
|
throw new Error("Validator must be a function");
|
|
84
103
|
}
|
|
104
|
+
function defineType(ruleName) {
|
|
105
|
+
return ruleName;
|
|
106
|
+
}
|
|
85
107
|
|
|
86
108
|
// src/core/useRegle/useRegle.ts
|
|
87
|
-
import { computed as
|
|
109
|
+
import { computed as computed6, isRef as isRef2, ref as ref4, shallowRef as shallowRef2, toRaw } from "vue";
|
|
88
110
|
|
|
89
111
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
90
|
-
import { reactive as reactive5 } from "vue";
|
|
112
|
+
import { computed as computed5, reactive as reactive5, unref as unref3 } from "vue";
|
|
113
|
+
|
|
114
|
+
// src/core/useStorage/useStorage.ts
|
|
115
|
+
import { onScopeDispose, ref, shallowRef } from "vue";
|
|
116
|
+
function useStorage() {
|
|
117
|
+
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
118
|
+
const fieldsStorage = shallowRef(
|
|
119
|
+
/* @__PURE__ */ new Map()
|
|
120
|
+
);
|
|
121
|
+
const collectionsStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
122
|
+
const dirtyStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
123
|
+
const ruleStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
124
|
+
const arrayStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
125
|
+
function getFieldsEntry($path) {
|
|
126
|
+
const existingFields = fieldsStorage.value.get($path);
|
|
127
|
+
if (existingFields) {
|
|
128
|
+
return existingFields;
|
|
129
|
+
} else {
|
|
130
|
+
const $fields = ref({});
|
|
131
|
+
fieldsStorage.value.set($path, $fields);
|
|
132
|
+
return $fields;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function getCollectionsEntry($path) {
|
|
136
|
+
const existingEach = collectionsStorage.value.get($path);
|
|
137
|
+
if (existingEach) {
|
|
138
|
+
return existingEach;
|
|
139
|
+
} else {
|
|
140
|
+
const $each = ref([]);
|
|
141
|
+
collectionsStorage.value.set($path, $each);
|
|
142
|
+
return $each;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function addArrayStatus($id, value) {
|
|
146
|
+
arrayStatusStorage.value.set($id, value);
|
|
147
|
+
}
|
|
148
|
+
function getArrayStatus($id) {
|
|
149
|
+
return arrayStatusStorage.value.get($id);
|
|
150
|
+
}
|
|
151
|
+
function deleteArrayStatus($id) {
|
|
152
|
+
if ($id) {
|
|
153
|
+
arrayStatusStorage.value.delete($id);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function setDirtyEntry($path, dirty) {
|
|
157
|
+
dirtyStorage.value.set($path, dirty);
|
|
158
|
+
}
|
|
159
|
+
function getDirtyState(path) {
|
|
160
|
+
return dirtyStorage.value.get(path) ?? false;
|
|
161
|
+
}
|
|
162
|
+
function addRuleDeclEntry($path, options) {
|
|
163
|
+
ruleDeclStorage.value.set($path, options);
|
|
164
|
+
}
|
|
165
|
+
function checkRuleDeclEntry($path, newRules) {
|
|
166
|
+
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
167
|
+
if (!storedRulesDefs)
|
|
168
|
+
return void 0;
|
|
169
|
+
const storedRules = storedRulesDefs;
|
|
170
|
+
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
171
|
+
if (!isValidCache)
|
|
172
|
+
return { valid: false };
|
|
173
|
+
return { valid: true };
|
|
174
|
+
}
|
|
175
|
+
function areRulesChanged(newRules, storedRules) {
|
|
176
|
+
const storedRulesKeys = Object.keys(storedRules);
|
|
177
|
+
const newRulesKeys = Object.keys(newRules);
|
|
178
|
+
if (newRulesKeys.length !== storedRulesKeys.length)
|
|
179
|
+
return false;
|
|
180
|
+
const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
|
|
181
|
+
if (!hasAllValidators)
|
|
182
|
+
return false;
|
|
183
|
+
return newRulesKeys.every((ruleKey) => {
|
|
184
|
+
const newRuleElement = newRules[ruleKey];
|
|
185
|
+
const storedRuleElement = storedRules[ruleKey];
|
|
186
|
+
if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
|
|
187
|
+
return false;
|
|
188
|
+
if (!newRuleElement._params)
|
|
189
|
+
return true;
|
|
190
|
+
return newRuleElement._params?.every((paramKey, index) => {
|
|
191
|
+
const storedParams = unwrapRuleParameters(storedRuleElement._params);
|
|
192
|
+
const newParams = unwrapRuleParameters(newRuleElement._params);
|
|
193
|
+
return storedParams?.[index] === newParams?.[index];
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function trySetRuleStatusRef(path) {
|
|
198
|
+
const ruleStatus = ruleStatusStorage.value.get(path);
|
|
199
|
+
if (ruleStatus) {
|
|
200
|
+
return ruleStatus;
|
|
201
|
+
} else {
|
|
202
|
+
const $pending = ref(false);
|
|
203
|
+
const $valid = ref(true);
|
|
204
|
+
const $metadata = ref({});
|
|
205
|
+
ruleStatusStorage.value.set(path, { $pending, $valid, $metadata });
|
|
206
|
+
return { $pending, $valid, $metadata };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
onScopeDispose(() => {
|
|
210
|
+
ruleDeclStorage.value.clear();
|
|
211
|
+
fieldsStorage.value.clear();
|
|
212
|
+
collectionsStorage.value.clear();
|
|
213
|
+
dirtyStorage.value.clear();
|
|
214
|
+
ruleStatusStorage.value.clear();
|
|
215
|
+
arrayStatusStorage.value.clear();
|
|
216
|
+
});
|
|
217
|
+
return {
|
|
218
|
+
addRuleDeclEntry,
|
|
219
|
+
setDirtyEntry,
|
|
220
|
+
checkRuleDeclEntry,
|
|
221
|
+
getDirtyState,
|
|
222
|
+
trySetRuleStatusRef,
|
|
223
|
+
getFieldsEntry,
|
|
224
|
+
getCollectionsEntry,
|
|
225
|
+
getArrayStatus,
|
|
226
|
+
addArrayStatus,
|
|
227
|
+
deleteArrayStatus
|
|
228
|
+
};
|
|
229
|
+
}
|
|
91
230
|
|
|
92
231
|
// src/core/useRegle/useErrors.ts
|
|
93
232
|
import { computed } from "vue";
|
|
@@ -124,7 +263,7 @@ function isEmpty(value) {
|
|
|
124
263
|
}
|
|
125
264
|
|
|
126
265
|
// src/utils/composables.ts
|
|
127
|
-
import { effectScope, getCurrentScope, onScopeDispose } from "vue";
|
|
266
|
+
import { effectScope, getCurrentScope, onScopeDispose as onScopeDispose2 } from "vue";
|
|
128
267
|
|
|
129
268
|
// src/core/useRegle/guards/ruleDef.guards.ts
|
|
130
269
|
function isNestedRulesDef(state, rule) {
|
|
@@ -154,14 +293,19 @@ function isFieldStatus(rule) {
|
|
|
154
293
|
return !!rule && "$rules" in rule;
|
|
155
294
|
}
|
|
156
295
|
|
|
296
|
+
// src/core/useRegle/guards/rule.errors.guards.ts
|
|
297
|
+
function isExternalErrorCollection(value) {
|
|
298
|
+
return "$each" in value || "$errors" in value;
|
|
299
|
+
}
|
|
300
|
+
|
|
157
301
|
// src/core/useRegle/useErrors.ts
|
|
158
|
-
function extractRulesErrors(rules) {
|
|
302
|
+
function extractRulesErrors(rules, externalErrors) {
|
|
159
303
|
return Object.entries(rules).map(([ruleKey, rule]) => {
|
|
160
304
|
if (!rule.$valid) {
|
|
161
305
|
return rule.$message;
|
|
162
306
|
}
|
|
163
307
|
return null;
|
|
164
|
-
}).filter((msg) => !!msg);
|
|
308
|
+
}).filter((msg) => !!msg).concat(externalErrors ?? []);
|
|
165
309
|
}
|
|
166
310
|
function processFieldErrors(fieldStatus) {
|
|
167
311
|
if (isNestedRulesStatus(fieldStatus)) {
|
|
@@ -171,8 +315,12 @@ function processFieldErrors(fieldStatus) {
|
|
|
171
315
|
$errors: fieldStatus.$rules ? extractRulesErrors(fieldStatus.$rules) : [],
|
|
172
316
|
$each: fieldStatus.$each.map(processFieldErrors)
|
|
173
317
|
};
|
|
174
|
-
} else if (isFieldStatus(fieldStatus)
|
|
175
|
-
|
|
318
|
+
} else if (isFieldStatus(fieldStatus)) {
|
|
319
|
+
if (fieldStatus.$error) {
|
|
320
|
+
return extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors);
|
|
321
|
+
} else {
|
|
322
|
+
return fieldStatus.$externalErrors ?? [];
|
|
323
|
+
}
|
|
176
324
|
}
|
|
177
325
|
return [];
|
|
178
326
|
}
|
|
@@ -192,8 +340,12 @@ function extractNestedErrors(fields) {
|
|
|
192
340
|
$each: extractCollectionError(fieldStatus)
|
|
193
341
|
}
|
|
194
342
|
];
|
|
195
|
-
} else if (isFieldStatus(fieldStatus)
|
|
196
|
-
|
|
343
|
+
} else if (isFieldStatus(fieldStatus)) {
|
|
344
|
+
if (fieldStatus.$error) {
|
|
345
|
+
return [fieldKey, extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors)];
|
|
346
|
+
} else {
|
|
347
|
+
return [fieldKey, fieldStatus.$externalErrors ?? []];
|
|
348
|
+
}
|
|
197
349
|
}
|
|
198
350
|
return [fieldKey, []];
|
|
199
351
|
})
|
|
@@ -212,16 +364,14 @@ import { computed as computed4, effectScope as effectScope4, reactive as reactiv
|
|
|
212
364
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
213
365
|
import { nextTick, reactive as reactive3, ref as ref3, toRef as toRef3, toRefs, watch as watch3 } from "vue";
|
|
214
366
|
|
|
367
|
+
// src/utils/randomId.ts
|
|
368
|
+
function randomId() {
|
|
369
|
+
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
370
|
+
return uint32.toString(8);
|
|
371
|
+
}
|
|
372
|
+
|
|
215
373
|
// src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
|
|
216
|
-
import {
|
|
217
|
-
computed as computed3,
|
|
218
|
-
effectScope as effectScope3,
|
|
219
|
-
reactive as reactive2,
|
|
220
|
-
ref as ref2,
|
|
221
|
-
toRef as toRef2,
|
|
222
|
-
unref as unref2,
|
|
223
|
-
watch as watch2
|
|
224
|
-
} from "vue";
|
|
374
|
+
import { computed as computed3, effectScope as effectScope3, reactive as reactive2, ref as ref2, toRef as toRef2, unref as unref2, watch as watch2 } from "vue";
|
|
225
375
|
|
|
226
376
|
// src/core/useRegle/useStateProperties/createReactiveRuleStatus.ts
|
|
227
377
|
import { computed as computed2, effectScope as effectScope2, reactive, watch } from "vue";
|
|
@@ -246,13 +396,18 @@ function createReactiveRuleStatus({
|
|
|
246
396
|
}) {
|
|
247
397
|
let scope = effectScope2();
|
|
248
398
|
let scopeState;
|
|
249
|
-
const { $pending, $valid } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
|
|
399
|
+
const { $pending, $valid, $metadata } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
|
|
250
400
|
function $watch() {
|
|
251
401
|
scopeState = scope.run(() => {
|
|
402
|
+
const $defaultMetadata = computed2(() => ({
|
|
403
|
+
$invalid: !$valid.value,
|
|
404
|
+
$params: $params.value,
|
|
405
|
+
...$metadata.value
|
|
406
|
+
}));
|
|
252
407
|
const $active = computed2(() => {
|
|
253
408
|
if (isFormRuleDefinition(rule)) {
|
|
254
409
|
if (typeof rule.value.active === "function") {
|
|
255
|
-
return rule.value.active(state.value,
|
|
410
|
+
return rule.value.active(state.value, $defaultMetadata.value);
|
|
256
411
|
} else {
|
|
257
412
|
return rule.value.active;
|
|
258
413
|
}
|
|
@@ -265,7 +420,7 @@ function createReactiveRuleStatus({
|
|
|
265
420
|
const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
|
|
266
421
|
if (customMessageRule) {
|
|
267
422
|
if (typeof customMessageRule === "function") {
|
|
268
|
-
message = customMessageRule(state.value,
|
|
423
|
+
message = customMessageRule(state.value, $defaultMetadata.value);
|
|
269
424
|
} else {
|
|
270
425
|
message = customMessageRule;
|
|
271
426
|
}
|
|
@@ -273,7 +428,7 @@ function createReactiveRuleStatus({
|
|
|
273
428
|
if (isFormRuleDefinition(rule)) {
|
|
274
429
|
if (!(customMessageRule && !rule.value._patched)) {
|
|
275
430
|
if (typeof rule.value.message === "function") {
|
|
276
|
-
message = rule.value.message(state.value,
|
|
431
|
+
message = rule.value.message(state.value, $defaultMetadata.value);
|
|
277
432
|
} else {
|
|
278
433
|
message = rule.value.message;
|
|
279
434
|
}
|
|
@@ -286,7 +441,7 @@ function createReactiveRuleStatus({
|
|
|
286
441
|
return message;
|
|
287
442
|
});
|
|
288
443
|
const $type = computed2(() => {
|
|
289
|
-
if (isFormRuleDefinition(rule)) {
|
|
444
|
+
if (isFormRuleDefinition(rule) && rule.value.type) {
|
|
290
445
|
return Object.values(InternalRuleType).includes(rule.value.type) ? ruleKey : rule.value.type;
|
|
291
446
|
} else {
|
|
292
447
|
return ruleKey;
|
|
@@ -330,7 +485,13 @@ function createReactiveRuleStatus({
|
|
|
330
485
|
$valid.value = true;
|
|
331
486
|
$pending.value = true;
|
|
332
487
|
const promiseResult = await resultOrPromise;
|
|
333
|
-
|
|
488
|
+
if (typeof promiseResult === "boolean") {
|
|
489
|
+
ruleResult = promiseResult;
|
|
490
|
+
} else {
|
|
491
|
+
const { $valid: $valid2, ...rest } = promiseResult;
|
|
492
|
+
ruleResult = $valid2;
|
|
493
|
+
$metadata.value = rest;
|
|
494
|
+
}
|
|
334
495
|
} catch (e) {
|
|
335
496
|
ruleResult = false;
|
|
336
497
|
} finally {
|
|
@@ -338,9 +499,19 @@ function createReactiveRuleStatus({
|
|
|
338
499
|
}
|
|
339
500
|
}
|
|
340
501
|
} else {
|
|
341
|
-
|
|
502
|
+
if (resultOrPromise != null) {
|
|
503
|
+
if (typeof resultOrPromise === "boolean") {
|
|
504
|
+
ruleResult = resultOrPromise;
|
|
505
|
+
} else {
|
|
506
|
+
const { $valid: $valid2, ...rest } = resultOrPromise;
|
|
507
|
+
ruleResult = $valid2;
|
|
508
|
+
$metadata.value = rest;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
342
511
|
}
|
|
343
512
|
$valid.value = ruleResult;
|
|
513
|
+
if (options.$externalErrors) {
|
|
514
|
+
}
|
|
344
515
|
return ruleResult;
|
|
345
516
|
}
|
|
346
517
|
function $unwatch() {
|
|
@@ -365,13 +536,22 @@ function createReactiveFieldStatus({
|
|
|
365
536
|
customMessages,
|
|
366
537
|
path,
|
|
367
538
|
storage,
|
|
368
|
-
options
|
|
539
|
+
options,
|
|
540
|
+
externalErrors
|
|
369
541
|
}) {
|
|
370
542
|
let scope = effectScope3();
|
|
371
543
|
let scopeState;
|
|
372
544
|
const $dirty = ref2(false);
|
|
373
545
|
const $anyDirty = computed3(() => $dirty.value);
|
|
374
546
|
const triggerPunishment = ref2(false);
|
|
547
|
+
const $externalErrors = ref2([]);
|
|
548
|
+
function collectExternalErrors() {
|
|
549
|
+
if (externalErrors.value) {
|
|
550
|
+
$externalErrors.value = externalErrors.value;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
collectExternalErrors();
|
|
554
|
+
const $unwatchExternalErrors = watch2(externalErrors, collectExternalErrors);
|
|
375
555
|
function createReactiveRulesResult() {
|
|
376
556
|
const declaredRules = rulesDef.value;
|
|
377
557
|
const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
|
|
@@ -417,6 +597,7 @@ function createReactiveFieldStatus({
|
|
|
417
597
|
if (!unref2(options.lazy)) {
|
|
418
598
|
$commit();
|
|
419
599
|
}
|
|
600
|
+
$externalErrors.value = [];
|
|
420
601
|
});
|
|
421
602
|
function $unwatch() {
|
|
422
603
|
if ($rules.value) {
|
|
@@ -430,6 +611,7 @@ function createReactiveFieldStatus({
|
|
|
430
611
|
}
|
|
431
612
|
$unwatchState();
|
|
432
613
|
$unwatchValid();
|
|
614
|
+
$unwatchExternalErrors();
|
|
433
615
|
scope.stop();
|
|
434
616
|
scope = effectScope3();
|
|
435
617
|
}
|
|
@@ -480,6 +662,7 @@ function createReactiveFieldStatus({
|
|
|
480
662
|
});
|
|
481
663
|
function $reset() {
|
|
482
664
|
$dirty.value = false;
|
|
665
|
+
$externalErrors.value = [];
|
|
483
666
|
}
|
|
484
667
|
function $touch() {
|
|
485
668
|
$dirty.value = true;
|
|
@@ -502,6 +685,9 @@ function createReactiveFieldStatus({
|
|
|
502
685
|
return false;
|
|
503
686
|
}
|
|
504
687
|
}
|
|
688
|
+
function $clearExternalErrors() {
|
|
689
|
+
$externalErrors.value = [];
|
|
690
|
+
}
|
|
505
691
|
return reactive2({
|
|
506
692
|
$dirty,
|
|
507
693
|
$anyDirty,
|
|
@@ -509,22 +695,18 @@ function createReactiveFieldStatus({
|
|
|
509
695
|
$error: scopeState.$error,
|
|
510
696
|
$pending: scopeState.$pending,
|
|
511
697
|
$valid: scopeState.$valid,
|
|
698
|
+
$externalErrors,
|
|
512
699
|
$value: state,
|
|
513
700
|
$rules,
|
|
514
701
|
$reset,
|
|
515
702
|
$touch,
|
|
516
703
|
$validate,
|
|
517
704
|
$unwatch,
|
|
518
|
-
$watch
|
|
705
|
+
$watch,
|
|
706
|
+
$clearExternalErrors
|
|
519
707
|
});
|
|
520
708
|
}
|
|
521
709
|
|
|
522
|
-
// src/utils/randomId.ts
|
|
523
|
-
function randomId() {
|
|
524
|
-
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
525
|
-
return uint32.toString(8);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
710
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
529
711
|
function createCollectionElement({
|
|
530
712
|
path,
|
|
@@ -533,7 +715,8 @@ function createCollectionElement({
|
|
|
533
715
|
storage,
|
|
534
716
|
value,
|
|
535
717
|
customMessages,
|
|
536
|
-
rules
|
|
718
|
+
rules,
|
|
719
|
+
externalErrors
|
|
537
720
|
}) {
|
|
538
721
|
const $id = randomId();
|
|
539
722
|
const $path = `${path}.${$id}`;
|
|
@@ -548,13 +731,15 @@ function createCollectionElement({
|
|
|
548
731
|
});
|
|
549
732
|
}
|
|
550
733
|
const $state = toRefs(value);
|
|
734
|
+
const $externalErrors = toRef3(() => externalErrors.value?.[index]);
|
|
551
735
|
const $status = createReactiveChildrenStatus({
|
|
552
736
|
state: $state[index],
|
|
553
737
|
rulesDef: toRef3(() => rules),
|
|
554
738
|
customMessages,
|
|
555
739
|
path: $path,
|
|
556
740
|
storage,
|
|
557
|
-
options
|
|
741
|
+
options,
|
|
742
|
+
externalErrors: $externalErrors
|
|
558
743
|
});
|
|
559
744
|
if ($status) {
|
|
560
745
|
$status.$id = value[index].$id ?? $id;
|
|
@@ -568,7 +753,8 @@ function createReactiveCollectionStatus({
|
|
|
568
753
|
customMessages,
|
|
569
754
|
path,
|
|
570
755
|
storage,
|
|
571
|
-
options
|
|
756
|
+
options,
|
|
757
|
+
externalErrors
|
|
572
758
|
}) {
|
|
573
759
|
if (Array.isArray(state.value) && !rulesDef.value.$each) {
|
|
574
760
|
return null;
|
|
@@ -580,13 +766,28 @@ function createReactiveCollectionStatus({
|
|
|
580
766
|
$watch();
|
|
581
767
|
function createStatus() {
|
|
582
768
|
const { $each, ...otherFields } = rulesDef.value;
|
|
769
|
+
const $externalErrorsField = toRef3(() => {
|
|
770
|
+
if (externalErrors.value) {
|
|
771
|
+
if (isExternalErrorCollection(externalErrors.value)) {
|
|
772
|
+
return externalErrors.value.$errors;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
const $externalErrorsEach = toRef3(() => {
|
|
777
|
+
if (externalErrors.value) {
|
|
778
|
+
if (isExternalErrorCollection(externalErrors.value)) {
|
|
779
|
+
return externalErrors.value.$each;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
});
|
|
583
783
|
$fieldStatus.value = createReactiveFieldStatus({
|
|
584
784
|
state,
|
|
585
785
|
rulesDef: toRef3(() => otherFields),
|
|
586
786
|
customMessages,
|
|
587
787
|
path,
|
|
588
788
|
storage,
|
|
589
|
-
options
|
|
789
|
+
options,
|
|
790
|
+
externalErrors: $externalErrorsField
|
|
590
791
|
});
|
|
591
792
|
if (Array.isArray(state.value) && $each) {
|
|
592
793
|
$eachStatus.value = state.value.map((value, index) => {
|
|
@@ -602,7 +803,8 @@ function createReactiveCollectionStatus({
|
|
|
602
803
|
value: state.value,
|
|
603
804
|
index,
|
|
604
805
|
options,
|
|
605
|
-
storage
|
|
806
|
+
storage,
|
|
807
|
+
externalErrors: $externalErrorsEach
|
|
606
808
|
});
|
|
607
809
|
}
|
|
608
810
|
}).filter((f) => !!f);
|
|
@@ -626,6 +828,13 @@ function createReactiveCollectionStatus({
|
|
|
626
828
|
$eachStatus.value[index].$unwatch();
|
|
627
829
|
}
|
|
628
830
|
} else {
|
|
831
|
+
const $externalErrorsEach = toRef3(() => {
|
|
832
|
+
if (externalErrors.value) {
|
|
833
|
+
if ("$each" in externalErrors.value) {
|
|
834
|
+
return externalErrors.value.$each;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
});
|
|
629
838
|
const newElement = createCollectionElement({
|
|
630
839
|
value: state.value,
|
|
631
840
|
rules: $each,
|
|
@@ -633,7 +842,8 @@ function createReactiveCollectionStatus({
|
|
|
633
842
|
path,
|
|
634
843
|
storage,
|
|
635
844
|
options,
|
|
636
|
-
index
|
|
845
|
+
index,
|
|
846
|
+
externalErrors: $externalErrorsEach
|
|
637
847
|
});
|
|
638
848
|
if (newElement) {
|
|
639
849
|
$eachStatus.value[index] = newElement;
|
|
@@ -712,7 +922,8 @@ function createReactiveNestedStatus({
|
|
|
712
922
|
path = "",
|
|
713
923
|
rootRules,
|
|
714
924
|
storage,
|
|
715
|
-
options
|
|
925
|
+
options,
|
|
926
|
+
externalErrors
|
|
716
927
|
}) {
|
|
717
928
|
let scope = effectScope4();
|
|
718
929
|
let scopeState;
|
|
@@ -723,6 +934,7 @@ function createReactiveNestedStatus({
|
|
|
723
934
|
if (statePropRules) {
|
|
724
935
|
const stateRef = toRef4(state.value, statePropKey);
|
|
725
936
|
const statePropRulesRef = toRef4(() => statePropRules);
|
|
937
|
+
const $externalErrors = toRef4(() => externalErrors.value?.[statePropKey]);
|
|
726
938
|
return [
|
|
727
939
|
statePropKey,
|
|
728
940
|
createReactiveChildrenStatus({
|
|
@@ -731,7 +943,8 @@ function createReactiveNestedStatus({
|
|
|
731
943
|
customMessages,
|
|
732
944
|
path: path ? `${path}.${statePropKey}` : statePropKey,
|
|
733
945
|
storage,
|
|
734
|
-
options
|
|
946
|
+
options,
|
|
947
|
+
externalErrors: $externalErrors
|
|
735
948
|
})
|
|
736
949
|
];
|
|
737
950
|
}
|
|
@@ -826,6 +1039,11 @@ function createReactiveNestedStatus({
|
|
|
826
1039
|
scope.stop();
|
|
827
1040
|
scope = effectScope4();
|
|
828
1041
|
}
|
|
1042
|
+
function $clearExternalErrors() {
|
|
1043
|
+
Object.entries($fields.value).forEach(([_, field]) => {
|
|
1044
|
+
field.$clearExternalErrors();
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
829
1047
|
return reactive4({
|
|
830
1048
|
...scopeState,
|
|
831
1049
|
$fields,
|
|
@@ -834,7 +1052,8 @@ function createReactiveNestedStatus({
|
|
|
834
1052
|
$touch,
|
|
835
1053
|
$validate,
|
|
836
1054
|
$unwatch,
|
|
837
|
-
$watch
|
|
1055
|
+
$watch,
|
|
1056
|
+
$clearExternalErrors
|
|
838
1057
|
});
|
|
839
1058
|
}
|
|
840
1059
|
function createReactiveChildrenStatus({
|
|
@@ -843,7 +1062,8 @@ function createReactiveChildrenStatus({
|
|
|
843
1062
|
customMessages,
|
|
844
1063
|
path,
|
|
845
1064
|
storage,
|
|
846
|
-
options
|
|
1065
|
+
options,
|
|
1066
|
+
externalErrors
|
|
847
1067
|
}) {
|
|
848
1068
|
if (isCollectionRulesDef(rulesDef)) {
|
|
849
1069
|
return createReactiveCollectionStatus({
|
|
@@ -852,7 +1072,8 @@ function createReactiveChildrenStatus({
|
|
|
852
1072
|
customMessages,
|
|
853
1073
|
path,
|
|
854
1074
|
storage,
|
|
855
|
-
options
|
|
1075
|
+
options,
|
|
1076
|
+
externalErrors
|
|
856
1077
|
});
|
|
857
1078
|
} else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
|
|
858
1079
|
return createReactiveNestedStatus({
|
|
@@ -861,7 +1082,8 @@ function createReactiveChildrenStatus({
|
|
|
861
1082
|
customMessages,
|
|
862
1083
|
path,
|
|
863
1084
|
storage,
|
|
864
|
-
options
|
|
1085
|
+
options,
|
|
1086
|
+
externalErrors
|
|
865
1087
|
});
|
|
866
1088
|
} else if (isValidatorRulesDef(rulesDef)) {
|
|
867
1089
|
return createReactiveFieldStatus({
|
|
@@ -870,123 +1092,17 @@ function createReactiveChildrenStatus({
|
|
|
870
1092
|
customMessages,
|
|
871
1093
|
path,
|
|
872
1094
|
storage,
|
|
873
|
-
options
|
|
1095
|
+
options,
|
|
1096
|
+
externalErrors
|
|
874
1097
|
});
|
|
875
1098
|
}
|
|
876
1099
|
return null;
|
|
877
1100
|
}
|
|
878
1101
|
|
|
879
|
-
// src/core/useStorage/useStorage.ts
|
|
880
|
-
import { ref as ref4, shallowRef } from "vue";
|
|
881
|
-
function useStorage() {
|
|
882
|
-
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
883
|
-
const fieldsStorage = shallowRef(
|
|
884
|
-
/* @__PURE__ */ new Map()
|
|
885
|
-
);
|
|
886
|
-
const collectionsStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
887
|
-
const dirtyStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
888
|
-
const ruleStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
889
|
-
const arrayStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
890
|
-
function getFieldsEntry($path) {
|
|
891
|
-
const existingFields = fieldsStorage.value.get($path);
|
|
892
|
-
if (existingFields) {
|
|
893
|
-
return existingFields;
|
|
894
|
-
} else {
|
|
895
|
-
const $fields = ref4({});
|
|
896
|
-
fieldsStorage.value.set($path, $fields);
|
|
897
|
-
return $fields;
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
function getCollectionsEntry($path) {
|
|
901
|
-
const existingEach = collectionsStorage.value.get($path);
|
|
902
|
-
if (existingEach) {
|
|
903
|
-
return existingEach;
|
|
904
|
-
} else {
|
|
905
|
-
const $each = ref4([]);
|
|
906
|
-
collectionsStorage.value.set($path, $each);
|
|
907
|
-
return $each;
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
function addArrayStatus($id, value) {
|
|
911
|
-
arrayStatusStorage.value.set($id, value);
|
|
912
|
-
}
|
|
913
|
-
function getArrayStatus($id) {
|
|
914
|
-
return arrayStatusStorage.value.get($id);
|
|
915
|
-
}
|
|
916
|
-
function deleteArrayStatus($id) {
|
|
917
|
-
if ($id) {
|
|
918
|
-
arrayStatusStorage.value.delete($id);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
function setDirtyEntry($path, dirty) {
|
|
922
|
-
dirtyStorage.value.set($path, dirty);
|
|
923
|
-
}
|
|
924
|
-
function getDirtyState(path) {
|
|
925
|
-
return dirtyStorage.value.get(path) ?? false;
|
|
926
|
-
}
|
|
927
|
-
function addRuleDeclEntry($path, options) {
|
|
928
|
-
ruleDeclStorage.value.set($path, options);
|
|
929
|
-
}
|
|
930
|
-
function checkRuleDeclEntry($path, newRules) {
|
|
931
|
-
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
932
|
-
if (!storedRulesDefs)
|
|
933
|
-
return void 0;
|
|
934
|
-
const storedRules = storedRulesDefs;
|
|
935
|
-
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
936
|
-
if (!isValidCache)
|
|
937
|
-
return { valid: false };
|
|
938
|
-
return { valid: true };
|
|
939
|
-
}
|
|
940
|
-
function areRulesChanged(newRules, storedRules) {
|
|
941
|
-
const storedRulesKeys = Object.keys(storedRules);
|
|
942
|
-
const newRulesKeys = Object.keys(newRules);
|
|
943
|
-
if (newRulesKeys.length !== storedRulesKeys.length)
|
|
944
|
-
return false;
|
|
945
|
-
const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
|
|
946
|
-
if (!hasAllValidators)
|
|
947
|
-
return false;
|
|
948
|
-
return newRulesKeys.every((ruleKey) => {
|
|
949
|
-
const newRuleElement = newRules[ruleKey];
|
|
950
|
-
const storedRuleElement = storedRules[ruleKey];
|
|
951
|
-
if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
|
|
952
|
-
return false;
|
|
953
|
-
if (!newRuleElement._params)
|
|
954
|
-
return true;
|
|
955
|
-
return newRuleElement._params?.every((paramKey, index) => {
|
|
956
|
-
const storedParams = unwrapRuleParameters(storedRuleElement._params);
|
|
957
|
-
const newParams = unwrapRuleParameters(newRuleElement._params);
|
|
958
|
-
return storedParams?.[index] === newParams?.[index];
|
|
959
|
-
});
|
|
960
|
-
});
|
|
961
|
-
}
|
|
962
|
-
function trySetRuleStatusRef(path) {
|
|
963
|
-
const ruleStatus = ruleStatusStorage.value.get(path);
|
|
964
|
-
if (ruleStatus) {
|
|
965
|
-
return ruleStatus;
|
|
966
|
-
} else {
|
|
967
|
-
const $pending = ref4(false);
|
|
968
|
-
const $valid = ref4(true);
|
|
969
|
-
ruleStatusStorage.value.set(path, { $pending, $valid });
|
|
970
|
-
return { $pending, $valid };
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
return {
|
|
974
|
-
addRuleDeclEntry,
|
|
975
|
-
setDirtyEntry,
|
|
976
|
-
checkRuleDeclEntry,
|
|
977
|
-
getDirtyState,
|
|
978
|
-
trySetRuleStatusRef,
|
|
979
|
-
getFieldsEntry,
|
|
980
|
-
getCollectionsEntry,
|
|
981
|
-
getArrayStatus,
|
|
982
|
-
addArrayStatus,
|
|
983
|
-
deleteArrayStatus
|
|
984
|
-
};
|
|
985
|
-
}
|
|
986
|
-
|
|
987
1102
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
988
1103
|
function useStateProperties(scopeRules, state, options, customRules) {
|
|
989
1104
|
const storage = useStorage();
|
|
1105
|
+
const externalErrors = computed5(() => unref3(options.$externalErrors));
|
|
990
1106
|
const $regle = reactive5(
|
|
991
1107
|
createReactiveNestedStatus({
|
|
992
1108
|
rootRules: scopeRules,
|
|
@@ -994,7 +1110,8 @@ function useStateProperties(scopeRules, state, options, customRules) {
|
|
|
994
1110
|
state,
|
|
995
1111
|
customMessages: customRules?.(),
|
|
996
1112
|
storage,
|
|
997
|
-
options
|
|
1113
|
+
options,
|
|
1114
|
+
externalErrors
|
|
998
1115
|
})
|
|
999
1116
|
);
|
|
1000
1117
|
const errors = useErrors($regle);
|
|
@@ -1009,22 +1126,29 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1009
1126
|
rewardEarly: options?.rewardEarly ?? false
|
|
1010
1127
|
};
|
|
1011
1128
|
function useRegle2(state, rulesFactory, options2) {
|
|
1012
|
-
const scopeRules = isRef2(rulesFactory) ? rulesFactory :
|
|
1129
|
+
const scopeRules = isRef2(rulesFactory) ? rulesFactory : computed6(rulesFactory);
|
|
1013
1130
|
const resolvedOptions = {
|
|
1014
1131
|
...globalOptions,
|
|
1015
1132
|
...options2
|
|
1016
1133
|
};
|
|
1017
|
-
const
|
|
1134
|
+
const processedState = ref4(state);
|
|
1135
|
+
const initialState = shallowRef2(structuredClone(toRaw(processedState.value)));
|
|
1018
1136
|
const { $regle, errors } = useStateProperties(
|
|
1019
1137
|
scopeRules,
|
|
1020
|
-
|
|
1138
|
+
processedState,
|
|
1021
1139
|
resolvedOptions,
|
|
1022
1140
|
customRules
|
|
1023
1141
|
);
|
|
1024
1142
|
function resetForm() {
|
|
1025
|
-
state.value =
|
|
1143
|
+
state.value = toRaw(initialState.value);
|
|
1026
1144
|
$regle.$reset();
|
|
1027
1145
|
}
|
|
1146
|
+
const $valid = computed6(() => {
|
|
1147
|
+
return $regle.$valid && $regle.$dirty && !$regle.$pending;
|
|
1148
|
+
});
|
|
1149
|
+
const $invalid = computed6(() => {
|
|
1150
|
+
return $regle.$invalid && $regle.$dirty || $regle.$pending;
|
|
1151
|
+
});
|
|
1028
1152
|
async function validateForm() {
|
|
1029
1153
|
$regle.$touch();
|
|
1030
1154
|
const result = await $regle.$validate();
|
|
@@ -1034,19 +1158,21 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1034
1158
|
return false;
|
|
1035
1159
|
}
|
|
1036
1160
|
return {
|
|
1037
|
-
state,
|
|
1161
|
+
$state: state,
|
|
1038
1162
|
$regle,
|
|
1039
|
-
errors,
|
|
1163
|
+
$errors: errors,
|
|
1040
1164
|
resetForm,
|
|
1041
|
-
validateForm
|
|
1165
|
+
validateForm,
|
|
1166
|
+
$valid,
|
|
1167
|
+
$invalid
|
|
1042
1168
|
};
|
|
1043
1169
|
}
|
|
1044
1170
|
return useRegle2;
|
|
1045
1171
|
}
|
|
1046
1172
|
var useRegle = createUseRegleComposable();
|
|
1047
1173
|
|
|
1048
|
-
// src/core/
|
|
1049
|
-
function
|
|
1174
|
+
// src/core/defineRegleConfig.ts
|
|
1175
|
+
function defineRegleConfig({
|
|
1050
1176
|
rules,
|
|
1051
1177
|
options
|
|
1052
1178
|
}) {
|
|
@@ -1056,6 +1182,8 @@ function defineRegleOptions({
|
|
|
1056
1182
|
export {
|
|
1057
1183
|
InternalRuleType,
|
|
1058
1184
|
createRule,
|
|
1059
|
-
|
|
1060
|
-
|
|
1185
|
+
defineRegleConfig,
|
|
1186
|
+
defineType,
|
|
1187
|
+
unwrapRuleParameters,
|
|
1188
|
+
useRegle
|
|
1061
1189
|
};
|