@sanity/validation 2.22.3 → 2.22.6-field-groups.125
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/objectValidator.d.ts.map +1 -1
- package/lib/validateDocument.js +5 -4
- package/lib/validators/objectValidator.js +44 -7
- package/package.json +5 -5
- package/src/validateDocument.test.ts +6 -0
- package/src/validateDocument.ts +3 -1
- package/src/validators/objectValidator.ts +23 -4
|
@@ -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,EAMjB,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,EAMjB,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,CA4HnD"}
|
|
@@ -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,UAwDvB,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
|
|
|
@@ -5,10 +5,16 @@ 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 }; }
|
|
11
13
|
|
|
14
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
15
|
+
|
|
16
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
17
|
+
|
|
12
18
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
13
19
|
|
|
14
20
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
@@ -31,17 +37,48 @@ var objectValidators = _objectSpread(_objectSpread({}, _genericValidator.default
|
|
|
31
37
|
|
|
32
38
|
return true;
|
|
33
39
|
},
|
|
34
|
-
reference: (
|
|
35
|
-
|
|
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 || true;
|
|
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
|
+
});
|
|
68
|
+
|
|
69
|
+
if (!exists) {
|
|
70
|
+
return 'This reference must be published';
|
|
71
|
+
}
|
|
72
|
+
|
|
36
73
|
return true;
|
|
37
|
-
}
|
|
74
|
+
});
|
|
38
75
|
|
|
39
|
-
|
|
40
|
-
return
|
|
76
|
+
function reference(_x, _x2, _x3, _x4) {
|
|
77
|
+
return _reference.apply(this, arguments);
|
|
41
78
|
}
|
|
42
79
|
|
|
43
|
-
return
|
|
44
|
-
},
|
|
80
|
+
return reference;
|
|
81
|
+
}(),
|
|
45
82
|
assetRequired: (flag, value, message) => {
|
|
46
83
|
if (!value || !value.asset || !value.asset._ref) {
|
|
47
84
|
var assetType = flag.assetType || 'Asset';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/validation",
|
|
3
|
-
"version": "2.22.
|
|
3
|
+
"version": "2.22.6-field-groups.125+fee9fc17c",
|
|
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.22.
|
|
36
|
-
"@sanity/schema": "2.22.
|
|
35
|
+
"@sanity/client": "2.22.6-field-groups.125+fee9fc17c",
|
|
36
|
+
"@sanity/schema": "2.22.6-field-groups.125+fee9fc17c"
|
|
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.22.
|
|
47
|
+
"@sanity/types": "2.22.6-field-groups.125+fee9fc17c",
|
|
48
48
|
"date-fns": "^2.16.1",
|
|
49
49
|
"lodash": "^4.17.15"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "fee9fc17c075eec38fed4e9b56cf158ef1daff84"
|
|
52
52
|
}
|
|
@@ -168,6 +168,7 @@ describe('validateItem', () => {
|
|
|
168
168
|
path: [],
|
|
169
169
|
parent: undefined,
|
|
170
170
|
type: schema.get('testObj'),
|
|
171
|
+
getDocumentExists: undefined,
|
|
171
172
|
})
|
|
172
173
|
).resolves.toMatchObject([
|
|
173
174
|
{
|
|
@@ -241,6 +242,7 @@ describe('validateItem', () => {
|
|
|
241
242
|
path: undefined,
|
|
242
243
|
type: schema.get('testObj'),
|
|
243
244
|
value: {foo: 5},
|
|
245
|
+
getDocumentExists: undefined,
|
|
244
246
|
})
|
|
245
247
|
).resolves.toMatchObject([
|
|
246
248
|
{
|
|
@@ -406,6 +408,7 @@ describe('validateItem', () => {
|
|
|
406
408
|
path: [],
|
|
407
409
|
type: schema.get('blockTest'),
|
|
408
410
|
value: document,
|
|
411
|
+
getDocumentExists: undefined,
|
|
409
412
|
})
|
|
410
413
|
|
|
411
414
|
expect(result).toMatchObject([
|
|
@@ -508,6 +511,7 @@ describe('validateItem', () => {
|
|
|
508
511
|
path: [],
|
|
509
512
|
type: schema.get('values'),
|
|
510
513
|
value: values,
|
|
514
|
+
getDocumentExists: undefined,
|
|
511
515
|
})
|
|
512
516
|
).resolves.toEqual([
|
|
513
517
|
{
|
|
@@ -605,6 +609,7 @@ describe('validateItem', () => {
|
|
|
605
609
|
document,
|
|
606
610
|
parent: document,
|
|
607
611
|
path: undefined,
|
|
612
|
+
getDocumentExists: undefined,
|
|
608
613
|
})
|
|
609
614
|
).resolves.toMatchObject([
|
|
610
615
|
{
|
|
@@ -745,6 +750,7 @@ describe('validateItem', () => {
|
|
|
745
750
|
parent: undefined,
|
|
746
751
|
path: undefined,
|
|
747
752
|
type: schema.get('root'),
|
|
753
|
+
getDocumentExists: undefined,
|
|
748
754
|
})
|
|
749
755
|
|
|
750
756
|
// after `validateItem(...)` has been initiated, all of the custom
|
package/src/validateDocument.ts
CHANGED
|
@@ -48,7 +48,8 @@ export function resolveTypeForArrayItem(
|
|
|
48
48
|
|
|
49
49
|
export default async function validateDocument(
|
|
50
50
|
doc: SanityDocument,
|
|
51
|
-
schema: Schema
|
|
51
|
+
schema: Schema,
|
|
52
|
+
context?: Pick<ValidationContext, 'getDocumentExists'>
|
|
52
53
|
): Promise<ValidationMarker[]> {
|
|
53
54
|
const documentType = schema.get(doc._type)
|
|
54
55
|
if (!documentType) {
|
|
@@ -63,6 +64,7 @@ export default async function validateDocument(
|
|
|
63
64
|
path: [],
|
|
64
65
|
document: doc,
|
|
65
66
|
type: documentType,
|
|
67
|
+
getDocumentExists: context?.getDocumentExists,
|
|
66
68
|
})
|
|
67
69
|
} catch (err) {
|
|
68
70
|
console.error(err)
|
|
@@ -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,13 +20,32 @@ 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 (
|
|
29
|
-
return message ||
|
|
28
|
+
if (!isReference(value)) {
|
|
29
|
+
return message || true
|
|
30
|
+
}
|
|
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
|
+
if (!exists) {
|
|
48
|
+
return 'This reference must be published'
|
|
30
49
|
}
|
|
31
50
|
|
|
32
51
|
return true
|