@regle/core 0.6.1 → 0.7.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.cjs +180 -94
- package/dist/regle-core.d.cts +8 -5
- package/dist/regle-core.d.ts +8 -5
- package/dist/regle-core.min.cjs +2 -2
- package/dist/regle-core.min.mjs +2 -2
- package/dist/regle-core.mjs +180 -94
- package/package.json +2 -1
package/dist/regle-core.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { toValue, version, ref, getCurrentScope, onScopeDispose, reactive, shall
|
|
|
2
2
|
|
|
3
3
|
// ../shared/utils/isEmpty.ts
|
|
4
4
|
function isEmpty(value) {
|
|
5
|
-
if (value ===
|
|
5
|
+
if (value === undefined || value === null) {
|
|
6
6
|
return true;
|
|
7
7
|
}
|
|
8
8
|
if (value instanceof Date) {
|
|
@@ -308,7 +308,7 @@ function useStorage() {
|
|
|
308
308
|
}
|
|
309
309
|
function checkRuleDeclEntry($path, newRules) {
|
|
310
310
|
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
311
|
-
if (!storedRulesDefs) return
|
|
311
|
+
if (!storedRulesDefs) return undefined;
|
|
312
312
|
const storedRules = storedRulesDefs;
|
|
313
313
|
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
314
314
|
if (!isValidCache) return { valid: false };
|
|
@@ -403,7 +403,7 @@ function debounce(func, wait, immediate) {
|
|
|
403
403
|
const debouncedFn = (...args) => new Promise((resolve) => {
|
|
404
404
|
clearTimeout(timeout);
|
|
405
405
|
timeout = setTimeout(() => {
|
|
406
|
-
timeout =
|
|
406
|
+
timeout = undefined;
|
|
407
407
|
{
|
|
408
408
|
Promise.resolve(func.apply(this, [...args])).then(resolve);
|
|
409
409
|
}
|
|
@@ -411,7 +411,7 @@ function debounce(func, wait, immediate) {
|
|
|
411
411
|
});
|
|
412
412
|
debouncedFn.cancel = () => {
|
|
413
413
|
clearTimeout(timeout);
|
|
414
|
-
timeout =
|
|
414
|
+
timeout = undefined;
|
|
415
415
|
};
|
|
416
416
|
return debouncedFn;
|
|
417
417
|
}
|
|
@@ -473,10 +473,10 @@ function extractRulesErrors({
|
|
|
473
473
|
field,
|
|
474
474
|
silent = false
|
|
475
475
|
}) {
|
|
476
|
-
return Object.entries(field.$rules ?? {}).map(([
|
|
476
|
+
return Object.entries(field.$rules ?? {}).map(([_, rule]) => {
|
|
477
477
|
if (silent) {
|
|
478
478
|
return rule.$message;
|
|
479
|
-
} else if (!rule.$valid && field.$
|
|
479
|
+
} else if (!rule.$valid && field.$error && !rule.$validating) {
|
|
480
480
|
return rule.$message;
|
|
481
481
|
}
|
|
482
482
|
return null;
|
|
@@ -486,10 +486,10 @@ function extractRulesErrors({
|
|
|
486
486
|
} else {
|
|
487
487
|
return acc?.concat(value);
|
|
488
488
|
}
|
|
489
|
-
}, []).concat(field.$
|
|
489
|
+
}, []).concat(field.$error ? field.$externalErrors ?? [] : []).concat(field.$error ? field.$schemaErrors ?? [] : []);
|
|
490
490
|
}
|
|
491
491
|
function extractRulesTooltips({ field }) {
|
|
492
|
-
return Object.entries(field.$rules ?? {}).map(([
|
|
492
|
+
return Object.entries(field.$rules ?? {}).map(([_, rule]) => rule.$tooltip).filter((tooltip) => !!tooltip).reduce((acc, value) => {
|
|
493
493
|
if (typeof value === "string") {
|
|
494
494
|
return acc?.concat([value]);
|
|
495
495
|
} else {
|
|
@@ -544,7 +544,7 @@ function createReactiveRuleStatus({
|
|
|
544
544
|
});
|
|
545
545
|
function computeRuleProcessor(key) {
|
|
546
546
|
let result = "";
|
|
547
|
-
const customProcessor = customMessages ? customMessages[ruleKey]?.[key] :
|
|
547
|
+
const customProcessor = customMessages ? customMessages[ruleKey]?.[key] : undefined;
|
|
548
548
|
if (customProcessor) {
|
|
549
549
|
if (typeof customProcessor === "function") {
|
|
550
550
|
result = customProcessor($defaultMetadata.value);
|
|
@@ -718,6 +718,8 @@ function createReactiveFieldStatus({
|
|
|
718
718
|
storage,
|
|
719
719
|
options,
|
|
720
720
|
externalErrors,
|
|
721
|
+
schemaErrors,
|
|
722
|
+
schemaMode,
|
|
721
723
|
onUnwatch,
|
|
722
724
|
$isArray,
|
|
723
725
|
initialState,
|
|
@@ -810,7 +812,7 @@ function createReactiveFieldStatus({
|
|
|
810
812
|
}
|
|
811
813
|
scopeState = scope.run(() => {
|
|
812
814
|
const $dirty = ref(false);
|
|
813
|
-
const
|
|
815
|
+
const triggerPunishment2 = ref(false);
|
|
814
816
|
const $anyDirty = computed(() => $dirty.value);
|
|
815
817
|
const $debounce2 = computed(() => {
|
|
816
818
|
return $localOptions.value.$debounce;
|
|
@@ -866,16 +868,25 @@ function createReactiveFieldStatus({
|
|
|
866
868
|
return $invalid.value && !$pending.value && $dirty.value;
|
|
867
869
|
});
|
|
868
870
|
const $errors = computed(() => {
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
871
|
+
return extractRulesErrors({
|
|
872
|
+
field: {
|
|
873
|
+
$error: $error.value,
|
|
874
|
+
$externalErrors: externalErrors?.value,
|
|
875
|
+
$schemaErrors: schemaErrors?.value,
|
|
876
|
+
$rules: $rules.value
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
const $silentErrors = computed(() => {
|
|
881
|
+
return extractRulesErrors({
|
|
882
|
+
field: {
|
|
883
|
+
$error: true,
|
|
884
|
+
$externalErrors: externalErrors?.value,
|
|
885
|
+
$schemaErrors: schemaErrors?.value,
|
|
886
|
+
$rules: $rules.value
|
|
887
|
+
},
|
|
888
|
+
silent: true
|
|
889
|
+
});
|
|
879
890
|
});
|
|
880
891
|
const $edited = computed(() => {
|
|
881
892
|
if ($dirty.value) {
|
|
@@ -900,16 +911,6 @@ function createReactiveFieldStatus({
|
|
|
900
911
|
}
|
|
901
912
|
});
|
|
902
913
|
});
|
|
903
|
-
const $silentErrors = computed(() => {
|
|
904
|
-
return extractRulesErrors({
|
|
905
|
-
field: {
|
|
906
|
-
$dirty: $dirty.value,
|
|
907
|
-
$externalErrors: externalErrors?.value,
|
|
908
|
-
$rules: $rules.value
|
|
909
|
-
},
|
|
910
|
-
silent: true
|
|
911
|
-
});
|
|
912
|
-
});
|
|
913
914
|
const $ready = computed(() => {
|
|
914
915
|
if (!$autoDirty2.value) {
|
|
915
916
|
return !($invalid.value || $pending.value);
|
|
@@ -917,7 +918,7 @@ function createReactiveFieldStatus({
|
|
|
917
918
|
return $anyDirty.value && !($invalid.value || $pending.value);
|
|
918
919
|
});
|
|
919
920
|
const $pending = computed(() => {
|
|
920
|
-
if (
|
|
921
|
+
if (triggerPunishment2.value || !$rewardEarly2.value) {
|
|
921
922
|
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
922
923
|
return ruleResult.$pending;
|
|
923
924
|
});
|
|
@@ -925,11 +926,11 @@ function createReactiveFieldStatus({
|
|
|
925
926
|
return false;
|
|
926
927
|
});
|
|
927
928
|
const $invalid = computed(() => {
|
|
928
|
-
if (externalErrors?.value?.length) {
|
|
929
|
+
if (externalErrors?.value?.length || schemaErrors?.value?.length) {
|
|
929
930
|
return true;
|
|
930
|
-
} else if (
|
|
931
|
+
} else if ($inactive.value) {
|
|
931
932
|
return false;
|
|
932
|
-
} else if (!$rewardEarly2.value || $rewardEarly2.value &&
|
|
933
|
+
} else if (!$rewardEarly2.value || $rewardEarly2.value && triggerPunishment2.value) {
|
|
933
934
|
return Object.entries($rules.value).some(([_, ruleResult]) => {
|
|
934
935
|
return !ruleResult.$valid;
|
|
935
936
|
});
|
|
@@ -938,7 +939,7 @@ function createReactiveFieldStatus({
|
|
|
938
939
|
});
|
|
939
940
|
const $name = computed(() => fieldName);
|
|
940
941
|
const $inactive = computed(() => {
|
|
941
|
-
if (isEmpty($rules.value)) {
|
|
942
|
+
if (isEmpty($rules.value) && !schemaMode) {
|
|
942
943
|
return true;
|
|
943
944
|
}
|
|
944
945
|
return false;
|
|
@@ -949,9 +950,13 @@ function createReactiveFieldStatus({
|
|
|
949
950
|
} else if ($inactive.value) {
|
|
950
951
|
return false;
|
|
951
952
|
} else if ($dirty.value && !isEmpty(state.value) && !$validating2.value) {
|
|
952
|
-
|
|
953
|
-
return
|
|
954
|
-
}
|
|
953
|
+
if (schemaMode) {
|
|
954
|
+
return !schemaErrors?.value?.length;
|
|
955
|
+
} else {
|
|
956
|
+
return Object.values($rules.value).every((ruleResult) => {
|
|
957
|
+
return ruleResult.$valid && ruleResult.$active;
|
|
958
|
+
});
|
|
959
|
+
}
|
|
955
960
|
}
|
|
956
961
|
return false;
|
|
957
962
|
});
|
|
@@ -999,7 +1004,7 @@ function createReactiveFieldStatus({
|
|
|
999
1004
|
const $shortcuts2 = {};
|
|
1000
1005
|
watch($invalid, (value) => {
|
|
1001
1006
|
if (!value) {
|
|
1002
|
-
|
|
1007
|
+
triggerPunishment2.value = false;
|
|
1003
1008
|
}
|
|
1004
1009
|
});
|
|
1005
1010
|
return {
|
|
@@ -1024,7 +1029,7 @@ function createReactiveFieldStatus({
|
|
|
1024
1029
|
$validating: $validating2,
|
|
1025
1030
|
$tooltips,
|
|
1026
1031
|
$dirty,
|
|
1027
|
-
triggerPunishment,
|
|
1032
|
+
triggerPunishment: triggerPunishment2,
|
|
1028
1033
|
processShortcuts,
|
|
1029
1034
|
$silentValue,
|
|
1030
1035
|
$inactive
|
|
@@ -1107,7 +1112,9 @@ function createReactiveFieldStatus({
|
|
|
1107
1112
|
} else if (scopeState.$autoDirty.value && scopeState.$dirty.value && !scopeState.$pending.value) {
|
|
1108
1113
|
return { result: !scopeState.$error.value, data };
|
|
1109
1114
|
}
|
|
1110
|
-
if (
|
|
1115
|
+
if (schemaMode) {
|
|
1116
|
+
return { result: !schemaErrors?.value?.length, data };
|
|
1117
|
+
} else if (isEmpty($rules.value)) {
|
|
1111
1118
|
return { result: true, data };
|
|
1112
1119
|
}
|
|
1113
1120
|
const results = await Promise.allSettled(
|
|
@@ -1158,6 +1165,7 @@ function createReactiveFieldStatus({
|
|
|
1158
1165
|
$haveAnyAsyncRule,
|
|
1159
1166
|
$debounce,
|
|
1160
1167
|
$lazy,
|
|
1168
|
+
triggerPunishment,
|
|
1161
1169
|
...restScope
|
|
1162
1170
|
} = scopeState;
|
|
1163
1171
|
return reactive({
|
|
@@ -1186,9 +1194,11 @@ function createCollectionElement({
|
|
|
1186
1194
|
customMessages,
|
|
1187
1195
|
rules,
|
|
1188
1196
|
externalErrors,
|
|
1197
|
+
schemaErrors,
|
|
1189
1198
|
initialState,
|
|
1190
1199
|
shortcuts,
|
|
1191
|
-
fieldName
|
|
1200
|
+
fieldName,
|
|
1201
|
+
schemaMode
|
|
1192
1202
|
}) {
|
|
1193
1203
|
const $fieldId = rules.$key ? rules.$key : randomId();
|
|
1194
1204
|
let $path = `${path}.${String($fieldId)}`;
|
|
@@ -1206,6 +1216,8 @@ function createCollectionElement({
|
|
|
1206
1216
|
$path = `${path}.${stateValue.value.$id}`;
|
|
1207
1217
|
}
|
|
1208
1218
|
}
|
|
1219
|
+
const $externalErrors = toRef(externalErrors?.value ?? [], index);
|
|
1220
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.[index]);
|
|
1209
1221
|
const $status = createReactiveChildrenStatus({
|
|
1210
1222
|
state: stateValue,
|
|
1211
1223
|
rulesDef: toRef(() => rules),
|
|
@@ -1213,10 +1225,12 @@ function createCollectionElement({
|
|
|
1213
1225
|
path: $path,
|
|
1214
1226
|
storage,
|
|
1215
1227
|
options,
|
|
1216
|
-
externalErrors:
|
|
1228
|
+
externalErrors: $externalErrors,
|
|
1229
|
+
schemaErrors: $schemaErrors,
|
|
1217
1230
|
initialState,
|
|
1218
1231
|
shortcuts,
|
|
1219
|
-
fieldName
|
|
1232
|
+
fieldName,
|
|
1233
|
+
schemaMode
|
|
1220
1234
|
});
|
|
1221
1235
|
if ($status) {
|
|
1222
1236
|
const valueId = stateValue.value?.$id;
|
|
@@ -1235,6 +1249,8 @@ function createReactiveCollectionStatus({
|
|
|
1235
1249
|
storage,
|
|
1236
1250
|
options,
|
|
1237
1251
|
externalErrors,
|
|
1252
|
+
schemaErrors,
|
|
1253
|
+
schemaMode,
|
|
1238
1254
|
initialState,
|
|
1239
1255
|
shortcuts,
|
|
1240
1256
|
fieldName
|
|
@@ -1245,7 +1261,7 @@ function createReactiveCollectionStatus({
|
|
|
1245
1261
|
let immediateScopeState;
|
|
1246
1262
|
let collectionScopes = [];
|
|
1247
1263
|
if (!Array.isArray(state.value) && !rulesDef.value.$each) {
|
|
1248
|
-
return
|
|
1264
|
+
return undefined;
|
|
1249
1265
|
}
|
|
1250
1266
|
const $id = ref();
|
|
1251
1267
|
const $value = ref(state.value);
|
|
@@ -1298,6 +1314,8 @@ function createReactiveCollectionStatus({
|
|
|
1298
1314
|
collectionScopes.push(scope2);
|
|
1299
1315
|
}
|
|
1300
1316
|
const initialStateRef = toRef(initialState.value ?? [], index);
|
|
1317
|
+
const $externalErrors = toRef(externalErrors?.value ?? {}, `$each`);
|
|
1318
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.$each);
|
|
1301
1319
|
const element = createCollectionElement({
|
|
1302
1320
|
$id: $id.value,
|
|
1303
1321
|
path,
|
|
@@ -1307,10 +1325,12 @@ function createReactiveCollectionStatus({
|
|
|
1307
1325
|
index,
|
|
1308
1326
|
options,
|
|
1309
1327
|
storage,
|
|
1310
|
-
externalErrors:
|
|
1328
|
+
externalErrors: $externalErrors,
|
|
1329
|
+
schemaErrors: $schemaErrors,
|
|
1311
1330
|
initialState: initialStateRef,
|
|
1312
1331
|
shortcuts,
|
|
1313
|
-
fieldName
|
|
1332
|
+
fieldName,
|
|
1333
|
+
schemaMode
|
|
1314
1334
|
});
|
|
1315
1335
|
if (element) {
|
|
1316
1336
|
return element;
|
|
@@ -1328,10 +1348,12 @@ function createReactiveCollectionStatus({
|
|
|
1328
1348
|
storage,
|
|
1329
1349
|
options,
|
|
1330
1350
|
externalErrors: toRef(externalErrors?.value ?? {}, `$self`),
|
|
1351
|
+
schemaErrors: computed(() => schemaErrors?.value?.$self),
|
|
1331
1352
|
$isArray: true,
|
|
1332
1353
|
initialState,
|
|
1333
1354
|
shortcuts,
|
|
1334
|
-
fieldName
|
|
1355
|
+
fieldName,
|
|
1356
|
+
schemaMode
|
|
1335
1357
|
});
|
|
1336
1358
|
}
|
|
1337
1359
|
function updateStatus() {
|
|
@@ -1351,26 +1373,28 @@ function createReactiveCollectionStatus({
|
|
|
1351
1373
|
if (scope2) {
|
|
1352
1374
|
collectionScopes.push(scope2);
|
|
1353
1375
|
}
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1376
|
+
const $externalErrors = toRef(externalErrors?.value ?? {}, `$each`);
|
|
1377
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.$each ?? []);
|
|
1378
|
+
const element = createCollectionElement({
|
|
1379
|
+
$id: $id.value,
|
|
1380
|
+
path,
|
|
1381
|
+
customMessages,
|
|
1382
|
+
rules: unwrapped ?? {},
|
|
1383
|
+
stateValue: currentValue,
|
|
1384
|
+
index,
|
|
1385
|
+
options,
|
|
1386
|
+
storage,
|
|
1387
|
+
externalErrors: $externalErrors,
|
|
1388
|
+
schemaErrors: $schemaErrors,
|
|
1389
|
+
initialState: toRef(initialState.value ?? [], index),
|
|
1390
|
+
shortcuts,
|
|
1391
|
+
fieldName,
|
|
1392
|
+
schemaMode
|
|
1393
|
+
});
|
|
1394
|
+
if (element) {
|
|
1395
|
+
return element;
|
|
1373
1396
|
}
|
|
1397
|
+
return null;
|
|
1374
1398
|
}
|
|
1375
1399
|
}).filter((each) => !!each);
|
|
1376
1400
|
previousStatus.filter(($each) => !state.value?.find((f) => $each.$id === f.$id)).forEach((_, index) => {
|
|
@@ -1626,6 +1650,8 @@ function createReactiveNestedStatus({
|
|
|
1626
1650
|
path = "",
|
|
1627
1651
|
rootRules,
|
|
1628
1652
|
externalErrors,
|
|
1653
|
+
schemaErrors,
|
|
1654
|
+
rootSchemaErrors,
|
|
1629
1655
|
validationGroups,
|
|
1630
1656
|
initialState,
|
|
1631
1657
|
fieldName,
|
|
@@ -1635,6 +1661,7 @@ function createReactiveNestedStatus({
|
|
|
1635
1661
|
let scopeState;
|
|
1636
1662
|
let nestedScopes = [];
|
|
1637
1663
|
let $unwatchRules = null;
|
|
1664
|
+
let $unwatchSchemaErrors = null;
|
|
1638
1665
|
let $unwatchExternalErrors = null;
|
|
1639
1666
|
let $unwatchState = null;
|
|
1640
1667
|
async function createReactiveFieldsStatus(watchSources = true) {
|
|
@@ -1645,6 +1672,7 @@ function createReactiveNestedStatus({
|
|
|
1645
1672
|
const stateRef = toRef(state.value ?? {}, statePropKey);
|
|
1646
1673
|
const statePropRulesRef = toRef(() => statePropRules);
|
|
1647
1674
|
const $externalErrors = toRef(externalErrors?.value ?? {}, statePropKey);
|
|
1675
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.[statePropKey]);
|
|
1648
1676
|
const initialStateRef = toRef(initialState?.value ?? {}, statePropKey);
|
|
1649
1677
|
return [
|
|
1650
1678
|
statePropKey,
|
|
@@ -1653,6 +1681,7 @@ function createReactiveNestedStatus({
|
|
|
1653
1681
|
rulesDef: statePropRulesRef,
|
|
1654
1682
|
path: path ? `${path}.${statePropKey}` : statePropKey,
|
|
1655
1683
|
externalErrors: $externalErrors,
|
|
1684
|
+
schemaErrors: $schemaErrors,
|
|
1656
1685
|
initialState: initialStateRef,
|
|
1657
1686
|
fieldName: statePropKey,
|
|
1658
1687
|
...commonArgs
|
|
@@ -1665,6 +1694,8 @@ function createReactiveNestedStatus({
|
|
|
1665
1694
|
const externalRulesStatus = Object.fromEntries(
|
|
1666
1695
|
Object.entries(unref(externalErrors) ?? {}).filter(([key, errors]) => !(key in rulesDef.value) && !!errors).map(([key]) => {
|
|
1667
1696
|
const stateRef = toRef(state.value ?? {}, key);
|
|
1697
|
+
const $externalErrors = toRef(externalErrors?.value ?? {}, key);
|
|
1698
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.[key]);
|
|
1668
1699
|
const initialStateRef = toRef(initialState?.value ?? {}, key);
|
|
1669
1700
|
return [
|
|
1670
1701
|
key,
|
|
@@ -1672,7 +1703,29 @@ function createReactiveNestedStatus({
|
|
|
1672
1703
|
state: stateRef,
|
|
1673
1704
|
rulesDef: computed(() => ({})),
|
|
1674
1705
|
path: path ? `${path}.${key}` : key,
|
|
1675
|
-
externalErrors:
|
|
1706
|
+
externalErrors: $externalErrors,
|
|
1707
|
+
schemaErrors: $schemaErrors,
|
|
1708
|
+
initialState: initialStateRef,
|
|
1709
|
+
fieldName: key,
|
|
1710
|
+
...commonArgs
|
|
1711
|
+
})
|
|
1712
|
+
];
|
|
1713
|
+
})
|
|
1714
|
+
);
|
|
1715
|
+
const schemasRulesStatus = Object.fromEntries(
|
|
1716
|
+
Object.entries(unref(schemaErrors) ?? {}).map(([key]) => {
|
|
1717
|
+
const stateRef = toRef(state.value ?? {}, key);
|
|
1718
|
+
const $externalErrors = toRef(externalErrors?.value ?? {}, key);
|
|
1719
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.[key]);
|
|
1720
|
+
const initialStateRef = toRef(initialState?.value ?? {}, key);
|
|
1721
|
+
return [
|
|
1722
|
+
key,
|
|
1723
|
+
createReactiveChildrenStatus({
|
|
1724
|
+
state: stateRef,
|
|
1725
|
+
rulesDef: computed(() => ({})),
|
|
1726
|
+
path: path ? `${path}.${key}` : key,
|
|
1727
|
+
externalErrors: $externalErrors,
|
|
1728
|
+
schemaErrors: $schemaErrors,
|
|
1676
1729
|
initialState: initialStateRef,
|
|
1677
1730
|
fieldName: key,
|
|
1678
1731
|
...commonArgs
|
|
@@ -1681,8 +1734,12 @@ function createReactiveNestedStatus({
|
|
|
1681
1734
|
})
|
|
1682
1735
|
);
|
|
1683
1736
|
const statesWithNoRules = Object.fromEntries(
|
|
1684
|
-
Object.entries(state.value ?? {}).filter(
|
|
1737
|
+
Object.entries(state.value ?? {}).filter(
|
|
1738
|
+
([key]) => !(key in rulesDef.value) && !(key in (externalRulesStatus ?? {})) && !(key in (schemasRulesStatus ?? {}))
|
|
1739
|
+
).map(([key]) => {
|
|
1685
1740
|
const stateRef = toRef(state.value ?? {}, key);
|
|
1741
|
+
const $externalErrors = toRef(externalErrors?.value ?? {}, key);
|
|
1742
|
+
const $schemaErrors = computed(() => schemaErrors?.value?.[key]);
|
|
1686
1743
|
const initialStateRef = toRef(initialState?.value ?? {}, key);
|
|
1687
1744
|
return [
|
|
1688
1745
|
key,
|
|
@@ -1690,7 +1747,8 @@ function createReactiveNestedStatus({
|
|
|
1690
1747
|
state: stateRef,
|
|
1691
1748
|
rulesDef: computed(() => ({})),
|
|
1692
1749
|
path: path ? `${path}.${key}` : key,
|
|
1693
|
-
externalErrors:
|
|
1750
|
+
externalErrors: $externalErrors,
|
|
1751
|
+
schemaErrors: $schemaErrors,
|
|
1694
1752
|
initialState: initialStateRef,
|
|
1695
1753
|
fieldName: key,
|
|
1696
1754
|
...commonArgs
|
|
@@ -1701,6 +1759,7 @@ function createReactiveNestedStatus({
|
|
|
1701
1759
|
$fields.value = {
|
|
1702
1760
|
...scopedRulesStatus,
|
|
1703
1761
|
...externalRulesStatus,
|
|
1762
|
+
...schemasRulesStatus,
|
|
1704
1763
|
...statesWithNoRules
|
|
1705
1764
|
};
|
|
1706
1765
|
if (watchSources) {
|
|
@@ -1723,7 +1782,7 @@ function createReactiveNestedStatus({
|
|
|
1723
1782
|
});
|
|
1724
1783
|
}
|
|
1725
1784
|
function define$WatchExternalErrors() {
|
|
1726
|
-
if (externalErrors
|
|
1785
|
+
if (externalErrors) {
|
|
1727
1786
|
$unwatchExternalErrors = watch(
|
|
1728
1787
|
externalErrors,
|
|
1729
1788
|
() => {
|
|
@@ -1758,6 +1817,17 @@ function createReactiveNestedStatus({
|
|
|
1758
1817
|
);
|
|
1759
1818
|
define$WatchExternalErrors();
|
|
1760
1819
|
}
|
|
1820
|
+
if (rootSchemaErrors) {
|
|
1821
|
+
$unwatchSchemaErrors?.();
|
|
1822
|
+
$unwatchSchemaErrors = watch(
|
|
1823
|
+
rootSchemaErrors,
|
|
1824
|
+
() => {
|
|
1825
|
+
$unwatch();
|
|
1826
|
+
createReactiveFieldsStatus();
|
|
1827
|
+
},
|
|
1828
|
+
{ deep: true, flush: "post" }
|
|
1829
|
+
);
|
|
1830
|
+
}
|
|
1761
1831
|
define$watchState();
|
|
1762
1832
|
scope = effectScope();
|
|
1763
1833
|
scopeState = scope.run(() => {
|
|
@@ -1819,8 +1889,9 @@ function createReactiveNestedStatus({
|
|
|
1819
1889
|
}
|
|
1820
1890
|
return $anyDirty.value && !($invalid.value || $pending.value);
|
|
1821
1891
|
});
|
|
1892
|
+
const $localPending2 = ref(false);
|
|
1822
1893
|
const $pending = computed(() => {
|
|
1823
|
-
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
1894
|
+
return $localPending2.value || Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
1824
1895
|
return statusOrField?.$pending;
|
|
1825
1896
|
});
|
|
1826
1897
|
});
|
|
@@ -1932,7 +2003,8 @@ function createReactiveNestedStatus({
|
|
|
1932
2003
|
$groups,
|
|
1933
2004
|
$silentValue,
|
|
1934
2005
|
$edited,
|
|
1935
|
-
$anyEdited
|
|
2006
|
+
$anyEdited,
|
|
2007
|
+
$localPending: $localPending2
|
|
1936
2008
|
};
|
|
1937
2009
|
});
|
|
1938
2010
|
}
|
|
@@ -1982,25 +2054,37 @@ function createReactiveNestedStatus({
|
|
|
1982
2054
|
}
|
|
1983
2055
|
async function $validate() {
|
|
1984
2056
|
try {
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
);
|
|
1991
|
-
const validationResults = results.every((value) => {
|
|
1992
|
-
if (value.status === "fulfilled") {
|
|
1993
|
-
return value.value.result === true;
|
|
2057
|
+
if (commonArgs.schemaMode) {
|
|
2058
|
+
if (commonArgs.onValidate) {
|
|
2059
|
+
$touch(false);
|
|
2060
|
+
scopeState.$localPending.value = true;
|
|
2061
|
+
return commonArgs.onValidate();
|
|
1994
2062
|
} else {
|
|
1995
|
-
return false;
|
|
2063
|
+
return { result: false, data: state.value };
|
|
1996
2064
|
}
|
|
1997
|
-
}
|
|
1998
|
-
|
|
2065
|
+
} else {
|
|
2066
|
+
const data = state.value;
|
|
2067
|
+
const results = await Promise.allSettled(
|
|
2068
|
+
Object.values($fields.value).map((statusOrField) => {
|
|
2069
|
+
return statusOrField.$validate();
|
|
2070
|
+
})
|
|
2071
|
+
);
|
|
2072
|
+
const validationResults = results.every((value) => {
|
|
2073
|
+
if (value.status === "fulfilled") {
|
|
2074
|
+
return value.value.result === true;
|
|
2075
|
+
} else {
|
|
2076
|
+
return false;
|
|
2077
|
+
}
|
|
2078
|
+
});
|
|
2079
|
+
return { result: validationResults, data };
|
|
2080
|
+
}
|
|
1999
2081
|
} catch (e) {
|
|
2000
2082
|
return { result: false, data: state.value };
|
|
2083
|
+
} finally {
|
|
2084
|
+
scopeState.$localPending.value = false;
|
|
2001
2085
|
}
|
|
2002
2086
|
}
|
|
2003
|
-
const { $shortcuts, ...restScopeState } = scopeState;
|
|
2087
|
+
const { $shortcuts, $localPending, ...restScopeState } = scopeState;
|
|
2004
2088
|
return reactive({
|
|
2005
2089
|
...restScopeState,
|
|
2006
2090
|
...$shortcuts,
|
|
@@ -2018,20 +2102,17 @@ function createReactiveNestedStatus({
|
|
|
2018
2102
|
}
|
|
2019
2103
|
function createReactiveChildrenStatus({
|
|
2020
2104
|
rulesDef,
|
|
2021
|
-
externalErrors,
|
|
2022
2105
|
...properties
|
|
2023
2106
|
}) {
|
|
2024
2107
|
if (isCollectionRulesDef(rulesDef, properties.state)) {
|
|
2025
2108
|
return createReactiveCollectionStatus({
|
|
2026
2109
|
rulesDef,
|
|
2027
|
-
externalErrors,
|
|
2028
2110
|
...properties
|
|
2029
2111
|
});
|
|
2030
2112
|
} else if (isNestedRulesDef(properties.state, rulesDef)) {
|
|
2031
2113
|
if (isRefObject(properties.state)) {
|
|
2032
2114
|
return createReactiveNestedStatus({
|
|
2033
2115
|
rulesDef,
|
|
2034
|
-
externalErrors,
|
|
2035
2116
|
...properties
|
|
2036
2117
|
});
|
|
2037
2118
|
} else {
|
|
@@ -2057,7 +2138,6 @@ function createReactiveChildrenStatus({
|
|
|
2057
2138
|
const { state, ...restProperties } = properties;
|
|
2058
2139
|
return createReactiveNestedStatus({
|
|
2059
2140
|
rulesDef,
|
|
2060
|
-
externalErrors,
|
|
2061
2141
|
...restProperties,
|
|
2062
2142
|
state: scopeState.fakeState
|
|
2063
2143
|
});
|
|
@@ -2065,11 +2145,10 @@ function createReactiveChildrenStatus({
|
|
|
2065
2145
|
} else if (isValidatorRulesDef(rulesDef)) {
|
|
2066
2146
|
return createReactiveFieldStatus({
|
|
2067
2147
|
rulesDef,
|
|
2068
|
-
externalErrors,
|
|
2069
2148
|
...properties
|
|
2070
2149
|
});
|
|
2071
2150
|
}
|
|
2072
|
-
return
|
|
2151
|
+
return undefined;
|
|
2073
2152
|
}
|
|
2074
2153
|
|
|
2075
2154
|
// src/core/useRegle/root/useRootStorage.ts
|
|
@@ -2079,7 +2158,10 @@ function useRootStorage({
|
|
|
2079
2158
|
scopeRules,
|
|
2080
2159
|
state,
|
|
2081
2160
|
customRules,
|
|
2082
|
-
shortcuts
|
|
2161
|
+
shortcuts,
|
|
2162
|
+
schemaErrors,
|
|
2163
|
+
schemaMode = false,
|
|
2164
|
+
onValidate
|
|
2083
2165
|
}) {
|
|
2084
2166
|
const storage = useStorage();
|
|
2085
2167
|
const regle = ref();
|
|
@@ -2095,7 +2177,11 @@ function useRootStorage({
|
|
|
2095
2177
|
initialState,
|
|
2096
2178
|
shortcuts,
|
|
2097
2179
|
fieldName: "root",
|
|
2098
|
-
path: ""
|
|
2180
|
+
path: "",
|
|
2181
|
+
schemaErrors,
|
|
2182
|
+
rootSchemaErrors: schemaErrors,
|
|
2183
|
+
schemaMode,
|
|
2184
|
+
onValidate
|
|
2099
2185
|
});
|
|
2100
2186
|
if (getCurrentScope()) {
|
|
2101
2187
|
onScopeDispose(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Type safe form validation library for Vue 3",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"vue": "^3.3.0"
|
|
@@ -89,6 +89,7 @@
|
|
|
89
89
|
"scripts": {
|
|
90
90
|
"typecheck": "tsc --noEmit",
|
|
91
91
|
"build": "tsup",
|
|
92
|
+
"build:dev": "tsup --config=tsup.dev.ts",
|
|
92
93
|
"build:sourcemaps": "tsup --config=tsup.sourcemap.ts",
|
|
93
94
|
"dev": "tsup --config=tsup.dev.ts --watch",
|
|
94
95
|
"test": "echo 'no tests'"
|