@regle/core 1.18.0-beta.3 → 1.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/regle-core.d.ts +348 -312
- package/dist/regle-core.js +207 -113
- package/dist/regle-core.min.js +2 -2
- package/package.json +6 -6
package/dist/regle-core.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @regle/core v1.18.0
|
|
2
|
+
* @regle/core v1.18.0
|
|
3
3
|
* (c) 2026 Victor Garcia
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -18,6 +18,22 @@ function isObject(obj) {
|
|
|
18
18
|
if (obj && (obj instanceof Date || obj.constructor.name == "File" || obj.constructor.name == "FileList")) return false;
|
|
19
19
|
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
20
20
|
}
|
|
21
|
+
function getDotPath(obj, propsArg, defaultValue) {
|
|
22
|
+
if (!obj) return defaultValue;
|
|
23
|
+
var props, prop;
|
|
24
|
+
if (Array.isArray(propsArg)) props = propsArg.slice(0);
|
|
25
|
+
if (typeof propsArg == "string") props = propsArg.split(".");
|
|
26
|
+
if (typeof propsArg == "symbol") props = [propsArg];
|
|
27
|
+
if (!Array.isArray(props)) throw new Error("props arg must be an array, a string or a symbol");
|
|
28
|
+
while (props.length) {
|
|
29
|
+
prop = props.shift();
|
|
30
|
+
if (!obj) return defaultValue;
|
|
31
|
+
if (!prop) return defaultValue;
|
|
32
|
+
obj = obj[prop];
|
|
33
|
+
if (obj === void 0) return defaultValue;
|
|
34
|
+
}
|
|
35
|
+
return obj;
|
|
36
|
+
}
|
|
21
37
|
function merge(_obj1, ..._objs) {
|
|
22
38
|
var args = [].slice.call(arguments);
|
|
23
39
|
var arg;
|
|
@@ -381,15 +397,15 @@ function defineRuleProcessors(definition, ...params) {
|
|
|
381
397
|
else return definition.tooltip ?? [];
|
|
382
398
|
},
|
|
383
399
|
exec(value) {
|
|
384
|
-
const validator
|
|
400
|
+
const validator = definition.validator(value, ...unwrapRuleParameters(params));
|
|
385
401
|
let rawResult;
|
|
386
|
-
if (validator
|
|
402
|
+
if (validator instanceof Promise) return validator.then((result) => {
|
|
387
403
|
rawResult = result;
|
|
388
404
|
if (typeof rawResult === "object" && "$valid" in rawResult) return rawResult.$valid;
|
|
389
405
|
else if (typeof rawResult === "boolean") return rawResult;
|
|
390
406
|
return false;
|
|
391
407
|
});
|
|
392
|
-
else rawResult = validator
|
|
408
|
+
else rawResult = validator;
|
|
393
409
|
if (typeof rawResult === "object" && "$valid" in rawResult) return rawResult.$valid;
|
|
394
410
|
else if (typeof rawResult === "boolean") return rawResult;
|
|
395
411
|
return false;
|
|
@@ -725,8 +741,8 @@ const REGLE_FLAGS = { REGLE_STATIC: "__regle_static" };
|
|
|
725
741
|
function isNestedRulesDef(state, rules) {
|
|
726
742
|
return !isStatic(state.value) && (isRefObject(state) || isObject(rules.value) && !isEmpty(rules.value) && !Object.entries(rules.value).some(([_, rule]) => isRuleDef(rule) || typeof rule === "function"));
|
|
727
743
|
}
|
|
728
|
-
function isCollectionRulesDef(rules, state
|
|
729
|
-
return !!rules.value && isObject(rules.value) && "$each" in rules.value ||
|
|
744
|
+
function isCollectionRulesDef(rules, state) {
|
|
745
|
+
return !!rules.value && isObject(rules.value) && "$each" in rules.value || Array.isArray(state.value);
|
|
730
746
|
}
|
|
731
747
|
function isValidatorRulesDef(rules) {
|
|
732
748
|
return !!rules.value && (isObject(rules.value) || isRefObject(rules.value));
|
|
@@ -953,8 +969,8 @@ function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path,
|
|
|
953
969
|
if (state.value !== cachedValue) return true;
|
|
954
970
|
if (typeof validatorResult === "boolean") ruleResult = validatorResult;
|
|
955
971
|
else {
|
|
956
|
-
const { $valid
|
|
957
|
-
ruleResult = $valid
|
|
972
|
+
const { $valid, ...rest } = validatorResult;
|
|
973
|
+
ruleResult = $valid;
|
|
958
974
|
$metadata.value = rest;
|
|
959
975
|
}
|
|
960
976
|
} catch {
|
|
@@ -976,8 +992,8 @@ function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path,
|
|
|
976
992
|
if (resultOrPromise instanceof Promise) console.warn("You used a async validator function on a non-async rule, please use \"async await\" or the \"withAsync\" helper");
|
|
977
993
|
else if (resultOrPromise != null) if (typeof resultOrPromise === "boolean") ruleResult = resultOrPromise;
|
|
978
994
|
else {
|
|
979
|
-
const { $valid
|
|
980
|
-
ruleResult = $valid
|
|
995
|
+
const { $valid, ...rest } = resultOrPromise;
|
|
996
|
+
ruleResult = $valid;
|
|
981
997
|
$metadata.value = rest;
|
|
982
998
|
}
|
|
983
999
|
}
|
|
@@ -1048,9 +1064,9 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1048
1064
|
function createReactiveRulesResult() {
|
|
1049
1065
|
const declaredRules = rulesDef.value;
|
|
1050
1066
|
const storeResult = storage.checkRuleDeclEntry(cachePath, declaredRules);
|
|
1051
|
-
const options
|
|
1052
|
-
for (const key in declaredRules) if (key.startsWith("$")) options
|
|
1053
|
-
$localOptions.value = options
|
|
1067
|
+
const options = {};
|
|
1068
|
+
for (const key in declaredRules) if (key.startsWith("$")) options[key] = declaredRules[key];
|
|
1069
|
+
$localOptions.value = options;
|
|
1054
1070
|
$watch();
|
|
1055
1071
|
const rules = rulesDef.value;
|
|
1056
1072
|
const entries = [];
|
|
@@ -1118,7 +1134,7 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1118
1134
|
const $isArrayOrRegleStatic = computed(() => {
|
|
1119
1135
|
return $isArray || isStatic(state.value);
|
|
1120
1136
|
});
|
|
1121
|
-
const $debounce
|
|
1137
|
+
const $debounce = computed(() => {
|
|
1122
1138
|
if ($localOptions.value.$debounce != null) return $localOptions.value.$debounce;
|
|
1123
1139
|
if (scopeState.$haveAnyAsyncRule.value) return DEFAULT_DEBOUNCE_TIME;
|
|
1124
1140
|
return 0;
|
|
@@ -1127,7 +1143,7 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1127
1143
|
if ($localOptions.value.$deepCompare != null) return $localOptions.value.$deepCompare;
|
|
1128
1144
|
return false;
|
|
1129
1145
|
});
|
|
1130
|
-
const $lazy
|
|
1146
|
+
const $lazy = computed(() => {
|
|
1131
1147
|
if ($localOptions.value.$lazy != null) return $localOptions.value.$lazy;
|
|
1132
1148
|
else if (unref(options.lazy) != null) return unref(options.lazy);
|
|
1133
1149
|
return false;
|
|
@@ -1137,29 +1153,29 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1137
1153
|
else if (unref(options.immediateDirty) != null) return unref(options.immediateDirty);
|
|
1138
1154
|
return false;
|
|
1139
1155
|
});
|
|
1140
|
-
const $rewardEarly
|
|
1156
|
+
const $rewardEarly = computed(() => {
|
|
1141
1157
|
if ($localOptions.value.$rewardEarly != null) return $localOptions.value.$rewardEarly;
|
|
1142
1158
|
else if (unref(options.rewardEarly) != null) return unref(options.rewardEarly);
|
|
1143
1159
|
return false;
|
|
1144
1160
|
});
|
|
1145
|
-
const $clearExternalErrorsOnChange
|
|
1161
|
+
const $clearExternalErrorsOnChange = computed(() => {
|
|
1146
1162
|
if ($localOptions.value.$clearExternalErrorsOnChange != null) return $localOptions.value.$clearExternalErrorsOnChange;
|
|
1147
1163
|
else if (unref(options.clearExternalErrorsOnChange) != null) return unref(options.clearExternalErrorsOnChange);
|
|
1148
1164
|
else if ($silent.value) return false;
|
|
1149
1165
|
return true;
|
|
1150
1166
|
});
|
|
1151
1167
|
const $silent = computed(() => {
|
|
1152
|
-
if ($rewardEarly
|
|
1168
|
+
if ($rewardEarly.value) return true;
|
|
1153
1169
|
else if ($localOptions.value.$silent != null) return $localOptions.value.$silent;
|
|
1154
1170
|
else if (unref(options.silent) != null) return unref(options.silent);
|
|
1155
1171
|
else return false;
|
|
1156
1172
|
});
|
|
1157
|
-
const $autoDirty
|
|
1173
|
+
const $autoDirty = computed(() => {
|
|
1158
1174
|
if ($localOptions.value.$autoDirty != null) return $localOptions.value.$autoDirty;
|
|
1159
1175
|
else if (unref(options.autoDirty) != null) return unref(options.autoDirty);
|
|
1160
1176
|
return true;
|
|
1161
1177
|
});
|
|
1162
|
-
const $validating
|
|
1178
|
+
const $validating = computed(() => {
|
|
1163
1179
|
for (const ruleResult of Object.values($rules.value)) if (ruleResult.$validating) return true;
|
|
1164
1180
|
return false;
|
|
1165
1181
|
});
|
|
@@ -1223,7 +1239,7 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1223
1239
|
return !($invalid.value || $pending.value);
|
|
1224
1240
|
});
|
|
1225
1241
|
const $pending = computed(() => {
|
|
1226
|
-
if (triggerPunishment.value || !$rewardEarly
|
|
1242
|
+
if (triggerPunishment.value || !$rewardEarly.value) return Object.entries($rules.value).some(([_, ruleResult]) => {
|
|
1227
1243
|
return ruleResult.$pending;
|
|
1228
1244
|
});
|
|
1229
1245
|
return false;
|
|
@@ -1241,7 +1257,7 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1241
1257
|
if (externalErrors?.value?.length) return false;
|
|
1242
1258
|
else if ($inactive.value) return false;
|
|
1243
1259
|
else if ($isDebouncing.value) return false;
|
|
1244
|
-
else if ($dirty.value && !isEmpty(state.value) && !$validating
|
|
1260
|
+
else if ($dirty.value && !isEmpty(state.value) && !$validating.value && !$pending.value) if (schemaMode) return !schemaErrors?.value?.length;
|
|
1245
1261
|
else {
|
|
1246
1262
|
const rules = Object.values($rules.value);
|
|
1247
1263
|
for (const rule of rules) if (rule.$active) {
|
|
@@ -1251,23 +1267,23 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1251
1267
|
}
|
|
1252
1268
|
return false;
|
|
1253
1269
|
});
|
|
1254
|
-
const $haveAnyAsyncRule
|
|
1270
|
+
const $haveAnyAsyncRule = computed(() => {
|
|
1255
1271
|
return Object.values($rules.value).some((rule) => rule.$haveAsync);
|
|
1256
1272
|
});
|
|
1257
1273
|
const $modifiers = computed(() => {
|
|
1258
1274
|
return {
|
|
1259
|
-
$debounce: $debounce
|
|
1260
|
-
$lazy: $lazy
|
|
1261
|
-
$rewardEarly: $rewardEarly
|
|
1262
|
-
$autoDirty: $autoDirty
|
|
1275
|
+
$debounce: $debounce.value,
|
|
1276
|
+
$lazy: $lazy.value,
|
|
1277
|
+
$rewardEarly: $rewardEarly.value,
|
|
1278
|
+
$autoDirty: $autoDirty.value,
|
|
1263
1279
|
$silent: $silent.value,
|
|
1264
|
-
$clearExternalErrorsOnChange: $clearExternalErrorsOnChange
|
|
1280
|
+
$clearExternalErrorsOnChange: $clearExternalErrorsOnChange.value
|
|
1265
1281
|
};
|
|
1266
1282
|
});
|
|
1267
1283
|
function processShortcuts() {
|
|
1268
1284
|
if (shortcuts?.fields) Object.entries(shortcuts.fields).forEach(([key, value]) => {
|
|
1269
|
-
const scope
|
|
1270
|
-
$shortcuts
|
|
1285
|
+
const scope = effectScope();
|
|
1286
|
+
$shortcuts[key] = scope.run(() => {
|
|
1271
1287
|
const result = ref();
|
|
1272
1288
|
watchEffect(() => {
|
|
1273
1289
|
result.value = value(reactive({
|
|
@@ -1293,35 +1309,35 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1293
1309
|
});
|
|
1294
1310
|
return result;
|
|
1295
1311
|
});
|
|
1296
|
-
fieldScopes.push(scope
|
|
1312
|
+
fieldScopes.push(scope);
|
|
1297
1313
|
});
|
|
1298
1314
|
}
|
|
1299
|
-
const $shortcuts
|
|
1315
|
+
const $shortcuts = {};
|
|
1300
1316
|
return {
|
|
1301
1317
|
$error,
|
|
1302
1318
|
$pending,
|
|
1303
1319
|
$invalid,
|
|
1304
1320
|
$correct,
|
|
1305
|
-
$debounce
|
|
1321
|
+
$debounce,
|
|
1306
1322
|
$deepCompare,
|
|
1307
|
-
$lazy
|
|
1323
|
+
$lazy,
|
|
1308
1324
|
$immediateDirty,
|
|
1309
1325
|
$ready,
|
|
1310
1326
|
$issues,
|
|
1311
1327
|
$silentIssues,
|
|
1312
1328
|
$errors,
|
|
1313
1329
|
$silentErrors,
|
|
1314
|
-
$rewardEarly
|
|
1315
|
-
$autoDirty
|
|
1330
|
+
$rewardEarly,
|
|
1331
|
+
$autoDirty,
|
|
1316
1332
|
$silent,
|
|
1317
|
-
$clearExternalErrorsOnChange
|
|
1333
|
+
$clearExternalErrorsOnChange,
|
|
1318
1334
|
$anyDirty,
|
|
1319
1335
|
$edited,
|
|
1320
1336
|
$anyEdited,
|
|
1321
1337
|
$name,
|
|
1322
|
-
$haveAnyAsyncRule
|
|
1323
|
-
$shortcuts
|
|
1324
|
-
$validating
|
|
1338
|
+
$haveAnyAsyncRule,
|
|
1339
|
+
$shortcuts,
|
|
1340
|
+
$validating,
|
|
1325
1341
|
$tooltips,
|
|
1326
1342
|
$dirty,
|
|
1327
1343
|
processShortcuts,
|
|
@@ -1372,24 +1388,24 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
|
|
|
1372
1388
|
const $rules = ref({});
|
|
1373
1389
|
const $localOptions = ref({});
|
|
1374
1390
|
createReactiveRulesResult();
|
|
1375
|
-
function $reset(options
|
|
1391
|
+
function $reset(options, fromParent) {
|
|
1376
1392
|
abortCommit();
|
|
1377
1393
|
$clearExternalErrors();
|
|
1378
1394
|
scopeState.$dirty.value = false;
|
|
1379
1395
|
storage.setDirtyEntry(cachePath, false);
|
|
1380
|
-
if (!fromParent) if (options
|
|
1396
|
+
if (!fromParent) if (options?.toOriginalState) {
|
|
1381
1397
|
state.value = cloneDeep(originalState);
|
|
1382
1398
|
initialState.value = cloneDeep(originalState);
|
|
1383
|
-
} else if (options
|
|
1384
|
-
else if (options
|
|
1399
|
+
} else if (options?.toInitialState) state.value = cloneDeep(initialState.value);
|
|
1400
|
+
else if (options?.toState) {
|
|
1385
1401
|
let newInitialState;
|
|
1386
|
-
if (typeof options
|
|
1387
|
-
else newInitialState = options
|
|
1402
|
+
if (typeof options?.toState === "function") newInitialState = options?.toState();
|
|
1403
|
+
else newInitialState = options?.toState;
|
|
1388
1404
|
initialState.value = cloneDeep(newInitialState);
|
|
1389
1405
|
state.value = cloneDeep(newInitialState);
|
|
1390
1406
|
} else initialState.value = isObject(state.value) && !isStatic(state.value) ? cloneDeep(state.value) : Array.isArray(state.value) ? [...state.value] : state.value;
|
|
1391
|
-
if (options
|
|
1392
|
-
if (!fromParent && !options
|
|
1407
|
+
if (options?.clearExternalErrors) $clearExternalErrors();
|
|
1408
|
+
if (!fromParent && !options?.keepValidationState) for (const rule of Object.values($rules.value)) rule.$reset();
|
|
1393
1409
|
if (!scopeState.$lazy.value && !scopeState.$silent.value && !fromParent) for (const rule of Object.values($rules.value)) rule.$parse();
|
|
1394
1410
|
}
|
|
1395
1411
|
function abortCommit() {
|
|
@@ -1579,8 +1595,8 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
|
|
|
1579
1595
|
}
|
|
1580
1596
|
$value.value = $selfStatus.value.$value;
|
|
1581
1597
|
if (Array.isArray(state.value) && (!immediateScopeState.isPrimitiveArray.value || schemaMode)) $eachStatus.value = state.value.map((value, index) => {
|
|
1582
|
-
const { scope
|
|
1583
|
-
if (scope
|
|
1598
|
+
const { scope, unwrapped } = unwrapGetter(rulesDef.value.$each, toRef(() => value), index);
|
|
1599
|
+
if (scope) collectionScopes.push(scope);
|
|
1584
1600
|
const initialStateRef = toRef(initialState.value ?? [], index);
|
|
1585
1601
|
const $externalErrors = toRef(externalErrors?.value ?? {}, `$each`);
|
|
1586
1602
|
const $schemaErrors = computed(() => schemaErrors?.value?.$each);
|
|
@@ -1639,8 +1655,8 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
|
|
|
1639
1655
|
}
|
|
1640
1656
|
return null;
|
|
1641
1657
|
} else {
|
|
1642
|
-
const { scope
|
|
1643
|
-
if (scope
|
|
1658
|
+
const { scope, unwrapped } = unwrapGetter(rulesDef.value.$each, currentValue, index);
|
|
1659
|
+
if (scope) collectionScopes.push(scope);
|
|
1644
1660
|
const $externalErrors = toRef(externalErrors?.value ?? {}, `$each`);
|
|
1645
1661
|
const $schemaErrors = computed(() => schemaErrors?.value?.$each ?? []);
|
|
1646
1662
|
const element = createCollectionElement({
|
|
@@ -1791,8 +1807,8 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
|
|
|
1791
1807
|
});
|
|
1792
1808
|
function processShortcuts() {
|
|
1793
1809
|
if (shortcuts?.collections) Object.entries(shortcuts?.collections).forEach(([key, value]) => {
|
|
1794
|
-
const scope
|
|
1795
|
-
$shortcuts
|
|
1810
|
+
const scope = effectScope();
|
|
1811
|
+
$shortcuts[key] = scope.run(() => {
|
|
1796
1812
|
const result = ref();
|
|
1797
1813
|
watchEffect(() => {
|
|
1798
1814
|
result.value = value(reactive({
|
|
@@ -1820,10 +1836,10 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
|
|
|
1820
1836
|
});
|
|
1821
1837
|
return result;
|
|
1822
1838
|
});
|
|
1823
|
-
collectionScopes.push(scope
|
|
1839
|
+
collectionScopes.push(scope);
|
|
1824
1840
|
});
|
|
1825
1841
|
}
|
|
1826
|
-
const $shortcuts
|
|
1842
|
+
const $shortcuts = {};
|
|
1827
1843
|
processShortcuts();
|
|
1828
1844
|
return {
|
|
1829
1845
|
$dirty,
|
|
@@ -1836,7 +1852,7 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
|
|
|
1836
1852
|
$silentErrors,
|
|
1837
1853
|
$ready,
|
|
1838
1854
|
$name,
|
|
1839
|
-
$shortcuts
|
|
1855
|
+
$shortcuts,
|
|
1840
1856
|
$silentValue,
|
|
1841
1857
|
$edited,
|
|
1842
1858
|
$anyEdited,
|
|
@@ -1868,24 +1884,24 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
|
|
|
1868
1884
|
$each.$touch(runCommit, withConditions);
|
|
1869
1885
|
});
|
|
1870
1886
|
}
|
|
1871
|
-
function $reset(options
|
|
1887
|
+
function $reset(options, fromParent) {
|
|
1872
1888
|
$unwatch();
|
|
1873
|
-
if (!fromParent) if (options
|
|
1889
|
+
if (!fromParent) if (options?.toOriginalState) {
|
|
1874
1890
|
state.value = cloneDeep(originalState);
|
|
1875
1891
|
initialState.value = cloneDeep(originalState);
|
|
1876
|
-
} else if (options
|
|
1877
|
-
else if (options
|
|
1892
|
+
} else if (options?.toInitialState) state.value = cloneDeep(initialState.value);
|
|
1893
|
+
else if (options?.toState) {
|
|
1878
1894
|
let newInitialState;
|
|
1879
|
-
if (typeof options
|
|
1880
|
-
else newInitialState = options
|
|
1895
|
+
if (typeof options?.toState === "function") newInitialState = options?.toState();
|
|
1896
|
+
else newInitialState = options?.toState;
|
|
1881
1897
|
initialState.value = cloneDeep(newInitialState);
|
|
1882
1898
|
state.value = cloneDeep(newInitialState);
|
|
1883
1899
|
} else initialState.value = cloneDeep(state.value);
|
|
1884
|
-
if (options
|
|
1885
|
-
if (!options
|
|
1886
|
-
$selfStatus.value.$reset(options
|
|
1900
|
+
if (options?.clearExternalErrors) $clearExternalErrors();
|
|
1901
|
+
if (!options?.keepValidationState) {
|
|
1902
|
+
$selfStatus.value.$reset(options, fromParent);
|
|
1887
1903
|
$eachStatus.value.forEach(($each) => {
|
|
1888
|
-
$each.$reset(options
|
|
1904
|
+
$each.$reset(options, true);
|
|
1889
1905
|
});
|
|
1890
1906
|
}
|
|
1891
1907
|
if (!fromParent) createStatus();
|
|
@@ -2239,8 +2255,8 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
|
|
|
2239
2255
|
});
|
|
2240
2256
|
function processShortcuts() {
|
|
2241
2257
|
if (commonArgs.shortcuts?.nested) Object.entries(commonArgs.shortcuts.nested).forEach(([key, value]) => {
|
|
2242
|
-
const scope
|
|
2243
|
-
$shortcuts
|
|
2258
|
+
const scope = effectScope();
|
|
2259
|
+
$shortcuts[key] = scope.run(() => {
|
|
2244
2260
|
const result = ref();
|
|
2245
2261
|
watchEffect(() => {
|
|
2246
2262
|
result.value = value(reactive({
|
|
@@ -2269,7 +2285,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
|
|
|
2269
2285
|
});
|
|
2270
2286
|
return result;
|
|
2271
2287
|
});
|
|
2272
|
-
nestedScopes.push(scope
|
|
2288
|
+
nestedScopes.push(scope);
|
|
2273
2289
|
});
|
|
2274
2290
|
}
|
|
2275
2291
|
const $groups = computed({
|
|
@@ -2291,7 +2307,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
|
|
|
2291
2307
|
},
|
|
2292
2308
|
set() {}
|
|
2293
2309
|
});
|
|
2294
|
-
const $shortcuts
|
|
2310
|
+
const $shortcuts = {};
|
|
2295
2311
|
processShortcuts();
|
|
2296
2312
|
return {
|
|
2297
2313
|
$dirty,
|
|
@@ -2305,7 +2321,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
|
|
|
2305
2321
|
$silentErrors,
|
|
2306
2322
|
$ready,
|
|
2307
2323
|
$name,
|
|
2308
|
-
$shortcuts
|
|
2324
|
+
$shortcuts,
|
|
2309
2325
|
$groups,
|
|
2310
2326
|
$silentValue,
|
|
2311
2327
|
$edited,
|
|
@@ -2433,7 +2449,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
|
|
|
2433
2449
|
...createStandardSchema($validate)
|
|
2434
2450
|
});
|
|
2435
2451
|
watchEffect(() => {
|
|
2436
|
-
for (const key of Object.keys(fullStatus).filter((key
|
|
2452
|
+
for (const key of Object.keys(fullStatus).filter((key) => !key.startsWith("$") && !key.startsWith("~"))) delete fullStatus[key];
|
|
2437
2453
|
for (const field of Object.values($fields.value)) if (field?.$name) Object.assign(fullStatus, { [field.$name]: field });
|
|
2438
2454
|
});
|
|
2439
2455
|
return fullStatus;
|
|
@@ -2442,7 +2458,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
|
|
|
2442
2458
|
* Main resolver divider, will distribute the logic depending on the type of the current value (primitive, object, array)
|
|
2443
2459
|
*/
|
|
2444
2460
|
function createReactiveChildrenStatus({ rulesDef, ...properties }) {
|
|
2445
|
-
if (isCollectionRulesDef(rulesDef, properties.state
|
|
2461
|
+
if (isCollectionRulesDef(rulesDef, properties.state)) return createReactiveCollectionStatus({
|
|
2446
2462
|
rulesDef,
|
|
2447
2463
|
...properties
|
|
2448
2464
|
});
|
|
@@ -2818,9 +2834,9 @@ function buildInspectorState(nodeId, getInstance) {
|
|
|
2818
2834
|
const ruleInfo = parseRuleNodeId(nodeId);
|
|
2819
2835
|
if (ruleInfo) {
|
|
2820
2836
|
const { instanceId, fieldName, ruleName } = ruleInfo;
|
|
2821
|
-
const instance
|
|
2822
|
-
if (!instance
|
|
2823
|
-
const fieldStatus = resolveFieldByPath(instance
|
|
2837
|
+
const instance = getInstance(instanceId);
|
|
2838
|
+
if (!instance || !instance.r$.$fields) return null;
|
|
2839
|
+
const fieldStatus = resolveFieldByPath(instance.r$.$fields, fieldName);
|
|
2824
2840
|
if (!fieldStatus || !("$rules" in fieldStatus)) return null;
|
|
2825
2841
|
const ruleStatus = fieldStatus.$rules[ruleName];
|
|
2826
2842
|
if (!ruleStatus) return null;
|
|
@@ -2829,9 +2845,9 @@ function buildInspectorState(nodeId, getInstance) {
|
|
|
2829
2845
|
const fieldInfo = parseFieldNodeId(nodeId);
|
|
2830
2846
|
if (fieldInfo) {
|
|
2831
2847
|
const { instanceId, fieldName } = fieldInfo;
|
|
2832
|
-
const instance
|
|
2833
|
-
if (!instance
|
|
2834
|
-
const fieldStatus = resolveFieldByPath(instance
|
|
2848
|
+
const instance = getInstance(instanceId);
|
|
2849
|
+
if (!instance || !instance.r$.$fields) return null;
|
|
2850
|
+
const fieldStatus = resolveFieldByPath(instance.r$.$fields, fieldName);
|
|
2835
2851
|
if (!fieldStatus) return null;
|
|
2836
2852
|
return buildFieldState(fieldStatus);
|
|
2837
2853
|
}
|
|
@@ -3117,7 +3133,7 @@ function filterInspectorTree(nodes, filter) {
|
|
|
3117
3133
|
return filtered;
|
|
3118
3134
|
}
|
|
3119
3135
|
|
|
3120
|
-
var version$1 = "1.18.0
|
|
3136
|
+
var version$1 = "1.18.0";
|
|
3121
3137
|
|
|
3122
3138
|
function createDevtools(app) {
|
|
3123
3139
|
setupDevtoolsPlugin({
|
|
@@ -3318,18 +3334,18 @@ function createUseRegleComposable(options) {
|
|
|
3318
3334
|
clearExternalErrorsOnChange: modifiers?.clearExternalErrorsOnChange,
|
|
3319
3335
|
immediateDirty: modifiers?.immediateDirty
|
|
3320
3336
|
};
|
|
3321
|
-
function useRegle
|
|
3337
|
+
function useRegle(state, rulesFactory, options) {
|
|
3322
3338
|
return { r$: createRootRegleLogic({
|
|
3323
3339
|
state: isRef(state) ? state : ref(state),
|
|
3324
3340
|
rulesFactory,
|
|
3325
|
-
options
|
|
3341
|
+
options,
|
|
3326
3342
|
globalOptions,
|
|
3327
3343
|
customRules,
|
|
3328
3344
|
shortcuts,
|
|
3329
3345
|
overrides
|
|
3330
3346
|
}).regle };
|
|
3331
3347
|
}
|
|
3332
|
-
return useRegle
|
|
3348
|
+
return useRegle;
|
|
3333
3349
|
}
|
|
3334
3350
|
/**
|
|
3335
3351
|
* `useRegle` serves as the foundation for validation logic.
|
|
@@ -3367,10 +3383,10 @@ function createUseRegleComposable(options) {
|
|
|
3367
3383
|
const useRegle = createUseRegleComposable();
|
|
3368
3384
|
|
|
3369
3385
|
function createInferRuleHelper() {
|
|
3370
|
-
function inferRules
|
|
3386
|
+
function inferRules(state, rulesFactory) {
|
|
3371
3387
|
return rulesFactory;
|
|
3372
3388
|
}
|
|
3373
|
-
return inferRules
|
|
3389
|
+
return inferRules;
|
|
3374
3390
|
}
|
|
3375
3391
|
/**
|
|
3376
3392
|
* Type helper to provide autocomplete and type-checking for your form rules.
|
|
@@ -3401,6 +3417,84 @@ function createInferRuleHelper() {
|
|
|
3401
3417
|
*/
|
|
3402
3418
|
const inferRules = createInferRuleHelper();
|
|
3403
3419
|
|
|
3420
|
+
/**
|
|
3421
|
+
* Retrieves error messages for a specific field using a dot-notation path.
|
|
3422
|
+
* Provides type-safe access to nested error arrays in a Regle instance.
|
|
3423
|
+
*
|
|
3424
|
+
* @typeParam TRegle - The Regle instance type (root or status)
|
|
3425
|
+
* @typeParam TPath - The dot-notation path type, validated at compile-time
|
|
3426
|
+
*
|
|
3427
|
+
* @param r$ - The Regle instance (e.g., from `useRegle()`)
|
|
3428
|
+
* @param path - Dot-notation path to the field (e.g., `'user.email'` or `'items.0.name'`)
|
|
3429
|
+
* @returns Array of error strings for the field, or `undefined` if path doesn't exist
|
|
3430
|
+
*
|
|
3431
|
+
* @example
|
|
3432
|
+
* ```ts
|
|
3433
|
+
* import { getErrors, useRegle } from '@regle/core';
|
|
3434
|
+
* import { required, email } from '@regle/rules';
|
|
3435
|
+
*
|
|
3436
|
+
* const { r$ } = useRegle(
|
|
3437
|
+
* { user: { email: '' } },
|
|
3438
|
+
* { user: { email: { required, email } } }
|
|
3439
|
+
* );
|
|
3440
|
+
*
|
|
3441
|
+
* await r$.$validate();
|
|
3442
|
+
*
|
|
3443
|
+
* // Type-safe access to nested errors
|
|
3444
|
+
* const emailErrors = getErrors(r$, 'user.email');
|
|
3445
|
+
* // ['This field is required']
|
|
3446
|
+
*
|
|
3447
|
+
* // Works with collections too
|
|
3448
|
+
* const itemErrors = getErrors(r$, 'items.0.name');
|
|
3449
|
+
* ```
|
|
3450
|
+
*
|
|
3451
|
+
* @see {@link https://reglejs.dev/core-concepts/displaying-errors#get-errors-by-path Documentation}
|
|
3452
|
+
*/
|
|
3453
|
+
function getErrors(r$, path) {
|
|
3454
|
+
return getDotPath(unref(r$).$errors, String(path));
|
|
3455
|
+
}
|
|
3456
|
+
|
|
3457
|
+
/**
|
|
3458
|
+
* Retrieves detailed validation issues for a specific field using a dot-notation path.
|
|
3459
|
+
* Issues contain more information than errors, including the rule name, property, and metadata.
|
|
3460
|
+
*
|
|
3461
|
+
* @typeParam TRegle - The Regle instance type (root or status)
|
|
3462
|
+
* @typeParam TPath - The dot-notation path type, validated at compile-time
|
|
3463
|
+
*
|
|
3464
|
+
* @param r$ - The Regle instance (e.g., from `useRegle()`)
|
|
3465
|
+
* @param path - Dot-notation path to the field (e.g., `'user.email'` or `'items.0.name'`)
|
|
3466
|
+
* @returns Array of `RegleFieldIssue` objects for the field, or `undefined` if path doesn't exist
|
|
3467
|
+
*
|
|
3468
|
+
* @example
|
|
3469
|
+
* ```ts
|
|
3470
|
+
* import { getIssues, useRegle } from '@regle/core';
|
|
3471
|
+
* import { required, email } from '@regle/rules';
|
|
3472
|
+
*
|
|
3473
|
+
* const { r$ } = useRegle(
|
|
3474
|
+
* { user: { email: '' } },
|
|
3475
|
+
* { user: { email: { required, email } } }
|
|
3476
|
+
* );
|
|
3477
|
+
*
|
|
3478
|
+
* await r$.$validate();
|
|
3479
|
+
*
|
|
3480
|
+
* // Type-safe access to nested issues with full metadata
|
|
3481
|
+
* const emailIssues = getIssues(r$, 'user.email');
|
|
3482
|
+
* // [{
|
|
3483
|
+
* // $message: 'This field is required',
|
|
3484
|
+
* // $property: 'email',
|
|
3485
|
+
* // $rule: 'required'
|
|
3486
|
+
* // }]
|
|
3487
|
+
*
|
|
3488
|
+
* // Works with collections too
|
|
3489
|
+
* const itemIssues = getIssues(r$, 'items.0.name');
|
|
3490
|
+
* ```
|
|
3491
|
+
*
|
|
3492
|
+
* @see {@link https://reglejs.dev/core-concepts/displaying-errors#get-issues-by-path Documentation}
|
|
3493
|
+
*/
|
|
3494
|
+
function getIssues(r$, path) {
|
|
3495
|
+
return getDotPath(unref(r$).$issues, String(path));
|
|
3496
|
+
}
|
|
3497
|
+
|
|
3404
3498
|
function createEmptyRuleState(rules) {
|
|
3405
3499
|
const result = {};
|
|
3406
3500
|
if (Object.entries(rules).some(([_, rule]) => isRuleDef(rule) || typeof rule === "function")) return null;
|
|
@@ -3422,19 +3516,19 @@ function createUseRulesComposable(options) {
|
|
|
3422
3516
|
clearExternalErrorsOnChange: modifiers?.clearExternalErrorsOnChange,
|
|
3423
3517
|
immediateDirty: modifiers?.immediateDirty
|
|
3424
3518
|
};
|
|
3425
|
-
function useRules
|
|
3519
|
+
function useRules(rulesFactory, options) {
|
|
3426
3520
|
const definedRules = isRef(rulesFactory) ? rulesFactory : typeof rulesFactory === "function" ? void 0 : computed(() => rulesFactory);
|
|
3427
3521
|
return createRootRegleLogic({
|
|
3428
3522
|
state: ref(createEmptyRuleState(definedRules?.value)),
|
|
3429
3523
|
rulesFactory: definedRules,
|
|
3430
|
-
options
|
|
3524
|
+
options,
|
|
3431
3525
|
globalOptions,
|
|
3432
3526
|
customRules,
|
|
3433
3527
|
shortcuts,
|
|
3434
3528
|
overrides
|
|
3435
3529
|
}).regle;
|
|
3436
3530
|
}
|
|
3437
|
-
return useRules
|
|
3531
|
+
return useRules;
|
|
3438
3532
|
}
|
|
3439
3533
|
/**
|
|
3440
3534
|
* `useRules` is a variant of `useRegle` that doesn't require you to provide initial state.
|
|
@@ -3534,34 +3628,34 @@ function applyMarkStatic(value) {
|
|
|
3534
3628
|
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
3535
3629
|
*/
|
|
3536
3630
|
function defineRegleConfig({ rules, modifiers, shortcuts, overrides }) {
|
|
3537
|
-
const useRegle
|
|
3631
|
+
const useRegle = createUseRegleComposable({
|
|
3538
3632
|
rules,
|
|
3539
3633
|
modifiers,
|
|
3540
3634
|
shortcuts,
|
|
3541
3635
|
overrides
|
|
3542
3636
|
});
|
|
3543
|
-
const useRules
|
|
3637
|
+
const useRules = createUseRulesComposable({
|
|
3544
3638
|
rules,
|
|
3545
3639
|
modifiers,
|
|
3546
3640
|
shortcuts,
|
|
3547
3641
|
overrides
|
|
3548
3642
|
});
|
|
3549
|
-
useRegle
|
|
3643
|
+
useRegle.__config = {
|
|
3550
3644
|
rules,
|
|
3551
3645
|
modifiers,
|
|
3552
3646
|
shortcuts,
|
|
3553
3647
|
overrides
|
|
3554
3648
|
};
|
|
3555
|
-
useRules
|
|
3649
|
+
useRules.__config = {
|
|
3556
3650
|
rules,
|
|
3557
3651
|
modifiers,
|
|
3558
3652
|
shortcuts,
|
|
3559
3653
|
overrides
|
|
3560
3654
|
};
|
|
3561
3655
|
return {
|
|
3562
|
-
useRegle
|
|
3656
|
+
useRegle,
|
|
3563
3657
|
inferRules: createInferRuleHelper(),
|
|
3564
|
-
useRules
|
|
3658
|
+
useRules
|
|
3565
3659
|
};
|
|
3566
3660
|
}
|
|
3567
3661
|
/**
|
|
@@ -3600,20 +3694,20 @@ function extendRegleConfig(regle, { rules, modifiers, shortcuts, overrides }) {
|
|
|
3600
3694
|
const newModifiers = rootConfig.modifiers && modifiers ? merge(rootConfig.modifiers, modifiers) : rootConfig.modifiers ?? modifiers;
|
|
3601
3695
|
const newShortcuts = rootConfig.shortcuts && shortcuts ? merge(rootConfig.shortcuts, shortcuts) : rootConfig.shortcuts ?? shortcuts;
|
|
3602
3696
|
const newOverrides = rootConfig.overrides && overrides ? merge(rootConfig.overrides, overrides) : rootConfig.overrides ?? overrides;
|
|
3603
|
-
const useRegle
|
|
3697
|
+
const useRegle = createUseRegleComposable({
|
|
3604
3698
|
rules: newRules,
|
|
3605
3699
|
modifiers: newModifiers,
|
|
3606
3700
|
shortcuts: newShortcuts,
|
|
3607
3701
|
overrides: newOverrides
|
|
3608
3702
|
});
|
|
3609
|
-
useRegle
|
|
3703
|
+
useRegle.__config = {
|
|
3610
3704
|
rules: newRules,
|
|
3611
3705
|
modifiers: newModifiers,
|
|
3612
3706
|
shortcuts: newShortcuts,
|
|
3613
3707
|
overrides: newOverrides
|
|
3614
3708
|
};
|
|
3615
3709
|
return {
|
|
3616
|
-
useRegle
|
|
3710
|
+
useRegle,
|
|
3617
3711
|
inferRules: createInferRuleHelper()
|
|
3618
3712
|
};
|
|
3619
3713
|
}
|
|
@@ -3827,14 +3921,14 @@ function mergeRegles(regles, _scoped) {
|
|
|
3827
3921
|
});
|
|
3828
3922
|
watchEffect(() => {
|
|
3829
3923
|
if (scoped) return;
|
|
3830
|
-
for (const key of Object.keys(fullStatus).filter((key
|
|
3924
|
+
for (const key of Object.keys(fullStatus).filter((key) => !key.startsWith("$") && !key.startsWith("~"))) delete fullStatus[key];
|
|
3831
3925
|
for (const [key, field] of Object.entries($instances.value)) Object.assign(fullStatus, { [key]: field });
|
|
3832
3926
|
});
|
|
3833
3927
|
return fullStatus;
|
|
3834
3928
|
}
|
|
3835
3929
|
|
|
3836
3930
|
function createUseCollectScope(instances, options) {
|
|
3837
|
-
function useCollectScope
|
|
3931
|
+
function useCollectScope(namespace) {
|
|
3838
3932
|
const computedNamespace = computed(() => toValue(namespace));
|
|
3839
3933
|
const namespaceInstances = reactive({});
|
|
3840
3934
|
setEmptyNamespace();
|
|
@@ -3843,8 +3937,8 @@ function createUseCollectScope(instances, options) {
|
|
|
3843
3937
|
function setEmptyNamespace() {
|
|
3844
3938
|
if (computedNamespace.value) {
|
|
3845
3939
|
if (typeof computedNamespace.value === "string" && !instances.value[computedNamespace.value]) instances.value[computedNamespace.value] = {};
|
|
3846
|
-
else if (Array.isArray(computedNamespace.value)) computedNamespace.value.forEach((namespace
|
|
3847
|
-
if (!instances.value[namespace
|
|
3940
|
+
else if (Array.isArray(computedNamespace.value)) computedNamespace.value.forEach((namespace) => {
|
|
3941
|
+
if (!instances.value[namespace]) instances.value[namespace] = {};
|
|
3848
3942
|
});
|
|
3849
3943
|
}
|
|
3850
3944
|
}
|
|
@@ -3858,9 +3952,9 @@ function createUseCollectScope(instances, options) {
|
|
|
3858
3952
|
Object.keys(namespaceInstances).forEach((key) => {
|
|
3859
3953
|
delete namespaceInstances[key];
|
|
3860
3954
|
});
|
|
3861
|
-
computedNamespace.value.forEach((namespace
|
|
3862
|
-
Object.entries(r$Instances[namespace
|
|
3863
|
-
Object.assign(namespaceInstances, { [key]: regle
|
|
3955
|
+
computedNamespace.value.forEach((namespace) => {
|
|
3956
|
+
Object.entries(r$Instances[namespace]).forEach(([key, regle]) => {
|
|
3957
|
+
Object.assign(namespaceInstances, { [key]: regle });
|
|
3864
3958
|
});
|
|
3865
3959
|
});
|
|
3866
3960
|
return mergeRegles(namespaceInstances, !options.asRecord);
|
|
@@ -3869,12 +3963,12 @@ function createUseCollectScope(instances, options) {
|
|
|
3869
3963
|
}
|
|
3870
3964
|
return { r$: regle.r$ };
|
|
3871
3965
|
}
|
|
3872
|
-
return { useCollectScope
|
|
3966
|
+
return { useCollectScope };
|
|
3873
3967
|
}
|
|
3874
3968
|
|
|
3875
3969
|
function createUseScopedRegleComposable(instances, customUseRegle) {
|
|
3876
3970
|
const scopedUseRegle = customUseRegle ?? useRegle;
|
|
3877
|
-
const useScopedRegle
|
|
3971
|
+
const useScopedRegle = ((state, rulesFactory, options) => {
|
|
3878
3972
|
const { namespace, scopeKey, id, ...restOptions } = options ?? {};
|
|
3879
3973
|
scopedUseRegle.__config ??= {};
|
|
3880
3974
|
const computedScopeId = computed(() => id ?? scopeKey);
|
|
@@ -3920,7 +4014,7 @@ function createUseScopedRegleComposable(instances, customUseRegle) {
|
|
|
3920
4014
|
register
|
|
3921
4015
|
};
|
|
3922
4016
|
});
|
|
3923
|
-
return { useScopedRegle
|
|
4017
|
+
return { useScopedRegle };
|
|
3924
4018
|
}
|
|
3925
4019
|
|
|
3926
4020
|
/**
|
|
@@ -3962,11 +4056,11 @@ function createScopedUseRegle(options) {
|
|
|
3962
4056
|
} : createGlobalState(() => {
|
|
3963
4057
|
return ref({ "~~global": {} });
|
|
3964
4058
|
}))();
|
|
3965
|
-
const { useScopedRegle
|
|
3966
|
-
const { useCollectScope
|
|
4059
|
+
const { useScopedRegle } = createUseScopedRegleComposable(instances, options?.customUseRegle);
|
|
4060
|
+
const { useCollectScope } = createUseCollectScope(instances, { asRecord: options?.asRecord });
|
|
3967
4061
|
return {
|
|
3968
|
-
useScopedRegle
|
|
3969
|
-
useCollectScope
|
|
4062
|
+
useScopedRegle,
|
|
4063
|
+
useCollectScope
|
|
3970
4064
|
};
|
|
3971
4065
|
}
|
|
3972
4066
|
const { useCollectScope: _useCollectScope, useScopedRegle: _useScopedRegle } = createScopedUseRegle();
|
|
@@ -4203,4 +4297,4 @@ const RegleVuePlugin = { install(app) {
|
|
|
4203
4297
|
if (typeof window !== "undefined" && true) createDevtools(app);
|
|
4204
4298
|
} };
|
|
4205
4299
|
|
|
4206
|
-
export { InternalRuleType, RegleVuePlugin, createRule, createScopedUseRegle, createVariant, defineRegleConfig, defineRules, extendRegleConfig, flatErrors, inferRules, markStatic, mergeRegles, narrowVariant, refineRules, unwrapRuleParameters, useCollectScope, useRegle, useRootStorage, useRules, useScopedRegle, variantToRef };
|
|
4300
|
+
export { InternalRuleType, RegleVuePlugin, createRule, createScopedUseRegle, createVariant, defineRegleConfig, defineRules, extendRegleConfig, flatErrors, getErrors, getIssues, inferRules, markStatic, mergeRegles, narrowVariant, refineRules, unwrapRuleParameters, useCollectScope, useRegle, useRootStorage, useRules, useScopedRegle, variantToRef };
|