@regle/core 1.8.5 → 1.9.0-beta.1

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.
@@ -67,7 +67,7 @@ function isObject(obj) {
67
67
  if (obj && (obj instanceof Date || obj.constructor.name == "File" || obj.constructor.name == "FileList")) return false;
68
68
  return typeof obj === "object" && obj !== null && !Array.isArray(obj);
69
69
  }
70
- function merge(obj1, ...objs) {
70
+ function merge(_obj1, ..._objs) {
71
71
  var args = [].slice.call(arguments);
72
72
  var arg;
73
73
  var i = args.length;
@@ -255,7 +255,7 @@ function mergeArrayGroupProperties(entries, property) {
255
255
  function unwrapRuleParameters(params) {
256
256
  try {
257
257
  return params.map((param) => toValue(param));
258
- } catch (e) {
258
+ } catch {
259
259
  return [];
260
260
  }
261
261
  }
@@ -274,13 +274,9 @@ function createReactiveParams(params) {
274
274
  * Due to `function.length` not returning default parameters, it needed to parse the func.toString()
275
275
  */
276
276
  function getFunctionParametersLength(func) {
277
- const funcStr = func.toString();
278
- const cleanStr = funcStr.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
279
- const paramsMatch = cleanStr.match(/^(?:async\s*)?(?:function\b.*?\(|\((.*?)\)|(\w+))\s*=>|\((.*?)\)\s*=>|function.*?\((.*?)\)|\((.*?)\)/);
277
+ const paramsMatch = func.toString().replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "").match(/^(?:async\s*)?(?:function\b.*?\(|\((.*?)\)|(\w+))\s*=>|\((.*?)\)\s*=>|function.*?\((.*?)\)|\((.*?)\)/);
280
278
  if (!paramsMatch) return 0;
281
- const paramsSection = paramsMatch[0] || paramsMatch[1] || paramsMatch[2] || paramsMatch[3] || paramsMatch[4] || "";
282
- const paramList = paramsSection.split(",").map((p) => p.trim()).filter((p) => p.length > 0);
283
- return paramList.length;
279
+ return (paramsMatch[0] || paramsMatch[1] || paramsMatch[2] || paramsMatch[3] || paramsMatch[4] || "").split(",").map((p) => p.trim()).filter((p) => p.length > 0).length;
284
280
  }
285
281
 
286
282
  //#endregion
@@ -288,7 +284,7 @@ function getFunctionParametersLength(func) {
288
284
  function defineRuleProcessors(definition, ...params) {
289
285
  const { validator, type, async } = definition;
290
286
  const isAsync = async || type === InternalRuleType.Async || validator.constructor.name === "AsyncFunction";
291
- const defaultProcessors = {
287
+ return markRaw({
292
288
  validator(value, ...args) {
293
289
  return definition.validator(value, ...unwrapRuleParameters(args.length ? args : params));
294
290
  },
@@ -326,10 +322,7 @@ function defineRuleProcessors(definition, ...params) {
326
322
  if (typeof rawResult === "object" && "$valid" in rawResult) return rawResult.$valid;
327
323
  else if (typeof rawResult === "boolean") return rawResult;
328
324
  return false;
329
- }
330
- };
331
- const processors = markRaw({
332
- ...defaultProcessors,
325
+ },
333
326
  type,
334
327
  _validator: definition.validator,
335
328
  _message: definition.message,
@@ -342,7 +335,6 @@ function defineRuleProcessors(definition, ...params) {
342
335
  _params: createReactiveParams(params),
343
336
  _brand: RegleRuleSymbol
344
337
  });
345
- return processors;
346
338
  }
347
339
 
348
340
  //#endregion
@@ -461,17 +453,14 @@ function useStorage() {
461
453
  function checkRuleDeclEntry($path, newRules) {
462
454
  const storedRulesDefs = ruleDeclStorage.value.get($path);
463
455
  if (!storedRulesDefs) return void 0;
464
- const storedRules = storedRulesDefs;
465
- const isValidCache = areRulesChanged(newRules, storedRules);
466
- if (!isValidCache) return { valid: false };
456
+ if (!areRulesChanged(newRules, storedRulesDefs)) return { valid: false };
467
457
  return { valid: true };
468
458
  }
469
459
  function areRulesChanged(newRules, storedRules) {
470
460
  const storedRulesKeys = Object.keys(storedRules);
471
461
  const newRulesKeys = Object.keys(newRules);
472
462
  if (newRulesKeys.length !== storedRulesKeys.length) return false;
473
- const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
474
- if (!hasAllValidators) return false;
463
+ if (!newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey))) return false;
475
464
  return newRulesKeys.every((ruleKey) => {
476
465
  const newRuleElement = newRules[ruleKey];
477
466
  const storedRuleElement = storedRules[ruleKey];
@@ -603,10 +592,7 @@ function uniqueIDNuxt() {
603
592
  */
604
593
  function randomId() {
605
594
  if (typeof window === "undefined") return uniqueIDNuxt();
606
- else {
607
- const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
608
- return uint32.toString(10);
609
- }
595
+ else return window.crypto.getRandomValues(new Uint32Array(1))[0].toString(10);
610
596
  }
611
597
 
612
598
  //#endregion
@@ -640,7 +626,7 @@ function createGlobalState(stateFactory) {
640
626
  //#endregion
641
627
  //#region src/core/useRegle/guards/ruleDef.guards.ts
642
628
  function isNestedRulesDef(state, rules) {
643
- return isRefObject(state) || isObject(rules.value) && !isEmpty(rules.value) && !Object.entries(rules.value).some(([key, rule]) => isRuleDef(rule) || typeof rule === "function");
629
+ return isRefObject(state) || isObject(rules.value) && !isEmpty(rules.value) && !Object.entries(rules.value).some(([_, rule]) => isRuleDef(rule) || typeof rule === "function");
644
630
  }
645
631
  function isCollectionRulesDef(rules, state, schemaMode = false) {
646
632
  return !!rules.value && isObject(rules.value) && "$each" in rules.value || schemaMode && Array.isArray(state.value) && state.value.some(isObject) || Array.isArray(state.value) && state.value.some(isObject);
@@ -671,22 +657,19 @@ function isFieldStatus(rule) {
671
657
  //#endregion
672
658
  //#region src/core/useRegle/useErrors.ts
673
659
  function extractRulesIssues({ field, silent = false }) {
674
- const issues = Object.entries(field.$rules ?? {}).map(([key, rule]) => {
660
+ const ruleIssues = Object.entries(field.$rules ?? {}).map(([key, rule]) => {
675
661
  let message = "";
676
662
  if (silent && !rule.$valid) message = rule.$message;
677
663
  else if (!rule.$valid && field.$error && !rule.$validating) message = rule.$message;
678
664
  else return null;
679
- const issue = {
665
+ return {
680
666
  $message: message,
681
667
  $property: field.fieldName,
682
668
  $rule: key,
683
669
  $type: rule.$type,
684
670
  ...typeof rule.$metadata === "object" ? rule.$metadata : {}
685
671
  };
686
- return issue;
687
- });
688
- const filteredIssues = issues.filter((msg) => !!msg);
689
- const ruleIssues = filteredIssues.reduce((acc, issue) => {
672
+ }).filter((msg) => !!msg).reduce((acc, issue) => {
690
673
  if (typeof issue.$message === "string") acc.push(issue);
691
674
  else acc.push(...issue.$message.map((msg) => ({
692
675
  ...issue,
@@ -726,8 +709,8 @@ function flatErrors(errors, options) {
726
709
  if (Array.isArray(errors) && errors.every((err) => !isObject(err))) return errors;
727
710
  else if (isCollectionError(errors)) {
728
711
  const selfErrors = includePath ? errors.$self?.map((err) => ({
729
- error: err,
730
- path: ""
712
+ message: err,
713
+ path: []
731
714
  })) ?? [] : errors.$self ?? [];
732
715
  const eachErrors = errors.$each?.map((err) => iterateErrors(err, includePath)) ?? [];
733
716
  return selfErrors?.concat(eachErrors.flat());
@@ -737,23 +720,23 @@ function iterateErrors(errors, includePath = false, _path) {
737
720
  const path = includePath && !_path ? [] : _path;
738
721
  if (Array.isArray(errors) && errors.every((err) => !isObject(err))) {
739
722
  if (includePath) return errors.map((err) => ({
740
- error: err,
741
- path: path?.join(".") ?? ""
723
+ message: err,
724
+ path: path ?? []
742
725
  }));
743
726
  return errors;
744
727
  } else if (isCollectionError(errors)) {
745
728
  const selfErrors = path?.length ? errors.$self?.map((err) => ({
746
- error: err,
747
- path: path.join(".")
729
+ message: err,
730
+ path: path ?? []
748
731
  })) ?? [] : errors.$self ?? [];
749
- const eachErrors = errors.$each?.map((err, index) => iterateErrors(err, includePath, path?.concat(index.toString()))) ?? [];
732
+ const eachErrors = errors.$each?.map((err, index) => iterateErrors(err, includePath, path?.concat(index))) ?? [];
750
733
  return selfErrors?.concat(eachErrors.flat());
751
734
  } else return Object.entries(errors).map(([key, value]) => iterateErrors(value, includePath, path?.concat(key))).flat();
752
735
  }
753
736
 
754
737
  //#endregion
755
738
  //#region src/core/useRegle/root/createReactiveRuleStatus.ts
756
- function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path, cachePath, storage, $debounce, modifiers }) {
739
+ function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path, cachePath, storage, modifiers }) {
757
740
  let scope = effectScope();
758
741
  let scopeState = {};
759
742
  let $unwatchState;
@@ -868,7 +851,7 @@ function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path,
868
851
  ruleResult = $valid$1;
869
852
  $metadata.value = rest;
870
853
  }
871
- } catch (e) {
854
+ } catch {
872
855
  ruleResult = false;
873
856
  } finally {
874
857
  $pending.value = false;
@@ -894,7 +877,7 @@ function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path,
894
877
  }
895
878
  $valid.value = ruleResult;
896
879
  return ruleResult;
897
- } catch (e) {
880
+ } catch {
898
881
  return false;
899
882
  } finally {
900
883
  $validating.value = false;
@@ -928,6 +911,23 @@ function createReactiveRuleStatus({ customMessages, rule, ruleKey, state, path,
928
911
  });
929
912
  }
930
913
 
914
+ //#endregion
915
+ //#region src/core/useRegle/root/standard-schemas.ts
916
+ function createStandardSchema(validateFn) {
917
+ return { "~standard": {
918
+ version: 1,
919
+ vendor: "regle",
920
+ validate: async (value) => {
921
+ const { valid, data, errors } = await validateFn(value);
922
+ if (valid) return {
923
+ value: data,
924
+ issues: []
925
+ };
926
+ return { issues: flatErrors(errors, { includePath: true }) };
927
+ }
928
+ } };
929
+ }
930
+
931
931
  //#endregion
932
932
  //#region src/core/useRegle/root/createReactiveFieldStatus.ts
933
933
  function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cachePath, fieldName, storage, options, externalErrors, schemaErrors, schemaMode, onUnwatch, $isArray, initialState, originalState, shortcuts, onValidate }) {
@@ -950,7 +950,7 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
950
950
  const rules = rulesDef.value;
951
951
  const entries = [];
952
952
  for (const ruleKey in rules) {
953
- if (ruleKey.startsWith("$")) continue;
953
+ if (ruleKey.startsWith("$") || ruleKey.startsWith("~")) continue;
954
954
  const rule = rules[ruleKey];
955
955
  if (!rule) continue;
956
956
  const ruleRef = toRef(() => rule);
@@ -1102,7 +1102,7 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
1102
1102
  return false;
1103
1103
  });
1104
1104
  const $invalid = computed(() => {
1105
- if (schemaErrors?.value && !Array.isArray(schemaErrors?.value) && "$self" in schemaErrors?.value) return schemaErrors?.value.$self?.length > 0;
1105
+ if (schemaErrors?.value && !Array.isArray(schemaErrors?.value) && "$self" in schemaErrors.value) return schemaErrors?.value.$self?.length > 0;
1106
1106
  else if (externalErrors?.value?.length || schemaErrors?.value?.length) return true;
1107
1107
  else if ($inactive.value) return false;
1108
1108
  else return Object.values($rules.value).some((ruleResult) => !ruleResult.$valid || ruleResult.$maybePending);
@@ -1257,39 +1257,50 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
1257
1257
  if (!scopeState.$silent.value || scopeState.$rewardEarly.value && scopeState.$error.value) $commit();
1258
1258
  } else if (runCommit) $commit();
1259
1259
  }
1260
- async function $validate() {
1260
+ async function $validate(forceValues) {
1261
1261
  try {
1262
+ if (forceValues) state.value = forceValues;
1262
1263
  if (schemaMode) if (onValidate) {
1263
1264
  $touch(false);
1264
1265
  return onValidate();
1265
1266
  } else return {
1266
1267
  valid: false,
1267
- data: state.value
1268
+ data: state.value,
1269
+ errors: scopeState.$errors.value,
1270
+ issues: scopeState.$issues.value
1268
1271
  };
1269
1272
  const data = state.value;
1270
1273
  if (!scopeState.$dirty.value) scopeState.$dirty.value = true;
1271
1274
  else if (!scopeState.$silent.value && scopeState.$dirty.value && !scopeState.$pending.value) return {
1272
1275
  valid: !scopeState.$error.value,
1273
- data
1276
+ data,
1277
+ errors: scopeState.$errors.value,
1278
+ issues: scopeState.$issues.value
1274
1279
  };
1275
1280
  if (schemaMode) return {
1276
1281
  valid: !schemaErrors?.value?.length,
1277
- data
1282
+ data,
1283
+ errors: scopeState.$errors.value,
1284
+ issues: scopeState.$issues.value
1278
1285
  };
1279
1286
  else if (isEmpty($rules.value)) return {
1280
1287
  valid: true,
1281
- data
1288
+ data,
1289
+ errors: scopeState.$errors.value,
1290
+ issues: scopeState.$issues.value
1282
1291
  };
1283
- const results = await Promise.allSettled(Object.values($rules.value).map((rule) => rule.$parse()));
1284
- const validationResults = results.every((value) => value.status === "fulfilled" && value.value === true);
1285
1292
  return {
1286
- valid: validationResults,
1287
- data
1293
+ valid: (await Promise.allSettled(Object.values($rules.value).map((rule) => rule.$parse()))).every((value) => value.status === "fulfilled" && value.value === true),
1294
+ data,
1295
+ errors: scopeState.$errors.value,
1296
+ issues: scopeState.$issues.value
1288
1297
  };
1289
1298
  } catch {
1290
1299
  return {
1291
1300
  valid: false,
1292
- data: state.value
1301
+ data: state.value,
1302
+ errors: scopeState.$errors.value,
1303
+ issues: scopeState.$issues.value
1293
1304
  };
1294
1305
  }
1295
1306
  }
@@ -1319,7 +1330,8 @@ function createReactiveFieldStatus({ state, rulesDef, customMessages, path, cach
1319
1330
  $unwatch,
1320
1331
  $watch,
1321
1332
  $extractDirtyFields,
1322
- $clearExternalErrors
1333
+ $clearExternalErrors,
1334
+ ...createStandardSchema($validate)
1323
1335
  });
1324
1336
  }
1325
1337
 
@@ -1357,8 +1369,7 @@ function createCollectionElement({ $id, path, cachePath, index, options, storage
1357
1369
  schemaMode
1358
1370
  });
1359
1371
  if ($status) {
1360
- const valueId = stateValue.value?.$id;
1361
- $status.$id = valueId ?? String($fieldId);
1372
+ $status.$id = stateValue.value?.$id ?? String($fieldId);
1362
1373
  storage.addArrayStatus($id, $status.$id, $status);
1363
1374
  }
1364
1375
  return $status;
@@ -1372,7 +1383,7 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
1372
1383
  let immediateScope = effectScope();
1373
1384
  let immediateScopeState;
1374
1385
  let collectionScopes = [];
1375
- if (!Array.isArray(state.value) && !rulesDef.value.$each) return void 0;
1386
+ if (!Array.isArray(state.value) && !rulesDef.value.$each) return;
1376
1387
  const $id = ref();
1377
1388
  const $value = ref(state.value);
1378
1389
  const $localOptions = ref({});
@@ -1381,13 +1392,12 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
1381
1392
  const $selfStatus = ref({});
1382
1393
  const $eachStatus = storage.getCollectionsEntry(path);
1383
1394
  immediateScopeState = immediateScope.run(() => {
1384
- const isPrimitiveArray = computed(() => {
1395
+ return { isPrimitiveArray: computed(() => {
1385
1396
  if (!state.value?.length) return false;
1386
1397
  if (Array.isArray(state.value) && state.value.length) return state.value.every((s) => typeof s !== "object");
1387
1398
  else if (rulesDef.value.$each && !(rulesDef.value.$each instanceof Function)) return Object.values(rulesDef.value.$each).every((rule) => isRuleDef(rule) || typeof rule === "function");
1388
1399
  return false;
1389
- });
1390
- return { isPrimitiveArray };
1400
+ }) };
1391
1401
  });
1392
1402
  createStatus();
1393
1403
  $watch();
@@ -1701,24 +1711,27 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
1701
1711
  });
1702
1712
  if (!fromParent) createStatus();
1703
1713
  }
1704
- async function $validate() {
1714
+ async function $validate(forceValues) {
1715
+ if (forceValues) state.value = forceValues;
1705
1716
  const data = state.value;
1706
1717
  try {
1707
- const results = await Promise.allSettled([$selfStatus.value.$validate(), ...$eachStatus.value.map((status) => {
1708
- return status.$validate();
1709
- })]);
1710
- const validationResults = results.every((value) => {
1711
- if (value.status === "fulfilled") return value.value.valid === true;
1712
- else return false;
1713
- });
1714
1718
  return {
1715
- valid: validationResults,
1716
- data
1719
+ valid: (await Promise.allSettled([$selfStatus.value.$validate(forceValues), ...$eachStatus.value.map((status) => {
1720
+ return status.$validate();
1721
+ })])).every((value) => {
1722
+ if (value.status === "fulfilled") return value.value.valid === true;
1723
+ else return false;
1724
+ }),
1725
+ data,
1726
+ errors: scopeState.$errors.value,
1727
+ issues: scopeState.$issues.value
1717
1728
  };
1718
- } catch (e) {
1729
+ } catch {
1719
1730
  return {
1720
1731
  valid: false,
1721
- data
1732
+ data,
1733
+ errors: scopeState.$errors.value,
1734
+ issues: scopeState.$issues.value
1722
1735
  };
1723
1736
  }
1724
1737
  }
@@ -1755,7 +1768,8 @@ function createReactiveCollectionStatus({ state, rulesDef, customMessages, path,
1755
1768
  $touch,
1756
1769
  $reset,
1757
1770
  $extractDirtyFields,
1758
- $clearExternalErrors
1771
+ $clearExternalErrors,
1772
+ ...createStandardSchema($validate)
1759
1773
  });
1760
1774
  }
1761
1775
 
@@ -1944,8 +1958,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
1944
1958
  if (commonArgs.schemaMode) return fields.every(([_, statusOrField]) => statusOrField.$correct);
1945
1959
  return fields.every(([_, statusOrField]) => {
1946
1960
  if (!isFieldStatus(statusOrField)) return statusOrField?.$correct;
1947
- const hasRequiredRule = "required" in statusOrField.$rules && statusOrField.$rules.required.$active;
1948
- return hasRequiredRule ? statusOrField.$correct : !statusOrField.$invalid && !statusOrField.$pending && !statusOrField.$isDebouncing;
1961
+ return "required" in statusOrField.$rules && statusOrField.$rules.required.$active ? statusOrField.$correct : !statusOrField.$invalid && !statusOrField.$pending && !statusOrField.$isDebouncing;
1949
1962
  });
1950
1963
  }
1951
1964
  return false;
@@ -1973,9 +1986,9 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
1973
1986
  if ($silent.value) return !($invalid.value || $pending.value);
1974
1987
  return $anyDirty.value && !($invalid.value || $pending.value);
1975
1988
  });
1976
- const $localPending$1 = ref(false);
1989
+ const $localPending = ref(false);
1977
1990
  const $pending = computed(() => {
1978
- if ($localPending$1.value) return true;
1991
+ if ($localPending.value) return true;
1979
1992
  const fields = $fields.value;
1980
1993
  for (const key in fields) if (fields[key]?.$pending) return true;
1981
1994
  return false;
@@ -2076,7 +2089,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
2076
2089
  $silentValue,
2077
2090
  $edited,
2078
2091
  $anyEdited,
2079
- $localPending: $localPending$1,
2092
+ $localPending,
2080
2093
  $autoDirty,
2081
2094
  $silent,
2082
2095
  $value
@@ -2103,7 +2116,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
2103
2116
  if (!fromParent) if (options?.toOriginalState) {
2104
2117
  state.value = cloneDeep({ ...originalState });
2105
2118
  initialState.value = cloneDeep({ ...originalState });
2106
- } else if (options?.toInitialState) state.value = cloneDeep({ ...initialState.value ?? {} });
2119
+ } else if (options?.toInitialState) state.value = cloneDeep({ ...initialState.value });
2107
2120
  else if (options?.toState) {
2108
2121
  let newInitialState;
2109
2122
  if (typeof options?.toState === "function") newInitialState = options?.toState();
@@ -2120,7 +2133,7 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
2120
2133
  for (const field of Object.values($fields.value)) field?.$touch(runCommit, withConditions);
2121
2134
  }
2122
2135
  function filterNullishFields(fields) {
2123
- return fields.filter(([key, value]) => {
2136
+ return fields.filter(([_, value]) => {
2124
2137
  if (isObject(value)) return !(value && typeof value === "object" && "_null" in value) && !isEmpty(value);
2125
2138
  else if (Array.isArray(value)) return value.length;
2126
2139
  else return true;
@@ -2133,35 +2146,40 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
2133
2146
  if (filterNullishValues) dirtyFields = filterNullishFields(dirtyFields);
2134
2147
  return Object.fromEntries(dirtyFields);
2135
2148
  }
2136
- async function $validate() {
2149
+ async function $validate(forceValues) {
2137
2150
  try {
2151
+ if (forceValues) state.value = forceValues;
2138
2152
  if (commonArgs.schemaMode) if (commonArgs.onValidate) {
2139
2153
  $touch(false);
2140
2154
  scopeState.$localPending.value = true;
2141
2155
  return commonArgs.onValidate();
2142
2156
  } else return {
2143
2157
  valid: false,
2144
- data: state.value
2158
+ data: state.value,
2159
+ errors: scopeState.$errors.value,
2160
+ issues: scopeState.$issues.value
2145
2161
  };
2146
2162
  else {
2147
2163
  const data = state.value;
2148
- const results = await Promise.allSettled(Object.values($fields.value).map((statusOrField) => statusOrField.$validate()));
2149
- const validationResults = results.every((value) => value.status === "fulfilled" && value?.value.valid === true);
2150
2164
  return {
2151
- valid: validationResults,
2152
- data
2165
+ valid: (await Promise.allSettled(Object.values($fields.value).map((statusOrField) => statusOrField.$validate()))).every((value) => value.status === "fulfilled" && value?.value.valid === true),
2166
+ data,
2167
+ errors: scopeState.$errors.value,
2168
+ issues: scopeState.$issues.value
2153
2169
  };
2154
2170
  }
2155
- } catch (e) {
2171
+ } catch {
2156
2172
  return {
2157
2173
  valid: false,
2158
- data: state.value
2174
+ data: state.value,
2175
+ errors: scopeState.$errors.value,
2176
+ issues: scopeState.$issues.value
2159
2177
  };
2160
2178
  } finally {
2161
2179
  scopeState.$localPending.value = false;
2162
2180
  }
2163
2181
  }
2164
- const { $shortcuts, $localPending,...restScopeState } = scopeState;
2182
+ const { $shortcuts, $localPending: _$localPending,...restScopeState } = scopeState;
2165
2183
  const fullStatus = reactive({
2166
2184
  ...restScopeState,
2167
2185
  ...$shortcuts,
@@ -2175,10 +2193,11 @@ function createReactiveNestedStatus({ rulesDef, state, path = "", cachePath, roo
2175
2193
  $unwatch,
2176
2194
  $watch,
2177
2195
  $clearExternalErrors,
2178
- $extractDirtyFields
2196
+ $extractDirtyFields,
2197
+ ...createStandardSchema($validate)
2179
2198
  });
2180
2199
  watchEffect(() => {
2181
- for (const key of Object.keys(fullStatus).filter((key$1) => !key$1.startsWith("$"))) delete fullStatus[key];
2200
+ for (const key of Object.keys(fullStatus).filter((key$1) => !key$1.startsWith("$") && !key$1.startsWith("~"))) delete fullStatus[key];
2182
2201
  for (const field of Object.values($fields.value)) if (field?.$name) Object.assign(fullStatus, { [field.$name]: field });
2183
2202
  });
2184
2203
  return fullStatus;
@@ -2196,8 +2215,7 @@ function createReactiveChildrenStatus({ rulesDef,...properties }) {
2196
2215
  ...properties
2197
2216
  });
2198
2217
  else {
2199
- const scope = effectScope();
2200
- const scopeState = scope.run(() => {
2218
+ const scopeState = effectScope().run(() => {
2201
2219
  const fakeState = toRef(properties.state.value ? properties.state : ref({}));
2202
2220
  watch(() => properties.state.value, (value) => {
2203
2221
  fakeState.value = value;
@@ -2207,7 +2225,7 @@ function createReactiveChildrenStatus({ rulesDef,...properties }) {
2207
2225
  }, { deep: true });
2208
2226
  return { fakeState };
2209
2227
  });
2210
- const { state,...restProperties } = properties;
2228
+ const { state: _state,...restProperties } = properties;
2211
2229
  return createReactiveNestedStatus({
2212
2230
  rulesDef,
2213
2231
  ...restProperties,
@@ -2218,7 +2236,6 @@ function createReactiveChildrenStatus({ rulesDef,...properties }) {
2218
2236
  rulesDef: isRef(rulesDef.value) ? rulesDef.value : rulesDef,
2219
2237
  ...properties
2220
2238
  });
2221
- return void 0;
2222
2239
  }
2223
2240
 
2224
2241
  //#endregion
@@ -2291,6 +2308,32 @@ function useRootStorage({ initialState, originalState, options, scopeRules, stat
2291
2308
  return reactive({ regle });
2292
2309
  }
2293
2310
 
2311
+ //#endregion
2312
+ //#region src/core/useRegle/shared.rootRegle.ts
2313
+ function createRootRegleLogic({ state, rulesFactory, options, globalOptions, customRules, shortcuts }) {
2314
+ const definedRules = isRef(rulesFactory) ? rulesFactory : typeof rulesFactory === "function" ? void 0 : computed(() => rulesFactory);
2315
+ const resolvedOptions = {
2316
+ ...globalOptions,
2317
+ ...options
2318
+ };
2319
+ const watchableRulesGetters = shallowRef(definedRules ?? {});
2320
+ if (typeof rulesFactory === "function") watchEffect(() => {
2321
+ watchableRulesGetters.value = rulesFactory(state);
2322
+ triggerRef(watchableRulesGetters);
2323
+ });
2324
+ const initialState = ref(isObject(state.value) ? { ...cloneDeep(state.value) } : cloneDeep(state.value));
2325
+ const originalState = isObject(state.value) ? { ...cloneDeep(state.value) } : cloneDeep(state.value);
2326
+ return useRootStorage({
2327
+ scopeRules: watchableRulesGetters,
2328
+ state,
2329
+ options: resolvedOptions,
2330
+ initialState,
2331
+ originalState,
2332
+ customRules,
2333
+ shortcuts
2334
+ });
2335
+ }
2336
+
2294
2337
  //#endregion
2295
2338
  //#region src/core/useRegle/useRegle.ts
2296
2339
  function createUseRegleComposable(customRules, options, shortcuts) {
@@ -2302,29 +2345,15 @@ function createUseRegleComposable(customRules, options, shortcuts) {
2302
2345
  clearExternalErrorsOnChange: options?.clearExternalErrorsOnChange
2303
2346
  };
2304
2347
  function useRegle$1(state, rulesFactory, options$1) {
2305
- const definedRules = isRef(rulesFactory) ? rulesFactory : typeof rulesFactory === "function" ? void 0 : computed(() => rulesFactory);
2306
- const resolvedOptions = {
2307
- ...globalOptions,
2308
- ...options$1
2309
- };
2310
2348
  const processedState = isRef(state) ? state : ref(state);
2311
- const watchableRulesGetters = shallowRef(definedRules ?? {});
2312
- if (typeof rulesFactory === "function") watchEffect(() => {
2313
- watchableRulesGetters.value = rulesFactory(processedState);
2314
- triggerRef(watchableRulesGetters);
2315
- });
2316
- const initialState = ref(isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value));
2317
- const originalState = isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value);
2318
- const regle = useRootStorage({
2319
- scopeRules: watchableRulesGetters,
2349
+ return { r$: createRootRegleLogic({
2320
2350
  state: processedState,
2321
- options: resolvedOptions,
2322
- initialState,
2323
- originalState,
2351
+ rulesFactory,
2352
+ options: options$1,
2353
+ globalOptions,
2324
2354
  customRules,
2325
2355
  shortcuts
2326
- });
2327
- return { r$: regle.regle };
2356
+ }).regle };
2328
2357
  }
2329
2358
  return useRegle$1;
2330
2359
  }
@@ -2366,6 +2395,60 @@ function createInferRuleHelper() {
2366
2395
  */
2367
2396
  const inferRules = createInferRuleHelper();
2368
2397
 
2398
+ //#endregion
2399
+ //#region src/core/useRegle/useRules.ts
2400
+ function createEmptyRuleState(rules) {
2401
+ const result = {};
2402
+ if (Object.entries(rules).some(([_, rule]) => isRuleDef(rule) || typeof rule === "function")) return null;
2403
+ for (const key in rules) {
2404
+ const item = rules[key];
2405
+ if (!!item && isObject(item) && "$each" in item && item["$each"] && isObject(item["$each"])) result[key] = [createEmptyRuleState(item["$each"])];
2406
+ else if (isObject(item) && !isEmpty(item) && !Object.entries(item).some(([_, rule]) => isRuleDef(rule) || typeof rule === "function")) result[key] = createEmptyRuleState(item);
2407
+ else result[key] = null;
2408
+ }
2409
+ return result;
2410
+ }
2411
+ function createUseRulesComposable(customRules, options, shortcuts) {
2412
+ const globalOptions = {
2413
+ autoDirty: options?.autoDirty,
2414
+ lazy: options?.lazy,
2415
+ rewardEarly: options?.rewardEarly,
2416
+ silent: options?.silent,
2417
+ clearExternalErrorsOnChange: options?.clearExternalErrorsOnChange
2418
+ };
2419
+ function useRules$1(rulesFactory, options$1) {
2420
+ const definedRules = isRef(rulesFactory) ? rulesFactory : typeof rulesFactory === "function" ? void 0 : computed(() => rulesFactory);
2421
+ const processedState = ref(createEmptyRuleState(definedRules?.value));
2422
+ return createRootRegleLogic({
2423
+ state: processedState,
2424
+ rulesFactory: definedRules,
2425
+ options: options$1,
2426
+ globalOptions,
2427
+ customRules,
2428
+ shortcuts
2429
+ }).regle;
2430
+ }
2431
+ return useRules$1;
2432
+ }
2433
+ /**
2434
+ * useRules is a clone of useRegle, without the need to provide a state.
2435
+ *
2436
+ * It accepts the following inputs:
2437
+ *
2438
+ * @param rules - Your rules object
2439
+ * @param modifiers - Customize regle behaviour
2440
+ *
2441
+ * ```ts
2442
+ * import { useRules } from '@regle/core';
2443
+ import { required } from '@regle/rules';
2444
+
2445
+ const { r$ } = useRules({
2446
+ email: { required }
2447
+ })
2448
+ * ```
2449
+ */
2450
+ const useRules = createUseRulesComposable();
2451
+
2369
2452
  //#endregion
2370
2453
  //#region src/core/defineRegleConfig.ts
2371
2454
  /**
@@ -2382,15 +2465,22 @@ const inferRules = createInferRuleHelper();
2382
2465
  */
2383
2466
  function defineRegleConfig({ rules, modifiers, shortcuts }) {
2384
2467
  const useRegle$1 = createUseRegleComposable(rules, modifiers, shortcuts);
2468
+ const useRules$1 = createUseRulesComposable(rules, modifiers, shortcuts);
2385
2469
  useRegle$1.__config = {
2386
2470
  rules,
2387
2471
  modifiers,
2388
2472
  shortcuts
2389
2473
  };
2474
+ useRules$1.__config = {
2475
+ rules,
2476
+ modifiers,
2477
+ shortcuts
2478
+ };
2390
2479
  const inferRules$1 = createInferRuleHelper();
2391
2480
  return {
2392
2481
  useRegle: useRegle$1,
2393
- inferRules: inferRules$1
2482
+ inferRules: inferRules$1,
2483
+ useRules: useRules$1
2394
2484
  };
2395
2485
  }
2396
2486
  /**
@@ -2481,6 +2571,22 @@ function mergeRegles(regles, _scoped) {
2481
2571
  return regle?.$pending;
2482
2572
  });
2483
2573
  });
2574
+ const $issues = computed(() => {
2575
+ if (scoped) return Object.entries(regles).map(([_, regle]) => {
2576
+ return regle.$issues;
2577
+ });
2578
+ else return Object.fromEntries(Object.entries(regles).map(([key, regle]) => {
2579
+ return [key, regle.$issues];
2580
+ }));
2581
+ });
2582
+ const $silentIssues = computed(() => {
2583
+ if (scoped) return Object.entries(regles).map(([_, regle]) => {
2584
+ return regle.$silentIssues;
2585
+ });
2586
+ else return Object.fromEntries(Object.entries(regles).map(([key, regle]) => {
2587
+ return [key, regle.$silentIssues];
2588
+ }));
2589
+ });
2484
2590
  const $errors = computed(() => {
2485
2591
  if (scoped) return Object.entries(regles).map(([_, regle]) => {
2486
2592
  return regle.$errors;
@@ -2530,30 +2636,35 @@ function mergeRegles(regles, _scoped) {
2530
2636
  regle.$clearExternalErrors();
2531
2637
  });
2532
2638
  }
2533
- async function $validate() {
2639
+ async function $validate(forceValues) {
2534
2640
  try {
2641
+ if (forceValues) $value.value = forceValues;
2535
2642
  const data = $value.value;
2536
- const results = await Promise.allSettled(Object.values(regles).map((regle) => {
2537
- return regle.$validate();
2538
- }));
2539
- const validationResults = results.every((value) => {
2540
- if (value.status === "fulfilled") return value.value.valid === true;
2541
- else return false;
2542
- });
2543
2643
  return {
2544
- valid: validationResults,
2545
- data
2644
+ valid: (await Promise.allSettled(Object.values(regles).map((regle) => {
2645
+ return regle.$validate();
2646
+ }))).every((value) => {
2647
+ if (value.status === "fulfilled") return value.value.valid === true;
2648
+ else return false;
2649
+ }),
2650
+ data,
2651
+ errors: $errors.value,
2652
+ issues: $issues.value
2546
2653
  };
2547
- } catch (e) {
2654
+ } catch {
2548
2655
  return {
2549
2656
  valid: false,
2550
- data: $value.value
2657
+ data: $value.value,
2658
+ errors: $errors.value,
2659
+ issues: $issues.value
2551
2660
  };
2552
2661
  }
2553
2662
  }
2554
2663
  return reactive({
2555
2664
  ...!scoped && { $silentValue },
2556
2665
  $errors,
2666
+ $issues,
2667
+ $silentIssues,
2557
2668
  $silentErrors,
2558
2669
  $instances,
2559
2670
  $value,
@@ -2605,7 +2716,7 @@ function createUseCollectScope(instances, options) {
2605
2716
  function createUseScopedRegleComposable(instances, customUseRegle) {
2606
2717
  const scopedUseRegle = customUseRegle ?? useRegle;
2607
2718
  const useScopedRegle$1 = ((state, rulesFactory, options) => {
2608
- const { namespace, scopeKey,...restOptions } = options ?? {};
2719
+ const { namespace, scopeKey: _scopeKey,...restOptions } = options ?? {};
2609
2720
  scopedUseRegle.__config ??= {};
2610
2721
  const computedNamespace = computed(() => toValue(namespace));
2611
2722
  const $id = ref(`${Object.keys(instances.value).length + 1}-${randomId()}`);
@@ -2652,17 +2763,15 @@ function createUseScopedRegleComposable(instances, customUseRegle) {
2652
2763
  //#endregion
2653
2764
  //#region src/core/createScopedUseRegle/createScopedUseRegle.ts
2654
2765
  function createScopedUseRegle(options) {
2655
- const useInstances = options?.customStore ? () => {
2766
+ const instances = (options?.customStore ? () => {
2656
2767
  if (options.customStore) {
2657
2768
  if (!options.customStore?.value["~~global"]) options.customStore.value["~~global"] = {};
2658
2769
  else if (options.customStore?.value) options.customStore.value = { "~~global": {} };
2659
2770
  }
2660
2771
  return options.customStore;
2661
2772
  } : createGlobalState(() => {
2662
- const $inst = ref({ "~~global": {} });
2663
- return $inst;
2664
- });
2665
- const instances = useInstances();
2773
+ return ref({ "~~global": {} });
2774
+ }))();
2666
2775
  const { useScopedRegle: useScopedRegle$1 } = createUseScopedRegleComposable(instances, options?.customUseRegle);
2667
2776
  const { useCollectScope: useCollectScope$1 } = createUseCollectScope(instances, { asRecord: options?.asRecord });
2668
2777
  return {
@@ -2696,7 +2805,7 @@ const { useCollectScope, useScopedRegle } = createScopedUseRegle();
2696
2805
  */
2697
2806
  function createVariant(root, discriminantKey, variants) {
2698
2807
  const watchableRoot = computed(() => toValue(root)[discriminantKey]);
2699
- const computedRules = computed(() => {
2808
+ return computed(() => {
2700
2809
  const selectedVariant = variants.find((variant) => {
2701
2810
  if (variant[discriminantKey] && "literal" in variant[discriminantKey]) {
2702
2811
  const literalRule = variant[discriminantKey]["literal"];
@@ -2710,7 +2819,6 @@ function createVariant(root, discriminantKey, variants) {
2710
2819
  else return {};
2711
2820
  }
2712
2821
  });
2713
- return computedRules;
2714
2822
  }
2715
2823
  /**
2716
2824
  * Narrow a nested variant field to a discriminated value
@@ -2781,4 +2889,4 @@ function refineRules(rules, refinement) {
2781
2889
  }
2782
2890
 
2783
2891
  //#endregion
2784
- export { InternalRuleType, createRule, createScopedUseRegle, createVariant, defineRegleConfig, defineRules, extendRegleConfig, flatErrors, inferRules, mergeRegles, narrowVariant, refineRules, unwrapRuleParameters, useCollectScope, useRegle, useRootStorage, useScopedRegle, variantToRef };
2892
+ export { InternalRuleType, createRule, createScopedUseRegle, createVariant, defineRegleConfig, defineRules, extendRegleConfig, flatErrors, inferRules, mergeRegles, narrowVariant, refineRules, unwrapRuleParameters, useCollectScope, useRegle, useRootStorage, useRules, useScopedRegle, variantToRef };