@sanity/validation 3.0.0-dev-preview.15 → 3.0.0-dev-preview.16

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.
@@ -11,7 +11,7 @@ import {
11
11
  isSpanSchemaType,
12
12
  } from '@sanity/types'
13
13
  import {uniqBy} from 'lodash'
14
- import {SanityClient} from '@sanity/client'
14
+ import type {SanityClient} from '@sanity/client'
15
15
  import typeString from './util/typeString'
16
16
  import ValidationErrorClass from './ValidationError'
17
17
  import normalizeValidationRules from './util/normalizeValidationRules'
@@ -48,7 +48,7 @@ export function resolveTypeForArrayItem(
48
48
  }
49
49
 
50
50
  export default async function validateDocument(
51
- client: SanityClient,
51
+ getClient: (options: {apiVersion: string}) => SanityClient,
52
52
  doc: SanityDocument,
53
53
  schema: Schema,
54
54
  context?: Pick<ValidationContext, 'getDocumentExists'>
@@ -59,16 +59,44 @@ export default async function validateDocument(
59
59
  return []
60
60
  }
61
61
 
62
- try {
63
- return await validateItem({
64
- client,
65
- parent: undefined,
66
- value: doc,
67
- path: [],
68
- document: doc,
69
- type: documentType,
70
- getDocumentExists: context?.getDocumentExists,
62
+ const client = getClient({apiVersion: '2021-06-07'})
63
+ const validationOptions: ValidateItemOptions = {
64
+ client,
65
+ getClient,
66
+ schema,
67
+ parent: undefined,
68
+ value: doc,
69
+ path: [],
70
+ document: doc,
71
+ type: documentType,
72
+ getDocumentExists: context?.getDocumentExists,
73
+ }
74
+
75
+ // <TEMPORARY UGLY HACK TO PRINT DEPRECATION WARNINGS ON USE>
76
+ /* eslint-disable no-proto */
77
+ const wrappedClient = client as any
78
+ validationOptions.client = [
79
+ ...Object.keys(client),
80
+ ...Object.keys(wrappedClient.__proto__),
81
+ ].reduce((acc, key) => {
82
+ const original = Object.hasOwnProperty.call(client, key)
83
+ ? wrappedClient[key]
84
+ : wrappedClient.__proto__[key]
85
+
86
+ return Object.defineProperty(acc, key, {
87
+ get() {
88
+ console.warn(
89
+ '`configContext.client` is deprecated and will be removed in the next release! Use `context.getClient({apiVersion: "2021-06-07"})` instead'
90
+ )
91
+ return original
92
+ },
71
93
  })
94
+ }, {}) as any as SanityClient
95
+ /* eslint-enable no-proto */
96
+ // </TEMPORARY UGLY HACK TO PRINT DEPRECATION WARNINGS ON USE>
97
+
98
+ try {
99
+ return await validateItem(validationOptions)
72
100
  } catch (err) {
73
101
  console.error(err)
74
102
  return [
@@ -23,7 +23,7 @@ function serializePath(path: Path): string {
23
23
  }
24
24
 
25
25
  const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
26
- const {client, document, path, type} = context
26
+ const {getClient, document, path, type} = context
27
27
  const schemaOptions = type?.options as {disableArrayWarning?: boolean} | undefined
28
28
 
29
29
  if (!document) {
@@ -48,7 +48,7 @@ const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
48
48
  `${atPath} == $slug`,
49
49
  ].join(' && ')
50
50
 
51
- return client.fetch<boolean>(
51
+ return getClient({apiVersion: '2022-09-09'}).fetch<boolean>(
52
52
  `!defined(*[${constraints}][0]._id)`,
53
53
  {
54
54
  docType,