@sanity/validation 3.3.1 → 3.4.0
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/dts/src/index.d.ts +17 -3
- package/lib/index.cjs.mjs +2 -0
- package/lib/index.esm.js +7 -16
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +7 -16
- package/lib/index.js.map +1 -1
- package/package.json +5 -8
- package/src/validateDocument.test.ts +5 -10
- package/src/validateDocument.ts +3 -29
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {SanityClient} from '@sanity/client'
|
|
2
1
|
import {
|
|
2
|
+
ArraySchemaType,
|
|
3
|
+
PortableTextTextBlock,
|
|
3
4
|
Rule,
|
|
4
|
-
SchemaType,
|
|
5
5
|
SanityDocument,
|
|
6
6
|
Schema,
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
SchemaType,
|
|
8
|
+
ValidationContext,
|
|
9
9
|
} from '@sanity/types'
|
|
10
10
|
import {createSchema} from '../test/createSchema'
|
|
11
11
|
import {createMockSanityClient} from '../test/mocks/mockSanityClient'
|
|
@@ -25,7 +25,7 @@ beforeEach(() => {
|
|
|
25
25
|
})
|
|
26
26
|
|
|
27
27
|
// mock client
|
|
28
|
-
const client = createMockSanityClient() as any as
|
|
28
|
+
const client = createMockSanityClient() as any as ReturnType<ValidationContext['getClient']>
|
|
29
29
|
const getClient = (options: {apiVersion: string}) => client
|
|
30
30
|
|
|
31
31
|
describe('resolveTypeForArrayItem', () => {
|
|
@@ -176,7 +176,6 @@ describe('validateItem', () => {
|
|
|
176
176
|
|
|
177
177
|
await expect(
|
|
178
178
|
validateItem({
|
|
179
|
-
client,
|
|
180
179
|
getClient,
|
|
181
180
|
schema,
|
|
182
181
|
value: {},
|
|
@@ -247,7 +246,6 @@ describe('validateItem', () => {
|
|
|
247
246
|
|
|
248
247
|
await expect(
|
|
249
248
|
validateItem({
|
|
250
|
-
client,
|
|
251
249
|
getClient,
|
|
252
250
|
schema,
|
|
253
251
|
document: undefined,
|
|
@@ -415,7 +413,6 @@ describe('validateItem', () => {
|
|
|
415
413
|
}
|
|
416
414
|
|
|
417
415
|
const result = await validateItem({
|
|
418
|
-
client,
|
|
419
416
|
getClient,
|
|
420
417
|
schema,
|
|
421
418
|
document: document,
|
|
@@ -519,7 +516,6 @@ describe('validateItem', () => {
|
|
|
519
516
|
|
|
520
517
|
await expect(
|
|
521
518
|
validateItem({
|
|
522
|
-
client,
|
|
523
519
|
getClient,
|
|
524
520
|
schema,
|
|
525
521
|
document: undefined,
|
|
@@ -619,7 +615,6 @@ describe('validateItem', () => {
|
|
|
619
615
|
|
|
620
616
|
await expect(
|
|
621
617
|
validateItem({
|
|
622
|
-
client,
|
|
623
618
|
getClient,
|
|
624
619
|
schema,
|
|
625
620
|
value,
|
package/src/validateDocument.ts
CHANGED
|
@@ -10,10 +10,9 @@ import {
|
|
|
10
10
|
isSpanSchemaType,
|
|
11
11
|
isPortableTextTextBlock,
|
|
12
12
|
} from '@sanity/types'
|
|
13
|
-
import {concat, defer,
|
|
13
|
+
import {concat, defer, lastValueFrom, merge, Observable, of} from 'rxjs'
|
|
14
14
|
import {catchError, map, mergeAll, mergeMap, toArray} from 'rxjs/operators'
|
|
15
15
|
import {flatten, uniqBy} from 'lodash'
|
|
16
|
-
import type {SanityClient} from '@sanity/client'
|
|
17
16
|
import typeString from './util/typeString'
|
|
18
17
|
import {cancelIdleCallback, requestIdleCallback} from './util/requestIdleCallback'
|
|
19
18
|
import ValidationErrorClass from './ValidationError'
|
|
@@ -52,7 +51,7 @@ export function resolveTypeForArrayItem(
|
|
|
52
51
|
const EMPTY_MARKERS: ValidationMarker[] = []
|
|
53
52
|
|
|
54
53
|
export default async function validateDocument(
|
|
55
|
-
getClient:
|
|
54
|
+
getClient: ValidateItemOptions['getClient'],
|
|
56
55
|
doc: SanityDocument,
|
|
57
56
|
schema: Schema,
|
|
58
57
|
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
@@ -61,7 +60,7 @@ export default async function validateDocument(
|
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
export function validateDocumentObservable(
|
|
64
|
-
getClient:
|
|
63
|
+
getClient: ValidateItemOptions['getClient'],
|
|
65
64
|
doc: SanityDocument,
|
|
66
65
|
schema: Schema,
|
|
67
66
|
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
@@ -72,9 +71,7 @@ export function validateDocumentObservable(
|
|
|
72
71
|
return of(EMPTY_MARKERS)
|
|
73
72
|
}
|
|
74
73
|
|
|
75
|
-
const client = getClient({apiVersion: '2021-06-07'})
|
|
76
74
|
const validationOptions: ValidateItemOptions = {
|
|
77
|
-
client,
|
|
78
75
|
getClient,
|
|
79
76
|
schema,
|
|
80
77
|
parent: undefined,
|
|
@@ -85,29 +82,6 @@ export function validateDocumentObservable(
|
|
|
85
82
|
getDocumentExists: context?.getDocumentExists,
|
|
86
83
|
}
|
|
87
84
|
|
|
88
|
-
// <TEMPORARY UGLY HACK TO PRINT DEPRECATION WARNINGS ON USE>
|
|
89
|
-
/* eslint-disable no-proto */
|
|
90
|
-
const wrappedClient = client as any
|
|
91
|
-
validationOptions.client = [
|
|
92
|
-
...Object.keys(client),
|
|
93
|
-
...Object.keys(wrappedClient.__proto__),
|
|
94
|
-
].reduce((acc, key) => {
|
|
95
|
-
const original = Object.hasOwnProperty.call(client, key)
|
|
96
|
-
? wrappedClient[key]
|
|
97
|
-
: wrappedClient.__proto__[key]
|
|
98
|
-
|
|
99
|
-
return Object.defineProperty(acc, key, {
|
|
100
|
-
get() {
|
|
101
|
-
console.warn(
|
|
102
|
-
'`configContext.client` is deprecated and will be removed in the next release! Use `context.getClient({apiVersion: "2021-06-07"})` instead'
|
|
103
|
-
)
|
|
104
|
-
return original
|
|
105
|
-
},
|
|
106
|
-
})
|
|
107
|
-
}, {}) as any as SanityClient
|
|
108
|
-
/* eslint-enable no-proto */
|
|
109
|
-
// </TEMPORARY UGLY HACK TO PRINT DEPRECATION WARNINGS ON USE>
|
|
110
|
-
|
|
111
85
|
return validateItemObservable(validationOptions).pipe(
|
|
112
86
|
catchError((err) => {
|
|
113
87
|
console.error(err)
|