@sanity/validation 6.12.0-next.68 → 6.12.0-next.95
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/README.md +26 -3
- package/lib/_internal.d.ts +5 -2
- package/lib/_internal.js +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -2
- package/lib/{validateDocument-CBOsd748.d.ts → validateDocument-6pLTIHIn.d.ts} +87 -9
- package/lib/{validateDocument-C77_Oewa.js → validateDocument-Cq33kUmN.js} +370 -86
- package/lib/validateDocument-Cq33kUmN.js.map +1 -0
- package/package.json +5 -5
- package/lib/validateDocument-C77_Oewa.js.map +0 -1
package/README.md
CHANGED
|
@@ -3,13 +3,36 @@
|
|
|
3
3
|
Validates complete Sanity documents against a compiled Sanity schema.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
|
-
import {validateDocument} from '@sanity/validation'
|
|
6
|
+
import {validateDocument, validationMarkerCodes} from '@sanity/validation'
|
|
7
7
|
|
|
8
8
|
const markers = await validateDocument({document, schema, client})
|
|
9
|
+
|
|
10
|
+
for (const marker of markers) {
|
|
11
|
+
if (marker.code === validationMarkerCodes.stringMinimumLength) {
|
|
12
|
+
const {actualLength, minimumLength} = marker.details || {}
|
|
13
|
+
if (typeof actualLength === 'number' && typeof minimumLength === 'number') {
|
|
14
|
+
console.log(`Expected at least ${minimumLength} characters, got ${actualLength}`)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const summary = markers
|
|
20
|
+
.map((marker) => {
|
|
21
|
+
const path = marker.path
|
|
22
|
+
.map((segment) => (typeof segment === 'object' ? segment._key : segment))
|
|
23
|
+
.join('.')
|
|
24
|
+
|
|
25
|
+
return `[${marker.level}] ${path || '<document>'}: ${marker.message} (${marker.code})`
|
|
26
|
+
})
|
|
27
|
+
.join('\n')
|
|
9
28
|
```
|
|
10
29
|
|
|
11
|
-
|
|
12
|
-
may
|
|
30
|
+
Every returned marker includes a stable machine-readable `code` alongside its localized `message`,
|
|
31
|
+
`level`, and `path`. Built-in failures may also include structured `details`. Custom validators can
|
|
32
|
+
return their own `code` and `details`; custom codes should be namespaced, for example
|
|
33
|
+
`custom.seo-title`.
|
|
34
|
+
|
|
35
|
+
The package does not apply mutations or decide whether a document may be edited or published.
|
|
13
36
|
|
|
14
37
|
## Migrating from `sanity`
|
|
15
38
|
|
package/lib/_internal.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as
|
|
1
|
+
import { c as resolveTypeForArrayItem, d as validateDocumentObservable, g as ValidationMarkerCode, n as ValidateDocumentObservableOptions, p as validateItem, t as ValidateDocumentInternalOptions, u as validateDocumentInternal, v as ValidationContext, y as LocaleSource } from "./validateDocument-6pLTIHIn.js";
|
|
2
2
|
import { Path, Rule as Rule$1, RuleClass, Schema, SchemaType, SchemaValidationValue, ValidationContext as ValidationContext$1, ValidationError, ValidationMarker } from "@sanity/types";
|
|
3
3
|
declare function getFallbackLocaleSource(): LocaleSource;
|
|
4
4
|
declare const validationLocaleStrings: {
|
|
@@ -84,7 +84,10 @@ declare function hasValidationContext(validation: SchemaValidationValue | undefi
|
|
|
84
84
|
* @internal
|
|
85
85
|
*/
|
|
86
86
|
declare const Rule: RuleClass;
|
|
87
|
-
declare function convertToValidationMarker(validatorResult: true | true[] | string | string[] | ValidationError | ValidationError[], level: 'error' | 'warning' | 'info' | undefined, context: ValidationContext
|
|
87
|
+
declare function convertToValidationMarker(validatorResult: true | true[] | string | string[] | ValidationError | ValidationError[], level: 'error' | 'warning' | 'info' | undefined, context: ValidationContext, fallback?: {
|
|
88
|
+
code: ValidationMarkerCode;
|
|
89
|
+
details?: Record<string, unknown>;
|
|
90
|
+
}): ValidationMarker[];
|
|
88
91
|
declare function getTypeChain(type: SchemaType | undefined, visited?: Set<SchemaType>): SchemaType[];
|
|
89
92
|
declare function normalizeValidationRules(typeDef: SchemaType | undefined, context?: ValidationContext$1): Rule$1[];
|
|
90
93
|
declare function pathToString(path?: Path | undefined): string;
|
package/lib/_internal.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as normalizeValidationRules, d as convertToValidationMarker, f as pathToString, i as validateDocumentObservable, l as Rule, m as validationLocaleStrings, o as validateItem, p as getFallbackLocaleSource, r as validateDocumentInternal, s as getTypeChain, t as resolveTypeForArrayItem, u as typeString } from "./validateDocument-
|
|
1
|
+
import { c as normalizeValidationRules, d as convertToValidationMarker, f as pathToString, i as validateDocumentObservable, l as Rule, m as validationLocaleStrings, o as validateItem, p as getFallbackLocaleSource, r as validateDocumentInternal, s as getTypeChain, t as resolveTypeForArrayItem, u as typeString } from "./validateDocument-Cq33kUmN.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 { a as
|
|
2
|
-
export { type ValidateDocumentOptions, type ValidateDocumentWorkspaceOptions, type ValidationSource, validateDocument, validateDocumentWithWorkspace };
|
|
1
|
+
import { _ as validationMarkerCodes, a as ValidationClient, f as validateDocumentWithWorkspace, g as ValidationMarkerCode, h as DocumentValidationMarker, i as ValidateDocumentWorkspaceOptions, l as validateDocument, m as BuiltInValidationMarkerCode, o as ValidationSchema, r as ValidateDocumentOptions, s as ValidationSource } from "./validateDocument-6pLTIHIn.js";
|
|
2
|
+
export { type BuiltInValidationMarkerCode, type DocumentValidationMarker, type ValidateDocumentOptions, type ValidateDocumentWorkspaceOptions, type ValidationClient, type ValidationMarkerCode, type ValidationSchema, type ValidationSource, validateDocument, validateDocumentWithWorkspace, validationMarkerCodes };
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as validateDocumentWithWorkspace, n as validateDocument } from "./validateDocument-
|
|
2
|
-
export { validateDocument, validateDocumentWithWorkspace };
|
|
1
|
+
import { a as validateDocumentWithWorkspace, h as validationMarkerCodes, n as validateDocument } from "./validateDocument-Cq33kUmN.js";
|
|
2
|
+
export { validateDocument, validateDocumentWithWorkspace, validationMarkerCodes };
|
|
@@ -34,6 +34,66 @@ declare module '@sanity/types' {
|
|
|
34
34
|
i18n: LocaleSource;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
+
/** Machine-readable codes emitted by built-in document validation. @beta */
|
|
38
|
+
declare const validationMarkerCodes: {
|
|
39
|
+
readonly arrayDuplicateItem: 'array.duplicate-item';
|
|
40
|
+
readonly arrayExactLength: 'array.exact-length';
|
|
41
|
+
readonly arrayMaximumLength: 'array.maximum-length';
|
|
42
|
+
readonly arrayMinimumLength: 'array.minimum-length';
|
|
43
|
+
readonly assetRequired: 'asset.required';
|
|
44
|
+
readonly custom: 'custom';
|
|
45
|
+
readonly dateInvalidFormat: 'date.invalid-format';
|
|
46
|
+
readonly dateMaximum: 'date.maximum';
|
|
47
|
+
readonly dateMinimum: 'date.minimum';
|
|
48
|
+
readonly documentUnknownType: 'document.unknown-type';
|
|
49
|
+
readonly mediaCustom: 'media.custom';
|
|
50
|
+
readonly mediaInvalidReference: 'media.invalid-reference';
|
|
51
|
+
readonly mediaNotFound: 'media.not-found';
|
|
52
|
+
readonly numberGreaterThan: 'number.greater-than';
|
|
53
|
+
readonly numberInteger: 'number.integer';
|
|
54
|
+
readonly numberLessThan: 'number.less-than';
|
|
55
|
+
readonly numberMaximum: 'number.maximum';
|
|
56
|
+
readonly numberMinimum: 'number.minimum';
|
|
57
|
+
readonly numberPrecision: 'number.precision';
|
|
58
|
+
readonly objectUnknownField: 'object.unknown-field';
|
|
59
|
+
readonly referenceInvalid: 'reference.invalid';
|
|
60
|
+
readonly referenceNotPublished: 'reference.not-published';
|
|
61
|
+
readonly ruleAllFailed: 'rule.all-failed';
|
|
62
|
+
readonly ruleEitherFailed: 'rule.either-failed';
|
|
63
|
+
readonly slugInvalidType: 'slug.invalid-type';
|
|
64
|
+
readonly slugMissingCurrent: 'slug.missing-current';
|
|
65
|
+
readonly slugNotUnique: 'slug.not-unique';
|
|
66
|
+
readonly stringEmail: 'string.email';
|
|
67
|
+
readonly stringExactLength: 'string.exact-length';
|
|
68
|
+
readonly stringLowercase: 'string.lowercase';
|
|
69
|
+
readonly stringMaximumLength: 'string.maximum-length';
|
|
70
|
+
readonly stringMinimumLength: 'string.minimum-length';
|
|
71
|
+
readonly stringRegexMatch: 'string.regex-match';
|
|
72
|
+
readonly stringRegexMismatch: 'string.regex-mismatch';
|
|
73
|
+
readonly stringUppercase: 'string.uppercase';
|
|
74
|
+
readonly stringUrlCredentialsNotAllowed: 'string.url.credentials-not-allowed';
|
|
75
|
+
readonly stringUrlInvalid: 'string.url.invalid';
|
|
76
|
+
readonly stringUrlNotAbsolute: 'string.url.not-absolute';
|
|
77
|
+
readonly stringUrlNotRelative: 'string.url.not-relative';
|
|
78
|
+
readonly stringUrlSchemeNotAllowed: 'string.url.scheme-not-allowed';
|
|
79
|
+
readonly validationException: 'validation.exception';
|
|
80
|
+
readonly validationFailed: 'validation.failed';
|
|
81
|
+
readonly valueNotAllowed: 'value.not-allowed';
|
|
82
|
+
readonly valueRequired: 'value.required';
|
|
83
|
+
readonly valueTypeMismatch: 'value.type-mismatch';
|
|
84
|
+
};
|
|
85
|
+
/** A code emitted by a built-in validator. @beta */
|
|
86
|
+
type BuiltInValidationMarkerCode = (typeof validationMarkerCodes)[keyof typeof validationMarkerCodes];
|
|
87
|
+
/**
|
|
88
|
+
* A built-in validation code or an application-defined custom code.
|
|
89
|
+
* Custom validators should namespace their codes, for example `custom.seo-title`.
|
|
90
|
+
* @beta
|
|
91
|
+
*/
|
|
92
|
+
type ValidationMarkerCode = BuiltInValidationMarkerCode | (string & {});
|
|
93
|
+
/** A validation marker emitted by `validateDocument`, which always has a code. @beta */
|
|
94
|
+
type DocumentValidationMarker = Omit<ValidationMarker, 'code'> & {
|
|
95
|
+
code: ValidationMarkerCode;
|
|
96
|
+
};
|
|
37
97
|
/**
|
|
38
98
|
* @internal
|
|
39
99
|
*/
|
|
@@ -47,9 +107,9 @@ interface ValidateDocumentOptions {
|
|
|
47
107
|
*/
|
|
48
108
|
document: SanityDocument;
|
|
49
109
|
/** The compiled schema to validate against. */
|
|
50
|
-
schema:
|
|
110
|
+
schema: ValidationSchema;
|
|
51
111
|
/** A configured client used for reference checks and custom validators. */
|
|
52
|
-
client:
|
|
112
|
+
client: ValidationClient;
|
|
53
113
|
/**
|
|
54
114
|
* Function used to check if referenced documents exists (and is published).
|
|
55
115
|
*
|
|
@@ -89,6 +149,24 @@ interface ValidateDocumentOptions {
|
|
|
89
149
|
*/
|
|
90
150
|
currentUser?: Omit<CurrentUser, 'role'> | null;
|
|
91
151
|
}
|
|
152
|
+
/** A compiled schema accepted across compatible `@sanity/types` versions. @beta */
|
|
153
|
+
interface ValidationSchema {
|
|
154
|
+
get(name: string): unknown;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A configured Sanity client accepted across compatible client versions.
|
|
158
|
+
*
|
|
159
|
+
* This structural type describes the capabilities used internally by validation.
|
|
160
|
+
* The configured client is exposed to custom validators as a `SanityClient`, so it must be compatible with the full client API.
|
|
161
|
+
*
|
|
162
|
+
* @beta
|
|
163
|
+
*/
|
|
164
|
+
interface ValidationClient {
|
|
165
|
+
fetch: SanityClient['fetch'];
|
|
166
|
+
getDataUrl: SanityClient['getDataUrl'];
|
|
167
|
+
observable: Pick<SanityClient['observable'], 'fetch' | 'request'>;
|
|
168
|
+
withConfig(config: Parameters<SanityClient['withConfig']>[0]): ValidationClient;
|
|
169
|
+
}
|
|
92
170
|
/**
|
|
93
171
|
* The validation capabilities required from a resolved Studio source or workspace.
|
|
94
172
|
*
|
|
@@ -127,7 +205,7 @@ interface ValidateDocumentWorkspaceOptions extends Omit<ValidateDocumentOptions,
|
|
|
127
205
|
* @beta
|
|
128
206
|
* @deprecated Prefer {@link validateDocument} with `{document, schema, client}` for new code.
|
|
129
207
|
*/
|
|
130
|
-
declare function validateDocumentWithWorkspace({ document, workspace, getClient, getDocumentExists, environment, maxCustomValidationConcurrency, maxFetchConcurrency, currentUser }: ValidateDocumentWorkspaceOptions): Promise<
|
|
208
|
+
declare function validateDocumentWithWorkspace({ document, workspace, getClient, getDocumentExists, environment, maxCustomValidationConcurrency, maxFetchConcurrency, currentUser }: ValidateDocumentWorkspaceOptions): Promise<DocumentValidationMarker[]>;
|
|
131
209
|
/**
|
|
132
210
|
* Validates a document against the schema in a resolved Studio workspace or source.
|
|
133
211
|
*
|
|
@@ -136,14 +214,14 @@ declare function validateDocumentWithWorkspace({ document, workspace, getClient,
|
|
|
136
214
|
*
|
|
137
215
|
* @beta
|
|
138
216
|
*/
|
|
139
|
-
declare function validateDocument(options: ValidateDocumentWorkspaceOptions): Promise<
|
|
217
|
+
declare function validateDocument(options: ValidateDocumentWorkspaceOptions): Promise<DocumentValidationMarker[]>;
|
|
140
218
|
/**
|
|
141
219
|
* Validates a document against a compiled schema. Returns validation markers
|
|
142
220
|
* without deciding whether the document may be edited or published.
|
|
143
221
|
*
|
|
144
222
|
* @beta
|
|
145
223
|
*/
|
|
146
|
-
declare function validateDocument(options: ValidateDocumentOptions): Promise<
|
|
224
|
+
declare function validateDocument(options: ValidateDocumentOptions): Promise<DocumentValidationMarker[]>;
|
|
147
225
|
/** @internal */
|
|
148
226
|
interface ValidateDocumentInternalOptions {
|
|
149
227
|
document: SanityDocument;
|
|
@@ -161,7 +239,7 @@ interface ValidateDocumentInternalOptions {
|
|
|
161
239
|
currentUser?: Omit<CurrentUser, 'role'> | null;
|
|
162
240
|
}
|
|
163
241
|
/** @internal */
|
|
164
|
-
declare function validateDocumentInternal({ document, schema, getClient, getDocumentExists, i18n, environment, maxCustomValidationConcurrency, maxFetchConcurrency, currentUser }: ValidateDocumentInternalOptions): Promise<
|
|
242
|
+
declare function validateDocumentInternal({ document, schema, getClient, getDocumentExists, i18n, environment, maxCustomValidationConcurrency, maxFetchConcurrency, currentUser }: ValidateDocumentInternalOptions): Promise<DocumentValidationMarker[]>;
|
|
165
243
|
/**
|
|
166
244
|
* @internal
|
|
167
245
|
*/
|
|
@@ -179,7 +257,7 @@ interface ValidateDocumentObservableOptions extends Pick<ValidationContext$1, 'g
|
|
|
179
257
|
* Validates a document against the given schema, returning an Observable
|
|
180
258
|
* @internal
|
|
181
259
|
*/
|
|
182
|
-
declare function validateDocumentObservable({ document, getClient, i18n, schema, getDocumentExists, environment, maxCustomValidationConcurrency, currentUser }: ValidateDocumentObservableOptions): Observable<
|
|
260
|
+
declare function validateDocumentObservable({ document, getClient, i18n, schema, getDocumentExists, environment, maxCustomValidationConcurrency, currentUser }: ValidateDocumentObservableOptions): Observable<DocumentValidationMarker[]>;
|
|
183
261
|
/**
|
|
184
262
|
* this is used make optional properties required by replacing optionals with
|
|
185
263
|
* `T[P] | undefined`. this is used to prevent errors in `validateItem` where
|
|
@@ -195,5 +273,5 @@ type ValidateItemOptions = {
|
|
|
195
273
|
currentUser?: Omit<CurrentUser, 'role'> | null;
|
|
196
274
|
} & ExplicitUndefined<Omit<ValidationContext$1, 'hidden'>>;
|
|
197
275
|
declare function validateItem(opts: ValidateItemOptions): Promise<ValidationMarker[]>;
|
|
198
|
-
export {
|
|
199
|
-
//# sourceMappingURL=validateDocument-
|
|
276
|
+
export { validationMarkerCodes as _, ValidationClient as a, resolveTypeForArrayItem as c, validateDocumentObservable as d, validateDocumentWithWorkspace as f, ValidationMarkerCode as g, DocumentValidationMarker as h, ValidateDocumentWorkspaceOptions as i, validateDocument as l, BuiltInValidationMarkerCode as m, ValidateDocumentObservableOptions as n, ValidationSchema as o, validateItem as p, ValidateDocumentOptions as r, ValidationSource as s, ValidateDocumentInternalOptions as t, validateDocumentInternal as u, ValidationContext$1 as v, LocaleSource as y };
|
|
277
|
+
//# sourceMappingURL=validateDocument-6pLTIHIn.d.ts.map
|