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