@sanity/validation 6.21.0-next.98 → 6.21.1-next.2
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/lib/_internal.d.ts +1 -1
- package/lib/_internal.js +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/{validateDocument-xOvAoTbA.d.ts → validateDocument-Bqq2KtlY.d.ts} +25 -19
- package/lib/{validateDocument-bmF6wprp.js → validateDocument-lfNM88pm.js} +238 -114
- package/lib/validateDocument-lfNM88pm.js.map +1 -0
- package/package.json +4 -4
- package/lib/validateDocument-bmF6wprp.js.map +0 -1
package/lib/_internal.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { E as LocaleSource, S as ValidationMarkerCode, T as ValidationScheduling, d as evaluateDocumentInternal, f as evaluateDocumentObservable, g as validateDocumentObservable, h as validateDocumentInternal, n as ValidateDocumentInternalOptions, p as resolveTypeForArrayItem, r as ValidateDocumentObservableOptions, s as ValidateItemOptions, w as ValidationContext, y as validateItem } from "./validateDocument-Bqq2KtlY.js";
|
|
2
2
|
import { Path, Rule as Rule$1, RuleClass, Schema, SchemaType, SchemaValidationValue, ValidationContext as ValidationContext$1, ValidationError, ValidationMarker } from "@sanity/types";
|
|
3
3
|
export declare function getFallbackLocaleSource(): LocaleSource;
|
|
4
4
|
export declare const validationLocaleStrings: {
|
package/lib/_internal.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as getFallbackLocaleSource, a as validateDocumentInternal, d as normalizeValidationRules, f as Rule, g as pathToString, h as convertToValidationMarker, l as validateItem, m as typeString, n as evaluateDocumentObservable, o as validateDocumentObservable, p as getTypeChain, r as resolveTypeForArrayItem, t as evaluateDocumentInternal, u as compileValidationRules, v as validationLocaleStrings } from "./validateDocument-
|
|
1
|
+
import { _ as getFallbackLocaleSource, a as validateDocumentInternal, d as normalizeValidationRules, f as Rule, g as pathToString, h as convertToValidationMarker, l as validateItem, m as typeString, n as evaluateDocumentObservable, o as validateDocumentObservable, p as getTypeChain, r as resolveTypeForArrayItem, t as evaluateDocumentInternal, u as compileValidationRules, v as validationLocaleStrings } from "./validateDocument-lfNM88pm.js";
|
|
2
2
|
function inferFromSchemaType(typeDef) {
|
|
3
3
|
return traverse(typeDef, /* @__PURE__ */ new Set()), typeDef;
|
|
4
4
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { C as validationMarkerCodes, S as ValidationMarkerCode, _ as validateDocumentWithWorkspace, a as ValidateDocumentWorkspaceOptions, b as BuiltInValidationMarkerCode, c as ValidationClient, i as ValidateDocumentOptions, l as ValidationSchema, m as validateDocument, o as ValidateDocumentsOptions, t as DocumentValidationResult, u as ValidationSource, v as validateDocuments, x as DocumentValidationMarker } from "./validateDocument-Bqq2KtlY.js";
|
|
2
2
|
export { type BuiltInValidationMarkerCode, type DocumentValidationMarker, type DocumentValidationResult, type ValidateDocumentOptions, type ValidateDocumentWorkspaceOptions, type ValidateDocumentsOptions, type ValidationClient, type ValidationMarkerCode, type ValidationSchema, type ValidationSource, validateDocument, validateDocumentWithWorkspace, validateDocuments, validationMarkerCodes };
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { i as validateDocument, s as validateDocumentWithWorkspace, c as validateDocuments, y as validationMarkerCodes } from "./validateDocument-
|
|
1
|
+
export { i as validateDocument, s as validateDocumentWithWorkspace, c as validateDocuments, y as validationMarkerCodes } from "./validateDocument-lfNM88pm.js";
|
|
@@ -10,6 +10,26 @@ interface LocaleSource {
|
|
|
10
10
|
loadNamespaces(namespaces: string[]): Promise<void>;
|
|
11
11
|
t: TFunction;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* How validation work is paced.
|
|
15
|
+
*
|
|
16
|
+
* - `idle` (default): every rule and nested value waits for a `requestIdleCallback` before it
|
|
17
|
+
* runs, so validation yields to rendering and input while a document is being edited. A page
|
|
18
|
+
* that is busy or hidden grants few or no idle periods, so a run can take long to finish.
|
|
19
|
+
* - `immediate`: nothing waits for idle time. The run uses the thread as soon as it can, and hands
|
|
20
|
+
* it back once every {@link IMMEDIATE_YIELD_EVERY} nodes so that input and rendering get a turn
|
|
21
|
+
* in between: through `scheduler.yield()` where the browser has it, and elsewhere (Safari) by
|
|
22
|
+
* waiting for the next task. Meant for the moment a user is waiting on the result, such as
|
|
23
|
+
* publishing.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
type ValidationScheduling = 'idle' | 'immediate';
|
|
28
|
+
/** What every validation node waits for before it runs. @internal */
|
|
29
|
+
interface Pace {
|
|
30
|
+
wait(signal?: AbortSignal): Promise<void> | undefined;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
}
|
|
13
33
|
declare module '@sanity/types' {
|
|
14
34
|
/**
|
|
15
35
|
* Extended validation context that includes internationalization
|
|
@@ -44,7 +64,7 @@ interface InternalValidationContext extends ValidationContext {
|
|
|
44
64
|
};
|
|
45
65
|
markIncomplete?: () => void;
|
|
46
66
|
/** The run's pacer: what every node of the run waits for before it runs. */
|
|
47
|
-
pace?:
|
|
67
|
+
pace?: Pace;
|
|
48
68
|
};
|
|
49
69
|
}
|
|
50
70
|
/** Machine-readable codes emitted by built-in document validation. @beta */
|
|
@@ -107,21 +127,6 @@ type ValidationMarkerCode = BuiltInValidationMarkerCode | (string & {});
|
|
|
107
127
|
type DocumentValidationMarker = Omit<ValidationMarker, 'code'> & {
|
|
108
128
|
code: ValidationMarkerCode;
|
|
109
129
|
};
|
|
110
|
-
/**
|
|
111
|
-
* How validation work is paced.
|
|
112
|
-
*
|
|
113
|
-
* - `idle` (default): every rule and nested value waits for a `requestIdleCallback` before it
|
|
114
|
-
* runs, so validation yields to rendering and input while a document is being edited. A page
|
|
115
|
-
* that is busy or hidden grants few or no idle periods, so a run can take long to finish.
|
|
116
|
-
* - `immediate`: nothing waits for idle time. The run uses the thread as soon as it can, and hands
|
|
117
|
-
* it back once every {@link IMMEDIATE_YIELD_EVERY} nodes so that input and rendering get a turn
|
|
118
|
-
* in between: through `scheduler.yield()` where the browser has it, and elsewhere (Safari) by
|
|
119
|
-
* waiting for the next task. Meant for the moment a user is waiting on the result, such as
|
|
120
|
-
* publishing.
|
|
121
|
-
*
|
|
122
|
-
* @internal
|
|
123
|
-
*/
|
|
124
|
-
type ValidationScheduling = 'idle' | 'immediate';
|
|
125
130
|
/**
|
|
126
131
|
* @internal
|
|
127
132
|
*/
|
|
@@ -218,7 +223,8 @@ interface ValidationSchema {
|
|
|
218
223
|
interface ValidationClient {
|
|
219
224
|
fetch: SanityClient['fetch'];
|
|
220
225
|
getDataUrl: SanityClient['getDataUrl'];
|
|
221
|
-
|
|
226
|
+
request: SanityClient['request'];
|
|
227
|
+
observable: Pick<SanityClient['observable'], 'fetch'>;
|
|
222
228
|
withConfig(config: Parameters<SanityClient['withConfig']>[0]): ValidationClient;
|
|
223
229
|
}
|
|
224
230
|
/** The result of validating a complete document. @beta */
|
|
@@ -363,5 +369,5 @@ type ValidateItemOptions = {
|
|
|
363
369
|
__internal?: InternalValidationContext['__internal'];
|
|
364
370
|
} & Pick<ValidationContext$1, 'currentUser'> & ExplicitUndefined<Omit<ValidationContext$1, 'hidden' | 'signal' | 'currentUser'>>;
|
|
365
371
|
declare function validateItem(opts: ValidateItemOptions): Promise<ValidationMarker[]>;
|
|
366
|
-
export {
|
|
367
|
-
//# sourceMappingURL=validateDocument-
|
|
372
|
+
export { validationMarkerCodes as C, LocaleSource as E, ValidationMarkerCode as S, ValidationScheduling as T, validateDocumentWithWorkspace as _, ValidateDocumentWorkspaceOptions as a, BuiltInValidationMarkerCode as b, ValidationClient as c, evaluateDocumentInternal as d, evaluateDocumentObservable as f, validateDocumentObservable as g, validateDocumentInternal as h, ValidateDocumentOptions as i, ValidationSchema as l, validateDocument as m, ValidateDocumentInternalOptions as n, ValidateDocumentsOptions as o, resolveTypeForArrayItem as p, ValidateDocumentObservableOptions as r, ValidateItemOptions as s, DocumentValidationResult as t, ValidationSource as u, validateDocuments as v, ValidationContext$1 as w, DocumentValidationMarker as x, validateItem as y };
|
|
373
|
+
//# sourceMappingURL=validateDocument-Bqq2KtlY.d.ts.map
|
|
@@ -4,8 +4,8 @@ import { createClientConcurrencyLimiter } from "@sanity/util/client";
|
|
|
4
4
|
import { ConcurrencyLimiter } from "@sanity/util/concurrency-limiter";
|
|
5
5
|
import { dequal } from "dequal/lite";
|
|
6
6
|
import flatten from "lodash-es/flatten.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { Observable } from "rxjs";
|
|
8
|
+
import { map } from "rxjs/operators";
|
|
9
9
|
import { createInstance } from "i18next";
|
|
10
10
|
import { Rule as Rule$1 } from "@sanity/schema";
|
|
11
11
|
import get from "lodash-es/get.js";
|
|
@@ -61,16 +61,6 @@ const validationMarkerCodes = {
|
|
|
61
61
|
valueRequired: "value.required",
|
|
62
62
|
valueTypeMismatch: "value.type-mismatch"
|
|
63
63
|
};
|
|
64
|
-
function abortSignalAsObservable(signal) {
|
|
65
|
-
return new Observable((subscriber) => {
|
|
66
|
-
let onAbort = () => subscriber.error(signal.reason);
|
|
67
|
-
if (signal.aborted) onAbort();
|
|
68
|
-
else return signal.addEventListener("abort", onAbort, { once: !0 }), () => signal.removeEventListener("abort", onAbort);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
function cancelWith(signal) {
|
|
72
|
-
return (source) => signal ? source.pipe(takeUntil(abortSignalAsObservable(signal))) : source;
|
|
73
|
-
}
|
|
74
64
|
var ClientUnavailableError = class extends Error {
|
|
75
65
|
name = "ClientUnavailableError";
|
|
76
66
|
constructor() {
|
|
@@ -151,27 +141,62 @@ function resolveConditionalProperty(property, context) {
|
|
|
151
141
|
path
|
|
152
142
|
}) === !0;
|
|
153
143
|
}
|
|
144
|
+
function settleCheck(check, result) {
|
|
145
|
+
check.settled || (check.settled = !0, check.signal && check.onAbort && check.signal.removeEventListener("abort", check.onAbort), "error" in result ? check.reject(result.error) : check.resolve(result.exists));
|
|
146
|
+
}
|
|
154
147
|
function createBatchedGetDocumentExists(client, defaultSignal) {
|
|
155
|
-
let
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
148
|
+
let buffer = /* @__PURE__ */ new Map(), queuedBatches = [], bufferTimer, draining = !1, rejectBatch = (batch, reason) => {
|
|
149
|
+
for (let checks of batch.values()) for (let check of checks) settleCheck(check, { error: reason });
|
|
150
|
+
}, rejectPending = (reason) => {
|
|
151
|
+
bufferTimer !== void 0 && clearTimeout(bufferTimer), bufferTimer = void 0, rejectBatch(buffer, reason), buffer = /* @__PURE__ */ new Map();
|
|
152
|
+
for (let batch of queuedBatches.splice(0)) rejectBatch(batch, reason);
|
|
153
|
+
}, flush = () => {
|
|
154
|
+
bufferTimer !== void 0 && clearTimeout(bufferTimer), bufferTimer = void 0, buffer.size !== 0 && (queuedBatches.push(buffer), buffer = /* @__PURE__ */ new Map(), drain());
|
|
155
|
+
}, scheduleFlush = () => {
|
|
156
|
+
buffer.size >= 100 ? flush() : bufferTimer === void 0 && (bufferTimer = setTimeout(flush, 250));
|
|
157
|
+
};
|
|
158
|
+
async function drain() {
|
|
159
|
+
if (!draining) {
|
|
160
|
+
draining = !0;
|
|
161
|
+
try {
|
|
162
|
+
for (; queuedBatches.length > 0;) {
|
|
163
|
+
defaultSignal?.throwIfAborted();
|
|
164
|
+
let batch = queuedBatches.shift();
|
|
165
|
+
if (!batch) continue;
|
|
166
|
+
let ids = [...batch.entries()].filter(([, checks]) => checks.some((check) => !check.settled)).map(([id]) => id);
|
|
167
|
+
if (ids.length !== 0) try {
|
|
168
|
+
let availability = await client.request({
|
|
169
|
+
url: client.getDataUrl("doc", ids.join(",")),
|
|
170
|
+
query: { excludeContent: "true" },
|
|
171
|
+
signal: defaultSignal,
|
|
172
|
+
tag: "documents-availability"
|
|
173
|
+
}), missingIds = new Set(availability.omitted.filter(({ reason }) => reason === "existence").map(({ id }) => id));
|
|
174
|
+
for (let id of ids) for (let check of batch.get(id) ?? []) settleCheck(check, { exists: !missingIds.has(id) });
|
|
175
|
+
} catch (error) {
|
|
176
|
+
rejectBatch(batch, error), rejectPending(error);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
rejectPending(error);
|
|
182
|
+
} finally {
|
|
183
|
+
draining = !1;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return function getDocumentExists(options) {
|
|
171
188
|
let signal = options.signal || defaultSignal;
|
|
172
|
-
signal?.
|
|
173
|
-
|
|
174
|
-
|
|
189
|
+
return signal?.aborted ? Promise.reject(signal.reason) : new Promise((resolve, reject) => {
|
|
190
|
+
let check = {
|
|
191
|
+
reject,
|
|
192
|
+
resolve,
|
|
193
|
+
settled: !1,
|
|
194
|
+
signal
|
|
195
|
+
};
|
|
196
|
+
signal && (check.onAbort = () => settleCheck(check, { error: signal.reason }), signal.addEventListener("abort", check.onAbort, { once: !0 }));
|
|
197
|
+
let checks = buffer.get(options.id);
|
|
198
|
+
checks ? checks.push(check) : buffer.set(options.id, [check]), scheduleFlush();
|
|
199
|
+
});
|
|
175
200
|
};
|
|
176
201
|
}
|
|
177
202
|
function pathToString(path = []) {
|
|
@@ -936,15 +961,7 @@ const requestIdleCallbackShim = function requestIdleCallbackShim(callback, _opti
|
|
|
936
961
|
}, 0);
|
|
937
962
|
}, cancelIdleCallbackShim = function cancelIdleCallbackShim(handle) {
|
|
938
963
|
return globalThis.clearTimeout(handle);
|
|
939
|
-
}, win = typeof window > "u" ? void 0 : window, requestIdleCallback = win?.requestIdleCallback || requestIdleCallbackShim, cancelIdleCallback = win?.cancelIdleCallback || cancelIdleCallbackShim, microtask =
|
|
940
|
-
/**
|
|
941
|
-
* How an `immediate` run hands the thread back. `scheduler.yield()` resumes the run ahead of the
|
|
942
|
-
* tasks that were already waiting. Where the browser has none (Safari), the run resumes in a task
|
|
943
|
-
* of its own, behind them, which it gets the way React's scheduler does: from `setImmediate` where
|
|
944
|
-
* there is one (Node), because a `MessageChannel` would keep a Node process alive; otherwise from
|
|
945
|
-
* a `MessageChannel`, because chained timeouts are clamped to 4 ms and throttled in background
|
|
946
|
-
* tabs; and from `setTimeout` as a last resort.
|
|
947
|
-
*/
|
|
964
|
+
}, win = typeof window > "u" ? void 0 : window, requestIdleCallback = win?.requestIdleCallback || requestIdleCallbackShim, cancelIdleCallback = win?.cancelIdleCallback || cancelIdleCallbackShim, microtask = Promise.resolve();
|
|
948
965
|
function getYieldToBrowser() {
|
|
949
966
|
let { scheduler, setImmediate } = globalThis;
|
|
950
967
|
return typeof scheduler?.yield == "function" ? scheduler.yield.bind(scheduler) : typeof setImmediate == "function" ? () => new Promise((resolve) => setImmediate(resolve)) : typeof MessageChannel == "function" ? yieldThroughMessageChannel : yieldThroughTimeout;
|
|
@@ -964,42 +981,83 @@ function yieldThroughMessageChannel() {
|
|
|
964
981
|
function yieldThroughTimeout() {
|
|
965
982
|
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
966
983
|
}
|
|
967
|
-
function
|
|
968
|
-
return new
|
|
969
|
-
|
|
970
|
-
|
|
984
|
+
function abortReason(signal) {
|
|
985
|
+
return signal.reason ?? new DOMException("Validation aborted", "AbortError");
|
|
986
|
+
}
|
|
987
|
+
function cancelWithSignal(wait, signal) {
|
|
988
|
+
return signal ? signal.aborted ? Promise.reject(abortReason(signal)) : new Promise((resolve, reject) => {
|
|
989
|
+
let onAbort = () => {
|
|
990
|
+
signal.removeEventListener("abort", onAbort), reject(abortReason(signal));
|
|
991
|
+
};
|
|
992
|
+
signal.addEventListener("abort", onAbort, { once: !0 }), wait.then(() => {
|
|
993
|
+
signal.removeEventListener("abort", onAbort), resolve();
|
|
994
|
+
}, (error) => {
|
|
995
|
+
signal.removeEventListener("abort", onAbort), reject(error);
|
|
971
996
|
});
|
|
972
|
-
|
|
997
|
+
}) : wait;
|
|
998
|
+
}
|
|
999
|
+
function waitForIdle(signal, waiters) {
|
|
1000
|
+
return new Promise((resolve, reject) => {
|
|
1001
|
+
let settled = !1, handle, finish = (reason) => {
|
|
1002
|
+
settled || (settled = !0, handle !== void 0 && cancelIdleCallback(handle), waiters?.delete(finish), signal?.removeEventListener("abort", onAbort), reason === void 0 ? resolve() : reject(reason));
|
|
1003
|
+
}, onAbort = () => {
|
|
1004
|
+
signal && finish(abortReason(signal));
|
|
1005
|
+
};
|
|
1006
|
+
signal?.aborted ? onAbort() : (waiters?.add(finish), signal?.addEventListener("abort", onAbort, { once: !0 }), handle = requestIdleCallback(() => finish()));
|
|
973
1007
|
});
|
|
974
1008
|
}
|
|
975
1009
|
/**
|
|
976
1010
|
* Yields to the browser once every `every` nodes and lets the others through on a microtask.
|
|
977
|
-
* Every node that comes along while the yield is pending waits for that same yield
|
|
978
|
-
* requesting one of its own, which would let the run carry on around the waiting nodes), and the
|
|
979
|
-
* count starts over when it resolves.
|
|
1011
|
+
* Every node that comes along while the yield is pending waits for that same yield.
|
|
980
1012
|
*/
|
|
981
1013
|
function createYieldingPace(yieldToBrowser, every) {
|
|
982
1014
|
let count = 0, pending;
|
|
983
|
-
return
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1015
|
+
return {
|
|
1016
|
+
wait(signal) {
|
|
1017
|
+
if (signal?.throwIfAborted(), pending) return cancelWithSignal(pending, signal);
|
|
1018
|
+
if (count += 1, count < every) return cancelWithSignal(microtask, signal);
|
|
1019
|
+
count = 0;
|
|
1020
|
+
let settle = () => {
|
|
1021
|
+
pending === yielded && (pending = void 0);
|
|
1022
|
+
}, yielded = yieldToBrowser().then(settle, settle);
|
|
1023
|
+
return pending = yielded, cancelWithSignal(yielded, signal);
|
|
1024
|
+
},
|
|
1025
|
+
dispose() {}
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
/** A pacer for server validation, where yielding to a browser is unnecessary. */
|
|
1029
|
+
function createUnpacedPace() {
|
|
1030
|
+
return {
|
|
1031
|
+
wait(signal) {
|
|
1032
|
+
signal?.throwIfAborted();
|
|
1033
|
+
},
|
|
1034
|
+
dispose() {}
|
|
991
1035
|
};
|
|
992
1036
|
}
|
|
993
1037
|
/**
|
|
994
|
-
* The pacer for one run, `idle` without a scheduling.
|
|
995
|
-
*
|
|
996
|
-
* `immediate` lets the nodes that are waiting for idle time go ahead, so the run picks up speed
|
|
997
|
-
* where it is instead of starting over.
|
|
1038
|
+
* The pacer for one Studio run, `idle` without a scheduling. One subscription observes scheduling
|
|
1039
|
+
* changes for the whole run rather than creating one RxJS subscription per validation node.
|
|
998
1040
|
*/
|
|
999
1041
|
function createPace(scheduling) {
|
|
1000
|
-
if (!scheduling) return
|
|
1001
|
-
|
|
1002
|
-
|
|
1042
|
+
if (!scheduling) return {
|
|
1043
|
+
wait: (signal) => waitForIdle(signal),
|
|
1044
|
+
dispose() {}
|
|
1045
|
+
};
|
|
1046
|
+
let current = scheduling.getValue(), disposed = !1, waiters = /* @__PURE__ */ new Set(), immediate = createYieldingPace(getYieldToBrowser(), 100), subscription = scheduling.subscribe((next) => {
|
|
1047
|
+
if (current = next, next === "immediate") for (let release of waiters) release();
|
|
1048
|
+
});
|
|
1049
|
+
return {
|
|
1050
|
+
wait(signal) {
|
|
1051
|
+
return disposed ? Promise.reject(/* @__PURE__ */ Error("Validation pace has been disposed")) : current === "immediate" ? immediate.wait(signal) : waitForIdle(signal, waiters);
|
|
1052
|
+
},
|
|
1053
|
+
dispose() {
|
|
1054
|
+
if (!disposed) {
|
|
1055
|
+
disposed = !0, subscription.unsubscribe();
|
|
1056
|
+
for (let release of waiters) release(/* @__PURE__ */ Error("Validation pace has been disposed"));
|
|
1057
|
+
immediate.dispose();
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1003
1061
|
}
|
|
1004
1062
|
/**
|
|
1005
1063
|
* Given a schema type, returns a custom validator used to warn users of unknown
|
|
@@ -1018,6 +1076,19 @@ const unknownFieldsValidator = (type) => (value) => {
|
|
|
1018
1076
|
path: [unknownField]
|
|
1019
1077
|
}));
|
|
1020
1078
|
}, DEFAULT_VALIDATION_CLIENT_OPTIONS = { apiVersion: "2025-02-19" }, isRecord = (maybeRecord) => typeof maybeRecord == "object" && !!maybeRecord && !Array.isArray(maybeRecord);
|
|
1079
|
+
/** Rejects on abort without waiting for work that does not observe the signal itself. */
|
|
1080
|
+
function raceWithSignal(promise, signal) {
|
|
1081
|
+
return signal ? signal.aborted ? Promise.reject(signal.reason) : new Promise((resolve, reject) => {
|
|
1082
|
+
let onAbort = () => {
|
|
1083
|
+
signal.removeEventListener("abort", onAbort), reject(signal.reason);
|
|
1084
|
+
};
|
|
1085
|
+
signal.addEventListener("abort", onAbort, { once: !0 }), promise.then((value) => {
|
|
1086
|
+
signal.removeEventListener("abort", onAbort), resolve(value);
|
|
1087
|
+
}, (error) => {
|
|
1088
|
+
signal.removeEventListener("abort", onAbort), reject(error);
|
|
1089
|
+
});
|
|
1090
|
+
}) : promise;
|
|
1091
|
+
}
|
|
1021
1092
|
function throwClientUnavailable() {
|
|
1022
1093
|
throw new ClientUnavailableError();
|
|
1023
1094
|
}
|
|
@@ -1140,7 +1211,7 @@ function evaluateDocumentsInternal({ documents, schema, getClient, getDocumentEx
|
|
|
1140
1211
|
if (signal?.aborted) return Promise.reject(signal.reason);
|
|
1141
1212
|
if (documents.length === 0) return Promise.resolve([]);
|
|
1142
1213
|
let limitConcurrency = createClientConcurrencyLimiter(maxFetchConcurrency ?? 25, signal), getConcurrencyLimitedClient = (clientOptions) => limitConcurrency(getClient(clientOptions)), sharedGetDocumentExists = getDocumentExists || createBatchedGetDocumentExists(getClient(DEFAULT_VALIDATION_CLIENT_OPTIONS), signal);
|
|
1143
|
-
return Promise.all(documents.map((document) =>
|
|
1214
|
+
return Promise.all(documents.map((document) => evaluateDocument({
|
|
1144
1215
|
document,
|
|
1145
1216
|
getClient: getConcurrencyLimitedClient,
|
|
1146
1217
|
i18n,
|
|
@@ -1150,8 +1221,9 @@ function evaluateDocumentsInternal({ documents, schema, getClient, getDocumentEx
|
|
|
1150
1221
|
maxCustomValidationConcurrency,
|
|
1151
1222
|
currentUser,
|
|
1152
1223
|
customValidation,
|
|
1153
|
-
signal
|
|
1154
|
-
|
|
1224
|
+
signal,
|
|
1225
|
+
pace: createUnpacedPace()
|
|
1226
|
+
})));
|
|
1155
1227
|
}
|
|
1156
1228
|
const customValidationConcurrencyLimiters = /* @__PURE__ */ new WeakMap();
|
|
1157
1229
|
/**
|
|
@@ -1159,28 +1231,47 @@ const customValidationConcurrencyLimiters = /* @__PURE__ */ new WeakMap();
|
|
|
1159
1231
|
* @internal
|
|
1160
1232
|
*/
|
|
1161
1233
|
function validateDocumentObservable(options) {
|
|
1162
|
-
return evaluateDocumentObservable(options).pipe(map
|
|
1234
|
+
return evaluateDocumentObservable(options).pipe(map(({ markers }) => markers));
|
|
1163
1235
|
}
|
|
1164
1236
|
/**
|
|
1165
1237
|
* Validates a document against the given schema, including completion status.
|
|
1166
1238
|
* @internal
|
|
1167
1239
|
*/
|
|
1168
1240
|
function evaluateDocumentObservable(options) {
|
|
1169
|
-
|
|
1170
|
-
|
|
1241
|
+
return new Observable((subscriber) => {
|
|
1242
|
+
let controller = new AbortController(), { signal } = options, abort = () => controller.abort(signal?.reason);
|
|
1243
|
+
return signal?.aborted ? abort() : signal?.addEventListener("abort", abort, { once: !0 }), evaluateDocument({
|
|
1244
|
+
...options,
|
|
1245
|
+
signal: controller.signal,
|
|
1246
|
+
pace: createPace(options.scheduling)
|
|
1247
|
+
}).then((result) => {
|
|
1248
|
+
subscriber.next(result), subscriber.complete();
|
|
1249
|
+
}, (error) => subscriber.error(error)), () => {
|
|
1250
|
+
signal?.removeEventListener("abort", abort), controller.abort();
|
|
1251
|
+
};
|
|
1252
|
+
});
|
|
1171
1253
|
}
|
|
1172
|
-
function
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1254
|
+
async function evaluateDocument(options) {
|
|
1255
|
+
let { pace, signal } = options;
|
|
1256
|
+
try {
|
|
1257
|
+
return signal?.throwIfAborted(), await raceWithSignal(evaluateDocumentWithoutCancellation(options), signal);
|
|
1258
|
+
} finally {
|
|
1259
|
+
pace.dispose();
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
async function evaluateDocumentWithoutCancellation({ document, getClient, i18n = getFallbackLocaleSource(), schema, getDocumentExists, environment, maxCustomValidationConcurrency, currentUser, customValidation = !0, signal, pace }) {
|
|
1263
|
+
try {
|
|
1264
|
+
if (typeof document?._type != "string") throw Error("Tried to validate a value without a '_type'");
|
|
1265
|
+
let documentType = schema.get(document._type);
|
|
1266
|
+
if (!documentType) return environment === "studio" ? (console.warn("Schema type for object type \"%s\" not found, skipping validation", document._type), createDocumentValidationResult([], !1)) : createDocumentValidationResult([{
|
|
1267
|
+
code: validationMarkerCodes.documentUnknownType,
|
|
1268
|
+
details: { documentType: document._type },
|
|
1269
|
+
level: "warning",
|
|
1270
|
+
message: `Could not find schema type for type '${document._type}', skipping validation`,
|
|
1271
|
+
path: []
|
|
1272
|
+
}], !1);
|
|
1273
|
+
let customValidationConcurrencyLimiter = customValidationConcurrencyLimiters.get(schema);
|
|
1274
|
+
customValidationConcurrencyLimiter || (customValidationConcurrencyLimiter = new ConcurrencyLimiter(maxCustomValidationConcurrency ?? 5), customValidationConcurrencyLimiters.set(schema, customValidationConcurrencyLimiter));
|
|
1184
1275
|
let complete = !0, validationOptions = {
|
|
1185
1276
|
getClient,
|
|
1186
1277
|
schema,
|
|
@@ -1196,39 +1287,66 @@ function evaluateDocumentObservableWithoutCancellation({ document, getClient, i1
|
|
|
1196
1287
|
currentUser,
|
|
1197
1288
|
customValidation,
|
|
1198
1289
|
signal,
|
|
1199
|
-
scheduling,
|
|
1200
1290
|
__internal: {
|
|
1201
1291
|
markIncomplete: () => {
|
|
1202
1292
|
complete = !1;
|
|
1203
1293
|
},
|
|
1204
|
-
pace
|
|
1294
|
+
pace
|
|
1205
1295
|
}
|
|
1206
1296
|
};
|
|
1207
|
-
return
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1297
|
+
return await i18n.loadNamespaces(["validation"]), createDocumentValidationResult(await validateItemAsync(validationOptions), complete);
|
|
1298
|
+
} catch (err) {
|
|
1299
|
+
if (signal?.aborted) throw signal.reason;
|
|
1300
|
+
if (typeof document?._type != "string") throw err;
|
|
1301
|
+
console.error(err);
|
|
1302
|
+
let message = err instanceof Error || isRecord(err) && typeof err.message == "string" ? err.message : "Unknown error";
|
|
1303
|
+
return createDocumentValidationResult([{
|
|
1304
|
+
code: validationMarkerCodes.validationException,
|
|
1305
|
+
level: "error",
|
|
1306
|
+
message,
|
|
1307
|
+
item: { message },
|
|
1308
|
+
path: []
|
|
1309
|
+
}], !1);
|
|
1310
|
+
}
|
|
1220
1311
|
}
|
|
1221
|
-
function validateItem(opts) {
|
|
1222
|
-
|
|
1312
|
+
async function validateItem(opts) {
|
|
1313
|
+
opts.signal?.throwIfAborted();
|
|
1314
|
+
let inheritedPace = opts.__internal?.pace, pace = inheritedPace ?? createPace(opts.scheduling);
|
|
1315
|
+
try {
|
|
1316
|
+
return await raceWithSignal(validateItemAsync(inheritedPace ? opts : {
|
|
1317
|
+
...opts,
|
|
1318
|
+
__internal: {
|
|
1319
|
+
...opts.__internal,
|
|
1320
|
+
pace
|
|
1321
|
+
}
|
|
1322
|
+
}), opts.signal);
|
|
1323
|
+
} finally {
|
|
1324
|
+
inheritedPace || pace.dispose();
|
|
1325
|
+
}
|
|
1223
1326
|
}
|
|
1224
1327
|
function validateRule(rule, value, context) {
|
|
1225
1328
|
return rule.validate(value, context);
|
|
1226
1329
|
}
|
|
1227
|
-
function
|
|
1228
|
-
let
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1330
|
+
async function runValidationTasks(tasks, concurrency) {
|
|
1331
|
+
let results = [], nextIndex = 0, failed = !1, failure;
|
|
1332
|
+
async function worker() {
|
|
1333
|
+
for (; !failed && nextIndex < tasks.length;) {
|
|
1334
|
+
let task = tasks[nextIndex];
|
|
1335
|
+
nextIndex += 1;
|
|
1336
|
+
try {
|
|
1337
|
+
results.push(await task());
|
|
1338
|
+
} catch (error) {
|
|
1339
|
+
failed || (failed = !0, failure = error);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
if (await Promise.all(Array.from({ length: Math.min(concurrency, tasks.length) }, () => worker())), failed) throw failure;
|
|
1344
|
+
return results;
|
|
1345
|
+
}
|
|
1346
|
+
async function validateItemAsync({ value, type, path = [], parent, customValidationConcurrencyLimiter, environment, customValidation = !0, scheduling: _scheduling, __internal: inheritedInternal, ...restOfContext }) {
|
|
1347
|
+
let pace = inheritedInternal?.pace;
|
|
1348
|
+
if (!pace) throw Error("Validation item was started without a pace");
|
|
1349
|
+
let __internal = inheritedInternal, ancestorHidden = restOfContext.hidden === !0, resolveHiddenForType = (schemaType, schemaValue, schemaParent, schemaPath, ancestorHiddenValue) => schemaType ? ancestorHiddenValue || resolveConditionalProperty(schemaType.hidden, {
|
|
1232
1350
|
...restOfContext,
|
|
1233
1351
|
parent: schemaParent,
|
|
1234
1352
|
value: schemaValue,
|
|
@@ -1246,7 +1364,7 @@ function validateItemObservable({ value, type, path = [], parent, customValidati
|
|
|
1246
1364
|
parent,
|
|
1247
1365
|
path,
|
|
1248
1366
|
type
|
|
1249
|
-
}), selfChecks = rules.map(addUnknownFieldsValidator).map((rule) =>
|
|
1367
|
+
}), selfChecks = rules.map(addUnknownFieldsValidator).map((rule) => () => validateRule(rule, value, {
|
|
1250
1368
|
...restOfContext,
|
|
1251
1369
|
environment,
|
|
1252
1370
|
hidden,
|
|
@@ -1258,7 +1376,7 @@ function validateItemObservable({ value, type, path = [], parent, customValidati
|
|
|
1258
1376
|
customValidation,
|
|
1259
1377
|
customValidationConcurrencyLimiter
|
|
1260
1378
|
}
|
|
1261
|
-
}))
|
|
1379
|
+
})), nestedChecks = [];
|
|
1262
1380
|
if (type?.jsonType === "object" && (value || value == null && rules.some((rule) => rule.isRequired()))) {
|
|
1263
1381
|
let fieldTypes = type.fields.reduce((acc, field) => (acc[field.name] = field.type, acc), {});
|
|
1264
1382
|
nestedChecks = nestedChecks.concat(rules.flatMap((rule) => extractFieldRulesFromRule(rule)).flatMap((fieldResults) => Object.entries(fieldResults)).flatMap(([name, validation]) => {
|
|
@@ -1273,15 +1391,15 @@ function validateItemObservable({ value, type, path = [], parent, customValidati
|
|
|
1273
1391
|
return normalizeValidationRules({
|
|
1274
1392
|
...fieldType,
|
|
1275
1393
|
validation
|
|
1276
|
-
}, fieldContext).map(addUnknownFieldsValidator).map((subRule) =>
|
|
1394
|
+
}, fieldContext).map(addUnknownFieldsValidator).map((subRule) => () => validateRule(subRule, nestedValue, {
|
|
1277
1395
|
...fieldContext,
|
|
1278
1396
|
__internal: {
|
|
1279
1397
|
...__internal,
|
|
1280
1398
|
customValidation,
|
|
1281
1399
|
customValidationConcurrencyLimiter
|
|
1282
1400
|
}
|
|
1283
|
-
}))
|
|
1284
|
-
})), nestedChecks = nestedChecks.concat(type.fields.map((field) =>
|
|
1401
|
+
}));
|
|
1402
|
+
})), nestedChecks = nestedChecks.concat(type.fields.map((field) => () => validateItemAsync({
|
|
1285
1403
|
...restOfContext,
|
|
1286
1404
|
hidden,
|
|
1287
1405
|
parent: value,
|
|
@@ -1291,11 +1409,10 @@ function validateItemObservable({ value, type, path = [], parent, customValidati
|
|
|
1291
1409
|
environment,
|
|
1292
1410
|
customValidationConcurrencyLimiter,
|
|
1293
1411
|
customValidation,
|
|
1294
|
-
scheduling,
|
|
1295
1412
|
__internal
|
|
1296
|
-
})))
|
|
1413
|
+
})));
|
|
1297
1414
|
}
|
|
1298
|
-
|
|
1415
|
+
if (type?.jsonType === "array" && Array.isArray(value) && (nestedChecks = nestedChecks.concat(value.map((item, index) => () => validateItemAsync({
|
|
1299
1416
|
...restOfContext,
|
|
1300
1417
|
hidden,
|
|
1301
1418
|
parent: value,
|
|
@@ -1305,9 +1422,16 @@ function validateItemObservable({ value, type, path = [], parent, customValidati
|
|
|
1305
1422
|
environment,
|
|
1306
1423
|
customValidationConcurrencyLimiter,
|
|
1307
1424
|
customValidation,
|
|
1308
|
-
scheduling,
|
|
1309
1425
|
__internal
|
|
1310
|
-
}))))
|
|
1426
|
+
})))), selfChecks.length === 1 && nestedChecks.length === 0 && !rules.some((rule) => extractFieldRulesFromRule(rule).length > 0)) {
|
|
1427
|
+
let wait = pace.wait(restOfContext.signal);
|
|
1428
|
+
return wait && await wait, restOfContext.signal?.throwIfAborted(), selfChecks[0]();
|
|
1429
|
+
}
|
|
1430
|
+
let results = flatten(await runValidationTasks([...selfChecks, ...nestedChecks].map((validateNode) => async () => {
|
|
1431
|
+
let wait = pace.wait(restOfContext.signal);
|
|
1432
|
+
return wait && await wait, restOfContext.signal?.throwIfAborted(), validateNode();
|
|
1433
|
+
}), 40));
|
|
1434
|
+
return rules.some((rule) => extractFieldRulesFromRule(rule).length > 0) ? deduplicateMarkers(results) : results;
|
|
1311
1435
|
}
|
|
1312
1436
|
function deduplicateMarkers(markers) {
|
|
1313
1437
|
let buckets = /* @__PURE__ */ new Map();
|
|
@@ -1332,4 +1456,4 @@ function toDocumentValidationMarker(marker) {
|
|
|
1332
1456
|
}
|
|
1333
1457
|
export { getFallbackLocaleSource as _, validateDocumentInternal as a, validateDocuments as c, normalizeValidationRules as d, Rule$2 as f, pathToString as g, convertToValidationMarker as h, validateDocument as i, validateItem as l, typeString as m, evaluateDocumentObservable as n, validateDocumentObservable as o, getTypeChain as p, resolveTypeForArrayItem as r, validateDocumentWithWorkspace as s, evaluateDocumentInternal as t, compileValidationRules as u, validationLocaleStrings as v, validationMarkerCodes as y };
|
|
1334
1458
|
|
|
1335
|
-
//# sourceMappingURL=validateDocument-
|
|
1459
|
+
//# sourceMappingURL=validateDocument-lfNM88pm.js.map
|