@regle/core 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +83 -30
- package/dist/index.d.cts +175 -74
- package/dist/index.d.ts +175 -74
- package/dist/index.js +94 -43
- package/package.json +21 -19
package/dist/index.js
CHANGED
|
@@ -18,29 +18,47 @@ function createReactiveParams(params) {
|
|
|
18
18
|
return toRef(() => param);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
function getFunctionParametersLength(func) {
|
|
22
|
+
const funcStr = func.toString();
|
|
23
|
+
const params = funcStr.slice(funcStr.indexOf("(") + 1, funcStr.indexOf(")")).split(",").map((param) => param.trim());
|
|
24
|
+
const defaults = params.filter((param) => param.includes("="));
|
|
25
|
+
return defaults.length + func.length;
|
|
26
|
+
}
|
|
21
27
|
|
|
22
28
|
// src/core/createRule/defineRuleProcessors.ts
|
|
23
29
|
function defineRuleProcessors(definition, ...params) {
|
|
24
30
|
const { message, validator, active, ...properties } = definition;
|
|
25
31
|
const isAsync = validator.constructor.name === "AsyncFunction";
|
|
26
|
-
const
|
|
27
|
-
|
|
32
|
+
const defaultProcessors = {
|
|
33
|
+
validator(value, ...args) {
|
|
34
|
+
return definition.validator(value, ...unwrapRuleParameters(args.length ? args : params));
|
|
35
|
+
},
|
|
36
|
+
message(value, metadata) {
|
|
28
37
|
if (typeof definition.message === "function") {
|
|
29
|
-
return definition.message(value,
|
|
38
|
+
return definition.message(value, {
|
|
39
|
+
...metadata,
|
|
40
|
+
$params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
|
|
41
|
+
});
|
|
30
42
|
} else {
|
|
31
43
|
return definition.message;
|
|
32
44
|
}
|
|
33
45
|
},
|
|
34
|
-
|
|
35
|
-
return definition.validator(value, ...args.length ? args : params);
|
|
36
|
-
},
|
|
37
|
-
active(value, ...args) {
|
|
46
|
+
active(value, metadata) {
|
|
38
47
|
if (typeof definition.active === "function") {
|
|
39
|
-
return definition.active(value,
|
|
48
|
+
return definition.active(value, {
|
|
49
|
+
...metadata,
|
|
50
|
+
$params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
|
|
51
|
+
});
|
|
40
52
|
} else {
|
|
41
53
|
return definition.active ?? true;
|
|
42
54
|
}
|
|
43
55
|
},
|
|
56
|
+
exec(value) {
|
|
57
|
+
return definition.validator(value, ...unwrapRuleParameters(params));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const processors = {
|
|
61
|
+
...defaultProcessors,
|
|
44
62
|
...properties,
|
|
45
63
|
...{
|
|
46
64
|
_validator: definition.validator,
|
|
@@ -61,7 +79,7 @@ function createRule(definition) {
|
|
|
61
79
|
let fakeParams = [];
|
|
62
80
|
const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
|
|
63
81
|
const isAsync = definition.validator.constructor.name === "AsyncFunction";
|
|
64
|
-
if (definition.validator
|
|
82
|
+
if (getFunctionParametersLength(definition.validator) > 1) {
|
|
65
83
|
const ruleFactory = function(...params) {
|
|
66
84
|
return defineRuleProcessors(definition, ...params);
|
|
67
85
|
};
|
|
@@ -69,9 +87,10 @@ function createRule(definition) {
|
|
|
69
87
|
ruleFactory.message = staticProcessors.message;
|
|
70
88
|
ruleFactory.active = staticProcessors.active;
|
|
71
89
|
ruleFactory.type = staticProcessors.type;
|
|
72
|
-
ruleFactory.
|
|
73
|
-
ruleFactory.
|
|
74
|
-
ruleFactory.
|
|
90
|
+
ruleFactory.exec = staticProcessors.exec;
|
|
91
|
+
ruleFactory._validator = staticProcessors.validator;
|
|
92
|
+
ruleFactory._message = staticProcessors.message;
|
|
93
|
+
ruleFactory._active = staticProcessors.active;
|
|
75
94
|
ruleFactory._type = definition.type;
|
|
76
95
|
ruleFactory._patched = false;
|
|
77
96
|
ruleFactory._async = isAsync;
|
|
@@ -82,15 +101,18 @@ function createRule(definition) {
|
|
|
82
101
|
}
|
|
83
102
|
throw new Error("Validator must be a function");
|
|
84
103
|
}
|
|
104
|
+
function defineType(ruleName) {
|
|
105
|
+
return ruleName;
|
|
106
|
+
}
|
|
85
107
|
|
|
86
108
|
// src/core/useRegle/useRegle.ts
|
|
87
|
-
import { computed as
|
|
109
|
+
import { computed as computed6, isRef as isRef2, ref as ref4, shallowRef as shallowRef2, toRaw } from "vue";
|
|
88
110
|
|
|
89
111
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
90
|
-
import { computed as
|
|
112
|
+
import { computed as computed5, reactive as reactive5, unref as unref3 } from "vue";
|
|
91
113
|
|
|
92
114
|
// src/core/useStorage/useStorage.ts
|
|
93
|
-
import { ref, shallowRef } from "vue";
|
|
115
|
+
import { onScopeDispose, ref, shallowRef } from "vue";
|
|
94
116
|
function useStorage() {
|
|
95
117
|
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
96
118
|
const fieldsStorage = shallowRef(
|
|
@@ -179,10 +201,19 @@ function useStorage() {
|
|
|
179
201
|
} else {
|
|
180
202
|
const $pending = ref(false);
|
|
181
203
|
const $valid = ref(true);
|
|
182
|
-
|
|
183
|
-
|
|
204
|
+
const $metadata = ref({});
|
|
205
|
+
ruleStatusStorage.value.set(path, { $pending, $valid, $metadata });
|
|
206
|
+
return { $pending, $valid, $metadata };
|
|
184
207
|
}
|
|
185
208
|
}
|
|
209
|
+
onScopeDispose(() => {
|
|
210
|
+
ruleDeclStorage.value.clear();
|
|
211
|
+
fieldsStorage.value.clear();
|
|
212
|
+
collectionsStorage.value.clear();
|
|
213
|
+
dirtyStorage.value.clear();
|
|
214
|
+
ruleStatusStorage.value.clear();
|
|
215
|
+
arrayStatusStorage.value.clear();
|
|
216
|
+
});
|
|
186
217
|
return {
|
|
187
218
|
addRuleDeclEntry,
|
|
188
219
|
setDirtyEntry,
|
|
@@ -232,7 +263,7 @@ function isEmpty(value) {
|
|
|
232
263
|
}
|
|
233
264
|
|
|
234
265
|
// src/utils/composables.ts
|
|
235
|
-
import { effectScope, getCurrentScope, onScopeDispose } from "vue";
|
|
266
|
+
import { effectScope, getCurrentScope, onScopeDispose as onScopeDispose2 } from "vue";
|
|
236
267
|
|
|
237
268
|
// src/core/useRegle/guards/ruleDef.guards.ts
|
|
238
269
|
function isNestedRulesDef(state, rule) {
|
|
@@ -328,7 +359,7 @@ function useErrors($regle) {
|
|
|
328
359
|
}
|
|
329
360
|
|
|
330
361
|
// src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
|
|
331
|
-
import { computed as
|
|
362
|
+
import { computed as computed4, effectScope as effectScope4, reactive as reactive4, toRef as toRef4, watch as watch4 } from "vue";
|
|
332
363
|
|
|
333
364
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
334
365
|
import { nextTick, reactive as reactive3, ref as ref3, toRef as toRef3, toRefs, watch as watch3 } from "vue";
|
|
@@ -365,13 +396,18 @@ function createReactiveRuleStatus({
|
|
|
365
396
|
}) {
|
|
366
397
|
let scope = effectScope2();
|
|
367
398
|
let scopeState;
|
|
368
|
-
const { $pending, $valid } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
|
|
399
|
+
const { $pending, $valid, $metadata } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
|
|
369
400
|
function $watch() {
|
|
370
401
|
scopeState = scope.run(() => {
|
|
402
|
+
const $defaultMetadata = computed2(() => ({
|
|
403
|
+
$invalid: !$valid.value,
|
|
404
|
+
$params: $params.value,
|
|
405
|
+
...$metadata.value
|
|
406
|
+
}));
|
|
371
407
|
const $active = computed2(() => {
|
|
372
408
|
if (isFormRuleDefinition(rule)) {
|
|
373
409
|
if (typeof rule.value.active === "function") {
|
|
374
|
-
return rule.value.active(state.value,
|
|
410
|
+
return rule.value.active(state.value, $defaultMetadata.value);
|
|
375
411
|
} else {
|
|
376
412
|
return rule.value.active;
|
|
377
413
|
}
|
|
@@ -384,7 +420,7 @@ function createReactiveRuleStatus({
|
|
|
384
420
|
const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
|
|
385
421
|
if (customMessageRule) {
|
|
386
422
|
if (typeof customMessageRule === "function") {
|
|
387
|
-
message = customMessageRule(state.value,
|
|
423
|
+
message = customMessageRule(state.value, $defaultMetadata.value);
|
|
388
424
|
} else {
|
|
389
425
|
message = customMessageRule;
|
|
390
426
|
}
|
|
@@ -392,7 +428,7 @@ function createReactiveRuleStatus({
|
|
|
392
428
|
if (isFormRuleDefinition(rule)) {
|
|
393
429
|
if (!(customMessageRule && !rule.value._patched)) {
|
|
394
430
|
if (typeof rule.value.message === "function") {
|
|
395
|
-
message = rule.value.message(state.value,
|
|
431
|
+
message = rule.value.message(state.value, $defaultMetadata.value);
|
|
396
432
|
} else {
|
|
397
433
|
message = rule.value.message;
|
|
398
434
|
}
|
|
@@ -405,7 +441,7 @@ function createReactiveRuleStatus({
|
|
|
405
441
|
return message;
|
|
406
442
|
});
|
|
407
443
|
const $type = computed2(() => {
|
|
408
|
-
if (isFormRuleDefinition(rule)) {
|
|
444
|
+
if (isFormRuleDefinition(rule) && rule.value.type) {
|
|
409
445
|
return Object.values(InternalRuleType).includes(rule.value.type) ? ruleKey : rule.value.type;
|
|
410
446
|
} else {
|
|
411
447
|
return ruleKey;
|
|
@@ -449,7 +485,13 @@ function createReactiveRuleStatus({
|
|
|
449
485
|
$valid.value = true;
|
|
450
486
|
$pending.value = true;
|
|
451
487
|
const promiseResult = await resultOrPromise;
|
|
452
|
-
|
|
488
|
+
if (typeof promiseResult === "boolean") {
|
|
489
|
+
ruleResult = promiseResult;
|
|
490
|
+
} else {
|
|
491
|
+
const { $valid: $valid2, ...rest } = promiseResult;
|
|
492
|
+
ruleResult = $valid2;
|
|
493
|
+
$metadata.value = rest;
|
|
494
|
+
}
|
|
453
495
|
} catch (e) {
|
|
454
496
|
ruleResult = false;
|
|
455
497
|
} finally {
|
|
@@ -457,7 +499,15 @@ function createReactiveRuleStatus({
|
|
|
457
499
|
}
|
|
458
500
|
}
|
|
459
501
|
} else {
|
|
460
|
-
|
|
502
|
+
if (resultOrPromise != null) {
|
|
503
|
+
if (typeof resultOrPromise === "boolean") {
|
|
504
|
+
ruleResult = resultOrPromise;
|
|
505
|
+
} else {
|
|
506
|
+
const { $valid: $valid2, ...rest } = resultOrPromise;
|
|
507
|
+
ruleResult = $valid2;
|
|
508
|
+
$metadata.value = rest;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
461
511
|
}
|
|
462
512
|
$valid.value = ruleResult;
|
|
463
513
|
if (options.$externalErrors) {
|
|
@@ -548,7 +598,6 @@ function createReactiveFieldStatus({
|
|
|
548
598
|
$commit();
|
|
549
599
|
}
|
|
550
600
|
$externalErrors.value = [];
|
|
551
|
-
console.log($externalErrors.value);
|
|
552
601
|
});
|
|
553
602
|
function $unwatch() {
|
|
554
603
|
if ($rules.value) {
|
|
@@ -682,7 +731,7 @@ function createCollectionElement({
|
|
|
682
731
|
});
|
|
683
732
|
}
|
|
684
733
|
const $state = toRefs(value);
|
|
685
|
-
const $externalErrors =
|
|
734
|
+
const $externalErrors = toRef3(() => externalErrors.value?.[index]);
|
|
686
735
|
const $status = createReactiveChildrenStatus({
|
|
687
736
|
state: $state[index],
|
|
688
737
|
rulesDef: toRef3(() => rules),
|
|
@@ -690,7 +739,7 @@ function createCollectionElement({
|
|
|
690
739
|
path: $path,
|
|
691
740
|
storage,
|
|
692
741
|
options,
|
|
693
|
-
externalErrors: $externalErrors
|
|
742
|
+
externalErrors: $externalErrors
|
|
694
743
|
});
|
|
695
744
|
if ($status) {
|
|
696
745
|
$status.$id = value[index].$id ?? $id;
|
|
@@ -942,28 +991,28 @@ function createReactiveNestedStatus({
|
|
|
942
991
|
);
|
|
943
992
|
}
|
|
944
993
|
scopeState = scope.run(() => {
|
|
945
|
-
const $dirty =
|
|
994
|
+
const $dirty = computed4(() => {
|
|
946
995
|
return Object.entries($fields.value).every(([key, statusOrField]) => {
|
|
947
996
|
return statusOrField.$dirty;
|
|
948
997
|
});
|
|
949
998
|
});
|
|
950
|
-
const $anyDirty =
|
|
999
|
+
const $anyDirty = computed4(() => {
|
|
951
1000
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
952
1001
|
return statusOrField.$dirty;
|
|
953
1002
|
});
|
|
954
1003
|
});
|
|
955
|
-
const $invalid =
|
|
1004
|
+
const $invalid = computed4(() => {
|
|
956
1005
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
957
1006
|
return statusOrField.$invalid;
|
|
958
1007
|
});
|
|
959
1008
|
});
|
|
960
|
-
const $valid =
|
|
961
|
-
const $error =
|
|
1009
|
+
const $valid = computed4(() => !$invalid.value);
|
|
1010
|
+
const $error = computed4(() => {
|
|
962
1011
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
963
1012
|
return statusOrField.$error;
|
|
964
1013
|
});
|
|
965
1014
|
});
|
|
966
|
-
const $pending =
|
|
1015
|
+
const $pending = computed4(() => {
|
|
967
1016
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
968
1017
|
return statusOrField.$pending;
|
|
969
1018
|
});
|
|
@@ -1053,7 +1102,7 @@ function createReactiveChildrenStatus({
|
|
|
1053
1102
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
1054
1103
|
function useStateProperties(scopeRules, state, options, customRules) {
|
|
1055
1104
|
const storage = useStorage();
|
|
1056
|
-
const externalErrors =
|
|
1105
|
+
const externalErrors = computed5(() => unref3(options.$externalErrors));
|
|
1057
1106
|
const $regle = reactive5(
|
|
1058
1107
|
createReactiveNestedStatus({
|
|
1059
1108
|
rootRules: scopeRules,
|
|
@@ -1077,7 +1126,7 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1077
1126
|
rewardEarly: options?.rewardEarly ?? false
|
|
1078
1127
|
};
|
|
1079
1128
|
function useRegle2(state, rulesFactory, options2) {
|
|
1080
|
-
const scopeRules = isRef2(rulesFactory) ? rulesFactory :
|
|
1129
|
+
const scopeRules = isRef2(rulesFactory) ? rulesFactory : computed6(rulesFactory);
|
|
1081
1130
|
const resolvedOptions = {
|
|
1082
1131
|
...globalOptions,
|
|
1083
1132
|
...options2
|
|
@@ -1094,10 +1143,10 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1094
1143
|
state.value = toRaw(initialState.value);
|
|
1095
1144
|
$regle.$reset();
|
|
1096
1145
|
}
|
|
1097
|
-
const $valid =
|
|
1146
|
+
const $valid = computed6(() => {
|
|
1098
1147
|
return $regle.$valid && $regle.$dirty && !$regle.$pending;
|
|
1099
1148
|
});
|
|
1100
|
-
const $invalid =
|
|
1149
|
+
const $invalid = computed6(() => {
|
|
1101
1150
|
return $regle.$invalid && $regle.$dirty || $regle.$pending;
|
|
1102
1151
|
});
|
|
1103
1152
|
async function validateForm() {
|
|
@@ -1122,8 +1171,8 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1122
1171
|
}
|
|
1123
1172
|
var useRegle = createUseRegleComposable();
|
|
1124
1173
|
|
|
1125
|
-
// src/core/
|
|
1126
|
-
function
|
|
1174
|
+
// src/core/defineRegleConfig.ts
|
|
1175
|
+
function defineRegleConfig({
|
|
1127
1176
|
rules,
|
|
1128
1177
|
options
|
|
1129
1178
|
}) {
|
|
@@ -1133,6 +1182,8 @@ function defineRegleOptions({
|
|
|
1133
1182
|
export {
|
|
1134
1183
|
InternalRuleType,
|
|
1135
1184
|
createRule,
|
|
1136
|
-
|
|
1137
|
-
|
|
1185
|
+
defineRegleConfig,
|
|
1186
|
+
defineType,
|
|
1187
|
+
unwrapRuleParameters,
|
|
1188
|
+
useRegle
|
|
1138
1189
|
};
|
package/package.json
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Vue form validator",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint --ext .ts --ext .vue .",
|
|
7
7
|
"typecheck": "tsc --noEmit",
|
|
8
8
|
"release": "npm publish",
|
|
9
9
|
"build": "tsup",
|
|
10
|
-
"
|
|
10
|
+
"build:local": "tsup --clean false",
|
|
11
|
+
"dev": "tsup --config=tsup.dev.ts --watch",
|
|
12
|
+
"test": "echo 'no tests'"
|
|
11
13
|
},
|
|
12
14
|
"devDependencies": {
|
|
15
|
+
"@total-typescript/ts-reset": "0.5.1",
|
|
16
|
+
"@types/node": "20.11.5",
|
|
13
17
|
"@types/prettier": "3.0.0",
|
|
14
|
-
"@typescript-eslint/eslint-plugin": "6.
|
|
15
|
-
"@typescript-eslint/parser": "6.
|
|
16
|
-
"@
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"changelogithub": "0.13.0",
|
|
18
|
+
"@typescript-eslint/eslint-plugin": "6.19.0",
|
|
19
|
+
"@typescript-eslint/parser": "6.19.0",
|
|
20
|
+
"@vue/test-utils": "2.4.3",
|
|
21
|
+
"bumpp": "9.3.0",
|
|
22
|
+
"changelogithub": "0.13.3",
|
|
20
23
|
"cross-env": "7.0.3",
|
|
21
|
-
"eslint": "8.
|
|
22
|
-
"eslint-config-prettier": "9.
|
|
23
|
-
"eslint-plugin-vue": "9.
|
|
24
|
-
"prettier": "3.
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"vue
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"type-fest": "4.4.0"
|
|
24
|
+
"eslint": "8.56.0",
|
|
25
|
+
"eslint-config-prettier": "9.1.0",
|
|
26
|
+
"eslint-plugin-vue": "9.20.1",
|
|
27
|
+
"prettier": "3.2.4",
|
|
28
|
+
"tsup": "8.0.1",
|
|
29
|
+
"type-fest": "4.9.0",
|
|
30
|
+
"typescript": "5.3.3",
|
|
31
|
+
"vue": "3.4.14",
|
|
32
|
+
"vue-eslint-parser": "9.4.0",
|
|
33
|
+
"vue-tsc": "1.8.27"
|
|
32
34
|
},
|
|
33
35
|
"type": "module",
|
|
34
36
|
"exports": {
|