@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.
- package/lib/dts/src/validateDocument.d.ts +4 -2
- package/lib/dts/src/validateDocument.d.ts.map +1 -1
- package/lib/dts/src/validateDocument.js +33 -10
- package/lib/dts/src/validateDocument.js.map +1 -1
- package/lib/dts/src/validateDocument.test.js +20 -7
- package/lib/dts/src/validateDocument.test.js.map +1 -1
- package/lib/dts/src/validators/slugValidator.js +2 -2
- package/lib/dts/src/validators/slugValidator.js.map +1 -1
- package/lib/dts/test/infer.test.js +10 -9
- package/lib/dts/test/infer.test.js.map +1 -1
- package/lib/dts/tsconfig.lib.tsbuildinfo +1 -1
- package/lib/dts/tsconfig.tsbuildinfo +1 -1
- package/lib/index.cjs +35 -12
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +35 -12
- package/lib/index.js.map +1 -1
- package/package.json +5 -5
- package/src/validateDocument.test.ts +22 -8
- package/src/validateDocument.ts +39 -11
- package/src/validators/slugValidator.ts +2 -2
package/src/validateDocument.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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 {
|
|
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
|
|
51
|
+
return getClient({apiVersion: '2022-09-09'}).fetch<boolean>(
|
|
52
52
|
`!defined(*[${constraints}][0]._id)`,
|
|
53
53
|
{
|
|
54
54
|
docType,
|