@regle/schemas 1.1.0-beta.2 → 1.1.0-beta.3
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-schemas.d.ts +575 -35
- package/dist/regle-schemas.min.mjs +1 -1
- package/dist/regle-schemas.mjs +2 -2
- package/package.json +16 -23
- package/dist/regle-schemas.cjs +0 -250
- package/dist/regle-schemas.d.cts +0 -360
- package/dist/regle-schemas.min.cjs +0 -1
- package/index.cjs +0 -7
- package/index.js +0 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
import {useRootStorage}from'@regle/core';import {ref,computed,unref,isRef,watch}from'vue';function
|
|
1
|
+
import {useRootStorage}from'@regle/core';import {ref,computed,unref,isRef,watch}from'vue';function k(e){if(typeof e.source.flags=="string")return e.source.flags;{let t=[];return e.global&&t.push("g"),e.ignoreCase&&t.push("i"),e.multiline&&t.push("m"),e.sticky&&t.push("y"),e.unicode&&t.push("u"),t.join("")}}function f(e){let t=e,r={}.toString.call(e).slice(8,-1);if(r=="Set"&&(t=new Set([...e].map(a=>f(a)))),r=="Map"&&(t=new Map([...e].map(a=>[f(a[0]),f(a[1])]))),r=="Date"&&(t=new Date(e.getTime())),r=="RegExp"&&(t=RegExp(e.source,k(e))),r=="Array"||r=="Object"){t=Array.isArray(e)?[]:{};for(let a in e)t[a]=f(e[a]);}return t}function S(e){return e&&(e instanceof Date||e.constructor.name=="File"||e.constructor.name=="FileList")?false:typeof e=="object"&&e!==null&&!Array.isArray(e)}function v(e,t,r,a){var n,o;if(Array.isArray(t)&&(n=t.slice(0)),typeof t=="string"&&(n=t.split(".")),typeof t=="symbol"&&(n=[t]),!Array.isArray(n))throw new Error("props arg must be an array, a string or a symbol");if(o=n.pop(),!o)return false;w(o);for(var s;s=n.shift();)if(w(s),isNaN(parseInt(s))?(typeof e[s]>"u"&&(e[s]={}),e=e[s]):((e.$each??=[])[s]={},e=e.$each[s]),!e||typeof e!="object")return false;return a?e[o]?e[o].$self=(e[o].$self??=[]).concat(r):e[o]={...e[o],$self:r}:Array.isArray(e[o])?e[o]=e[o].concat(r):e[o]=r,true}function b(e,t,r){if(!e)return r;var a,n;if(Array.isArray(t)&&(a=t.slice(0)),typeof t=="string"&&(a=t.split(".")),typeof t=="symbol"&&(a=[t]),!Array.isArray(a))throw new Error("props arg must be an array, a string or a symbol");for(;a.length;)if(n=a.shift(),!e||!n||(e=e[n],e===void 0))return r;return e}function w(e){if(e=="__proto__"||e=="constructor"||e=="prototype")throw new Error("setting of prototype values not supported")}function g(e,t){let r={autoDirty:e?.autoDirty,lazy:e?.lazy,rewardEarly:e?.rewardEarly,clearExternalErrorsOnChange:e?.clearExternalErrorsOnChange};function a(n,o,s){let I=ref({}),p=computed(()=>unref(o)),A={...r,...s},M=computed(()=>!S(l.value)),l=isRef(n)?n:ref(n),C=ref(S(l.value)?{...f(l.value)}:f(l.value)),y=ref({}),T;if(!p.value?.["~standard"])throw new Error('Only "standard-schema" compatible libraries are supported');function V(i){let u={};return i.issues&&i.issues.map(c=>{let x=c.path?.map(d=>typeof d=="object"?d.key:d.toString()).join(".")??"",h=c.path?.[c.path.length-1],B=(typeof h=="object"&&"value"in h?Array.isArray(h.value):false)||("type"in c?c.type==="array":false)||Array.isArray(b(l.value,x));return {path:x,message:c.message,isArray:B}}).forEach(c=>{v(u,c.path,[c.message],c.isArray);}),u}async function D(){let i=p.value["~standard"].validate(l.value);return i instanceof Promise&&(i=await i),M.value?y.value=i.issues?.map(u=>u.message)??[]:y.value=V(i),i}return watch([l,p],D,{deep:true,immediate:true}),T=async()=>{try{return {valid:!(await D()).issues?.length,data:l.value}}catch(i){return Promise.reject(i)}},{r$:useRootStorage({scopeRules:I,state:l,options:A,schemaErrors:y,initialState:C,shortcuts:t,schemaMode:true,onValidate:T}).regle}}return a}var P=g();function F(e,t){return e}function R(){function e(t,r){return r}return e}var E=R();function N({modifiers:e,shortcuts:t}){let r=g(e,t),a=R();return {useRegleSchema:r,inferSchema:a}}export{N as defineRegleSchemaConfig,E as inferSchema,P as useRegleSchema,F as withDeps};
|
package/dist/regle-schemas.mjs
CHANGED
|
@@ -225,10 +225,10 @@ function withDeps(schema, depsArray) {
|
|
|
225
225
|
return schema;
|
|
226
226
|
}
|
|
227
227
|
function createInferSchemaHelper() {
|
|
228
|
-
function
|
|
228
|
+
function inferSchema2(state, rulesFactory) {
|
|
229
229
|
return rulesFactory;
|
|
230
230
|
}
|
|
231
|
-
return
|
|
231
|
+
return inferSchema2;
|
|
232
232
|
}
|
|
233
233
|
var inferSchema = createInferSchemaHelper();
|
|
234
234
|
|
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/schemas",
|
|
3
|
-
"version": "1.1.0-beta.
|
|
3
|
+
"version": "1.1.0-beta.3",
|
|
4
4
|
"description": "Schemas adapter for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@standard-schema/spec": "1.0.0",
|
|
7
|
-
"@regle/core": "1.1.0-beta.
|
|
8
|
-
"@regle/rules": "1.1.0-beta.
|
|
7
|
+
"@regle/core": "1.1.0-beta.3",
|
|
8
|
+
"@regle/rules": "1.1.0-beta.3"
|
|
9
9
|
},
|
|
10
10
|
"peerDependencies": {
|
|
11
|
-
"valibot": "^1.0.0
|
|
12
|
-
"zod": "^3.24.0"
|
|
11
|
+
"valibot": "^1.0.0",
|
|
12
|
+
"zod": "^3.24.0",
|
|
13
|
+
"arktype": "^2.1.0"
|
|
13
14
|
},
|
|
14
15
|
"peerDependenciesMeta": {
|
|
15
16
|
"valibot": {
|
|
@@ -17,28 +18,28 @@
|
|
|
17
18
|
},
|
|
18
19
|
"zod": {
|
|
19
20
|
"optional": true
|
|
21
|
+
},
|
|
22
|
+
"arktype": {
|
|
23
|
+
"optional": true
|
|
20
24
|
}
|
|
21
25
|
},
|
|
22
26
|
"devDependencies": {
|
|
23
27
|
"@total-typescript/ts-reset": "0.6.1",
|
|
24
|
-
"@types/node": "22.13.
|
|
28
|
+
"@types/node": "22.13.17",
|
|
25
29
|
"@typescript-eslint/eslint-plugin": "8.28.0",
|
|
26
30
|
"@typescript-eslint/parser": "8.28.0",
|
|
27
31
|
"@vue/test-utils": "2.4.6",
|
|
28
|
-
"bumpp": "10.1.0",
|
|
29
|
-
"changelogithub": "13.13.0",
|
|
30
|
-
"cross-env": "7.0.3",
|
|
31
32
|
"eslint": "9.15.0",
|
|
32
33
|
"eslint-config-prettier": "9.1.0",
|
|
33
34
|
"eslint-plugin-vue": "9.31.0",
|
|
34
|
-
"prettier": "3.
|
|
35
|
+
"prettier": "3.5.3",
|
|
35
36
|
"tsup": "8.4.0",
|
|
36
|
-
"type-fest": "4.
|
|
37
|
+
"type-fest": "4.38.0",
|
|
37
38
|
"typescript": "5.8.2",
|
|
38
39
|
"valibot": "1.0.0",
|
|
39
|
-
"vitest": "3.
|
|
40
|
+
"vitest": "3.1.1",
|
|
40
41
|
"vue": "3.5.13",
|
|
41
|
-
"vue-eslint-parser": "
|
|
42
|
+
"vue-eslint-parser": "10.1.3",
|
|
42
43
|
"vue-tsc": "2.2.8",
|
|
43
44
|
"zod": "3.24.2"
|
|
44
45
|
},
|
|
@@ -51,26 +52,18 @@
|
|
|
51
52
|
"production": "./dist/regle-schemas.min.mjs",
|
|
52
53
|
"development": "./dist/regle-schemas.mjs",
|
|
53
54
|
"default": "./dist/regle-schemas.mjs"
|
|
54
|
-
},
|
|
55
|
-
"require": {
|
|
56
|
-
"production": "./dist/regle-schemas.min.cjs",
|
|
57
|
-
"development": "./dist/regle-schemas.cjs",
|
|
58
|
-
"default": "./index.js"
|
|
59
55
|
}
|
|
60
56
|
},
|
|
61
|
-
"import": "./dist/regle-schemas.mjs"
|
|
62
|
-
"require": "./index.js"
|
|
57
|
+
"import": "./dist/regle-schemas.mjs"
|
|
63
58
|
},
|
|
64
59
|
"./package.json": "./package.json",
|
|
65
60
|
"./dist/*": "./dist/*"
|
|
66
61
|
},
|
|
67
|
-
"main": "
|
|
62
|
+
"main": "./dist/regle-schemas.mjs",
|
|
68
63
|
"module": "./dist/regle-schemas.mjs",
|
|
69
64
|
"types": "./dist/regle-schemas.d.ts",
|
|
70
65
|
"files": [
|
|
71
66
|
"dist",
|
|
72
|
-
"index.js",
|
|
73
|
-
"index.cjs",
|
|
74
67
|
"LICENSE",
|
|
75
68
|
"README.md"
|
|
76
69
|
],
|
package/dist/regle-schemas.cjs
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var core = require('@regle/core');
|
|
4
|
-
var vue = require('vue');
|
|
5
|
-
|
|
6
|
-
// src/core/useRegleSchema.ts
|
|
7
|
-
|
|
8
|
-
// ../shared/utils/cloneDeep.ts
|
|
9
|
-
function getRegExpFlags(regExp) {
|
|
10
|
-
if (typeof regExp.source.flags == "string") {
|
|
11
|
-
return regExp.source.flags;
|
|
12
|
-
} else {
|
|
13
|
-
let flags = [];
|
|
14
|
-
regExp.global && flags.push("g");
|
|
15
|
-
regExp.ignoreCase && flags.push("i");
|
|
16
|
-
regExp.multiline && flags.push("m");
|
|
17
|
-
regExp.sticky && flags.push("y");
|
|
18
|
-
regExp.unicode && flags.push("u");
|
|
19
|
-
return flags.join("");
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
function cloneDeep(obj) {
|
|
23
|
-
let result = obj;
|
|
24
|
-
let type = {}.toString.call(obj).slice(8, -1);
|
|
25
|
-
if (type == "Set") {
|
|
26
|
-
result = new Set([...obj].map((value) => cloneDeep(value)));
|
|
27
|
-
}
|
|
28
|
-
if (type == "Map") {
|
|
29
|
-
result = new Map([...obj].map((kv) => [cloneDeep(kv[0]), cloneDeep(kv[1])]));
|
|
30
|
-
}
|
|
31
|
-
if (type == "Date") {
|
|
32
|
-
result = new Date(obj.getTime());
|
|
33
|
-
}
|
|
34
|
-
if (type == "RegExp") {
|
|
35
|
-
result = RegExp(obj.source, getRegExpFlags(obj));
|
|
36
|
-
}
|
|
37
|
-
if (type == "Array" || type == "Object") {
|
|
38
|
-
result = Array.isArray(obj) ? [] : {};
|
|
39
|
-
for (let key in obj) {
|
|
40
|
-
result[key] = cloneDeep(obj[key]);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// ../shared/utils/object.utils.ts
|
|
47
|
-
function isObject(obj) {
|
|
48
|
-
if (obj && (obj instanceof Date || obj.constructor.name == "File" || obj.constructor.name == "FileList")) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
52
|
-
}
|
|
53
|
-
function setObjectError(obj, propsArg, value, isArray) {
|
|
54
|
-
var props, lastProp;
|
|
55
|
-
if (Array.isArray(propsArg)) {
|
|
56
|
-
props = propsArg.slice(0);
|
|
57
|
-
}
|
|
58
|
-
if (typeof propsArg == "string") {
|
|
59
|
-
props = propsArg.split(".");
|
|
60
|
-
}
|
|
61
|
-
if (typeof propsArg == "symbol") {
|
|
62
|
-
props = [propsArg];
|
|
63
|
-
}
|
|
64
|
-
if (!Array.isArray(props)) {
|
|
65
|
-
throw new Error("props arg must be an array, a string or a symbol");
|
|
66
|
-
}
|
|
67
|
-
lastProp = props.pop();
|
|
68
|
-
if (!lastProp) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
prototypeCheck(lastProp);
|
|
72
|
-
var thisProp;
|
|
73
|
-
while (thisProp = props.shift()) {
|
|
74
|
-
prototypeCheck(thisProp);
|
|
75
|
-
if (!isNaN(parseInt(thisProp))) {
|
|
76
|
-
(obj.$each ??= [])[thisProp] = {};
|
|
77
|
-
obj = obj.$each[thisProp];
|
|
78
|
-
} else {
|
|
79
|
-
if (typeof obj[thisProp] == "undefined") {
|
|
80
|
-
obj[thisProp] = {};
|
|
81
|
-
}
|
|
82
|
-
obj = obj[thisProp];
|
|
83
|
-
}
|
|
84
|
-
if (!obj || typeof obj != "object") {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (isArray) {
|
|
89
|
-
if (!obj[lastProp]) {
|
|
90
|
-
obj[lastProp] = { ...obj[lastProp], $self: value };
|
|
91
|
-
} else {
|
|
92
|
-
obj[lastProp].$self = (obj[lastProp].$self ??= []).concat(value);
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
if (Array.isArray(obj[lastProp])) {
|
|
96
|
-
obj[lastProp] = obj[lastProp].concat(value);
|
|
97
|
-
} else {
|
|
98
|
-
obj[lastProp] = value;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
function getDotPath(obj, propsArg, defaultValue) {
|
|
104
|
-
if (!obj) {
|
|
105
|
-
return defaultValue;
|
|
106
|
-
}
|
|
107
|
-
var props, prop;
|
|
108
|
-
if (Array.isArray(propsArg)) {
|
|
109
|
-
props = propsArg.slice(0);
|
|
110
|
-
}
|
|
111
|
-
if (typeof propsArg == "string") {
|
|
112
|
-
props = propsArg.split(".");
|
|
113
|
-
}
|
|
114
|
-
if (typeof propsArg == "symbol") {
|
|
115
|
-
props = [propsArg];
|
|
116
|
-
}
|
|
117
|
-
if (!Array.isArray(props)) {
|
|
118
|
-
throw new Error("props arg must be an array, a string or a symbol");
|
|
119
|
-
}
|
|
120
|
-
while (props.length) {
|
|
121
|
-
prop = props.shift();
|
|
122
|
-
if (!obj) {
|
|
123
|
-
return defaultValue;
|
|
124
|
-
}
|
|
125
|
-
if (!prop) {
|
|
126
|
-
return defaultValue;
|
|
127
|
-
}
|
|
128
|
-
obj = obj[prop];
|
|
129
|
-
if (obj === void 0) {
|
|
130
|
-
return defaultValue;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return obj;
|
|
134
|
-
}
|
|
135
|
-
function prototypeCheck(prop) {
|
|
136
|
-
if (prop == "__proto__" || prop == "constructor" || prop == "prototype") {
|
|
137
|
-
throw new Error("setting of prototype values not supported");
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// src/core/useRegleSchema.ts
|
|
142
|
-
function createUseRegleSchemaComposable(options, shortcuts) {
|
|
143
|
-
const globalOptions = {
|
|
144
|
-
autoDirty: options?.autoDirty,
|
|
145
|
-
lazy: options?.lazy,
|
|
146
|
-
rewardEarly: options?.rewardEarly,
|
|
147
|
-
clearExternalErrorsOnChange: options?.clearExternalErrorsOnChange
|
|
148
|
-
};
|
|
149
|
-
function useRegleSchema2(state, schema, options2) {
|
|
150
|
-
const convertedRules = vue.ref({});
|
|
151
|
-
const computedSchema = vue.computed(() => vue.unref(schema));
|
|
152
|
-
const resolvedOptions = {
|
|
153
|
-
...globalOptions,
|
|
154
|
-
...options2
|
|
155
|
-
};
|
|
156
|
-
const isSingleField = vue.computed(() => !isObject(processedState.value));
|
|
157
|
-
const processedState = vue.isRef(state) ? state : vue.ref(state);
|
|
158
|
-
const initialState = vue.ref(
|
|
159
|
-
isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value)
|
|
160
|
-
);
|
|
161
|
-
const customErrors = vue.ref({});
|
|
162
|
-
let onValidate = void 0;
|
|
163
|
-
if (!computedSchema.value?.["~standard"]) {
|
|
164
|
-
throw new Error(`Only "standard-schema" compatible libraries are supported`);
|
|
165
|
-
}
|
|
166
|
-
function issuesToRegleErrors(result) {
|
|
167
|
-
const output = {};
|
|
168
|
-
if (result.issues) {
|
|
169
|
-
const errors = result.issues.map((issue) => {
|
|
170
|
-
const path = issue.path?.map((item) => typeof item === "object" ? item.key : item.toString()).join(".") ?? "";
|
|
171
|
-
const lastItem = issue.path?.[issue.path.length - 1];
|
|
172
|
-
const isArray = (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false) || Array.isArray(getDotPath(processedState.value, path));
|
|
173
|
-
return {
|
|
174
|
-
path,
|
|
175
|
-
message: issue.message,
|
|
176
|
-
isArray
|
|
177
|
-
};
|
|
178
|
-
});
|
|
179
|
-
errors.forEach((error) => {
|
|
180
|
-
setObjectError(output, error.path, [error.message], error.isArray);
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
return output;
|
|
184
|
-
}
|
|
185
|
-
async function computeErrors() {
|
|
186
|
-
let result = computedSchema.value["~standard"].validate(processedState.value);
|
|
187
|
-
if (result instanceof Promise) {
|
|
188
|
-
result = await result;
|
|
189
|
-
}
|
|
190
|
-
if (isSingleField.value) {
|
|
191
|
-
customErrors.value = result.issues?.map((issue) => issue.message) ?? [];
|
|
192
|
-
} else {
|
|
193
|
-
customErrors.value = issuesToRegleErrors(result);
|
|
194
|
-
}
|
|
195
|
-
return result;
|
|
196
|
-
}
|
|
197
|
-
vue.watch([processedState, computedSchema], computeErrors, { deep: true, immediate: true });
|
|
198
|
-
onValidate = async () => {
|
|
199
|
-
try {
|
|
200
|
-
const result = await computeErrors();
|
|
201
|
-
return {
|
|
202
|
-
valid: !result.issues?.length,
|
|
203
|
-
data: processedState.value
|
|
204
|
-
};
|
|
205
|
-
} catch (e) {
|
|
206
|
-
return Promise.reject(e);
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
const regle = core.useRootStorage({
|
|
210
|
-
scopeRules: convertedRules,
|
|
211
|
-
state: processedState,
|
|
212
|
-
options: resolvedOptions,
|
|
213
|
-
schemaErrors: customErrors,
|
|
214
|
-
initialState,
|
|
215
|
-
shortcuts,
|
|
216
|
-
schemaMode: true,
|
|
217
|
-
onValidate
|
|
218
|
-
});
|
|
219
|
-
return {
|
|
220
|
-
r$: regle.regle
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
return useRegleSchema2;
|
|
224
|
-
}
|
|
225
|
-
var useRegleSchema = createUseRegleSchemaComposable();
|
|
226
|
-
function withDeps(schema, depsArray) {
|
|
227
|
-
return schema;
|
|
228
|
-
}
|
|
229
|
-
function createInferSchemaHelper() {
|
|
230
|
-
function inferRules(state, rulesFactory) {
|
|
231
|
-
return rulesFactory;
|
|
232
|
-
}
|
|
233
|
-
return inferRules;
|
|
234
|
-
}
|
|
235
|
-
var inferSchema = createInferSchemaHelper();
|
|
236
|
-
|
|
237
|
-
// src/core/defineRegleSchemaConfig.ts
|
|
238
|
-
function defineRegleSchemaConfig({
|
|
239
|
-
modifiers,
|
|
240
|
-
shortcuts
|
|
241
|
-
}) {
|
|
242
|
-
const useRegleSchema2 = createUseRegleSchemaComposable(modifiers, shortcuts);
|
|
243
|
-
const inferSchema2 = createInferSchemaHelper();
|
|
244
|
-
return { useRegleSchema: useRegleSchema2, inferSchema: inferSchema2 };
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
exports.defineRegleSchemaConfig = defineRegleSchemaConfig;
|
|
248
|
-
exports.inferSchema = inferSchema;
|
|
249
|
-
exports.useRegleSchema = useRegleSchema;
|
|
250
|
-
exports.withDeps = withDeps;
|