@regle/schemas 1.19.11 → 1.19.12

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @regle/schemas v1.19.11
2
+ * @regle/schemas v1.19.12
3
3
  * (c) 2026 Victor Garcia
4
4
  * @license MIT
5
5
  */
@@ -1,11 +1,11 @@
1
1
  /**
2
- * @regle/schemas v1.19.11
2
+ * @regle/schemas v1.19.12
3
3
  * (c) 2026 Victor Garcia
4
4
  * @license MIT
5
5
  */
6
6
 
7
7
  import { createScopedUseRegle, useRootStorage } from "@regle/core";
8
- import { computed, getCurrentScope, isRef, onScopeDispose, ref, unref, watch } from "vue";
8
+ import { computed, effectScope, getCurrentScope, isRef, nextTick, onScopeDispose, reactive, ref, toValue, unref, watch } from "vue";
9
9
 
10
10
  /**
11
11
  * Server side friendly way of checking for a File
@@ -152,10 +152,194 @@ function cloneDeep(obj, dep = 0) {
152
152
  return result;
153
153
  }
154
154
 
155
+ /**
156
+ * Converts ref to reactive.
157
+ *
158
+ * @see https://vueuse.org/toReactive
159
+ * @param objectRef A ref of object
160
+ */
161
+ function toReactive(objectRef) {
162
+ if (!isRef(objectRef)) return reactive(objectRef);
163
+ return reactive(new Proxy({}, {
164
+ get(_, p, receiver) {
165
+ if (objectRef.value === void 0) return void 0;
166
+ return unref(Reflect.get(objectRef.value, p, receiver));
167
+ },
168
+ set(_, p, value) {
169
+ if (isRef(objectRef.value[p]) && !isRef(value)) objectRef.value[p].value = value;
170
+ else objectRef.value[p] = value;
171
+ return true;
172
+ },
173
+ deleteProperty(_, p) {
174
+ return Reflect.deleteProperty(objectRef.value, p);
175
+ },
176
+ has(_, p) {
177
+ if (objectRef.value === void 0) return false;
178
+ return Reflect.has(objectRef.value, p);
179
+ },
180
+ ownKeys() {
181
+ if (objectRef.value === void 0) return [];
182
+ return Object.keys(objectRef.value);
183
+ },
184
+ getOwnPropertyDescriptor() {
185
+ return {
186
+ enumerable: true,
187
+ configurable: true
188
+ };
189
+ }
190
+ }));
191
+ }
192
+
193
+ function getIssuePath(issue) {
194
+ return issue.path?.map((item) => typeof item === "object" ? item.key : item.toString()).join(".") ?? "";
195
+ }
196
+ function getIssueLastPathKey(issue) {
197
+ const lastItem = issue.path?.at(-1);
198
+ return typeof lastItem === "object" ? lastItem.key : lastItem;
199
+ }
200
+ function getParentArrayPath(issue) {
201
+ const lastItem = issue.path?.at(-1);
202
+ const isNestedPath = typeof lastItem === "object" ? typeof lastItem.key === "string" : typeof lastItem === "string";
203
+ const index = issue.path?.findLastIndex((item) => typeof item === "object" ? typeof item.key === "number" : typeof item === "number");
204
+ if (!isNestedPath && index === -1) return;
205
+ if (index != null) {
206
+ const truncatedPath = issue.path?.slice(0, index + 1);
207
+ return {
208
+ ...issue,
209
+ path: truncatedPath
210
+ };
211
+ }
212
+ }
213
+ function getPropertiesFromIssue(issue, processedState) {
214
+ const $path = getIssuePath(issue);
215
+ const lastItem = issue.path?.[issue.path.length - 1];
216
+ const lastItemKey = typeof lastItem === "object" ? lastItem.key : lastItem;
217
+ return {
218
+ 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)),
219
+ $path,
220
+ lastItemKey,
221
+ lastItem
222
+ };
223
+ }
224
+
225
+ function filterIssues(issues, previousIssues, rewardEarly, isValidate = false) {
226
+ if (!isValidate && rewardEarly) {
227
+ if (previousIssues.value.length) return previousIssues.value.reduce((acc, prevIssue) => {
228
+ if ("$currentArrayValue" in prevIssue && isObject(prevIssue.$currentArrayValue) && "$id" in prevIssue.$currentArrayValue) {
229
+ const previousItemId = prevIssue.$currentArrayValue.$id;
230
+ const previousLastPathKey = getIssueLastPathKey(prevIssue);
231
+ const previousArrayIssue = issues.find((currentIssue) => currentIssue?.$currentArrayValue?.["$id"] === previousItemId && getIssueLastPathKey(currentIssue) === previousLastPathKey);
232
+ if (previousArrayIssue) acc.push({
233
+ ...prevIssue,
234
+ path: previousArrayIssue?.path ?? []
235
+ });
236
+ } else if (issues.some((issue) => getIssuePath(issue) === getIssuePath(prevIssue))) acc.push(prevIssue);
237
+ return acc;
238
+ }, []);
239
+ return [];
240
+ }
241
+ return issues;
242
+ }
243
+ function mapSingleFieldIssues(issues, previousIssues, rewardEarly, isValidate = false) {
244
+ return filterIssues(issues, previousIssues, rewardEarly, isValidate)?.map((issue) => ({
245
+ $message: issue.message,
246
+ $property: issue.path?.[issue.path.length - 1]?.toString() ?? "-",
247
+ $rule: "schema",
248
+ ...issue
249
+ })) ?? [];
250
+ }
251
+ function issuesToRegleErrors({ result, previousIssues, processedState, rewardEarly, isValidate = false }) {
252
+ const output = {};
253
+ const mappedIssues = result.issues?.map((issue) => {
254
+ const parentArrayPath = getParentArrayPath(issue);
255
+ if (parentArrayPath) {
256
+ const $currentArrayValue = getDotPath(processedState.value, getIssuePath(parentArrayPath));
257
+ Object.defineProperty(issue, "$currentArrayValue", {
258
+ value: $currentArrayValue,
259
+ enumerable: true,
260
+ configurable: true,
261
+ writable: true
262
+ });
263
+ }
264
+ return issue;
265
+ });
266
+ const filteredIssues = filterIssues(mappedIssues ?? [], previousIssues, rewardEarly, isValidate);
267
+ if (mappedIssues?.length) {
268
+ const issues = filteredIssues.map((issue) => {
269
+ const { isArray, $path, lastItemKey } = getPropertiesFromIssue(issue, processedState);
270
+ return {
271
+ ...issue,
272
+ $path,
273
+ isArray,
274
+ $property: lastItemKey,
275
+ $rule: "schema",
276
+ $message: issue.message
277
+ };
278
+ });
279
+ issues.forEach(({ isArray, $path, ...issue }) => {
280
+ setObjectError(output, $path, [issue], isArray);
281
+ });
282
+ previousIssues.value = issues;
283
+ } else previousIssues.value = [];
284
+ return output;
285
+ }
286
+
287
+ function createSchemaState(state) {
288
+ const processedState = isRef(state) ? state : ref(state);
289
+ return {
290
+ processedState,
291
+ isSingleField: computed(() => !isObject(processedState.value)),
292
+ initialState: ref(isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value)),
293
+ originalState: isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value)
294
+ };
295
+ }
296
+
297
+ function createSchemaValidationRunner({ processedState, getSchema, isSingleField, customErrors, previousIssues, resolvedOptions, syncOnUpdate, syncOnValidate }) {
298
+ let unWatchState;
299
+ function defineWatchState() {
300
+ stopWatching();
301
+ unWatchState = watch([processedState, getSchema], () => {
302
+ if (resolvedOptions.silent) return;
303
+ if (getSchema()["~standard"].vendor === "regle") return;
304
+ computeErrors();
305
+ }, { deep: true });
306
+ }
307
+ function stopWatching() {
308
+ unWatchState?.();
309
+ unWatchState = void 0;
310
+ }
311
+ async function computeErrors(isValidate = false) {
312
+ let result = getSchema()["~standard"].validate(processedState.value);
313
+ if (result instanceof Promise) result = await result;
314
+ if (isSingleField.value) customErrors.value = mapSingleFieldIssues(result.issues ?? [], previousIssues, toValue(resolvedOptions.rewardEarly), isValidate);
315
+ else customErrors.value = issuesToRegleErrors({
316
+ result,
317
+ previousIssues,
318
+ processedState,
319
+ rewardEarly: toValue(resolvedOptions.rewardEarly),
320
+ isValidate
321
+ });
322
+ if (!result.issues) {
323
+ if (isValidate && syncOnValidate || !isValidate && syncOnUpdate) {
324
+ stopWatching();
325
+ if (isObject(processedState.value)) processedState.value = merge(processedState.value, result.value);
326
+ else processedState.value = result.value;
327
+ defineWatchState();
328
+ }
329
+ }
330
+ return result;
331
+ }
332
+ return {
333
+ computeErrors,
334
+ defineWatchState,
335
+ stopWatching
336
+ };
337
+ }
338
+
155
339
  function createUseRegleSchemaComposable(params) {
156
340
  const { options: modifiers, shortcuts, overrides } = params ?? {};
157
341
  function useRegleSchema(state, schema, options) {
158
- const computedSchema = computed(() => unref(schema));
342
+ if (!unref(schema)?.["~standard"]) throw new Error(`Only "standard-schema" compatible libraries are supported`);
159
343
  const { syncState = {
160
344
  onUpdate: false,
161
345
  onValidate: false
@@ -165,136 +349,55 @@ function createUseRegleSchemaComposable(params) {
165
349
  ...modifiers,
166
350
  ...defaultOptions
167
351
  };
168
- const isSingleField = computed(() => !isObject(processedState.value));
169
- const processedState = isRef(state) ? state : ref(state);
170
- const initialState = ref(isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value));
171
- const originalState = isObject(processedState.value) ? { ...cloneDeep(processedState.value) } : cloneDeep(processedState.value);
352
+ const { processedState, isSingleField, initialState, originalState } = createSchemaState(state);
172
353
  const customErrors = ref({});
173
354
  const previousIssues = ref([]);
174
- let onValidate = void 0;
175
- function getPropertiesFromIssue(issue) {
176
- let $path = getIssuePath(issue);
177
- const lastItem = issue.path?.[issue.path.length - 1];
178
- const lastItemKey = typeof lastItem === "object" ? lastItem.key : lastItem;
179
- return {
180
- 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)),
181
- $path,
182
- lastItemKey,
183
- lastItem
184
- };
185
- }
186
- function getIssuePath(issue) {
187
- return issue.path?.map((item) => typeof item === "object" ? item.key : item.toString()).join(".") ?? "";
188
- }
189
- function getIssueLastPathKey(issue) {
190
- const lastItem = issue.path?.at(-1);
191
- return typeof lastItem === "object" ? lastItem.key : lastItem;
192
- }
193
- function getParentArrayPath(issue) {
194
- const lastItem = issue.path?.at(-1);
195
- const isNestedPath = typeof lastItem === "object" ? typeof lastItem.key === "string" : typeof lastItem === "string";
196
- const index = issue.path?.findLastIndex((item) => typeof item === "object" ? typeof item.key === "number" : typeof item === "number");
197
- if (!isNestedPath && index === -1) return;
198
- if (index != null) {
199
- const truncatedPath = issue.path?.slice(0, index + 1);
200
- return {
201
- ...issue,
202
- path: truncatedPath
203
- };
204
- }
205
- }
206
- if (!computedSchema.value?.["~standard"]) throw new Error(`Only "standard-schema" compatible libraries are supported`);
207
- function filterIssues(issues, isValidate = false) {
208
- if (!isValidate && resolvedOptions.rewardEarly) {
209
- if (previousIssues.value.length) return previousIssues.value.reduce((acc, prevIssue) => {
210
- if ("$currentArrayValue" in prevIssue && isObject(prevIssue.$currentArrayValue) && "$id" in prevIssue.$currentArrayValue) {
211
- const previousItemId = prevIssue.$currentArrayValue.$id;
212
- const previousLastPathKey = getIssueLastPathKey(prevIssue);
213
- const previousArrayIssue = issues.find((currentIssue) => currentIssue?.$currentArrayValue?.["$id"] === previousItemId && getIssueLastPathKey(currentIssue) === previousLastPathKey);
214
- if (previousArrayIssue) acc.push({
215
- ...prevIssue,
216
- path: previousArrayIssue?.path ?? []
217
- });
218
- } else if (issues.some((i) => getIssuePath(i) === getIssuePath(prevIssue))) acc.push(prevIssue);
219
- return acc;
220
- }, []);
221
- return [];
222
- }
223
- return issues;
224
- }
225
- function issuesToRegleErrors(result, isValidate = false) {
226
- const output = {};
227
- const mappedIssues = result.issues?.map((issue) => {
228
- const parentArrayPath = getParentArrayPath(issue);
229
- if (parentArrayPath) {
230
- const $currentArrayValue = getDotPath(processedState.value, getIssuePath(parentArrayPath));
231
- Object.defineProperty(issue, "$currentArrayValue", {
232
- value: $currentArrayValue,
233
- enumerable: true,
234
- configurable: true,
235
- writable: true
236
- });
237
- }
238
- return issue;
355
+ let schemaScope;
356
+ let runner;
357
+ function createRunner() {
358
+ return createSchemaValidationRunner({
359
+ processedState,
360
+ getSchema: () => unref(schema),
361
+ isSingleField,
362
+ customErrors,
363
+ previousIssues,
364
+ resolvedOptions,
365
+ syncOnUpdate,
366
+ syncOnValidate
239
367
  });
240
- const filteredIssues = filterIssues(mappedIssues ?? [], isValidate);
241
- if (mappedIssues?.length) {
242
- const issues = filteredIssues.map((issue) => {
243
- let { isArray, $path, lastItemKey } = getPropertiesFromIssue(issue);
244
- return {
245
- ...issue,
246
- $path,
247
- isArray,
248
- $property: lastItemKey,
249
- $rule: "schema",
250
- $message: issue.message
251
- };
252
- });
253
- issues.forEach(({ isArray, $path, ...issue }) => {
254
- setObjectError(output, $path, [issue], isArray);
255
- });
256
- previousIssues.value = issues;
257
- } else previousIssues.value = [];
258
- return output;
259
368
  }
260
- async function computeErrors(isValidate = false) {
261
- let result = computedSchema.value["~standard"].validate(processedState.value);
262
- if (result instanceof Promise) result = await result;
263
- if (isSingleField.value) customErrors.value = filterIssues(result.issues ?? [], isValidate)?.map((issue) => ({
264
- $message: issue.message,
265
- $property: issue.path?.[issue.path.length - 1]?.toString() ?? "-",
266
- $rule: "schema",
267
- ...issue
268
- })) ?? [];
269
- else customErrors.value = issuesToRegleErrors(result, isValidate);
270
- if (!result.issues) {
271
- if (isValidate && syncOnValidate || !isValidate && syncOnUpdate) {
272
- unWatchState?.();
273
- if (isObject(processedState.value)) processedState.value = merge(processedState.value, result.value);
274
- else processedState.value = result.value;
275
- defineWatchState();
276
- }
277
- }
278
- return result;
369
+ function startSchemaRuntime() {
370
+ schemaScope?.stop();
371
+ schemaScope = effectScope();
372
+ schemaScope.run(() => {
373
+ runner = createRunner();
374
+ runner.defineWatchState();
375
+ runner.computeErrors();
376
+ });
279
377
  }
280
- let unWatchState;
281
- function defineWatchState() {
282
- unWatchState = watch([processedState, computedSchema], () => {
283
- if (resolvedOptions.silent) return;
284
- if (computedSchema.value["~standard"].vendor === "regle") return;
285
- computeErrors();
286
- }, { deep: true });
378
+ function stopSchemaRuntime() {
379
+ runner?.stopWatching();
380
+ runner = void 0;
381
+ schemaScope?.stop();
382
+ schemaScope = void 0;
287
383
  }
288
- defineWatchState();
289
- computeErrors();
290
- onValidate = async () => {
384
+ startSchemaRuntime();
385
+ const unwatchDisabled = watch(() => toValue(resolvedOptions.disabled), (disabled) => {
386
+ if (disabled) stopSchemaRuntime();
387
+ else startSchemaRuntime();
388
+ });
389
+ if (toValue(resolvedOptions.disabled)) nextTick().then(() => {
390
+ stopSchemaRuntime();
391
+ });
392
+ let regle;
393
+ const onValidate = async () => {
291
394
  try {
292
- const result = await computeErrors(true);
293
- regle?.regle?.$touch();
395
+ const result = await (runner ?? createRunner()).computeErrors(true);
396
+ regle?.value?.$touch();
294
397
  return {
295
398
  valid: !result.issues?.length,
296
399
  data: processedState.value,
297
- errors: regle?.regle?.$errors,
400
+ errors: regle?.value?.$errors,
298
401
  issues: customErrors.value
299
402
  };
300
403
  } catch (e) {
@@ -302,9 +405,10 @@ function createUseRegleSchemaComposable(params) {
302
405
  }
303
406
  };
304
407
  if (getCurrentScope()) onScopeDispose(() => {
305
- unWatchState();
408
+ unwatchDisabled();
409
+ stopSchemaRuntime();
306
410
  });
307
- const regle = useRootStorage({
411
+ regle = useRootStorage({
308
412
  scopeRules: computed(() => ({})),
309
413
  state: processedState,
310
414
  options: resolvedOptions,
@@ -316,7 +420,7 @@ function createUseRegleSchemaComposable(params) {
316
420
  onValidate,
317
421
  overrides
318
422
  });
319
- return { r$: regle.regle };
423
+ return { r$: toReactive(regle) };
320
424
  }
321
425
  return useRegleSchema;
322
426
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @regle/schemas v1.19.11
2
+ * @regle/schemas v1.19.12
3
3
  * (c) 2026 Victor Garcia
4
4
  * @license MIT
5
5
  */
6
6
 
7
- import{createScopedUseRegle as e,useRootStorage as t}from"@regle/core";import{computed as n,getCurrentScope as r,isRef as i,onScopeDispose as a,ref as o,unref as s,watch as c}from"vue";function l(e){return e?.constructor?.name==`File`}function u(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function d(e,t,n,r){var i,a;if(Array.isArray(t)&&(i=t.slice(0)),typeof t==`string`&&(i=t.split(`.`)),typeof t==`symbol`&&(i=[t]),!Array.isArray(i))throw Error(`props arg must be an array, a string or a symbol`);if(a=i.pop(),!a)return!1;p(a);for(var o;o=i.shift();)if(p(o),isNaN(parseInt(o))?(e[o]===void 0&&(e[o]={}),e=e[o]):(e.$each??=[],h(e.$each[o])&&(e.$each[o]={}),e=e.$each[o]),!e||typeof e!=`object`)return!1;return r?e[a]?e[a].$self=(e[a].$self??=[]).concat(n):e[a]={$self:n}:isNaN(parseInt(a))?Array.isArray(e[a])?e[a]=e[a].concat(n):e[a]=n:(e.$each??=[],e.$each[a]=(e.$each[a]??=[]).concat(n)),!0}function f(e,t,n){if(!e)return n;var r,i;if(Array.isArray(t)&&(r=t.slice(0)),typeof t==`string`&&(r=t.split(`.`)),typeof t==`symbol`&&(r=[t]),!Array.isArray(r))throw Error(`props arg must be an array, a string or a symbol`);for(;r.length;)if(i=r.shift(),!e||!i||(e=e[i],e===void 0))return n;return e}function p(e){if(e==`__proto__`||e==`constructor`||e==`prototype`)throw Error(`setting of prototype values not supported`)}function m(e,...t){for(var n=[].slice.call(arguments),r,i=n.length;r=n[i-1],i--;)if(!r||typeof r!=`object`&&typeof r!=`function`)throw Error(`expected object, got `+r);for(var a=n[0],o=n.slice(1),s=o.length,i=0;i<s;i++){var c=o[i];for(var l in c)a[l]=c[l]}return a}function h(e,t=!0,n=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):l(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:u(e)?e==null?!0:n?Object.keys(e).length===0:!1:!String(e).length}function g(e){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 _(e,t=0){if(t>20)return e;let n=e,r={}.toString.call(e).slice(8,-1);if(r==`Set`&&(n=new Set([...e].map(e=>_(e,t++)))),r==`Map`&&(n=new Map([...e].map(e=>[_(e[0]),_(e[1])]))),r==`Date`&&(n=new Date(e.getTime())),r==`RegExp`&&(n=RegExp(e.source,g(e))),r==`Array`||r==`Object`){n=Array.isArray(e)?[]:{};for(let r in e)n[r]=_(e[r],t++)}return n}function v(e){let{options:l,shortcuts:p,overrides:h}=e??{};function g(e,g,v){let y=n(()=>s(g)),{syncState:b={onUpdate:!1,onValidate:!1},...x}=v??{},{onUpdate:S=!1,onValidate:C=!1}=b,w={...l,...x},T=n(()=>!u(E.value)),E=i(e)?e:o(e),D=o(u(E.value)?{..._(E.value)}:_(E.value)),O=u(E.value)?{..._(E.value)}:_(E.value),k=o({}),A=o([]),j;function M(e){let t=N(e),n=e.path?.[e.path.length-1],r=typeof n==`object`?n.key:n;return{isArray:(typeof n==`object`&&`value`in n?Array.isArray(n.value):!1)||(`type`in e?e.type===`array`:!1)||Array.isArray(f(E.value,t)),$path:t,lastItemKey:r,lastItem:n}}function N(e){return e.path?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``}function P(e){let t=e.path?.at(-1);return typeof t==`object`?t.key:t}function F(e){let t=e.path?.at(-1),n=typeof t==`object`?typeof t.key==`string`:typeof t==`string`,r=e.path?.findLastIndex(e=>typeof e==`object`?typeof e.key==`number`:typeof e==`number`);if(!(!n&&r===-1)&&r!=null){let t=e.path?.slice(0,r+1);return{...e,path:t}}}if(!y.value?.[`~standard`])throw Error(`Only "standard-schema" compatible libraries are supported`);function I(e,t=!1){return!t&&w.rewardEarly?A.value.length?A.value.reduce((t,n)=>{if(`$currentArrayValue`in n&&u(n.$currentArrayValue)&&`$id`in n.$currentArrayValue){let r=n.$currentArrayValue.$id,i=P(n),a=e.find(e=>e?.$currentArrayValue?.$id===r&&P(e)===i);a&&t.push({...n,path:a?.path??[]})}else e.some(e=>N(e)===N(n))&&t.push(n);return t},[]):[]:e}function L(e,t=!1){let n={},r=e.issues?.map(e=>{let t=F(e);if(t){let n=f(E.value,N(t));Object.defineProperty(e,`$currentArrayValue`,{value:n,enumerable:!0,configurable:!0,writable:!0})}return e}),i=I(r??[],t);if(r?.length){let e=i.map(e=>{let{isArray:t,$path:n,lastItemKey:r}=M(e);return{...e,$path:n,isArray:t,$property:r,$rule:`schema`,$message:e.message}});e.forEach(({isArray:e,$path:t,...r})=>{d(n,t,[r],e)}),A.value=e}else A.value=[];return n}async function R(e=!1){let t=y.value[`~standard`].validate(E.value);return t instanceof Promise&&(t=await t),T.value?k.value=I(t.issues??[],e)?.map(e=>({$message:e.message,$property:e.path?.[e.path.length-1]?.toString()??`-`,$rule:`schema`,...e}))??[]:k.value=L(t,e),t.issues||(e&&C||!e&&S)&&(z?.(),u(E.value)?E.value=m(E.value,t.value):E.value=t.value,B()),t}let z;function B(){z=c([E,y],()=>{w.silent||y.value[`~standard`].vendor!==`regle`&&R()},{deep:!0})}B(),R(),j=async()=>{try{let e=await R(!0);return V?.regle?.$touch(),{valid:!e.issues?.length,data:E.value,errors:V?.regle?.$errors,issues:k.value}}catch(e){return Promise.reject(e)}},r()&&a(()=>{z()});let V=t({scopeRules:n(()=>({})),state:E,options:w,schemaErrors:k,initialState:D,originalState:O,shortcuts:p,schemaMode:!0,onValidate:j,overrides:h});return{r$:V.regle}}return g}const y=v();function b(e,t){return e}function x(){function e(e,t){return t}return e}const S=x();function C({modifiers:e,shortcuts:t,overrides:n}){return{useRegleSchema:v({options:e,shortcuts:t,overrides:n}),inferSchema:x()}}const w=t=>{let{customStore:n,customUseRegle:r=y,asRecord:i=!1}=t??{};return e({customStore:n,customUseRegle:r,asRecord:i})},{useCollectScope:T,useScopedRegle:E}=e({customUseRegle:y}),D=T,O=E;export{w as createScopedUseRegleSchema,C as defineRegleSchemaConfig,S as inferSchema,D as useCollectSchemaScope,y as useRegleSchema,O as useScopedRegleSchema,b as withDeps};
7
+ import{createScopedUseRegle as e,useRootStorage as t}from"@regle/core";import{computed as n,effectScope as r,getCurrentScope as i,isRef as a,nextTick as o,onScopeDispose as s,reactive as c,ref as l,toValue as u,unref as d,watch as f}from"vue";function p(e){return e?.constructor?.name==`File`}function m(e){return e&&(e instanceof Date||e.constructor.name==`File`||e.constructor.name==`FileList`)?!1:typeof e==`object`&&!!e&&!Array.isArray(e)}function h(e,t,n,r){var i,a;if(Array.isArray(t)&&(i=t.slice(0)),typeof t==`string`&&(i=t.split(`.`)),typeof t==`symbol`&&(i=[t]),!Array.isArray(i))throw Error(`props arg must be an array, a string or a symbol`);if(a=i.pop(),!a)return!1;_(a);for(var o;o=i.shift();)if(_(o),isNaN(parseInt(o))?(e[o]===void 0&&(e[o]={}),e=e[o]):(e.$each??=[],y(e.$each[o])&&(e.$each[o]={}),e=e.$each[o]),!e||typeof e!=`object`)return!1;return r?e[a]?e[a].$self=(e[a].$self??=[]).concat(n):e[a]={$self:n}:isNaN(parseInt(a))?Array.isArray(e[a])?e[a]=e[a].concat(n):e[a]=n:(e.$each??=[],e.$each[a]=(e.$each[a]??=[]).concat(n)),!0}function g(e,t,n){if(!e)return n;var r,i;if(Array.isArray(t)&&(r=t.slice(0)),typeof t==`string`&&(r=t.split(`.`)),typeof t==`symbol`&&(r=[t]),!Array.isArray(r))throw Error(`props arg must be an array, a string or a symbol`);for(;r.length;)if(i=r.shift(),!e||!i||(e=e[i],e===void 0))return n;return e}function _(e){if(e==`__proto__`||e==`constructor`||e==`prototype`)throw Error(`setting of prototype values not supported`)}function v(e,...t){for(var n=[].slice.call(arguments),r,i=n.length;r=n[i-1],i--;)if(!r||typeof r!=`object`&&typeof r!=`function`)throw Error(`expected object, got `+r);for(var a=n[0],o=n.slice(1),s=o.length,i=0;i<s;i++){var c=o[i];for(var l in c)a[l]=c[l]}return a}function y(e,t=!0,n=!0){return e==null?!0:e instanceof Date?isNaN(e.getTime()):p(e)?e.size<=0:Array.isArray(e)?t?e.length===0:!1:m(e)?e==null?!0:n?Object.keys(e).length===0:!1:!String(e).length}function b(e){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 x(e,t=0){if(t>20)return e;let n=e,r={}.toString.call(e).slice(8,-1);if(r==`Set`&&(n=new Set([...e].map(e=>x(e,t++)))),r==`Map`&&(n=new Map([...e].map(e=>[x(e[0]),x(e[1])]))),r==`Date`&&(n=new Date(e.getTime())),r==`RegExp`&&(n=RegExp(e.source,b(e))),r==`Array`||r==`Object`){n=Array.isArray(e)?[]:{};for(let r in e)n[r]=x(e[r],t++)}return n}function S(e){return a(e)?c(new Proxy({},{get(t,n,r){if(e.value!==void 0)return d(Reflect.get(e.value,n,r))},set(t,n,r){return a(e.value[n])&&!a(r)?e.value[n].value=r:e.value[n]=r,!0},deleteProperty(t,n){return Reflect.deleteProperty(e.value,n)},has(t,n){return e.value===void 0?!1:Reflect.has(e.value,n)},ownKeys(){return e.value===void 0?[]:Object.keys(e.value)},getOwnPropertyDescriptor(){return{enumerable:!0,configurable:!0}}})):c(e)}function C(e){return e.path?.map(e=>typeof e==`object`?e.key:e.toString()).join(`.`)??``}function w(e){let t=e.path?.at(-1);return typeof t==`object`?t.key:t}function T(e){let t=e.path?.at(-1),n=typeof t==`object`?typeof t.key==`string`:typeof t==`string`,r=e.path?.findLastIndex(e=>typeof e==`object`?typeof e.key==`number`:typeof e==`number`);if(!(!n&&r===-1)&&r!=null){let t=e.path?.slice(0,r+1);return{...e,path:t}}}function E(e,t){let n=C(e),r=e.path?.[e.path.length-1],i=typeof r==`object`?r.key:r;return{isArray:(typeof r==`object`&&`value`in r?Array.isArray(r.value):!1)||(`type`in e?e.type===`array`:!1)||Array.isArray(g(t.value,n)),$path:n,lastItemKey:i,lastItem:r}}function D(e,t,n,r=!1){return!r&&n?t.value.length?t.value.reduce((t,n)=>{if(`$currentArrayValue`in n&&m(n.$currentArrayValue)&&`$id`in n.$currentArrayValue){let r=n.$currentArrayValue.$id,i=w(n),a=e.find(e=>e?.$currentArrayValue?.$id===r&&w(e)===i);a&&t.push({...n,path:a?.path??[]})}else e.some(e=>C(e)===C(n))&&t.push(n);return t},[]):[]:e}function O(e,t,n,r=!1){return D(e,t,n,r)?.map(e=>({$message:e.message,$property:e.path?.[e.path.length-1]?.toString()??`-`,$rule:`schema`,...e}))??[]}function k({result:e,previousIssues:t,processedState:n,rewardEarly:r,isValidate:i=!1}){let a={},o=e.issues?.map(e=>{let t=T(e);if(t){let r=g(n.value,C(t));Object.defineProperty(e,`$currentArrayValue`,{value:r,enumerable:!0,configurable:!0,writable:!0})}return e}),s=D(o??[],t,r,i);if(o?.length){let e=s.map(e=>{let{isArray:t,$path:r,lastItemKey:i}=E(e,n);return{...e,$path:r,isArray:t,$property:i,$rule:`schema`,$message:e.message}});e.forEach(({isArray:e,$path:t,...n})=>{h(a,t,[n],e)}),t.value=e}else t.value=[];return a}function A(e){let t=a(e)?e:l(e);return{processedState:t,isSingleField:n(()=>!m(t.value)),initialState:l(m(t.value)?{...x(t.value)}:x(t.value)),originalState:m(t.value)?{...x(t.value)}:x(t.value)}}function j({processedState:e,getSchema:t,isSingleField:n,customErrors:r,previousIssues:i,resolvedOptions:a,syncOnUpdate:o,syncOnValidate:s}){let c;function l(){d(),c=f([e,t],()=>{a.silent||t()[`~standard`].vendor!==`regle`&&p()},{deep:!0})}function d(){c?.(),c=void 0}async function p(c=!1){let f=t()[`~standard`].validate(e.value);return f instanceof Promise&&(f=await f),n.value?r.value=O(f.issues??[],i,u(a.rewardEarly),c):r.value=k({result:f,previousIssues:i,processedState:e,rewardEarly:u(a.rewardEarly),isValidate:c}),f.issues||(c&&s||!c&&o)&&(d(),m(e.value)?e.value=v(e.value,f.value):e.value=f.value,l()),f}return{computeErrors:p,defineWatchState:l,stopWatching:d}}function M(e){let{options:a,shortcuts:c,overrides:p}=e??{};function m(e,m,h){if(!d(m)?.[`~standard`])throw Error(`Only "standard-schema" compatible libraries are supported`);let{syncState:g={onUpdate:!1,onValidate:!1},..._}=h??{},{onUpdate:v=!1,onValidate:y=!1}=g,b={...a,..._},{processedState:x,isSingleField:C,initialState:w,originalState:T}=A(e),E=l({}),D=l([]),O,k;function M(){return j({processedState:x,getSchema:()=>d(m),isSingleField:C,customErrors:E,previousIssues:D,resolvedOptions:b,syncOnUpdate:v,syncOnValidate:y})}function N(){O?.stop(),O=r(),O.run(()=>{k=M(),k.defineWatchState(),k.computeErrors()})}function P(){k?.stopWatching(),k=void 0,O?.stop(),O=void 0}N();let F=f(()=>u(b.disabled),e=>{e?P():N()});u(b.disabled)&&o().then(()=>{P()});let I;return i()&&s(()=>{F(),P()}),I=t({scopeRules:n(()=>({})),state:x,options:b,schemaErrors:E,initialState:w,originalState:T,shortcuts:c,schemaMode:!0,onValidate:async()=>{try{let e=await(k??M()).computeErrors(!0);return I?.value?.$touch(),{valid:!e.issues?.length,data:x.value,errors:I?.value?.$errors,issues:E.value}}catch(e){return Promise.reject(e)}},overrides:p}),{r$:S(I)}}return m}const N=M();function P(e,t){return e}function F(){function e(e,t){return t}return e}const I=F();function L({modifiers:e,shortcuts:t,overrides:n}){return{useRegleSchema:M({options:e,shortcuts:t,overrides:n}),inferSchema:F()}}const R=t=>{let{customStore:n,customUseRegle:r=N,asRecord:i=!1}=t??{};return e({customStore:n,customUseRegle:r,asRecord:i})},{useCollectScope:z,useScopedRegle:B}=e({customUseRegle:N}),V=z,H=B;export{R as createScopedUseRegleSchema,L as defineRegleSchemaConfig,I as inferSchema,V as useCollectSchemaScope,N as useRegleSchema,H as useScopedRegleSchema,P as withDeps};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@regle/schemas",
3
- "version": "1.19.11",
3
+ "version": "1.19.12",
4
4
  "description": "Schemas adapter for Regle",
5
5
  "homepage": "https://reglejs.dev/",
6
6
  "license": "MIT",
@@ -37,8 +37,8 @@
37
37
  "dependencies": {
38
38
  "@standard-schema/spec": "1.1.0",
39
39
  "type-fest": "5.4.4",
40
- "@regle/core": "1.19.11",
41
- "@regle/rules": "1.19.11"
40
+ "@regle/core": "1.19.12",
41
+ "@regle/rules": "1.19.12"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@total-typescript/ts-reset": "0.6.1",