@sanity/validation 3.2.6-client-v5.17 → 3.2.6
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 +3 -17
- package/lib/index.cjs.mjs +0 -2
- package/lib/index.esm.js +16 -7
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +16 -7
- package/lib/index.js.map +1 -1
- package/package.json +6 -6
- package/src/validateDocument.test.ts +10 -5
- package/src/validateDocument.ts +29 -3
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import type {SanityClient} from '@sanity/client'
|
|
1
2
|
import {
|
|
2
|
-
ArraySchemaType,
|
|
3
|
-
PortableTextTextBlock,
|
|
4
3
|
Rule,
|
|
4
|
+
SchemaType,
|
|
5
5
|
SanityDocument,
|
|
6
6
|
Schema,
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
ArraySchemaType,
|
|
8
|
+
PortableTextTextBlock,
|
|
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 SanityClient
|
|
29
29
|
const getClient = (options: {apiVersion: string}) => client
|
|
30
30
|
|
|
31
31
|
describe('resolveTypeForArrayItem', () => {
|
|
@@ -176,6 +176,7 @@ describe('validateItem', () => {
|
|
|
176
176
|
|
|
177
177
|
await expect(
|
|
178
178
|
validateItem({
|
|
179
|
+
client,
|
|
179
180
|
getClient,
|
|
180
181
|
schema,
|
|
181
182
|
value: {},
|
|
@@ -246,6 +247,7 @@ describe('validateItem', () => {
|
|
|
246
247
|
|
|
247
248
|
await expect(
|
|
248
249
|
validateItem({
|
|
250
|
+
client,
|
|
249
251
|
getClient,
|
|
250
252
|
schema,
|
|
251
253
|
document: undefined,
|
|
@@ -413,6 +415,7 @@ describe('validateItem', () => {
|
|
|
413
415
|
}
|
|
414
416
|
|
|
415
417
|
const result = await validateItem({
|
|
418
|
+
client,
|
|
416
419
|
getClient,
|
|
417
420
|
schema,
|
|
418
421
|
document: document,
|
|
@@ -516,6 +519,7 @@ describe('validateItem', () => {
|
|
|
516
519
|
|
|
517
520
|
await expect(
|
|
518
521
|
validateItem({
|
|
522
|
+
client,
|
|
519
523
|
getClient,
|
|
520
524
|
schema,
|
|
521
525
|
document: undefined,
|
|
@@ -615,6 +619,7 @@ describe('validateItem', () => {
|
|
|
615
619
|
|
|
616
620
|
await expect(
|
|
617
621
|
validateItem({
|
|
622
|
+
client,
|
|
618
623
|
getClient,
|
|
619
624
|
schema,
|
|
620
625
|
value,
|
package/src/validateDocument.ts
CHANGED
|
@@ -10,9 +10,10 @@ import {
|
|
|
10
10
|
isSpanSchemaType,
|
|
11
11
|
isPortableTextTextBlock,
|
|
12
12
|
} from '@sanity/types'
|
|
13
|
-
import {concat, defer, lastValueFrom, merge, Observable, of} from 'rxjs'
|
|
13
|
+
import {concat, defer, firstValueFrom, 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'
|
|
16
17
|
import typeString from './util/typeString'
|
|
17
18
|
import {cancelIdleCallback, requestIdleCallback} from './util/requestIdleCallback'
|
|
18
19
|
import ValidationErrorClass from './ValidationError'
|
|
@@ -51,7 +52,7 @@ export function resolveTypeForArrayItem(
|
|
|
51
52
|
const EMPTY_MARKERS: ValidationMarker[] = []
|
|
52
53
|
|
|
53
54
|
export default async function validateDocument(
|
|
54
|
-
getClient:
|
|
55
|
+
getClient: (options: {apiVersion: string}) => SanityClient,
|
|
55
56
|
doc: SanityDocument,
|
|
56
57
|
schema: Schema,
|
|
57
58
|
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
@@ -60,7 +61,7 @@ export default async function validateDocument(
|
|
|
60
61
|
}
|
|
61
62
|
|
|
62
63
|
export function validateDocumentObservable(
|
|
63
|
-
getClient:
|
|
64
|
+
getClient: (options: {apiVersion: string}) => SanityClient,
|
|
64
65
|
doc: SanityDocument,
|
|
65
66
|
schema: Schema,
|
|
66
67
|
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
@@ -71,7 +72,9 @@ export function validateDocumentObservable(
|
|
|
71
72
|
return of(EMPTY_MARKERS)
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
const client = getClient({apiVersion: '2021-06-07'})
|
|
74
76
|
const validationOptions: ValidateItemOptions = {
|
|
77
|
+
client,
|
|
75
78
|
getClient,
|
|
76
79
|
schema,
|
|
77
80
|
parent: undefined,
|
|
@@ -82,6 +85,29 @@ export function validateDocumentObservable(
|
|
|
82
85
|
getDocumentExists: context?.getDocumentExists,
|
|
83
86
|
}
|
|
84
87
|
|
|
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
|
+
|
|
85
111
|
return validateItemObservable(validationOptions).pipe(
|
|
86
112
|
catchError((err) => {
|
|
87
113
|
console.error(err)
|