@regle/core 0.4.5 → 0.4.6-beta.2
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/regle-core.cjs +1983 -0
- package/dist/regle-core.min.cjs +2 -0
- package/dist/regle-core.min.mjs +2 -0
- package/dist/regle-core.mjs +1976 -0
- package/index.cjs +7 -0
- package/index.js +7 -0
- package/package.json +30 -10
- package/dist/index.cjs +0 -2
- package/dist/index.js +0 -2
- /package/dist/{index.d.cts → regle-core.d.cts} +0 -0
- /package/dist/{index.d.ts → regle-core.d.ts} +0 -0
|
@@ -0,0 +1,1983 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vue = require('vue');
|
|
4
|
+
|
|
5
|
+
// src/types/rules/rule.internal.types.ts
|
|
6
|
+
var InternalRuleType = /* @__PURE__ */ ((InternalRuleType2) => {
|
|
7
|
+
InternalRuleType2["Inline"] = "__inline";
|
|
8
|
+
InternalRuleType2["Async"] = "__async";
|
|
9
|
+
return InternalRuleType2;
|
|
10
|
+
})(InternalRuleType || {});
|
|
11
|
+
function mergeBooleanGroupProperties(entries, property) {
|
|
12
|
+
return entries.some((entry) => {
|
|
13
|
+
return entry[property];
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function mergeArrayGroupProperties(entries, property) {
|
|
17
|
+
return entries.reduce((all, entry) => {
|
|
18
|
+
const fetchedProperty = entry[property] || [];
|
|
19
|
+
return all.concat(fetchedProperty);
|
|
20
|
+
}, []);
|
|
21
|
+
}
|
|
22
|
+
function unwrapRuleParameters(params) {
|
|
23
|
+
try {
|
|
24
|
+
return params.map((param) => {
|
|
25
|
+
if (param instanceof Function) {
|
|
26
|
+
try {
|
|
27
|
+
const result = param();
|
|
28
|
+
return result;
|
|
29
|
+
} catch (e) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return vue.unref(param);
|
|
34
|
+
});
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function createReactiveParams(params) {
|
|
40
|
+
return params.map((param) => {
|
|
41
|
+
if (param instanceof Function) {
|
|
42
|
+
return param;
|
|
43
|
+
} else if (vue.isRef(param)) {
|
|
44
|
+
return param;
|
|
45
|
+
}
|
|
46
|
+
return vue.toRef(() => param);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function getFunctionParametersLength(func) {
|
|
50
|
+
const funcStr = func.toString();
|
|
51
|
+
const cleanStr = funcStr.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
52
|
+
const paramsMatch = cleanStr.match(
|
|
53
|
+
/^(?:async\s*)?(?:function\b.*?\(|\((.*?)\)|(\w+))\s*=>|\((.*?)\)\s*=>|function.*?\((.*?)\)|\((.*?)\)/
|
|
54
|
+
);
|
|
55
|
+
if (!paramsMatch) return 0;
|
|
56
|
+
const paramsSection = paramsMatch[0] || paramsMatch[1] || paramsMatch[2] || paramsMatch[3] || paramsMatch[4] || "";
|
|
57
|
+
const paramList = paramsSection.split(",").map((p) => p.trim()).filter((p) => p.length > 0);
|
|
58
|
+
return paramList.length;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/core/createRule/defineRuleProcessors.ts
|
|
62
|
+
function defineRuleProcessors(definition, ...params) {
|
|
63
|
+
const { validator, type } = definition;
|
|
64
|
+
const isAsync = type === "__async" /* Async */ || validator.constructor.name === "AsyncFunction";
|
|
65
|
+
const defaultProcessors = {
|
|
66
|
+
validator(value, ...args) {
|
|
67
|
+
return definition.validator(value, ...unwrapRuleParameters(args.length ? args : params));
|
|
68
|
+
},
|
|
69
|
+
message(metadata) {
|
|
70
|
+
if (typeof definition.message === "function") {
|
|
71
|
+
return definition.message({
|
|
72
|
+
...metadata,
|
|
73
|
+
$params: unwrapRuleParameters(metadata?.$params?.length ? metadata.$params : params)
|
|
74
|
+
});
|
|
75
|
+
} else {
|
|
76
|
+
return definition.message;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
active(metadata) {
|
|
80
|
+
if (typeof definition.active === "function") {
|
|
81
|
+
return definition.active({
|
|
82
|
+
...metadata,
|
|
83
|
+
$params: unwrapRuleParameters(metadata?.$params?.length ? metadata.$params : params)
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
return definition.active ?? true;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
tooltip(metadata) {
|
|
90
|
+
if (typeof definition.tooltip === "function") {
|
|
91
|
+
return definition.tooltip({
|
|
92
|
+
...metadata,
|
|
93
|
+
$params: unwrapRuleParameters(metadata?.$params?.length ? metadata.$params : params)
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
return definition.tooltip ?? [];
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
exec(value) {
|
|
100
|
+
const validator2 = definition.validator(value, ...unwrapRuleParameters(params));
|
|
101
|
+
let rawResult;
|
|
102
|
+
if (validator2 instanceof Promise) {
|
|
103
|
+
return validator2.then((result) => {
|
|
104
|
+
rawResult = result;
|
|
105
|
+
if (typeof rawResult === "object" && "$valid" in rawResult) {
|
|
106
|
+
return rawResult.$valid;
|
|
107
|
+
} else if (typeof rawResult === "boolean") {
|
|
108
|
+
return rawResult;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
});
|
|
112
|
+
} else {
|
|
113
|
+
rawResult = validator2;
|
|
114
|
+
}
|
|
115
|
+
if (typeof rawResult === "object" && "$valid" in rawResult) {
|
|
116
|
+
return rawResult.$valid;
|
|
117
|
+
} else if (typeof rawResult === "boolean") {
|
|
118
|
+
return rawResult;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const processors = {
|
|
124
|
+
...defaultProcessors,
|
|
125
|
+
_validator: definition.validator,
|
|
126
|
+
_message: definition.message,
|
|
127
|
+
_active: definition.active,
|
|
128
|
+
_tooltip: definition.tooltip,
|
|
129
|
+
_type: definition.type,
|
|
130
|
+
_message_patched: false,
|
|
131
|
+
_tooltip_patched: false,
|
|
132
|
+
_async: isAsync,
|
|
133
|
+
_params: createReactiveParams(params)
|
|
134
|
+
};
|
|
135
|
+
return processors;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/core/createRule/createRule.ts
|
|
139
|
+
function createRule(definition) {
|
|
140
|
+
if (typeof definition.validator === "function") {
|
|
141
|
+
let fakeParams = [];
|
|
142
|
+
const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
|
|
143
|
+
const isAsync = definition.validator.constructor.name === "AsyncFunction";
|
|
144
|
+
if (getFunctionParametersLength(definition.validator) > 1) {
|
|
145
|
+
const ruleFactory = function(...params) {
|
|
146
|
+
return defineRuleProcessors(definition, ...params);
|
|
147
|
+
};
|
|
148
|
+
ruleFactory.validator = staticProcessors.validator;
|
|
149
|
+
ruleFactory.message = staticProcessors.message;
|
|
150
|
+
ruleFactory.active = staticProcessors.active;
|
|
151
|
+
ruleFactory.tooltip = staticProcessors.tooltip;
|
|
152
|
+
ruleFactory.type = staticProcessors.type;
|
|
153
|
+
ruleFactory.exec = staticProcessors.exec;
|
|
154
|
+
ruleFactory._validator = staticProcessors.validator;
|
|
155
|
+
ruleFactory._message = staticProcessors.message;
|
|
156
|
+
ruleFactory._active = staticProcessors.active;
|
|
157
|
+
ruleFactory._tooltip = staticProcessors.tooltip;
|
|
158
|
+
ruleFactory._type = definition.type;
|
|
159
|
+
ruleFactory._message_pacthed = false;
|
|
160
|
+
ruleFactory._tooltip_pacthed = false;
|
|
161
|
+
ruleFactory._async = isAsync;
|
|
162
|
+
return ruleFactory;
|
|
163
|
+
} else {
|
|
164
|
+
return staticProcessors;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
throw new Error("Validator must be a function");
|
|
168
|
+
}
|
|
169
|
+
function isObject(obj) {
|
|
170
|
+
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
171
|
+
}
|
|
172
|
+
function isRefObject(obj) {
|
|
173
|
+
return isObject(obj.value);
|
|
174
|
+
}
|
|
175
|
+
function cloneDeep(obj) {
|
|
176
|
+
let result = obj;
|
|
177
|
+
let type = {}.toString.call(obj).slice(8, -1);
|
|
178
|
+
if (type == "Set") {
|
|
179
|
+
result = new Set([...obj].map((value) => cloneDeep(value)));
|
|
180
|
+
}
|
|
181
|
+
if (type == "Map") {
|
|
182
|
+
result = new Map([...obj].map((kv) => [cloneDeep(kv[0]), cloneDeep(kv[1])]));
|
|
183
|
+
}
|
|
184
|
+
if (type == "Date") {
|
|
185
|
+
result = new Date(obj.getTime());
|
|
186
|
+
}
|
|
187
|
+
if (type == "RegExp") {
|
|
188
|
+
result = RegExp(obj.source, getRegExpFlags(obj));
|
|
189
|
+
}
|
|
190
|
+
if (type == "Array" || type == "Object") {
|
|
191
|
+
result = Array.isArray(obj) ? [] : {};
|
|
192
|
+
for (let key in obj) {
|
|
193
|
+
result[key] = cloneDeep(obj[key]);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
function getRegExpFlags(regExp) {
|
|
199
|
+
if (typeof regExp.source.flags == "string") {
|
|
200
|
+
return regExp.source.flags;
|
|
201
|
+
} else {
|
|
202
|
+
let flags = [];
|
|
203
|
+
regExp.global && flags.push("g");
|
|
204
|
+
regExp.ignoreCase && flags.push("i");
|
|
205
|
+
regExp.multiline && flags.push("m");
|
|
206
|
+
regExp.sticky && flags.push("y");
|
|
207
|
+
regExp.unicode && flags.push("u");
|
|
208
|
+
return flags.join("");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function unwrapGetter(getter, value, index) {
|
|
212
|
+
if (getter instanceof Function) {
|
|
213
|
+
return getter(value, index ?? 0);
|
|
214
|
+
}
|
|
215
|
+
return getter;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/utils/debounce.ts
|
|
219
|
+
function debounce(func, wait, immediate) {
|
|
220
|
+
let timeout;
|
|
221
|
+
const debouncedFn = (...args) => new Promise((resolve) => {
|
|
222
|
+
clearTimeout(timeout);
|
|
223
|
+
timeout = setTimeout(() => {
|
|
224
|
+
timeout = void 0;
|
|
225
|
+
{
|
|
226
|
+
Promise.resolve(func.apply(this, [...args])).then(resolve);
|
|
227
|
+
}
|
|
228
|
+
}, wait);
|
|
229
|
+
});
|
|
230
|
+
debouncedFn.cancel = () => {
|
|
231
|
+
clearTimeout(timeout);
|
|
232
|
+
timeout = void 0;
|
|
233
|
+
};
|
|
234
|
+
return debouncedFn;
|
|
235
|
+
}
|
|
236
|
+
function versionCompare(current, other) {
|
|
237
|
+
const cp = String(current).split(".");
|
|
238
|
+
const op = String(other).split(".");
|
|
239
|
+
for (let depth = 0; depth < Math.min(cp.length, op.length); depth++) {
|
|
240
|
+
const cn = Number(cp[depth]);
|
|
241
|
+
const on = Number(op[depth]);
|
|
242
|
+
if (cn > on) return 1 /* GreaterThan */;
|
|
243
|
+
if (on > cn) return -1 /* LessThan */;
|
|
244
|
+
if (!isNaN(cn) && isNaN(on)) return 1 /* GreaterThan */;
|
|
245
|
+
if (isNaN(cn) && !isNaN(on)) return -1 /* LessThan */;
|
|
246
|
+
}
|
|
247
|
+
return 0 /* EqualTo */;
|
|
248
|
+
}
|
|
249
|
+
var isVueSuperiorOrEqualTo3dotFive = versionCompare(vue.version, "3.5.0") === -1 ? false : true;
|
|
250
|
+
|
|
251
|
+
// src/utils/randomId.ts
|
|
252
|
+
function uniqueIDNuxt() {
|
|
253
|
+
return Math.floor(Math.random() * Date.now()).toString();
|
|
254
|
+
}
|
|
255
|
+
function randomId() {
|
|
256
|
+
if (typeof window === "undefined") {
|
|
257
|
+
return uniqueIDNuxt();
|
|
258
|
+
} else {
|
|
259
|
+
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
260
|
+
return uint32.toString(10);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function useStorage() {
|
|
264
|
+
const ruleDeclStorage = vue.shallowRef(/* @__PURE__ */ new Map());
|
|
265
|
+
const fieldsStorage = vue.shallowRef(/* @__PURE__ */ new Map());
|
|
266
|
+
const collectionsStorage = vue.shallowRef(/* @__PURE__ */ new Map());
|
|
267
|
+
const dirtyStorage = vue.shallowRef(/* @__PURE__ */ new Map());
|
|
268
|
+
const ruleStatusStorage = vue.shallowRef(/* @__PURE__ */ new Map());
|
|
269
|
+
const arrayStatusStorage = vue.shallowRef(/* @__PURE__ */ new Map());
|
|
270
|
+
function getFieldsEntry($path) {
|
|
271
|
+
const existingFields = fieldsStorage.value.get($path);
|
|
272
|
+
if (existingFields) {
|
|
273
|
+
return existingFields;
|
|
274
|
+
} else {
|
|
275
|
+
const $fields = vue.ref({});
|
|
276
|
+
fieldsStorage.value.set($path, $fields);
|
|
277
|
+
return $fields;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function getCollectionsEntry($path) {
|
|
281
|
+
const existingEach = collectionsStorage.value.get($path);
|
|
282
|
+
if (existingEach) {
|
|
283
|
+
return existingEach;
|
|
284
|
+
} else {
|
|
285
|
+
const $each = vue.ref([]);
|
|
286
|
+
collectionsStorage.value.set($path, $each);
|
|
287
|
+
return $each;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function addArrayStatus($arrayId, itemId, value) {
|
|
291
|
+
arrayStatusStorage.value.set(`${$arrayId}-${itemId}`, value);
|
|
292
|
+
}
|
|
293
|
+
function getArrayStatus($arrayId, itemId) {
|
|
294
|
+
return arrayStatusStorage.value.get(`${$arrayId}-${itemId}`);
|
|
295
|
+
}
|
|
296
|
+
function deleteArrayStatus($arrayId, itemId) {
|
|
297
|
+
if ($arrayId && itemId != null) {
|
|
298
|
+
arrayStatusStorage.value.delete(`${$arrayId}-${itemId}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function setDirtyEntry($path, dirty) {
|
|
302
|
+
dirtyStorage.value.set($path, dirty);
|
|
303
|
+
}
|
|
304
|
+
function getDirtyState(path) {
|
|
305
|
+
return dirtyStorage.value.get(path) ?? false;
|
|
306
|
+
}
|
|
307
|
+
function addRuleDeclEntry($path, options) {
|
|
308
|
+
ruleDeclStorage.value.set($path, options);
|
|
309
|
+
}
|
|
310
|
+
function checkRuleDeclEntry($path, newRules) {
|
|
311
|
+
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
312
|
+
if (!storedRulesDefs) return void 0;
|
|
313
|
+
const storedRules = storedRulesDefs;
|
|
314
|
+
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
315
|
+
if (!isValidCache) return { valid: false };
|
|
316
|
+
return { valid: true };
|
|
317
|
+
}
|
|
318
|
+
function areRulesChanged(newRules, storedRules) {
|
|
319
|
+
const storedRulesKeys = Object.keys(storedRules);
|
|
320
|
+
const newRulesKeys = Object.keys(newRules);
|
|
321
|
+
if (newRulesKeys.length !== storedRulesKeys.length) return false;
|
|
322
|
+
const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
|
|
323
|
+
if (!hasAllValidators) return false;
|
|
324
|
+
return newRulesKeys.every((ruleKey) => {
|
|
325
|
+
const newRuleElement = newRules[ruleKey];
|
|
326
|
+
const storedRuleElement = storedRules[ruleKey];
|
|
327
|
+
if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
|
|
328
|
+
return false;
|
|
329
|
+
if (typeof newRuleElement === "number") {
|
|
330
|
+
return false;
|
|
331
|
+
} else if (typeof newRuleElement === "boolean") {
|
|
332
|
+
return false;
|
|
333
|
+
} else if (!newRuleElement._params) return true;
|
|
334
|
+
else {
|
|
335
|
+
return newRuleElement._params?.every((paramKey, index) => {
|
|
336
|
+
if (typeof storedRuleElement === "number" || typeof storedRuleElement === "boolean") {
|
|
337
|
+
return true;
|
|
338
|
+
} else {
|
|
339
|
+
const storedParams = unwrapRuleParameters(storedRuleElement._params);
|
|
340
|
+
const newParams = unwrapRuleParameters(newRuleElement._params);
|
|
341
|
+
return storedParams?.[index] === newParams?.[index];
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
function trySetRuleStatusRef(path) {
|
|
348
|
+
const ruleStatus = ruleStatusStorage.value.get(path);
|
|
349
|
+
if (ruleStatus) {
|
|
350
|
+
return ruleStatus;
|
|
351
|
+
} else {
|
|
352
|
+
const $pending = vue.ref(false);
|
|
353
|
+
const $valid = vue.ref(true);
|
|
354
|
+
const $metadata = vue.ref({});
|
|
355
|
+
const $validating = vue.ref(false);
|
|
356
|
+
ruleStatusStorage.value.set(path, { $pending, $valid, $metadata, $validating });
|
|
357
|
+
return { $pending, $valid, $metadata, $validating };
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
vue.onScopeDispose(() => {
|
|
361
|
+
ruleDeclStorage.value.clear();
|
|
362
|
+
fieldsStorage.value.clear();
|
|
363
|
+
collectionsStorage.value.clear();
|
|
364
|
+
dirtyStorage.value.clear();
|
|
365
|
+
ruleStatusStorage.value.clear();
|
|
366
|
+
arrayStatusStorage.value.clear();
|
|
367
|
+
});
|
|
368
|
+
return {
|
|
369
|
+
addRuleDeclEntry,
|
|
370
|
+
setDirtyEntry,
|
|
371
|
+
checkRuleDeclEntry,
|
|
372
|
+
getDirtyState,
|
|
373
|
+
trySetRuleStatusRef,
|
|
374
|
+
getFieldsEntry,
|
|
375
|
+
getCollectionsEntry,
|
|
376
|
+
getArrayStatus,
|
|
377
|
+
addArrayStatus,
|
|
378
|
+
deleteArrayStatus,
|
|
379
|
+
arrayStatusStorage
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ../shared/utils/isEmpty.ts
|
|
384
|
+
function isEmpty(value) {
|
|
385
|
+
if (value === void 0 || value === null) {
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
if (value instanceof Date) {
|
|
389
|
+
return isNaN(value.getTime());
|
|
390
|
+
}
|
|
391
|
+
if (Array.isArray(value)) {
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
if (typeof value === "object" && value != null) {
|
|
395
|
+
return Object.keys(value).length === 0;
|
|
396
|
+
}
|
|
397
|
+
return !String(value).length;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// src/core/useRegle/guards/ruleDef.guards.ts
|
|
401
|
+
function isNestedRulesDef(state, rules) {
|
|
402
|
+
return isObject(state.value) && isObject(rules.value) && !Object.entries(rules.value).some((rule) => isRuleDef(rule));
|
|
403
|
+
}
|
|
404
|
+
function isCollectionRulesDef(rules, state) {
|
|
405
|
+
return !!rules.value && "$each" in rules.value || Array.isArray(state.value);
|
|
406
|
+
}
|
|
407
|
+
function isValidatorRulesDef(rules) {
|
|
408
|
+
return !!rules.value && isObject(rules.value);
|
|
409
|
+
}
|
|
410
|
+
function isRuleDef(rule) {
|
|
411
|
+
return isObject(rule) && "_validator" in rule;
|
|
412
|
+
}
|
|
413
|
+
function isFormRuleDefinition(rule) {
|
|
414
|
+
return !(typeof rule.value === "function");
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// src/core/useRegle/guards/rule.status.guards.ts
|
|
418
|
+
function isNestedRulesStatus(rule) {
|
|
419
|
+
return isObject(rule) && "$fields" in rule;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/core/useRegle/useErrors.ts
|
|
423
|
+
function extractRulesErrors({
|
|
424
|
+
field,
|
|
425
|
+
silent = false
|
|
426
|
+
}) {
|
|
427
|
+
return Object.entries(field.$rules ?? {}).map(([ruleKey, rule]) => {
|
|
428
|
+
if (silent) {
|
|
429
|
+
return rule.$message;
|
|
430
|
+
} else if (!rule.$valid && field.$dirty && !rule.$validating) {
|
|
431
|
+
return rule.$message;
|
|
432
|
+
}
|
|
433
|
+
return null;
|
|
434
|
+
}).filter((msg) => !!msg).reduce((acc, value) => {
|
|
435
|
+
if (typeof value === "string") {
|
|
436
|
+
return acc?.concat([value]);
|
|
437
|
+
} else {
|
|
438
|
+
return acc?.concat(value);
|
|
439
|
+
}
|
|
440
|
+
}, []).concat(field.$dirty ? field.$externalErrors ?? [] : []);
|
|
441
|
+
}
|
|
442
|
+
function extractRulesTooltips({ field }) {
|
|
443
|
+
return Object.entries(field.$rules ?? {}).map(([ruleKey, rule]) => rule.$tooltip).filter((tooltip) => !!tooltip).reduce((acc, value) => {
|
|
444
|
+
if (typeof value === "string") {
|
|
445
|
+
return acc?.concat([value]);
|
|
446
|
+
} else {
|
|
447
|
+
return acc?.concat(value);
|
|
448
|
+
}
|
|
449
|
+
}, []);
|
|
450
|
+
}
|
|
451
|
+
function createReactiveRuleStatus({
|
|
452
|
+
fieldProperties,
|
|
453
|
+
customMessages,
|
|
454
|
+
rule,
|
|
455
|
+
ruleKey,
|
|
456
|
+
state,
|
|
457
|
+
path,
|
|
458
|
+
storage,
|
|
459
|
+
$debounce,
|
|
460
|
+
modifiers
|
|
461
|
+
}) {
|
|
462
|
+
let scope = vue.effectScope();
|
|
463
|
+
let scopeState = {};
|
|
464
|
+
let $unwatchState;
|
|
465
|
+
const $haveAsync = vue.ref(false);
|
|
466
|
+
const { $pending, $valid, $metadata, $validating } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
|
|
467
|
+
function $watch() {
|
|
468
|
+
scope = vue.effectScope();
|
|
469
|
+
scopeState = scope.run(() => {
|
|
470
|
+
const $defaultMetadata = vue.computed(() => ({
|
|
471
|
+
$value: state.value,
|
|
472
|
+
$error: fieldProperties.$error.value,
|
|
473
|
+
$dirty: fieldProperties.$dirty.value,
|
|
474
|
+
$pending: fieldProperties.$pending.value,
|
|
475
|
+
$valid: fieldProperties.$valid.value,
|
|
476
|
+
$invalid: fieldProperties.$invalid.value,
|
|
477
|
+
$rule: {
|
|
478
|
+
$valid: $valid.value,
|
|
479
|
+
$invalid: !$valid.value,
|
|
480
|
+
$pending: $pending.value
|
|
481
|
+
},
|
|
482
|
+
$params: $params.value,
|
|
483
|
+
...$metadata.value
|
|
484
|
+
}));
|
|
485
|
+
const $active = vue.computed(() => {
|
|
486
|
+
if (isFormRuleDefinition(rule)) {
|
|
487
|
+
if (typeof rule.value.active === "function") {
|
|
488
|
+
return rule.value.active($defaultMetadata.value);
|
|
489
|
+
} else {
|
|
490
|
+
return !!rule.value.active;
|
|
491
|
+
}
|
|
492
|
+
} else {
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
function computeRuleProcessor(key) {
|
|
497
|
+
let result = "";
|
|
498
|
+
const customProcessor = customMessages ? customMessages[ruleKey]?.[key] : void 0;
|
|
499
|
+
if (customProcessor) {
|
|
500
|
+
if (typeof customProcessor === "function") {
|
|
501
|
+
result = customProcessor($defaultMetadata.value);
|
|
502
|
+
} else {
|
|
503
|
+
result = customProcessor;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (isFormRuleDefinition(rule)) {
|
|
507
|
+
const patchedKey = `_${key}_patched`;
|
|
508
|
+
if (!(customProcessor && !rule.value[patchedKey])) {
|
|
509
|
+
if (typeof rule.value[key] === "function") {
|
|
510
|
+
result = rule.value[key]($defaultMetadata.value);
|
|
511
|
+
} else {
|
|
512
|
+
result = rule.value[key] ?? "";
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
const $message = vue.computed(() => {
|
|
519
|
+
let message = computeRuleProcessor("message");
|
|
520
|
+
if (isEmpty(message)) {
|
|
521
|
+
message = "This field is not valid";
|
|
522
|
+
}
|
|
523
|
+
return message;
|
|
524
|
+
});
|
|
525
|
+
const $tooltip = vue.computed(() => {
|
|
526
|
+
return computeRuleProcessor("tooltip");
|
|
527
|
+
});
|
|
528
|
+
const $type = vue.computed(() => {
|
|
529
|
+
if (isFormRuleDefinition(rule) && rule.value.type) {
|
|
530
|
+
return rule.value.type;
|
|
531
|
+
} else {
|
|
532
|
+
return ruleKey;
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
const $validator = vue.computed(() => {
|
|
536
|
+
if (isFormRuleDefinition(rule)) {
|
|
537
|
+
return rule.value.validator;
|
|
538
|
+
} else {
|
|
539
|
+
return rule.value;
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
const $params = vue.computed(() => {
|
|
543
|
+
if (typeof rule.value === "function") {
|
|
544
|
+
return [];
|
|
545
|
+
}
|
|
546
|
+
return unwrapRuleParameters(rule.value._params ?? []);
|
|
547
|
+
});
|
|
548
|
+
const $path = vue.computed(() => `${path}.${$type.value}`);
|
|
549
|
+
return {
|
|
550
|
+
$active,
|
|
551
|
+
$message,
|
|
552
|
+
$type,
|
|
553
|
+
$validator,
|
|
554
|
+
$params,
|
|
555
|
+
$path,
|
|
556
|
+
$tooltip
|
|
557
|
+
};
|
|
558
|
+
});
|
|
559
|
+
$unwatchState = vue.watch(scopeState?.$params, () => {
|
|
560
|
+
if (modifiers.$autoDirty.value || modifiers.$rewardEarly.value && fieldProperties.$error.value) {
|
|
561
|
+
$validate();
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
$watch();
|
|
566
|
+
function updatePendingState() {
|
|
567
|
+
$valid.value = true;
|
|
568
|
+
if (fieldProperties.$dirty.value) {
|
|
569
|
+
$pending.value = true;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
async function computeAsyncResult() {
|
|
573
|
+
let ruleResult = false;
|
|
574
|
+
try {
|
|
575
|
+
const validator = scopeState.$validator.value;
|
|
576
|
+
const resultOrPromise = validator(state.value, ...scopeState.$params.value);
|
|
577
|
+
let cachedValue = state.value;
|
|
578
|
+
updatePendingState();
|
|
579
|
+
let validatorResult;
|
|
580
|
+
if (resultOrPromise instanceof Promise) {
|
|
581
|
+
validatorResult = await resultOrPromise;
|
|
582
|
+
} else {
|
|
583
|
+
validatorResult = resultOrPromise;
|
|
584
|
+
}
|
|
585
|
+
if (state.value !== cachedValue) {
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
if (typeof validatorResult === "boolean") {
|
|
589
|
+
ruleResult = validatorResult;
|
|
590
|
+
} else {
|
|
591
|
+
const { $valid: $valid2, ...rest } = validatorResult;
|
|
592
|
+
ruleResult = $valid2;
|
|
593
|
+
$metadata.value = rest;
|
|
594
|
+
}
|
|
595
|
+
} catch (e) {
|
|
596
|
+
ruleResult = false;
|
|
597
|
+
} finally {
|
|
598
|
+
$pending.value = false;
|
|
599
|
+
}
|
|
600
|
+
return ruleResult;
|
|
601
|
+
}
|
|
602
|
+
const $computeAsyncDebounce = debounce(computeAsyncResult, $debounce ?? 200);
|
|
603
|
+
async function $validate() {
|
|
604
|
+
try {
|
|
605
|
+
$validating.value = true;
|
|
606
|
+
let ruleResult = false;
|
|
607
|
+
if (isRuleDef(rule.value) && rule.value._async) {
|
|
608
|
+
ruleResult = await $computeAsyncDebounce();
|
|
609
|
+
} else {
|
|
610
|
+
const validator = scopeState.$validator.value;
|
|
611
|
+
const resultOrPromise = validator(state.value, ...scopeState.$params.value);
|
|
612
|
+
if (resultOrPromise instanceof Promise) {
|
|
613
|
+
console.warn(
|
|
614
|
+
'You used a async validator function on a non-async rule, please use "async await" or the "withAsync" helper'
|
|
615
|
+
);
|
|
616
|
+
} else {
|
|
617
|
+
if (resultOrPromise != null) {
|
|
618
|
+
if (typeof resultOrPromise === "boolean") {
|
|
619
|
+
ruleResult = resultOrPromise;
|
|
620
|
+
} else {
|
|
621
|
+
const { $valid: $valid2, ...rest } = resultOrPromise;
|
|
622
|
+
ruleResult = $valid2;
|
|
623
|
+
$metadata.value = rest;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
$valid.value = ruleResult;
|
|
629
|
+
return ruleResult;
|
|
630
|
+
} catch (e) {
|
|
631
|
+
return false;
|
|
632
|
+
} finally {
|
|
633
|
+
$validating.value = false;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
function $reset() {
|
|
637
|
+
$valid.value = true;
|
|
638
|
+
$metadata.value = {};
|
|
639
|
+
$pending.value = false;
|
|
640
|
+
$validating.value = false;
|
|
641
|
+
$watch();
|
|
642
|
+
}
|
|
643
|
+
function $unwatch() {
|
|
644
|
+
$unwatchState();
|
|
645
|
+
scope.stop();
|
|
646
|
+
scope = vue.effectScope();
|
|
647
|
+
}
|
|
648
|
+
return vue.reactive({
|
|
649
|
+
...scopeState,
|
|
650
|
+
$pending,
|
|
651
|
+
$valid,
|
|
652
|
+
$metadata,
|
|
653
|
+
$haveAsync,
|
|
654
|
+
$validating,
|
|
655
|
+
$validate,
|
|
656
|
+
$unwatch,
|
|
657
|
+
$watch,
|
|
658
|
+
$reset
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
|
|
663
|
+
function createReactiveFieldStatus({
|
|
664
|
+
state,
|
|
665
|
+
rulesDef,
|
|
666
|
+
customMessages,
|
|
667
|
+
path,
|
|
668
|
+
fieldName,
|
|
669
|
+
storage,
|
|
670
|
+
options,
|
|
671
|
+
externalErrors,
|
|
672
|
+
onUnwatch,
|
|
673
|
+
$isArray,
|
|
674
|
+
initialState,
|
|
675
|
+
shortcuts
|
|
676
|
+
}) {
|
|
677
|
+
let scope = vue.effectScope();
|
|
678
|
+
let scopeState;
|
|
679
|
+
let fieldScopes = [];
|
|
680
|
+
let $unwatchState;
|
|
681
|
+
let $unwatchValid;
|
|
682
|
+
let $unwatchDirty;
|
|
683
|
+
let $unwatchAsync;
|
|
684
|
+
let $commit = () => {
|
|
685
|
+
};
|
|
686
|
+
function createReactiveRulesResult() {
|
|
687
|
+
const declaredRules = rulesDef.value;
|
|
688
|
+
const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
|
|
689
|
+
$localOptions.value = Object.fromEntries(
|
|
690
|
+
Object.entries(declaredRules).filter(([ruleKey]) => ruleKey.startsWith("$"))
|
|
691
|
+
);
|
|
692
|
+
$watch();
|
|
693
|
+
$rules.value = Object.fromEntries(
|
|
694
|
+
Object.entries(rulesDef.value).filter(([ruleKey]) => !ruleKey.startsWith("$")).map(([ruleKey, rule]) => {
|
|
695
|
+
if (rule) {
|
|
696
|
+
const ruleRef = vue.toRef(() => rule);
|
|
697
|
+
return [
|
|
698
|
+
ruleKey,
|
|
699
|
+
createReactiveRuleStatus({
|
|
700
|
+
fieldProperties: {
|
|
701
|
+
$dirty: scopeState.$dirty,
|
|
702
|
+
$error: scopeState.$error,
|
|
703
|
+
$invalid: scopeState.$invalid,
|
|
704
|
+
$pending: scopeState.$pending,
|
|
705
|
+
$valid: scopeState.$valid
|
|
706
|
+
},
|
|
707
|
+
modifiers: {
|
|
708
|
+
$autoDirty: scopeState.$autoDirty,
|
|
709
|
+
$rewardEarly: scopeState.$rewardEarly
|
|
710
|
+
},
|
|
711
|
+
customMessages,
|
|
712
|
+
rule: ruleRef,
|
|
713
|
+
ruleKey,
|
|
714
|
+
state,
|
|
715
|
+
path,
|
|
716
|
+
storage,
|
|
717
|
+
$debounce: $localOptions.value.$debounce
|
|
718
|
+
})
|
|
719
|
+
];
|
|
720
|
+
}
|
|
721
|
+
return [];
|
|
722
|
+
}).filter((ruleDef) => !!ruleDef.length)
|
|
723
|
+
);
|
|
724
|
+
scopeState.processShortcuts();
|
|
725
|
+
define$commit();
|
|
726
|
+
if (storeResult?.valid != null) {
|
|
727
|
+
scopeState.$dirty.value = storage.getDirtyState(path);
|
|
728
|
+
if (scopeState.$dirty.value) {
|
|
729
|
+
$commit();
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
storage.addRuleDeclEntry(path, declaredRules);
|
|
733
|
+
}
|
|
734
|
+
function define$commit() {
|
|
735
|
+
$commit = scopeState.$debounce.value ? debounce($commitHandler, scopeState.$debounce.value ?? scopeState.$haveAnyAsyncRule ? 100 : 0) : $commitHandler;
|
|
736
|
+
}
|
|
737
|
+
function $unwatch() {
|
|
738
|
+
if ($rules.value) {
|
|
739
|
+
Object.entries($rules.value).forEach(([_, rule]) => {
|
|
740
|
+
rule.$unwatch();
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
$unwatchDirty();
|
|
744
|
+
if (scopeState.$dirty.value) {
|
|
745
|
+
storage.setDirtyEntry(path, scopeState.$dirty.value);
|
|
746
|
+
}
|
|
747
|
+
$unwatchState?.();
|
|
748
|
+
$unwatchValid?.();
|
|
749
|
+
scope.stop();
|
|
750
|
+
scope = vue.effectScope();
|
|
751
|
+
fieldScopes.forEach((s) => s.stop());
|
|
752
|
+
fieldScopes = [];
|
|
753
|
+
onUnwatch?.();
|
|
754
|
+
$unwatchAsync?.();
|
|
755
|
+
}
|
|
756
|
+
function $watch() {
|
|
757
|
+
if ($rules.value) {
|
|
758
|
+
Object.entries($rules.value).forEach(([_, rule]) => {
|
|
759
|
+
rule.$watch();
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
scopeState = scope.run(() => {
|
|
763
|
+
const $dirty = vue.ref(false);
|
|
764
|
+
const triggerPunishment = vue.ref(false);
|
|
765
|
+
const $anyDirty = vue.computed(() => $dirty.value);
|
|
766
|
+
const $debounce2 = vue.computed(() => {
|
|
767
|
+
return $localOptions.value.$debounce;
|
|
768
|
+
});
|
|
769
|
+
const $lazy2 = vue.computed(() => {
|
|
770
|
+
if ($localOptions.value.$lazy != null) {
|
|
771
|
+
return $localOptions.value.$lazy;
|
|
772
|
+
}
|
|
773
|
+
return vue.unref(options.lazy);
|
|
774
|
+
});
|
|
775
|
+
const $rewardEarly2 = vue.computed(() => {
|
|
776
|
+
if ($autoDirty2.value === true) {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
if ($localOptions.value.$rewardEarly != null) {
|
|
780
|
+
return $localOptions.value.$rewardEarly;
|
|
781
|
+
}
|
|
782
|
+
return vue.unref(options.rewardEarly);
|
|
783
|
+
});
|
|
784
|
+
const $clearExternalErrorsOnChange2 = vue.computed(() => {
|
|
785
|
+
if ($localOptions.value.$clearExternalErrorsOnChange != null) {
|
|
786
|
+
return $localOptions.value.$clearExternalErrorsOnChange;
|
|
787
|
+
}
|
|
788
|
+
return vue.unref(options.clearExternalErrorsOnChange);
|
|
789
|
+
});
|
|
790
|
+
const $autoDirty2 = vue.computed(() => {
|
|
791
|
+
if ($localOptions.value.$autoDirty != null) {
|
|
792
|
+
return $localOptions.value.$autoDirty;
|
|
793
|
+
}
|
|
794
|
+
return vue.unref(options.autoDirty);
|
|
795
|
+
});
|
|
796
|
+
const $validating2 = vue.computed(() => {
|
|
797
|
+
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
798
|
+
return ruleResult.$validating;
|
|
799
|
+
});
|
|
800
|
+
});
|
|
801
|
+
const $error = vue.computed(() => {
|
|
802
|
+
return $invalid.value && !$pending.value && $dirty.value;
|
|
803
|
+
});
|
|
804
|
+
const $errors = vue.computed(() => {
|
|
805
|
+
if ($error.value) {
|
|
806
|
+
return extractRulesErrors({
|
|
807
|
+
field: {
|
|
808
|
+
$dirty: $dirty.value,
|
|
809
|
+
$externalErrors: externalErrors?.value,
|
|
810
|
+
$rules: $rules.value
|
|
811
|
+
}
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
return [];
|
|
815
|
+
});
|
|
816
|
+
const $tooltips = vue.computed(() => {
|
|
817
|
+
return extractRulesTooltips({
|
|
818
|
+
field: {
|
|
819
|
+
$rules: $rules.value
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
});
|
|
823
|
+
const $silentErrors = vue.computed(() => {
|
|
824
|
+
return extractRulesErrors({
|
|
825
|
+
field: {
|
|
826
|
+
$dirty: $dirty.value,
|
|
827
|
+
$externalErrors: externalErrors?.value,
|
|
828
|
+
$rules: $rules.value
|
|
829
|
+
},
|
|
830
|
+
silent: true
|
|
831
|
+
});
|
|
832
|
+
});
|
|
833
|
+
const $ready = vue.computed(() => {
|
|
834
|
+
return !($invalid.value || $pending.value);
|
|
835
|
+
});
|
|
836
|
+
const $pending = vue.computed(() => {
|
|
837
|
+
if (triggerPunishment.value || !$rewardEarly2.value) {
|
|
838
|
+
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
839
|
+
return ruleResult.$pending;
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
return false;
|
|
843
|
+
});
|
|
844
|
+
const $invalid = vue.computed(() => {
|
|
845
|
+
if (externalErrors?.value?.length) {
|
|
846
|
+
return true;
|
|
847
|
+
} else if (isEmpty($rules.value)) {
|
|
848
|
+
return false;
|
|
849
|
+
} else if (!$rewardEarly2.value || $rewardEarly2.value && triggerPunishment.value) {
|
|
850
|
+
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
851
|
+
return !ruleResult.$valid;
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
return false;
|
|
855
|
+
});
|
|
856
|
+
const $name = vue.computed(() => fieldName);
|
|
857
|
+
const $valid = vue.computed(() => {
|
|
858
|
+
if (externalErrors?.value?.length) {
|
|
859
|
+
return false;
|
|
860
|
+
} else if (isEmpty($rules.value)) {
|
|
861
|
+
return false;
|
|
862
|
+
} else if ($dirty.value && !isEmpty(state.value) && !$validating2.value) {
|
|
863
|
+
return Object.values($rules.value).every((ruleResult) => {
|
|
864
|
+
return ruleResult.$valid && ruleResult.$active;
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
return false;
|
|
868
|
+
});
|
|
869
|
+
const $haveAnyAsyncRule2 = vue.computed(() => {
|
|
870
|
+
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
871
|
+
return ruleResult.$haveAsync;
|
|
872
|
+
});
|
|
873
|
+
});
|
|
874
|
+
function processShortcuts() {
|
|
875
|
+
if (shortcuts?.fields) {
|
|
876
|
+
Object.entries(shortcuts.fields).forEach(([key, value]) => {
|
|
877
|
+
const scope2 = vue.effectScope();
|
|
878
|
+
$shortcuts2[key] = scope2.run(() => {
|
|
879
|
+
const result = vue.ref();
|
|
880
|
+
vue.watchEffect(() => {
|
|
881
|
+
result.value = value(
|
|
882
|
+
vue.reactive({
|
|
883
|
+
$dirty,
|
|
884
|
+
$externalErrors: externalErrors?.value ?? [],
|
|
885
|
+
$value: state,
|
|
886
|
+
$rules,
|
|
887
|
+
$error,
|
|
888
|
+
$pending,
|
|
889
|
+
$invalid,
|
|
890
|
+
$valid,
|
|
891
|
+
$errors,
|
|
892
|
+
$ready,
|
|
893
|
+
$silentErrors,
|
|
894
|
+
$anyDirty,
|
|
895
|
+
$tooltips,
|
|
896
|
+
$name
|
|
897
|
+
})
|
|
898
|
+
);
|
|
899
|
+
});
|
|
900
|
+
return result;
|
|
901
|
+
});
|
|
902
|
+
fieldScopes.push(scope2);
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
const $shortcuts2 = {};
|
|
907
|
+
vue.watch($valid, (value) => {
|
|
908
|
+
if (value) {
|
|
909
|
+
triggerPunishment.value = false;
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
return {
|
|
913
|
+
$error,
|
|
914
|
+
$pending,
|
|
915
|
+
$invalid,
|
|
916
|
+
$valid,
|
|
917
|
+
$debounce: $debounce2,
|
|
918
|
+
$lazy: $lazy2,
|
|
919
|
+
$errors,
|
|
920
|
+
$ready,
|
|
921
|
+
$silentErrors,
|
|
922
|
+
$rewardEarly: $rewardEarly2,
|
|
923
|
+
$autoDirty: $autoDirty2,
|
|
924
|
+
$clearExternalErrorsOnChange: $clearExternalErrorsOnChange2,
|
|
925
|
+
$anyDirty,
|
|
926
|
+
$name,
|
|
927
|
+
$haveAnyAsyncRule: $haveAnyAsyncRule2,
|
|
928
|
+
$shortcuts: $shortcuts2,
|
|
929
|
+
$validating: $validating2,
|
|
930
|
+
$tooltips,
|
|
931
|
+
$dirty,
|
|
932
|
+
triggerPunishment,
|
|
933
|
+
processShortcuts
|
|
934
|
+
};
|
|
935
|
+
});
|
|
936
|
+
$unwatchState = vue.watch(
|
|
937
|
+
state,
|
|
938
|
+
() => {
|
|
939
|
+
if (scopeState.$autoDirty.value) {
|
|
940
|
+
if (!scopeState.$dirty.value) {
|
|
941
|
+
scopeState.$dirty.value = true;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
if (rulesDef.value instanceof Function) {
|
|
945
|
+
createReactiveRulesResult();
|
|
946
|
+
}
|
|
947
|
+
if (scopeState.$autoDirty.value || scopeState.$rewardEarly.value && scopeState.$error.value) {
|
|
948
|
+
$commit();
|
|
949
|
+
}
|
|
950
|
+
if (scopeState.$rewardEarly.value !== true && scopeState.$clearExternalErrorsOnChange.value) {
|
|
951
|
+
$clearExternalErrors();
|
|
952
|
+
}
|
|
953
|
+
},
|
|
954
|
+
{ deep: $isArray ? true : isVueSuperiorOrEqualTo3dotFive ? 1 : true }
|
|
955
|
+
);
|
|
956
|
+
$unwatchDirty = vue.watch(scopeState.$dirty, () => {
|
|
957
|
+
storage.setDirtyEntry(path, scopeState.$dirty.value);
|
|
958
|
+
});
|
|
959
|
+
$unwatchValid = vue.watch(scopeState.$valid, (valid) => {
|
|
960
|
+
if (scopeState.$rewardEarly.value && valid) {
|
|
961
|
+
scopeState.triggerPunishment.value = false;
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
$unwatchAsync = vue.watch(scopeState.$haveAnyAsyncRule, define$commit);
|
|
965
|
+
}
|
|
966
|
+
function $commitHandler() {
|
|
967
|
+
Object.values($rules.value).forEach((rule) => {
|
|
968
|
+
rule.$validate();
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
const $rules = vue.ref({});
|
|
972
|
+
const $localOptions = vue.ref({});
|
|
973
|
+
createReactiveRulesResult();
|
|
974
|
+
function $reset() {
|
|
975
|
+
$clearExternalErrors();
|
|
976
|
+
scopeState.$dirty.value = false;
|
|
977
|
+
storage.setDirtyEntry(path, false);
|
|
978
|
+
Object.entries($rules.value).forEach(([key, rule]) => {
|
|
979
|
+
rule.$reset();
|
|
980
|
+
});
|
|
981
|
+
if (!scopeState.$lazy.value && scopeState.$autoDirty.value) {
|
|
982
|
+
Object.values($rules.value).forEach((rule) => {
|
|
983
|
+
return rule.$validate();
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
function $touch(runCommit = true, withConditions = false) {
|
|
988
|
+
if (!scopeState.$dirty.value) {
|
|
989
|
+
scopeState.$dirty.value = true;
|
|
990
|
+
}
|
|
991
|
+
if (withConditions && runCommit) {
|
|
992
|
+
if (scopeState.$autoDirty.value || scopeState.$rewardEarly.value && scopeState.$error.value) {
|
|
993
|
+
$commit();
|
|
994
|
+
}
|
|
995
|
+
} else if (runCommit) {
|
|
996
|
+
$commit();
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
async function $validate() {
|
|
1000
|
+
try {
|
|
1001
|
+
const data = state.value;
|
|
1002
|
+
scopeState.triggerPunishment.value = true;
|
|
1003
|
+
if (!scopeState.$dirty.value) {
|
|
1004
|
+
scopeState.$dirty.value = true;
|
|
1005
|
+
} else if (scopeState.$autoDirty.value && scopeState.$dirty.value && !scopeState.$pending.value) {
|
|
1006
|
+
return { result: !scopeState.$error.value, data };
|
|
1007
|
+
}
|
|
1008
|
+
if (isEmpty($rules.value)) {
|
|
1009
|
+
return { result: true, data };
|
|
1010
|
+
}
|
|
1011
|
+
const results = await Promise.allSettled(
|
|
1012
|
+
Object.entries($rules.value).map(([key, rule]) => {
|
|
1013
|
+
return rule.$validate();
|
|
1014
|
+
})
|
|
1015
|
+
);
|
|
1016
|
+
const validationResults = results.every((value) => {
|
|
1017
|
+
if (value.status === "fulfilled") {
|
|
1018
|
+
return value.value === true;
|
|
1019
|
+
} else {
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
return { result: validationResults, data };
|
|
1024
|
+
} catch (e) {
|
|
1025
|
+
return { result: false, data: state.value };
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
function $resetAll() {
|
|
1029
|
+
$unwatch();
|
|
1030
|
+
state.value = cloneDeep(initialState);
|
|
1031
|
+
$reset();
|
|
1032
|
+
}
|
|
1033
|
+
function $extractDirtyFields(filterNullishValues = true) {
|
|
1034
|
+
if (scopeState.$dirty.value) {
|
|
1035
|
+
return state.value;
|
|
1036
|
+
}
|
|
1037
|
+
if (filterNullishValues) {
|
|
1038
|
+
return { _null: true };
|
|
1039
|
+
}
|
|
1040
|
+
return null;
|
|
1041
|
+
}
|
|
1042
|
+
function $clearExternalErrors() {
|
|
1043
|
+
if (externalErrors?.value?.length) {
|
|
1044
|
+
externalErrors.value = [];
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
if (!scopeState.$lazy.value && !scopeState.$dirty.value && scopeState.$autoDirty.value) {
|
|
1048
|
+
$commit();
|
|
1049
|
+
}
|
|
1050
|
+
const {
|
|
1051
|
+
$shortcuts,
|
|
1052
|
+
$validating,
|
|
1053
|
+
$autoDirty,
|
|
1054
|
+
$rewardEarly,
|
|
1055
|
+
$clearExternalErrorsOnChange,
|
|
1056
|
+
$haveAnyAsyncRule,
|
|
1057
|
+
$debounce,
|
|
1058
|
+
$lazy,
|
|
1059
|
+
...restScope
|
|
1060
|
+
} = scopeState;
|
|
1061
|
+
return vue.reactive({
|
|
1062
|
+
...restScope,
|
|
1063
|
+
$externalErrors: externalErrors,
|
|
1064
|
+
$value: state,
|
|
1065
|
+
$rules,
|
|
1066
|
+
...$shortcuts,
|
|
1067
|
+
$reset,
|
|
1068
|
+
$touch,
|
|
1069
|
+
$validate,
|
|
1070
|
+
$unwatch,
|
|
1071
|
+
$watch,
|
|
1072
|
+
$resetAll,
|
|
1073
|
+
$extractDirtyFields,
|
|
1074
|
+
$clearExternalErrors
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
function createCollectionElement({
|
|
1078
|
+
$id,
|
|
1079
|
+
path,
|
|
1080
|
+
index,
|
|
1081
|
+
options,
|
|
1082
|
+
storage,
|
|
1083
|
+
stateValue,
|
|
1084
|
+
customMessages,
|
|
1085
|
+
rules,
|
|
1086
|
+
externalErrors,
|
|
1087
|
+
initialState,
|
|
1088
|
+
shortcuts,
|
|
1089
|
+
fieldName
|
|
1090
|
+
}) {
|
|
1091
|
+
const $fieldId = rules.$key ? rules.$key : randomId();
|
|
1092
|
+
let $path = `${path}.${String($fieldId)}`;
|
|
1093
|
+
if (typeof stateValue.value === "object" && stateValue.value != null) {
|
|
1094
|
+
if (!stateValue.value.$id) {
|
|
1095
|
+
Object.defineProperties(stateValue.value, {
|
|
1096
|
+
$id: {
|
|
1097
|
+
value: $fieldId,
|
|
1098
|
+
enumerable: false,
|
|
1099
|
+
configurable: false,
|
|
1100
|
+
writable: false
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
} else {
|
|
1104
|
+
$path = `${path}.${stateValue.value.$id}`;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
const $status = createReactiveChildrenStatus({
|
|
1108
|
+
state: stateValue,
|
|
1109
|
+
rulesDef: vue.toRef(() => rules),
|
|
1110
|
+
customMessages,
|
|
1111
|
+
path: $path,
|
|
1112
|
+
storage,
|
|
1113
|
+
options,
|
|
1114
|
+
externalErrors: vue.toRef(externalErrors?.value ?? [], index),
|
|
1115
|
+
initialState: initialState?.[index],
|
|
1116
|
+
shortcuts,
|
|
1117
|
+
fieldName
|
|
1118
|
+
});
|
|
1119
|
+
if ($status) {
|
|
1120
|
+
const valueId = stateValue.value?.$id;
|
|
1121
|
+
$status.$id = valueId ?? String($fieldId);
|
|
1122
|
+
storage.addArrayStatus($id, $status.$id, $status);
|
|
1123
|
+
}
|
|
1124
|
+
return $status;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
// src/core/useRegle/useStateProperties/collections/createReactiveCollectionRoot.ts
|
|
1128
|
+
function createReactiveCollectionStatus({
|
|
1129
|
+
state,
|
|
1130
|
+
rulesDef,
|
|
1131
|
+
customMessages,
|
|
1132
|
+
path,
|
|
1133
|
+
storage,
|
|
1134
|
+
options,
|
|
1135
|
+
externalErrors,
|
|
1136
|
+
initialState,
|
|
1137
|
+
shortcuts,
|
|
1138
|
+
fieldName
|
|
1139
|
+
}) {
|
|
1140
|
+
let scope = vue.effectScope();
|
|
1141
|
+
let scopeState;
|
|
1142
|
+
let immediateScope = vue.effectScope();
|
|
1143
|
+
let immediateScopeState;
|
|
1144
|
+
let collectionScopes = [];
|
|
1145
|
+
if (!Array.isArray(state.value) && !rulesDef.value.$each) {
|
|
1146
|
+
return null;
|
|
1147
|
+
}
|
|
1148
|
+
const $id = vue.ref();
|
|
1149
|
+
const $value = vue.ref(state.value);
|
|
1150
|
+
let $unwatchState;
|
|
1151
|
+
const $fieldStatus = vue.ref({});
|
|
1152
|
+
const $eachStatus = storage.getCollectionsEntry(path);
|
|
1153
|
+
immediateScopeState = immediateScope.run(() => {
|
|
1154
|
+
const isPrimitiveArray = vue.computed(() => {
|
|
1155
|
+
if (Array.isArray(state.value) && state.value.length) {
|
|
1156
|
+
return state.value.some((s) => typeof s !== "object");
|
|
1157
|
+
} else if (rulesDef.value.$each && !(rulesDef.value.$each instanceof Function)) {
|
|
1158
|
+
return Object.values(rulesDef.value.$each).every((rule) => isRuleDef(rule));
|
|
1159
|
+
}
|
|
1160
|
+
return false;
|
|
1161
|
+
});
|
|
1162
|
+
return {
|
|
1163
|
+
isPrimitiveArray
|
|
1164
|
+
};
|
|
1165
|
+
});
|
|
1166
|
+
createStatus();
|
|
1167
|
+
$watch();
|
|
1168
|
+
function createStatus() {
|
|
1169
|
+
if (typeof state.value === "object") {
|
|
1170
|
+
if (state.value != null && !state.value?.$id && state.value !== null) {
|
|
1171
|
+
$id.value = randomId();
|
|
1172
|
+
Object.defineProperties(state.value, {
|
|
1173
|
+
$id: {
|
|
1174
|
+
value: $id.value,
|
|
1175
|
+
enumerable: false,
|
|
1176
|
+
configurable: false,
|
|
1177
|
+
writable: false
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
} else if (state.value?.$id) {
|
|
1181
|
+
$id.value = state.value.$id;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
if (immediateScopeState.isPrimitiveArray.value) {
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
$value.value = $fieldStatus.value.$value;
|
|
1188
|
+
if (Array.isArray(state.value)) {
|
|
1189
|
+
$eachStatus.value = state.value.map((value, index) => {
|
|
1190
|
+
const unwrapped$Each = unwrapGetter(
|
|
1191
|
+
rulesDef.value.$each,
|
|
1192
|
+
vue.toRef(() => value),
|
|
1193
|
+
index
|
|
1194
|
+
);
|
|
1195
|
+
const element = createCollectionElement({
|
|
1196
|
+
$id: $id.value,
|
|
1197
|
+
path,
|
|
1198
|
+
customMessages,
|
|
1199
|
+
rules: unwrapped$Each ?? {},
|
|
1200
|
+
stateValue: vue.toRef(() => value),
|
|
1201
|
+
index,
|
|
1202
|
+
options,
|
|
1203
|
+
storage,
|
|
1204
|
+
externalErrors: vue.toRef(externalErrors?.value ?? {}, `$each`),
|
|
1205
|
+
initialState: initialState?.[index],
|
|
1206
|
+
shortcuts,
|
|
1207
|
+
fieldName
|
|
1208
|
+
});
|
|
1209
|
+
if (element) {
|
|
1210
|
+
return element;
|
|
1211
|
+
}
|
|
1212
|
+
return null;
|
|
1213
|
+
}).filter((each) => !!each);
|
|
1214
|
+
} else {
|
|
1215
|
+
$eachStatus.value = [];
|
|
1216
|
+
}
|
|
1217
|
+
$fieldStatus.value = createReactiveFieldStatus({
|
|
1218
|
+
state,
|
|
1219
|
+
rulesDef,
|
|
1220
|
+
customMessages,
|
|
1221
|
+
path,
|
|
1222
|
+
storage,
|
|
1223
|
+
options,
|
|
1224
|
+
externalErrors: vue.toRef(externalErrors?.value ?? {}, `$self`),
|
|
1225
|
+
$isArray: true,
|
|
1226
|
+
initialState,
|
|
1227
|
+
shortcuts,
|
|
1228
|
+
fieldName
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
function updateStatus() {
|
|
1232
|
+
if (Array.isArray(state.value)) {
|
|
1233
|
+
const previousStatus = cloneDeep($eachStatus.value);
|
|
1234
|
+
$eachStatus.value = state.value.map((value, index) => {
|
|
1235
|
+
const currentValue = vue.toRef(() => value);
|
|
1236
|
+
if (value.$id && $eachStatus.value.find((each) => each.$id === value.$id)) {
|
|
1237
|
+
const existingStatus = storage.getArrayStatus($id.value, value.$id);
|
|
1238
|
+
if (existingStatus) {
|
|
1239
|
+
existingStatus.$value = currentValue;
|
|
1240
|
+
return existingStatus;
|
|
1241
|
+
}
|
|
1242
|
+
return null;
|
|
1243
|
+
} else {
|
|
1244
|
+
const unwrapped$Each = unwrapGetter(rulesDef.value.$each, currentValue, index);
|
|
1245
|
+
if (unwrapped$Each) {
|
|
1246
|
+
const element = createCollectionElement({
|
|
1247
|
+
$id: $id.value,
|
|
1248
|
+
path,
|
|
1249
|
+
customMessages,
|
|
1250
|
+
rules: unwrapped$Each,
|
|
1251
|
+
stateValue: currentValue,
|
|
1252
|
+
index,
|
|
1253
|
+
options,
|
|
1254
|
+
storage,
|
|
1255
|
+
externalErrors: vue.toRef(externalErrors?.value ?? {}, `$each`),
|
|
1256
|
+
initialState: initialState?.[index],
|
|
1257
|
+
shortcuts,
|
|
1258
|
+
fieldName
|
|
1259
|
+
});
|
|
1260
|
+
if (element) {
|
|
1261
|
+
return element;
|
|
1262
|
+
}
|
|
1263
|
+
return null;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
}).filter((each) => !!each);
|
|
1267
|
+
previousStatus.filter(($each) => !state.value.find((f) => $each.$id === f.$id)).forEach(($each, index) => {
|
|
1268
|
+
storage.deleteArrayStatus($id.value, index.toString());
|
|
1269
|
+
});
|
|
1270
|
+
} else {
|
|
1271
|
+
$eachStatus.value = [];
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
function $watch() {
|
|
1275
|
+
$unwatchState = vue.watch(
|
|
1276
|
+
state,
|
|
1277
|
+
() => {
|
|
1278
|
+
if (state.value != null && !Object.hasOwn(state.value, "$id")) {
|
|
1279
|
+
createStatus();
|
|
1280
|
+
} else {
|
|
1281
|
+
updateStatus();
|
|
1282
|
+
}
|
|
1283
|
+
},
|
|
1284
|
+
{ deep: isVueSuperiorOrEqualTo3dotFive ? 1 : true, flush: "pre" }
|
|
1285
|
+
);
|
|
1286
|
+
scope = vue.effectScope();
|
|
1287
|
+
scopeState = scope.run(() => {
|
|
1288
|
+
const $dirty = vue.computed(() => {
|
|
1289
|
+
return $fieldStatus.value.$dirty && $eachStatus.value.every((statusOrField) => {
|
|
1290
|
+
return statusOrField.$dirty;
|
|
1291
|
+
});
|
|
1292
|
+
});
|
|
1293
|
+
const $anyDirty = vue.computed(() => {
|
|
1294
|
+
return $fieldStatus.value.$anyDirty || $eachStatus.value.some((statusOrField) => {
|
|
1295
|
+
return statusOrField.$dirty;
|
|
1296
|
+
});
|
|
1297
|
+
});
|
|
1298
|
+
const $invalid = vue.computed(() => {
|
|
1299
|
+
return $fieldStatus.value.$invalid || $eachStatus.value.some((statusOrField) => {
|
|
1300
|
+
return statusOrField.$invalid;
|
|
1301
|
+
});
|
|
1302
|
+
});
|
|
1303
|
+
const $valid = vue.computed(() => {
|
|
1304
|
+
return (isEmpty($fieldStatus.value.$rules) ? true : $fieldStatus.value.$valid) && $eachStatus.value.every((statusOrField) => {
|
|
1305
|
+
return statusOrField.$valid;
|
|
1306
|
+
});
|
|
1307
|
+
});
|
|
1308
|
+
const $error = vue.computed(() => {
|
|
1309
|
+
return $fieldStatus.value.$error || $eachStatus.value.some((statusOrField) => {
|
|
1310
|
+
return statusOrField.$error;
|
|
1311
|
+
});
|
|
1312
|
+
});
|
|
1313
|
+
const $ready = vue.computed(() => {
|
|
1314
|
+
return !($invalid.value || $pending.value);
|
|
1315
|
+
});
|
|
1316
|
+
const $pending = vue.computed(() => {
|
|
1317
|
+
return $fieldStatus.value.$pending || $eachStatus.value.some((statusOrField) => {
|
|
1318
|
+
return statusOrField.$pending;
|
|
1319
|
+
});
|
|
1320
|
+
});
|
|
1321
|
+
const $errors = vue.computed(() => {
|
|
1322
|
+
return {
|
|
1323
|
+
$self: $fieldStatus.value.$errors,
|
|
1324
|
+
$each: $eachStatus.value.map(($each) => $each.$errors)
|
|
1325
|
+
};
|
|
1326
|
+
});
|
|
1327
|
+
const $silentErrors = vue.computed(() => {
|
|
1328
|
+
return {
|
|
1329
|
+
$self: $fieldStatus.value.$silentErrors,
|
|
1330
|
+
$each: $eachStatus.value.map(($each) => $each.$silentErrors)
|
|
1331
|
+
};
|
|
1332
|
+
});
|
|
1333
|
+
const $name = vue.computed(() => fieldName);
|
|
1334
|
+
function processShortcuts() {
|
|
1335
|
+
if (shortcuts?.collections) {
|
|
1336
|
+
Object.entries(shortcuts?.collections).forEach(([key, value]) => {
|
|
1337
|
+
const scope2 = vue.effectScope();
|
|
1338
|
+
$shortcuts2[key] = scope2.run(() => {
|
|
1339
|
+
const result = vue.ref();
|
|
1340
|
+
vue.watchEffect(() => {
|
|
1341
|
+
result.value = value(
|
|
1342
|
+
vue.reactive({
|
|
1343
|
+
$dirty,
|
|
1344
|
+
$error,
|
|
1345
|
+
$pending,
|
|
1346
|
+
$invalid,
|
|
1347
|
+
$valid,
|
|
1348
|
+
$errors,
|
|
1349
|
+
$ready,
|
|
1350
|
+
$silentErrors,
|
|
1351
|
+
$anyDirty,
|
|
1352
|
+
$name,
|
|
1353
|
+
$each: $eachStatus,
|
|
1354
|
+
$field: $fieldStatus,
|
|
1355
|
+
$value: state
|
|
1356
|
+
})
|
|
1357
|
+
);
|
|
1358
|
+
});
|
|
1359
|
+
return result;
|
|
1360
|
+
});
|
|
1361
|
+
collectionScopes.push(scope2);
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
const $shortcuts2 = {};
|
|
1366
|
+
processShortcuts();
|
|
1367
|
+
return {
|
|
1368
|
+
$dirty,
|
|
1369
|
+
$anyDirty,
|
|
1370
|
+
$invalid,
|
|
1371
|
+
$valid,
|
|
1372
|
+
$error,
|
|
1373
|
+
$pending,
|
|
1374
|
+
$errors,
|
|
1375
|
+
$silentErrors,
|
|
1376
|
+
$ready,
|
|
1377
|
+
$name,
|
|
1378
|
+
$shortcuts: $shortcuts2
|
|
1379
|
+
};
|
|
1380
|
+
});
|
|
1381
|
+
if (immediateScopeState.isPrimitiveArray.value) {
|
|
1382
|
+
console.warn(
|
|
1383
|
+
`${path} is a Array of primitives. Tracking can be lost when reassigning the Array. We advise to use an Array of objects instead`
|
|
1384
|
+
);
|
|
1385
|
+
$unwatchState();
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
function $unwatch() {
|
|
1389
|
+
if ($unwatchState) {
|
|
1390
|
+
$unwatchState();
|
|
1391
|
+
}
|
|
1392
|
+
if ($fieldStatus.value) {
|
|
1393
|
+
$fieldStatus.value.$unwatch();
|
|
1394
|
+
}
|
|
1395
|
+
if ($eachStatus.value) {
|
|
1396
|
+
$eachStatus.value.forEach((element) => {
|
|
1397
|
+
if ("$dirty" in element) {
|
|
1398
|
+
element.$unwatch();
|
|
1399
|
+
}
|
|
1400
|
+
});
|
|
1401
|
+
}
|
|
1402
|
+
scope.stop();
|
|
1403
|
+
scope = vue.effectScope();
|
|
1404
|
+
immediateScope.stop();
|
|
1405
|
+
immediateScope = vue.effectScope(true);
|
|
1406
|
+
collectionScopes.forEach((s) => s.stop());
|
|
1407
|
+
collectionScopes = [];
|
|
1408
|
+
}
|
|
1409
|
+
function $touch(runCommit = true, withConditions = false) {
|
|
1410
|
+
$fieldStatus.value.$touch(runCommit, withConditions);
|
|
1411
|
+
$eachStatus.value.forEach(($each) => {
|
|
1412
|
+
$each.$touch(runCommit, withConditions);
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
function $reset() {
|
|
1416
|
+
$fieldStatus.value.$reset();
|
|
1417
|
+
$eachStatus.value.forEach(($each) => {
|
|
1418
|
+
$each.$reset();
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
async function $validate() {
|
|
1422
|
+
const data = state.value;
|
|
1423
|
+
try {
|
|
1424
|
+
const results = await Promise.allSettled([
|
|
1425
|
+
$fieldStatus.value.$validate(),
|
|
1426
|
+
...$eachStatus.value.map((rule) => {
|
|
1427
|
+
return rule.$validate();
|
|
1428
|
+
})
|
|
1429
|
+
]);
|
|
1430
|
+
const validationResults = results.every((value) => {
|
|
1431
|
+
if (value.status === "fulfilled") {
|
|
1432
|
+
return value.value.result === true;
|
|
1433
|
+
} else {
|
|
1434
|
+
return false;
|
|
1435
|
+
}
|
|
1436
|
+
});
|
|
1437
|
+
return { result: validationResults, data };
|
|
1438
|
+
} catch (e) {
|
|
1439
|
+
return { result: false, data };
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
function $clearExternalErrors() {
|
|
1443
|
+
$fieldStatus.value.$clearExternalErrors();
|
|
1444
|
+
$eachStatus.value.forEach(($each) => {
|
|
1445
|
+
$each.$clearExternalErrors();
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1448
|
+
function $extractDirtyFields(filterNullishValues = true) {
|
|
1449
|
+
let dirtyFields = $eachStatus.value.map(($each) => {
|
|
1450
|
+
if (isNestedRulesStatus($each)) {
|
|
1451
|
+
return $each.$extractDirtyFields(filterNullishValues);
|
|
1452
|
+
}
|
|
1453
|
+
});
|
|
1454
|
+
if (filterNullishValues) {
|
|
1455
|
+
if (dirtyFields.every((value) => {
|
|
1456
|
+
return isEmpty(value);
|
|
1457
|
+
})) {
|
|
1458
|
+
dirtyFields = [];
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
return dirtyFields;
|
|
1462
|
+
}
|
|
1463
|
+
function $resetAll() {
|
|
1464
|
+
$unwatch();
|
|
1465
|
+
state.value = cloneDeep(initialState);
|
|
1466
|
+
$reset();
|
|
1467
|
+
}
|
|
1468
|
+
const { $shortcuts, ...restScopeState } = scopeState;
|
|
1469
|
+
return vue.reactive({
|
|
1470
|
+
$field: $fieldStatus,
|
|
1471
|
+
...restScopeState,
|
|
1472
|
+
...$shortcuts,
|
|
1473
|
+
$each: $eachStatus,
|
|
1474
|
+
$value: state,
|
|
1475
|
+
$validate,
|
|
1476
|
+
$unwatch,
|
|
1477
|
+
$watch,
|
|
1478
|
+
$touch,
|
|
1479
|
+
$reset,
|
|
1480
|
+
$resetAll,
|
|
1481
|
+
$extractDirtyFields,
|
|
1482
|
+
$clearExternalErrors
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
// src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
|
|
1487
|
+
function createReactiveNestedStatus({
|
|
1488
|
+
rulesDef,
|
|
1489
|
+
state,
|
|
1490
|
+
path = "",
|
|
1491
|
+
rootRules,
|
|
1492
|
+
externalErrors,
|
|
1493
|
+
validationGroups,
|
|
1494
|
+
initialState,
|
|
1495
|
+
fieldName,
|
|
1496
|
+
...commonArgs
|
|
1497
|
+
}) {
|
|
1498
|
+
let scope = vue.effectScope();
|
|
1499
|
+
let scopeState;
|
|
1500
|
+
let nestedScopes = [];
|
|
1501
|
+
let $unwatchRules = null;
|
|
1502
|
+
let $unwatchExternalErrors = null;
|
|
1503
|
+
let $unwatchState = null;
|
|
1504
|
+
async function createReactiveFieldsStatus(watch6 = true) {
|
|
1505
|
+
const mapOfRulesDef = Object.entries(rulesDef.value);
|
|
1506
|
+
const scopedRulesStatus = Object.fromEntries(
|
|
1507
|
+
mapOfRulesDef.filter(([_, rule]) => !!rule).map(([statePropKey, statePropRules]) => {
|
|
1508
|
+
if (statePropRules) {
|
|
1509
|
+
const stateRef = vue.toRef(state.value, statePropKey);
|
|
1510
|
+
const statePropRulesRef = vue.toRef(() => statePropRules);
|
|
1511
|
+
const $externalErrors = vue.toRef(externalErrors?.value ?? {}, statePropKey);
|
|
1512
|
+
return [
|
|
1513
|
+
statePropKey,
|
|
1514
|
+
createReactiveChildrenStatus({
|
|
1515
|
+
state: stateRef,
|
|
1516
|
+
rulesDef: statePropRulesRef,
|
|
1517
|
+
path: path ? `${path}.${statePropKey}` : statePropKey,
|
|
1518
|
+
externalErrors: $externalErrors,
|
|
1519
|
+
initialState: initialState?.[statePropKey],
|
|
1520
|
+
fieldName: statePropKey,
|
|
1521
|
+
...commonArgs
|
|
1522
|
+
})
|
|
1523
|
+
];
|
|
1524
|
+
}
|
|
1525
|
+
return [];
|
|
1526
|
+
})
|
|
1527
|
+
);
|
|
1528
|
+
const externalRulesStatus = Object.fromEntries(
|
|
1529
|
+
Object.entries(vue.unref(externalErrors) ?? {}).filter(([key, errors]) => !(key in rulesDef.value) && !!errors).map(([key]) => {
|
|
1530
|
+
const stateRef = vue.toRef(state.value, key);
|
|
1531
|
+
return [
|
|
1532
|
+
key,
|
|
1533
|
+
createReactiveChildrenStatus({
|
|
1534
|
+
state: stateRef,
|
|
1535
|
+
rulesDef: vue.computed(() => ({})),
|
|
1536
|
+
path: path ? `${path}.${key}` : key,
|
|
1537
|
+
externalErrors: vue.toRef(externalErrors?.value ?? {}, key),
|
|
1538
|
+
initialState: initialState?.[key],
|
|
1539
|
+
fieldName: key,
|
|
1540
|
+
...commonArgs
|
|
1541
|
+
})
|
|
1542
|
+
];
|
|
1543
|
+
})
|
|
1544
|
+
);
|
|
1545
|
+
const statesWithNoRules = Object.fromEntries(
|
|
1546
|
+
Object.entries(state.value).filter(([key]) => !(key in rulesDef.value) && !(key in (externalRulesStatus.value ?? {}))).map(([key]) => {
|
|
1547
|
+
const stateRef = vue.toRef(state.value, key);
|
|
1548
|
+
return [
|
|
1549
|
+
key,
|
|
1550
|
+
createReactiveChildrenStatus({
|
|
1551
|
+
state: stateRef,
|
|
1552
|
+
rulesDef: vue.computed(() => ({})),
|
|
1553
|
+
path: path ? `${path}.${key}` : key,
|
|
1554
|
+
externalErrors: vue.toRef(externalErrors?.value ?? {}, key),
|
|
1555
|
+
initialState: initialState?.[key],
|
|
1556
|
+
fieldName: key,
|
|
1557
|
+
...commonArgs
|
|
1558
|
+
})
|
|
1559
|
+
];
|
|
1560
|
+
})
|
|
1561
|
+
);
|
|
1562
|
+
$fields.value = {
|
|
1563
|
+
...scopedRulesStatus,
|
|
1564
|
+
...externalRulesStatus,
|
|
1565
|
+
...statesWithNoRules
|
|
1566
|
+
};
|
|
1567
|
+
if (watch6) {
|
|
1568
|
+
$watch();
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
const $fields = commonArgs.storage.getFieldsEntry(path);
|
|
1572
|
+
createReactiveFieldsStatus();
|
|
1573
|
+
function $reset() {
|
|
1574
|
+
$unwatchExternalErrors?.();
|
|
1575
|
+
Object.values($fields.value).forEach((statusOrField) => {
|
|
1576
|
+
statusOrField.$reset();
|
|
1577
|
+
});
|
|
1578
|
+
define$WatchExternalErrors();
|
|
1579
|
+
}
|
|
1580
|
+
function $touch(runCommit = true, withConditions = false) {
|
|
1581
|
+
Object.values($fields.value).forEach((statusOrField) => {
|
|
1582
|
+
statusOrField.$touch(runCommit, withConditions);
|
|
1583
|
+
});
|
|
1584
|
+
}
|
|
1585
|
+
function define$WatchExternalErrors() {
|
|
1586
|
+
if (externalErrors?.value) {
|
|
1587
|
+
$unwatchExternalErrors = vue.watch(
|
|
1588
|
+
externalErrors,
|
|
1589
|
+
() => {
|
|
1590
|
+
$unwatch();
|
|
1591
|
+
createReactiveFieldsStatus();
|
|
1592
|
+
},
|
|
1593
|
+
{ deep: true }
|
|
1594
|
+
);
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
function $watch() {
|
|
1598
|
+
if (rootRules) {
|
|
1599
|
+
$unwatchRules = vue.watch(
|
|
1600
|
+
rootRules,
|
|
1601
|
+
() => {
|
|
1602
|
+
$unwatch();
|
|
1603
|
+
createReactiveFieldsStatus();
|
|
1604
|
+
},
|
|
1605
|
+
{ deep: true, flush: "post" }
|
|
1606
|
+
);
|
|
1607
|
+
define$WatchExternalErrors();
|
|
1608
|
+
}
|
|
1609
|
+
$unwatchState = vue.watch(
|
|
1610
|
+
state,
|
|
1611
|
+
() => {
|
|
1612
|
+
$unwatch();
|
|
1613
|
+
createReactiveFieldsStatus();
|
|
1614
|
+
$touch(true, true);
|
|
1615
|
+
},
|
|
1616
|
+
{ flush: "sync" }
|
|
1617
|
+
);
|
|
1618
|
+
scope = vue.effectScope();
|
|
1619
|
+
scopeState = scope.run(() => {
|
|
1620
|
+
const $dirty = vue.computed({
|
|
1621
|
+
get: () => {
|
|
1622
|
+
return !!Object.entries($fields.value).length && Object.entries($fields.value).every(([key, statusOrField]) => {
|
|
1623
|
+
return statusOrField?.$dirty;
|
|
1624
|
+
});
|
|
1625
|
+
},
|
|
1626
|
+
set() {
|
|
1627
|
+
}
|
|
1628
|
+
});
|
|
1629
|
+
const $anyDirty = vue.computed({
|
|
1630
|
+
get: () => {
|
|
1631
|
+
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
1632
|
+
return statusOrField?.$dirty;
|
|
1633
|
+
});
|
|
1634
|
+
},
|
|
1635
|
+
set() {
|
|
1636
|
+
}
|
|
1637
|
+
});
|
|
1638
|
+
const $invalid = vue.computed({
|
|
1639
|
+
get: () => {
|
|
1640
|
+
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
1641
|
+
return statusOrField?.$invalid;
|
|
1642
|
+
});
|
|
1643
|
+
},
|
|
1644
|
+
set() {
|
|
1645
|
+
}
|
|
1646
|
+
});
|
|
1647
|
+
const $valid = vue.computed({
|
|
1648
|
+
get: () => {
|
|
1649
|
+
return Object.entries($fields.value).every(([key, statusOrField]) => {
|
|
1650
|
+
return statusOrField?.$valid;
|
|
1651
|
+
});
|
|
1652
|
+
},
|
|
1653
|
+
set() {
|
|
1654
|
+
}
|
|
1655
|
+
});
|
|
1656
|
+
const $error = vue.computed({
|
|
1657
|
+
get: () => {
|
|
1658
|
+
return $anyDirty.value && !$pending.value && $invalid.value;
|
|
1659
|
+
},
|
|
1660
|
+
set() {
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1663
|
+
const $ready = vue.computed({
|
|
1664
|
+
get: () => {
|
|
1665
|
+
if (!vue.unref(commonArgs.options.autoDirty)) {
|
|
1666
|
+
return !($invalid.value || $pending.value);
|
|
1667
|
+
}
|
|
1668
|
+
return $anyDirty.value && !($invalid.value || $pending.value);
|
|
1669
|
+
},
|
|
1670
|
+
set() {
|
|
1671
|
+
}
|
|
1672
|
+
});
|
|
1673
|
+
const $pending = vue.computed({
|
|
1674
|
+
get: () => {
|
|
1675
|
+
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
1676
|
+
return statusOrField?.$pending;
|
|
1677
|
+
});
|
|
1678
|
+
},
|
|
1679
|
+
set() {
|
|
1680
|
+
}
|
|
1681
|
+
});
|
|
1682
|
+
const $errors = vue.computed({
|
|
1683
|
+
get: () => {
|
|
1684
|
+
return Object.fromEntries(
|
|
1685
|
+
Object.entries($fields.value).map(([key, statusOrField]) => {
|
|
1686
|
+
return [key, statusOrField?.$errors];
|
|
1687
|
+
})
|
|
1688
|
+
);
|
|
1689
|
+
},
|
|
1690
|
+
set() {
|
|
1691
|
+
}
|
|
1692
|
+
});
|
|
1693
|
+
const $silentErrors = vue.computed({
|
|
1694
|
+
get: () => {
|
|
1695
|
+
return Object.fromEntries(
|
|
1696
|
+
Object.entries($fields.value).map(([key, statusOrField]) => {
|
|
1697
|
+
return [key, statusOrField?.$silentErrors];
|
|
1698
|
+
})
|
|
1699
|
+
);
|
|
1700
|
+
},
|
|
1701
|
+
set() {
|
|
1702
|
+
}
|
|
1703
|
+
});
|
|
1704
|
+
const $name = vue.computed({
|
|
1705
|
+
get: () => fieldName,
|
|
1706
|
+
set() {
|
|
1707
|
+
}
|
|
1708
|
+
});
|
|
1709
|
+
function processShortcuts() {
|
|
1710
|
+
if (commonArgs.shortcuts?.nested) {
|
|
1711
|
+
Object.entries(commonArgs.shortcuts.nested).forEach(([key, value]) => {
|
|
1712
|
+
const scope2 = vue.effectScope();
|
|
1713
|
+
$shortcuts2[key] = scope2.run(() => {
|
|
1714
|
+
const result = vue.ref();
|
|
1715
|
+
vue.watchEffect(() => {
|
|
1716
|
+
result.value = value(
|
|
1717
|
+
vue.reactive({
|
|
1718
|
+
$dirty,
|
|
1719
|
+
$value: state,
|
|
1720
|
+
$error,
|
|
1721
|
+
$pending,
|
|
1722
|
+
$invalid,
|
|
1723
|
+
$valid,
|
|
1724
|
+
$ready,
|
|
1725
|
+
$anyDirty,
|
|
1726
|
+
$name,
|
|
1727
|
+
$silentErrors,
|
|
1728
|
+
$errors,
|
|
1729
|
+
$fields
|
|
1730
|
+
})
|
|
1731
|
+
);
|
|
1732
|
+
});
|
|
1733
|
+
return result;
|
|
1734
|
+
});
|
|
1735
|
+
nestedScopes.push(scope2);
|
|
1736
|
+
});
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
const $groups = vue.computed({
|
|
1740
|
+
get: () => {
|
|
1741
|
+
if (validationGroups) {
|
|
1742
|
+
return Object.fromEntries(
|
|
1743
|
+
Object.entries(validationGroups?.($fields.value) ?? {}).map(([key, entries]) => {
|
|
1744
|
+
if (entries.length) {
|
|
1745
|
+
return [
|
|
1746
|
+
key,
|
|
1747
|
+
{
|
|
1748
|
+
...Object.fromEntries(
|
|
1749
|
+
["$invalid", "$error", "$pending", "$dirty", "$valid"].map((property) => [
|
|
1750
|
+
property,
|
|
1751
|
+
mergeBooleanGroupProperties(entries, property)
|
|
1752
|
+
])
|
|
1753
|
+
),
|
|
1754
|
+
...Object.fromEntries(
|
|
1755
|
+
["$errors", "$silentErrors"].map((property) => [
|
|
1756
|
+
property,
|
|
1757
|
+
mergeArrayGroupProperties(entries, property)
|
|
1758
|
+
])
|
|
1759
|
+
)
|
|
1760
|
+
}
|
|
1761
|
+
];
|
|
1762
|
+
}
|
|
1763
|
+
return [];
|
|
1764
|
+
})
|
|
1765
|
+
);
|
|
1766
|
+
}
|
|
1767
|
+
return {};
|
|
1768
|
+
},
|
|
1769
|
+
set() {
|
|
1770
|
+
}
|
|
1771
|
+
});
|
|
1772
|
+
const $shortcuts2 = {};
|
|
1773
|
+
processShortcuts();
|
|
1774
|
+
return {
|
|
1775
|
+
$dirty,
|
|
1776
|
+
$anyDirty,
|
|
1777
|
+
$invalid,
|
|
1778
|
+
$valid,
|
|
1779
|
+
$error,
|
|
1780
|
+
$pending,
|
|
1781
|
+
$errors,
|
|
1782
|
+
$silentErrors,
|
|
1783
|
+
$ready,
|
|
1784
|
+
$name,
|
|
1785
|
+
$shortcuts: $shortcuts2,
|
|
1786
|
+
$groups
|
|
1787
|
+
};
|
|
1788
|
+
});
|
|
1789
|
+
}
|
|
1790
|
+
function $unwatch() {
|
|
1791
|
+
$unwatchRules?.();
|
|
1792
|
+
$unwatchExternalErrors?.();
|
|
1793
|
+
$unwatchState?.();
|
|
1794
|
+
nestedScopes.forEach((s) => s.stop());
|
|
1795
|
+
nestedScopes = [];
|
|
1796
|
+
scope.stop();
|
|
1797
|
+
if ($fields.value) {
|
|
1798
|
+
Object.entries($fields.value).forEach(([_, field]) => {
|
|
1799
|
+
field.$unwatch();
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
function $clearExternalErrors() {
|
|
1804
|
+
Object.entries($fields.value).forEach(([_, field]) => {
|
|
1805
|
+
field.$clearExternalErrors();
|
|
1806
|
+
});
|
|
1807
|
+
}
|
|
1808
|
+
async function $resetAll() {
|
|
1809
|
+
$unwatch();
|
|
1810
|
+
state.value = cloneDeep({ ...initialState ?? {} });
|
|
1811
|
+
$reset();
|
|
1812
|
+
createReactiveFieldsStatus();
|
|
1813
|
+
}
|
|
1814
|
+
function filterNullishFields(fields) {
|
|
1815
|
+
return fields.filter(([key, value]) => {
|
|
1816
|
+
if (isObject(value)) {
|
|
1817
|
+
return !(value && typeof value === "object" && "_null" in value) && !isEmpty(value);
|
|
1818
|
+
} else if (Array.isArray(value)) {
|
|
1819
|
+
return value.length;
|
|
1820
|
+
} else {
|
|
1821
|
+
return true;
|
|
1822
|
+
}
|
|
1823
|
+
});
|
|
1824
|
+
}
|
|
1825
|
+
function $extractDirtyFields(filterNullishValues = true) {
|
|
1826
|
+
let dirtyFields = Object.entries($fields.value).map(([key, field]) => {
|
|
1827
|
+
return [key, field.$extractDirtyFields(filterNullishValues)];
|
|
1828
|
+
});
|
|
1829
|
+
if (filterNullishValues) {
|
|
1830
|
+
dirtyFields = filterNullishFields(dirtyFields);
|
|
1831
|
+
}
|
|
1832
|
+
return Object.fromEntries(dirtyFields);
|
|
1833
|
+
}
|
|
1834
|
+
async function $validate() {
|
|
1835
|
+
try {
|
|
1836
|
+
const data = state.value;
|
|
1837
|
+
const results = await Promise.allSettled(
|
|
1838
|
+
Object.values($fields.value).map((statusOrField) => {
|
|
1839
|
+
return statusOrField.$validate();
|
|
1840
|
+
})
|
|
1841
|
+
);
|
|
1842
|
+
const validationResults = results.every((value) => {
|
|
1843
|
+
if (value.status === "fulfilled") {
|
|
1844
|
+
return value.value.result === true;
|
|
1845
|
+
} else {
|
|
1846
|
+
return false;
|
|
1847
|
+
}
|
|
1848
|
+
});
|
|
1849
|
+
return { result: validationResults, data };
|
|
1850
|
+
} catch (e) {
|
|
1851
|
+
return { result: false, data: state.value };
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
const { $shortcuts, ...restScopeState } = scopeState;
|
|
1855
|
+
return vue.reactive({
|
|
1856
|
+
...restScopeState,
|
|
1857
|
+
...$shortcuts,
|
|
1858
|
+
$fields,
|
|
1859
|
+
$value: state,
|
|
1860
|
+
$resetAll,
|
|
1861
|
+
$reset,
|
|
1862
|
+
$touch,
|
|
1863
|
+
$validate,
|
|
1864
|
+
$unwatch,
|
|
1865
|
+
$watch,
|
|
1866
|
+
$clearExternalErrors,
|
|
1867
|
+
$extractDirtyFields
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
function createReactiveChildrenStatus({
|
|
1871
|
+
rulesDef,
|
|
1872
|
+
externalErrors,
|
|
1873
|
+
...properties
|
|
1874
|
+
}) {
|
|
1875
|
+
if (isCollectionRulesDef(rulesDef, properties.state)) {
|
|
1876
|
+
return createReactiveCollectionStatus({
|
|
1877
|
+
rulesDef,
|
|
1878
|
+
externalErrors,
|
|
1879
|
+
...properties
|
|
1880
|
+
});
|
|
1881
|
+
} else if (isNestedRulesDef(properties.state, rulesDef) && isRefObject(properties.state)) {
|
|
1882
|
+
return createReactiveNestedStatus({
|
|
1883
|
+
rulesDef,
|
|
1884
|
+
externalErrors,
|
|
1885
|
+
...properties
|
|
1886
|
+
});
|
|
1887
|
+
} else if (isValidatorRulesDef(rulesDef)) {
|
|
1888
|
+
return createReactiveFieldStatus({
|
|
1889
|
+
rulesDef,
|
|
1890
|
+
externalErrors,
|
|
1891
|
+
...properties
|
|
1892
|
+
});
|
|
1893
|
+
}
|
|
1894
|
+
return null;
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
1898
|
+
function useStateProperties({
|
|
1899
|
+
initialState,
|
|
1900
|
+
options,
|
|
1901
|
+
scopeRules,
|
|
1902
|
+
state,
|
|
1903
|
+
customRules,
|
|
1904
|
+
shortcuts
|
|
1905
|
+
}) {
|
|
1906
|
+
const storage = useStorage();
|
|
1907
|
+
const regle = vue.ref();
|
|
1908
|
+
regle.value = createReactiveNestedStatus({
|
|
1909
|
+
rootRules: scopeRules,
|
|
1910
|
+
rulesDef: scopeRules,
|
|
1911
|
+
state,
|
|
1912
|
+
customMessages: customRules?.(),
|
|
1913
|
+
storage,
|
|
1914
|
+
options,
|
|
1915
|
+
externalErrors: options.externalErrors,
|
|
1916
|
+
validationGroups: options.validationGroups,
|
|
1917
|
+
initialState,
|
|
1918
|
+
shortcuts,
|
|
1919
|
+
fieldName: "root",
|
|
1920
|
+
path: ""
|
|
1921
|
+
});
|
|
1922
|
+
vue.onScopeDispose(() => {
|
|
1923
|
+
regle.value?.$unwatch();
|
|
1924
|
+
});
|
|
1925
|
+
return vue.reactive({ regle });
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
// src/core/useRegle/useRegle.ts
|
|
1929
|
+
function createUseRegleComposable(customRules, options, shortcuts) {
|
|
1930
|
+
const globalOptions = {
|
|
1931
|
+
autoDirty: options?.autoDirty ?? true,
|
|
1932
|
+
lazy: options?.lazy ?? false,
|
|
1933
|
+
rewardEarly: options?.rewardEarly ?? false,
|
|
1934
|
+
clearExternalErrorsOnChange: options?.clearExternalErrorsOnChange ?? true
|
|
1935
|
+
};
|
|
1936
|
+
function useRegle2(state, rulesFactory, options2) {
|
|
1937
|
+
const scopeRules = vue.isRef(rulesFactory) ? rulesFactory : vue.computed(typeof rulesFactory === "function" ? rulesFactory : () => rulesFactory);
|
|
1938
|
+
const resolvedOptions = {
|
|
1939
|
+
...globalOptions,
|
|
1940
|
+
...options2
|
|
1941
|
+
};
|
|
1942
|
+
const processedState = vue.isRef(state) ? state : vue.ref(state);
|
|
1943
|
+
const initialState = { ...cloneDeep(processedState.value) };
|
|
1944
|
+
const regle = useStateProperties({
|
|
1945
|
+
scopeRules,
|
|
1946
|
+
state: processedState,
|
|
1947
|
+
options: resolvedOptions,
|
|
1948
|
+
initialState,
|
|
1949
|
+
customRules,
|
|
1950
|
+
shortcuts
|
|
1951
|
+
});
|
|
1952
|
+
return {
|
|
1953
|
+
r$: regle.regle
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
return useRegle2;
|
|
1957
|
+
}
|
|
1958
|
+
var useRegle = createUseRegleComposable();
|
|
1959
|
+
function createInferRuleHelper() {
|
|
1960
|
+
function inferRules2(state, rulesFactory) {
|
|
1961
|
+
return rulesFactory;
|
|
1962
|
+
}
|
|
1963
|
+
return inferRules2;
|
|
1964
|
+
}
|
|
1965
|
+
var inferRules = createInferRuleHelper();
|
|
1966
|
+
|
|
1967
|
+
// src/core/defineRegleConfig.ts
|
|
1968
|
+
function defineRegleConfig({
|
|
1969
|
+
rules,
|
|
1970
|
+
modifiers,
|
|
1971
|
+
shortcuts
|
|
1972
|
+
}) {
|
|
1973
|
+
const useRegle2 = createUseRegleComposable(rules, modifiers, shortcuts);
|
|
1974
|
+
const inferRules2 = createInferRuleHelper();
|
|
1975
|
+
return { useRegle: useRegle2, inferRules: inferRules2 };
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
exports.InternalRuleType = InternalRuleType;
|
|
1979
|
+
exports.createRule = createRule;
|
|
1980
|
+
exports.defineRegleConfig = defineRegleConfig;
|
|
1981
|
+
exports.inferRules = inferRules;
|
|
1982
|
+
exports.unwrapRuleParameters = unwrapRuleParameters;
|
|
1983
|
+
exports.useRegle = useRegle;
|