@sanity/validation 2.21.5 → 2.21.8-reference-updates.27
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/dist/dts/validateDocument.d.ts +1 -1
- package/dist/dts/validateDocument.d.ts.map +1 -1
- package/dist/dts/validators/blockValidator.d.ts.map +1 -1
- package/dist/dts/validators/objectValidator.d.ts.map +1 -1
- package/lib/validateDocument.js +5 -4
- package/lib/validators/blockValidator.js +2 -1
- package/lib/validators/objectValidator.js +41 -8
- package/package.json +5 -5
- package/src/validateDocument.test.ts +5 -0
- package/src/validateDocument.ts +3 -1
- package/src/validators/blockValidator.ts +1 -0
- package/src/validators/objectValidator.ts +23 -3
|
@@ -3,7 +3,7 @@ import { SanityDocument, Schema, SchemaType, ValidationContext, ValidationMarker
|
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
5
|
export declare function resolveTypeForArrayItem(item: unknown, candidates: SchemaType[]): SchemaType | undefined;
|
|
6
|
-
export default function validateDocument(doc: SanityDocument, schema: Schema): Promise<ValidationMarker[]>;
|
|
6
|
+
export default function validateDocument(doc: SanityDocument, schema: Schema, context?: Pick<ValidationContext, 'getDocumentExists'>): Promise<ValidationMarker[]>;
|
|
7
7
|
/**
|
|
8
8
|
* this is used make optional properties required by replacing optionals with
|
|
9
9
|
* `T[P] | undefined`. this is used to prevent errors in `validateItem` where
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateDocument.d.ts","sourceRoot":"","sources":["../../src/validateDocument.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EAGjB,MAAM,eAAe,CAAA;AAYtB;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,UAAU,EAAE,GACvB,UAAU,GAAG,SAAS,CAiBxB;AAED,wBAA8B,gBAAgB,CAC5C,GAAG,EAAE,cAAc,EACnB,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"validateDocument.d.ts","sourceRoot":"","sources":["../../src/validateDocument.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EAGjB,MAAM,eAAe,CAAA;AAYtB;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,UAAU,EAAE,GACvB,UAAU,GAAG,SAAS,CAiBxB;AAED,wBAA8B,gBAAgB,CAC5C,GAAG,EAAE,cAAc,EACnB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,IAAI,CAAC,iBAAiB,EAAE,mBAAmB,CAAC,GACrD,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA2B7B;AAED;;;;;;GAMG;AACH,aAAK,iBAAiB,CAAC,CAAC,IAAI;KACzB,CAAC,IAAI,MAAM,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS;CAC5F,CAAA;AAED,aAAK,mBAAmB,GAAG;IACzB,KAAK,EAAE,OAAO,CAAA;CACf,GAAG,iBAAiB,CAAC,iBAAiB,CAAC,CAAA;AAExC,wBAAsB,YAAY,CAAC,EACjC,KAAK,EACL,IAAI,EACJ,IAAS,EACT,MAAM,EACN,GAAG,aAAa,EACjB,EAAE,mBAAmB,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA8FnD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blockValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/blockValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,cAAc,EAAC,MAAM,eAAe,CAAA;AAG9D,eAAO,MAAM,cAAc,EAAE,
|
|
1
|
+
{"version":3,"file":"blockValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/blockValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,cAAc,EAAC,MAAM,eAAe,CAAA;AAG9D,eAAO,MAAM,cAAc,EAAE,cAmD5B,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objectValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/objectValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,
|
|
1
|
+
{"version":3,"file":"objectValidator.d.ts","sourceRoot":"","sources":["../../../src/validators/objectValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAc,MAAM,eAAe,CAAA;AAKrD,QAAA,MAAM,gBAAgB,EAAE,UAkEvB,CAAA;AAED,eAAe,gBAAgB,CAAA"}
|
package/lib/validateDocument.js
CHANGED
|
@@ -74,7 +74,7 @@ function resolveTypeForArrayItem(item, candidates) {
|
|
|
74
74
|
}) || candidates.find(candidate => candidate.name === itemType) || candidates.find(candidate => candidate.name === 'object' && primitive === 'object');
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
function validateDocument(_x, _x2) {
|
|
77
|
+
function validateDocument(_x, _x2, _x3) {
|
|
78
78
|
return _validateDocument.apply(this, arguments);
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
@@ -87,7 +87,7 @@ function validateDocument(_x, _x2) {
|
|
|
87
87
|
|
|
88
88
|
|
|
89
89
|
function _validateDocument() {
|
|
90
|
-
_validateDocument = _asyncToGenerator(function* (doc, schema) {
|
|
90
|
+
_validateDocument = _asyncToGenerator(function* (doc, schema, context) {
|
|
91
91
|
var documentType = schema.get(doc._type);
|
|
92
92
|
|
|
93
93
|
if (!documentType) {
|
|
@@ -101,7 +101,8 @@ function _validateDocument() {
|
|
|
101
101
|
value: doc,
|
|
102
102
|
path: [],
|
|
103
103
|
document: doc,
|
|
104
|
-
type: documentType
|
|
104
|
+
type: documentType,
|
|
105
|
+
getDocumentExists: context === null || context === void 0 ? void 0 : context.getDocumentExists
|
|
105
106
|
});
|
|
106
107
|
} catch (err) {
|
|
107
108
|
console.error(err);
|
|
@@ -116,7 +117,7 @@ function _validateDocument() {
|
|
|
116
117
|
return _validateDocument.apply(this, arguments);
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
function validateItem(
|
|
120
|
+
function validateItem(_x4) {
|
|
120
121
|
return _validateItem.apply(this, arguments);
|
|
121
122
|
}
|
|
122
123
|
|
|
@@ -44,7 +44,8 @@ var blockValidator = /*#__PURE__*/function () {
|
|
|
44
44
|
path: (context.path || []).concat(['markDefs', {
|
|
45
45
|
_key: markDef._key
|
|
46
46
|
}]),
|
|
47
|
-
parent: value
|
|
47
|
+
parent: value,
|
|
48
|
+
getDocumentExists: context.getDocumentExists
|
|
48
49
|
});
|
|
49
50
|
annotationValidations.push(validations);
|
|
50
51
|
});
|
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
var _types = require("@sanity/types");
|
|
9
|
+
|
|
8
10
|
var _genericValidator = _interopRequireDefault(require("./genericValidator"));
|
|
9
11
|
|
|
10
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -35,17 +37,48 @@ var objectValidators = _objectSpread(_objectSpread({}, _genericValidator.default
|
|
|
35
37
|
|
|
36
38
|
return true;
|
|
37
39
|
},
|
|
38
|
-
reference: (
|
|
39
|
-
|
|
40
|
+
reference: function () {
|
|
41
|
+
var _reference = _asyncToGenerator(function* (_unused, value, message, context) {
|
|
42
|
+
if (!value) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!(0, _types.isReference)(value)) {
|
|
47
|
+
return message || 'Must be a reference to a document';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
var type = context.type,
|
|
51
|
+
getDocumentExists = context.getDocumentExists;
|
|
52
|
+
|
|
53
|
+
if (!type) {
|
|
54
|
+
throw new Error("`type` was not provided in validation context");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if ('weak' in type && type.weak) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!getDocumentExists) {
|
|
62
|
+
throw new Error("`getDocumentExists` was not provided in validation context");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
var exists = yield getDocumentExists({
|
|
66
|
+
id: value._ref
|
|
67
|
+
}); // TODO: this error message copy may need improvement
|
|
68
|
+
|
|
69
|
+
if (!exists) {
|
|
70
|
+
return 'The referenced document is not yet published. Fix this error by opening the referenced document and publishing it';
|
|
71
|
+
}
|
|
72
|
+
|
|
40
73
|
return true;
|
|
41
|
-
}
|
|
74
|
+
});
|
|
42
75
|
|
|
43
|
-
|
|
44
|
-
return
|
|
76
|
+
function reference(_x, _x2, _x3, _x4) {
|
|
77
|
+
return _reference.apply(this, arguments);
|
|
45
78
|
}
|
|
46
79
|
|
|
47
|
-
return
|
|
48
|
-
},
|
|
80
|
+
return reference;
|
|
81
|
+
}(),
|
|
49
82
|
block: function () {
|
|
50
83
|
var _block = _asyncToGenerator(function* (validateBlock, value, message, context) {
|
|
51
84
|
var result = yield validateBlock(value, context);
|
|
@@ -57,7 +90,7 @@ var objectValidators = _objectSpread(_objectSpread({}, _genericValidator.default
|
|
|
57
90
|
return result;
|
|
58
91
|
});
|
|
59
92
|
|
|
60
|
-
function block(
|
|
93
|
+
function block(_x5, _x6, _x7, _x8) {
|
|
61
94
|
return _block.apply(this, arguments);
|
|
62
95
|
}
|
|
63
96
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/validation",
|
|
3
|
-
"version": "2.21.
|
|
3
|
+
"version": "2.21.8-reference-updates.27+3ea3a7ef74",
|
|
4
4
|
"description": "Validation and warning infrastructure for Sanity projects",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./dist/dts",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"directory": "packages/@sanity/validation"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@sanity/client": "2.21.
|
|
36
|
-
"@sanity/schema": "2.21.
|
|
35
|
+
"@sanity/client": "2.21.8-reference-updates.27+3ea3a7ef74",
|
|
36
|
+
"@sanity/schema": "2.21.8-reference-updates.27+3ea3a7ef74"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"@sanity/client": "^2.0.0"
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@sanity/types": "2.21.
|
|
47
|
+
"@sanity/types": "2.21.8-reference-updates.27+3ea3a7ef74",
|
|
48
48
|
"date-fns": "^2.16.1",
|
|
49
49
|
"lodash": "^4.17.15"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "3ea3a7ef7426d4eb4e4a7a81ec5c15eef4384f8a"
|
|
52
52
|
}
|
|
@@ -165,6 +165,7 @@ describe('validateItem', () => {
|
|
|
165
165
|
path: [],
|
|
166
166
|
parent: undefined,
|
|
167
167
|
type: schema.get('testObj'),
|
|
168
|
+
getDocumentExists: undefined,
|
|
168
169
|
})
|
|
169
170
|
).resolves.toMatchObject([
|
|
170
171
|
{
|
|
@@ -235,6 +236,7 @@ describe('validateItem', () => {
|
|
|
235
236
|
path: undefined,
|
|
236
237
|
type: schema.get('testObj'),
|
|
237
238
|
value: {foo: 5},
|
|
239
|
+
getDocumentExists: undefined,
|
|
238
240
|
})
|
|
239
241
|
).resolves.toMatchObject([
|
|
240
242
|
{
|
|
@@ -285,6 +287,7 @@ describe('validateItem', () => {
|
|
|
285
287
|
path: [],
|
|
286
288
|
type: schema.get('values'),
|
|
287
289
|
value: values,
|
|
290
|
+
getDocumentExists: undefined,
|
|
288
291
|
})
|
|
289
292
|
).resolves.toEqual([
|
|
290
293
|
{
|
|
@@ -377,6 +380,7 @@ describe('validateItem', () => {
|
|
|
377
380
|
document,
|
|
378
381
|
parent: document,
|
|
379
382
|
path: undefined,
|
|
383
|
+
getDocumentExists: undefined,
|
|
380
384
|
})
|
|
381
385
|
).resolves.toMatchObject([
|
|
382
386
|
{
|
|
@@ -517,6 +521,7 @@ describe('validateItem', () => {
|
|
|
517
521
|
parent: undefined,
|
|
518
522
|
path: undefined,
|
|
519
523
|
type: schema.get('root'),
|
|
524
|
+
getDocumentExists: undefined,
|
|
520
525
|
})
|
|
521
526
|
|
|
522
527
|
// after `validateItem(...)` has been initiated, all of the custom
|
package/src/validateDocument.ts
CHANGED
|
@@ -45,7 +45,8 @@ export function resolveTypeForArrayItem(
|
|
|
45
45
|
|
|
46
46
|
export default async function validateDocument(
|
|
47
47
|
doc: SanityDocument,
|
|
48
|
-
schema: Schema
|
|
48
|
+
schema: Schema,
|
|
49
|
+
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
49
50
|
): Promise<ValidationMarker[]> {
|
|
50
51
|
const documentType = schema.get(doc._type)
|
|
51
52
|
if (!documentType) {
|
|
@@ -60,6 +61,7 @@ export default async function validateDocument(
|
|
|
60
61
|
path: [],
|
|
61
62
|
document: doc,
|
|
62
63
|
type: documentType,
|
|
64
|
+
getDocumentExists: context?.getDocumentExists,
|
|
63
65
|
})
|
|
64
66
|
} catch (err) {
|
|
65
67
|
console.error(err)
|
|
@@ -38,6 +38,7 @@ export const blockValidator: BlockValidator = async (value, context) => {
|
|
|
38
38
|
type: annotationType,
|
|
39
39
|
path: (context.path || []).concat(['markDefs', {_key: markDef._key}]),
|
|
40
40
|
parent: value,
|
|
41
|
+
getDocumentExists: context.getDocumentExists,
|
|
41
42
|
})
|
|
42
43
|
annotationValidations.push(validations)
|
|
43
44
|
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Validators} from '@sanity/types'
|
|
1
|
+
import {Validators, isReference} from '@sanity/types'
|
|
2
2
|
import genericValidator from './genericValidator'
|
|
3
3
|
|
|
4
4
|
const metaKeys = ['_key', '_type', '_weak']
|
|
@@ -20,15 +20,35 @@ const objectValidators: Validators = {
|
|
|
20
20
|
return true
|
|
21
21
|
},
|
|
22
22
|
|
|
23
|
-
reference: (_unused, value, message) => {
|
|
23
|
+
reference: async (_unused, value: unknown, message, context) => {
|
|
24
24
|
if (!value) {
|
|
25
25
|
return true
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
if (
|
|
28
|
+
if (!isReference(value)) {
|
|
29
29
|
return message || 'Must be a reference to a document'
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
const {type, getDocumentExists} = context
|
|
33
|
+
|
|
34
|
+
if (!type) {
|
|
35
|
+
throw new Error(`\`type\` was not provided in validation context`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if ('weak' in type && type.weak) {
|
|
39
|
+
return true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!getDocumentExists) {
|
|
43
|
+
throw new Error(`\`getDocumentExists\` was not provided in validation context`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const exists = await getDocumentExists({id: value._ref})
|
|
47
|
+
// TODO: this error message copy may need improvement
|
|
48
|
+
if (!exists) {
|
|
49
|
+
return 'The referenced document is not yet published. Fix this error by opening the referenced document and publishing it'
|
|
50
|
+
}
|
|
51
|
+
|
|
32
52
|
return true
|
|
33
53
|
},
|
|
34
54
|
|