@sanity/validation 2.21.7 → 2.21.8-reference-updates.26

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.
@@ -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,GACb,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA0B7B;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,K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,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,cAkD5B,CAAA"}
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,EAAC,MAAM,eAAe,CAAA;AAKxC,QAAA,MAAM,gBAAgB,EAAE,UA8CvB,CAAA;AAED,eAAe,gBAAgB,CAAA"}
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/index.js CHANGED
@@ -9,25 +9,25 @@ Object.defineProperty(exports, "Rule", {
9
9
  return _Rule.default;
10
10
  }
11
11
  });
12
- exports.default = void 0;
13
- Object.defineProperty(exports, "inferFromSchema", {
12
+ Object.defineProperty(exports, "validateDocument", {
14
13
  enumerable: true,
15
14
  get: function get() {
16
- return _inferFromSchema.default;
15
+ return _validateDocument.default;
17
16
  }
18
17
  });
19
- Object.defineProperty(exports, "inferFromSchemaType", {
18
+ Object.defineProperty(exports, "inferFromSchema", {
20
19
  enumerable: true,
21
20
  get: function get() {
22
- return _inferFromSchemaType.default;
21
+ return _inferFromSchema.default;
23
22
  }
24
23
  });
25
- Object.defineProperty(exports, "validateDocument", {
24
+ Object.defineProperty(exports, "inferFromSchemaType", {
26
25
  enumerable: true,
27
26
  get: function get() {
28
- return _validateDocument.default;
27
+ return _inferFromSchemaType.default;
29
28
  }
30
29
  });
30
+ exports.default = void 0;
31
31
 
32
32
  var _Rule = _interopRequireDefault(require("./Rule"));
33
33
 
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = convertToValidationMarker;
7
6
  exports.isNonNullable = isNonNullable;
7
+ exports.default = convertToValidationMarker;
8
8
 
9
9
  var _ValidationError = _interopRequireDefault(require("../ValidationError"));
10
10
 
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = validateDocument;
7
6
  exports.resolveTypeForArrayItem = resolveTypeForArrayItem;
7
+ exports.default = validateDocument;
8
8
  exports.validateItem = validateItem;
9
9
 
10
10
  var _uniqBy2 = _interopRequireDefault(require("lodash/uniqBy"));
@@ -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(_x3) {
120
+ function validateItem(_x4) {
120
121
  return _validateItem.apply(this, arguments);
121
122
  }
122
123
 
@@ -140,10 +141,7 @@ function _validateItem() {
140
141
  var nestedChecks = [];
141
142
  var selfIsRequired = rules.some(rule => rule.isRequired());
142
143
  var shouldRunNestedObjectValidation = // run nested validation for objects
143
- (type === null || type === void 0 ? void 0 : type.jsonType) === 'object' && ( // if the value is truthy
144
- !!value || // or
145
- // (the value is null or undefined) and the top-level value is required
146
- (value === null || value === undefined) && selfIsRequired);
144
+ (type === null || type === void 0 ? void 0 : type.jsonType) === 'object' && (!!value || (value === null || value === undefined) && selfIsRequired);
147
145
 
148
146
  if (shouldRunNestedObjectValidation) {
149
147
  var fieldTypes = type.fields.reduce((acc, field) => {
@@ -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
  });
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
7
6
  exports.isRecord = isRecord;
7
+ exports.default = void 0;
8
8
 
9
9
  var _format = _interopRequireDefault(require("date-fns/format"));
10
10
 
@@ -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: (_unused, value, message) => {
39
- if (!value) {
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
- if (typeof value._ref !== 'string') {
44
- return message || 'Must be a reference to a document';
76
+ function reference(_x, _x2, _x3, _x4) {
77
+ return _reference.apply(this, arguments);
45
78
  }
46
79
 
47
- return true;
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(_x, _x2, _x3, _x4) {
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.7",
3
+ "version": "2.21.8-reference-updates.26+af861a1c8c",
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.7",
36
- "@sanity/schema": "2.21.5"
35
+ "@sanity/client": "2.21.8-reference-updates.26+af861a1c8c",
36
+ "@sanity/schema": "2.21.8-reference-updates.26+af861a1c8c"
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.7",
47
+ "@sanity/types": "2.21.8-reference-updates.26+af861a1c8c",
48
48
  "date-fns": "^2.16.1",
49
49
  "lodash": "^4.17.15"
50
50
  },
51
- "gitHead": "682dc95b41e56841ab63d7f95643aa2534069fb9"
51
+ "gitHead": "af861a1c8c538e5dc1233ca69704232789ec802e"
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
@@ -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 (typeof value._ref !== 'string') {
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