@sanity/validation 2.34.2-empty-template-cli.0 → 2.34.3-cdr-preview.7
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/Rule.js +23 -100
- package/lib/ValidationError.js +0 -9
- package/lib/getClient.js +0 -3
- package/lib/index.js +0 -8
- package/lib/inferFromSchema.js +0 -4
- package/lib/inferFromSchemaType.js +8 -20
- package/lib/util/convertToValidationMarker.js +8 -17
- package/lib/util/deepEquals.js +1 -18
- package/lib/util/escapeRegex.js +1 -4
- package/lib/util/normalizeValidationRules.js +11 -29
- package/lib/util/pathToString.js +0 -6
- package/lib/util/requestIdleCallback.js +0 -5
- package/lib/util/typeString.js +2 -5
- package/lib/validateDocument.js +30 -69
- package/lib/validators/arrayValidator.js +0 -27
- package/lib/validators/booleanValidator.js +0 -8
- package/lib/validators/dateValidator.js +8 -28
- package/lib/validators/genericValidator.js +2 -27
- package/lib/validators/numberValidator.js +0 -15
- package/lib/validators/objectValidator.js +1 -26
- package/lib/validators/slugValidator.js +5 -36
- package/lib/validators/stringValidator.js +3 -31
- package/package.json +4 -4
package/lib/Rule.js
CHANGED
|
@@ -4,43 +4,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _get2 = _interopRequireDefault(require("lodash/get"));
|
|
9
|
-
|
|
10
8
|
var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep"));
|
|
11
|
-
|
|
12
9
|
var _ValidationError = _interopRequireDefault(require("./ValidationError"));
|
|
13
|
-
|
|
14
10
|
var _escapeRegex = _interopRequireDefault(require("./util/escapeRegex"));
|
|
15
|
-
|
|
16
11
|
var _convertToValidationMarker = _interopRequireDefault(require("./util/convertToValidationMarker"));
|
|
17
|
-
|
|
18
12
|
var _pathToString = _interopRequireDefault(require("./util/pathToString"));
|
|
19
|
-
|
|
20
13
|
var _genericValidator = _interopRequireDefault(require("./validators/genericValidator"));
|
|
21
|
-
|
|
22
14
|
var _booleanValidator = _interopRequireDefault(require("./validators/booleanValidator"));
|
|
23
|
-
|
|
24
15
|
var _numberValidator = _interopRequireDefault(require("./validators/numberValidator"));
|
|
25
|
-
|
|
26
16
|
var _stringValidator = _interopRequireDefault(require("./validators/stringValidator"));
|
|
27
|
-
|
|
28
17
|
var _arrayValidator = _interopRequireDefault(require("./validators/arrayValidator"));
|
|
29
|
-
|
|
30
18
|
var _objectValidator = _interopRequireDefault(require("./validators/objectValidator"));
|
|
31
|
-
|
|
32
19
|
var _dateValidator = _interopRequireDefault(require("./validators/dateValidator"));
|
|
33
|
-
|
|
34
20
|
var _class;
|
|
35
|
-
|
|
36
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
37
|
-
|
|
38
22
|
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); } }
|
|
39
|
-
|
|
40
23
|
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); }); }; }
|
|
41
|
-
|
|
42
24
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
43
|
-
|
|
44
25
|
var typeValidators = {
|
|
45
26
|
Boolean: _booleanValidator.default,
|
|
46
27
|
Number: _numberValidator.default,
|
|
@@ -49,19 +30,18 @@ var typeValidators = {
|
|
|
49
30
|
Object: _objectValidator.default,
|
|
50
31
|
Date: _dateValidator.default
|
|
51
32
|
};
|
|
52
|
-
|
|
53
33
|
var getBaseType = type => {
|
|
54
34
|
return type && type.type ? getBaseType(type.type) : type;
|
|
55
35
|
};
|
|
56
|
-
|
|
57
36
|
var isFieldRef = constraint => {
|
|
58
37
|
if (typeof constraint !== 'object' || !constraint) return false;
|
|
59
38
|
return constraint.type === Rule.FIELD_REF;
|
|
60
39
|
};
|
|
61
|
-
|
|
62
40
|
var EMPTY_ARRAY = [];
|
|
63
41
|
var FIELD_REF = Symbol('FIELD_REF');
|
|
64
|
-
var ruleConstraintTypes = ['Array', 'Boolean', 'Date', 'Number', 'Object', 'String'];
|
|
42
|
+
var ruleConstraintTypes = ['Array', 'Boolean', 'Date', 'Number', 'Object', 'String'];
|
|
43
|
+
|
|
44
|
+
// Note: `RuleClass` and `Rule` are split to fit the current `@sanity/types`
|
|
65
45
|
// setup. Classes are a bit weird in the `@sanity/types` package because classes
|
|
66
46
|
// create an actual javascript class while simultaneously creating a type
|
|
67
47
|
// definition.
|
|
@@ -76,35 +56,26 @@ var ruleConstraintTypes = ['Array', 'Boolean', 'Date', 'Number', 'Object', 'Stri
|
|
|
76
56
|
// This package exports the RuleClass as a value without implicitly exporting
|
|
77
57
|
// an instance definition. This should help reminder downstream users to import
|
|
78
58
|
// from the `@sanity/types` package.
|
|
79
|
-
|
|
80
59
|
var Rule = (_class = class Rule {
|
|
81
60
|
constructor(typeDef) {
|
|
82
61
|
_defineProperty(this, "_type", undefined);
|
|
83
|
-
|
|
84
62
|
_defineProperty(this, "_level", undefined);
|
|
85
|
-
|
|
86
63
|
_defineProperty(this, "_required", undefined);
|
|
87
|
-
|
|
88
64
|
_defineProperty(this, "_typeDef", undefined);
|
|
89
|
-
|
|
90
65
|
_defineProperty(this, "_message", undefined);
|
|
91
|
-
|
|
92
66
|
_defineProperty(this, "_rules", []);
|
|
93
|
-
|
|
94
67
|
_defineProperty(this, "_fieldRules", undefined);
|
|
95
|
-
|
|
96
68
|
_defineProperty(this, "valueOfField", Rule.valueOfField.bind(Rule));
|
|
97
|
-
|
|
98
69
|
this._typeDef = typeDef;
|
|
99
70
|
this.reset();
|
|
100
71
|
}
|
|
101
|
-
|
|
102
72
|
_mergeRequired(next) {
|
|
103
73
|
if (this._required === 'required' || next._required === 'required') return 'required';
|
|
104
74
|
if (this._required === 'optional' || next._required === 'optional') return 'optional';
|
|
105
75
|
return undefined;
|
|
106
|
-
}
|
|
76
|
+
}
|
|
107
77
|
|
|
78
|
+
// Alias to static method, since we often have access to an _instance_ of a rule but not the actual Rule class
|
|
108
79
|
|
|
109
80
|
error(message) {
|
|
110
81
|
var rule = this.clone();
|
|
@@ -112,21 +83,18 @@ var Rule = (_class = class Rule {
|
|
|
112
83
|
rule._message = message || undefined;
|
|
113
84
|
return rule;
|
|
114
85
|
}
|
|
115
|
-
|
|
116
86
|
warning(message) {
|
|
117
87
|
var rule = this.clone();
|
|
118
88
|
rule._level = 'warning';
|
|
119
89
|
rule._message = message || undefined;
|
|
120
90
|
return rule;
|
|
121
91
|
}
|
|
122
|
-
|
|
123
92
|
info(message) {
|
|
124
93
|
var rule = this.clone();
|
|
125
94
|
rule._level = 'info';
|
|
126
95
|
rule._message = message || undefined;
|
|
127
96
|
return rule;
|
|
128
97
|
}
|
|
129
|
-
|
|
130
98
|
reset() {
|
|
131
99
|
this._type = this._type || undefined;
|
|
132
100
|
this._rules = (this._rules || []).filter(rule => rule.flag === 'type');
|
|
@@ -136,11 +104,9 @@ var Rule = (_class = class Rule {
|
|
|
136
104
|
this._fieldRules = undefined;
|
|
137
105
|
return this;
|
|
138
106
|
}
|
|
139
|
-
|
|
140
107
|
isRequired() {
|
|
141
108
|
return this._required === 'required';
|
|
142
109
|
}
|
|
143
|
-
|
|
144
110
|
clone() {
|
|
145
111
|
var rule = new Rule();
|
|
146
112
|
rule._type = this._type;
|
|
@@ -152,7 +118,6 @@ var Rule = (_class = class Rule {
|
|
|
152
118
|
rule._typeDef = this._typeDef;
|
|
153
119
|
return rule;
|
|
154
120
|
}
|
|
155
|
-
|
|
156
121
|
cloneWithRules(rules) {
|
|
157
122
|
var rule = this.clone();
|
|
158
123
|
var newRules = new Set();
|
|
@@ -160,7 +125,6 @@ var Rule = (_class = class Rule {
|
|
|
160
125
|
if (curr.flag === 'type') {
|
|
161
126
|
rule._type = curr.constraint;
|
|
162
127
|
}
|
|
163
|
-
|
|
164
128
|
newRules.add(curr.flag);
|
|
165
129
|
});
|
|
166
130
|
rule._rules = rule._rules.filter(curr => {
|
|
@@ -170,28 +134,24 @@ var Rule = (_class = class Rule {
|
|
|
170
134
|
}).concat(rules);
|
|
171
135
|
return rule;
|
|
172
136
|
}
|
|
173
|
-
|
|
174
137
|
merge(rule) {
|
|
175
138
|
if (this._type && rule._type && this._type !== rule._type) {
|
|
176
139
|
throw new Error('merge() failed: conflicting types');
|
|
177
140
|
}
|
|
178
|
-
|
|
179
141
|
var newRule = this.cloneWithRules(rule._rules);
|
|
180
142
|
newRule._type = this._type || rule._type;
|
|
181
143
|
newRule._message = this._message || rule._message;
|
|
182
144
|
newRule._required = this._mergeRequired(rule);
|
|
183
145
|
newRule._level = this._level === 'error' ? rule._level : this._level;
|
|
184
146
|
return newRule;
|
|
185
|
-
}
|
|
186
|
-
|
|
147
|
+
}
|
|
187
148
|
|
|
149
|
+
// Validation flag setters
|
|
188
150
|
type(targetType) {
|
|
189
151
|
var type = "".concat(targetType.slice(0, 1).toUpperCase()).concat(targetType.slice(1));
|
|
190
|
-
|
|
191
152
|
if (!ruleConstraintTypes.includes(type)) {
|
|
192
153
|
throw new Error("Unknown type \"".concat(targetType, "\""));
|
|
193
154
|
}
|
|
194
|
-
|
|
195
155
|
var rule = this.cloneWithRules([{
|
|
196
156
|
flag: 'type',
|
|
197
157
|
constraint: type
|
|
@@ -199,22 +159,20 @@ var Rule = (_class = class Rule {
|
|
|
199
159
|
rule._type = type;
|
|
200
160
|
return rule;
|
|
201
161
|
}
|
|
202
|
-
|
|
203
162
|
all(children) {
|
|
204
163
|
return this.cloneWithRules([{
|
|
205
164
|
flag: 'all',
|
|
206
165
|
constraint: children
|
|
207
166
|
}]);
|
|
208
167
|
}
|
|
209
|
-
|
|
210
168
|
either(children) {
|
|
211
169
|
return this.cloneWithRules([{
|
|
212
170
|
flag: 'either',
|
|
213
171
|
constraint: children
|
|
214
172
|
}]);
|
|
215
|
-
}
|
|
216
|
-
|
|
173
|
+
}
|
|
217
174
|
|
|
175
|
+
// Shared rules
|
|
218
176
|
optional() {
|
|
219
177
|
var rule = this.cloneWithRules([{
|
|
220
178
|
flag: 'presence',
|
|
@@ -223,7 +181,6 @@ var Rule = (_class = class Rule {
|
|
|
223
181
|
rule._required = 'optional';
|
|
224
182
|
return rule;
|
|
225
183
|
}
|
|
226
|
-
|
|
227
184
|
required() {
|
|
228
185
|
var rule = this.cloneWithRules([{
|
|
229
186
|
flag: 'presence',
|
|
@@ -232,114 +189,100 @@ var Rule = (_class = class Rule {
|
|
|
232
189
|
rule._required = 'required';
|
|
233
190
|
return rule;
|
|
234
191
|
}
|
|
235
|
-
|
|
236
192
|
custom(fn) {
|
|
237
193
|
return this.cloneWithRules([{
|
|
238
194
|
flag: 'custom',
|
|
239
195
|
constraint: fn
|
|
240
196
|
}]);
|
|
241
197
|
}
|
|
198
|
+
|
|
242
199
|
/**
|
|
243
200
|
* @deprecated use `Rule.custom` instead
|
|
244
201
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
202
|
block(fn) {
|
|
248
203
|
return this.cloneWithRules([{
|
|
249
204
|
flag: 'custom',
|
|
250
205
|
constraint: fn
|
|
251
206
|
}]);
|
|
252
207
|
}
|
|
253
|
-
|
|
254
208
|
min(len) {
|
|
255
209
|
return this.cloneWithRules([{
|
|
256
210
|
flag: 'min',
|
|
257
211
|
constraint: len
|
|
258
212
|
}]);
|
|
259
213
|
}
|
|
260
|
-
|
|
261
214
|
max(len) {
|
|
262
215
|
return this.cloneWithRules([{
|
|
263
216
|
flag: 'max',
|
|
264
217
|
constraint: len
|
|
265
218
|
}]);
|
|
266
219
|
}
|
|
267
|
-
|
|
268
220
|
length(len) {
|
|
269
221
|
return this.cloneWithRules([{
|
|
270
222
|
flag: 'length',
|
|
271
223
|
constraint: len
|
|
272
224
|
}]);
|
|
273
225
|
}
|
|
274
|
-
|
|
275
226
|
valid(value) {
|
|
276
227
|
var values = Array.isArray(value) ? value : [value];
|
|
277
228
|
return this.cloneWithRules([{
|
|
278
229
|
flag: 'valid',
|
|
279
230
|
constraint: values
|
|
280
231
|
}]);
|
|
281
|
-
}
|
|
282
|
-
|
|
232
|
+
}
|
|
283
233
|
|
|
234
|
+
// Numbers only
|
|
284
235
|
integer() {
|
|
285
236
|
return this.cloneWithRules([{
|
|
286
237
|
flag: 'integer'
|
|
287
238
|
}]);
|
|
288
239
|
}
|
|
289
|
-
|
|
290
240
|
precision(limit) {
|
|
291
241
|
return this.cloneWithRules([{
|
|
292
242
|
flag: 'precision',
|
|
293
243
|
constraint: limit
|
|
294
244
|
}]);
|
|
295
245
|
}
|
|
296
|
-
|
|
297
246
|
positive() {
|
|
298
247
|
return this.cloneWithRules([{
|
|
299
248
|
flag: 'min',
|
|
300
249
|
constraint: 0
|
|
301
250
|
}]);
|
|
302
251
|
}
|
|
303
|
-
|
|
304
252
|
negative() {
|
|
305
253
|
return this.cloneWithRules([{
|
|
306
254
|
flag: 'lessThan',
|
|
307
255
|
constraint: 0
|
|
308
256
|
}]);
|
|
309
257
|
}
|
|
310
|
-
|
|
311
258
|
greaterThan(num) {
|
|
312
259
|
return this.cloneWithRules([{
|
|
313
260
|
flag: 'greaterThan',
|
|
314
261
|
constraint: num
|
|
315
262
|
}]);
|
|
316
263
|
}
|
|
317
|
-
|
|
318
264
|
lessThan(num) {
|
|
319
265
|
return this.cloneWithRules([{
|
|
320
266
|
flag: 'lessThan',
|
|
321
267
|
constraint: num
|
|
322
268
|
}]);
|
|
323
|
-
}
|
|
324
|
-
|
|
269
|
+
}
|
|
325
270
|
|
|
271
|
+
// String only
|
|
326
272
|
uppercase() {
|
|
327
273
|
return this.cloneWithRules([{
|
|
328
274
|
flag: 'stringCasing',
|
|
329
275
|
constraint: 'uppercase'
|
|
330
276
|
}]);
|
|
331
277
|
}
|
|
332
|
-
|
|
333
278
|
lowercase() {
|
|
334
279
|
return this.cloneWithRules([{
|
|
335
280
|
flag: 'stringCasing',
|
|
336
281
|
constraint: 'lowercase'
|
|
337
282
|
}]);
|
|
338
283
|
}
|
|
339
|
-
|
|
340
284
|
regex(pattern, a, b) {
|
|
341
285
|
var _a$name, _a$invert;
|
|
342
|
-
|
|
343
286
|
var name = typeof a === 'string' ? a : (_a$name = a === null || a === void 0 ? void 0 : a.name) !== null && _a$name !== void 0 ? _a$name : b === null || b === void 0 ? void 0 : b.name;
|
|
344
287
|
var invert = typeof a === 'string' ? false : (_a$invert = a === null || a === void 0 ? void 0 : a.invert) !== null && _a$invert !== void 0 ? _a$invert : b === null || b === void 0 ? void 0 : b.invert;
|
|
345
288
|
var constraint = {
|
|
@@ -352,28 +295,23 @@ var Rule = (_class = class Rule {
|
|
|
352
295
|
constraint
|
|
353
296
|
}]);
|
|
354
297
|
}
|
|
355
|
-
|
|
356
298
|
email() {
|
|
357
299
|
return this.cloneWithRules([{
|
|
358
300
|
flag: 'email'
|
|
359
301
|
}]);
|
|
360
302
|
}
|
|
361
|
-
|
|
362
303
|
uri(opts) {
|
|
363
304
|
var optsScheme = (opts === null || opts === void 0 ? void 0 : opts.scheme) || ['http', 'https'];
|
|
364
305
|
var schemes = Array.isArray(optsScheme) ? optsScheme : [optsScheme];
|
|
365
|
-
|
|
366
306
|
if (!schemes.length) {
|
|
367
307
|
throw new Error('scheme must have at least 1 scheme specified');
|
|
368
308
|
}
|
|
369
|
-
|
|
370
309
|
var constraint = {
|
|
371
310
|
options: {
|
|
372
311
|
scheme: schemes.map(scheme => {
|
|
373
312
|
if (!(scheme instanceof RegExp) && typeof scheme !== 'string') {
|
|
374
313
|
throw new Error('scheme must be a RegExp or a String');
|
|
375
314
|
}
|
|
376
|
-
|
|
377
315
|
return typeof scheme === 'string' ? new RegExp("^".concat((0, _escapeRegex.default)(scheme), "$")) : scheme;
|
|
378
316
|
}),
|
|
379
317
|
allowRelative: (opts === null || opts === void 0 ? void 0 : opts.allowRelative) || false,
|
|
@@ -385,42 +323,37 @@ var Rule = (_class = class Rule {
|
|
|
385
323
|
flag: 'uri',
|
|
386
324
|
constraint
|
|
387
325
|
}]);
|
|
388
|
-
}
|
|
389
|
-
|
|
326
|
+
}
|
|
390
327
|
|
|
328
|
+
// Array only
|
|
391
329
|
unique() {
|
|
392
330
|
return this.cloneWithRules([{
|
|
393
331
|
flag: 'unique'
|
|
394
332
|
}]);
|
|
395
|
-
}
|
|
396
|
-
|
|
333
|
+
}
|
|
397
334
|
|
|
335
|
+
// Objects only
|
|
398
336
|
reference() {
|
|
399
337
|
return this.cloneWithRules([{
|
|
400
338
|
flag: 'reference'
|
|
401
339
|
}]);
|
|
402
340
|
}
|
|
403
|
-
|
|
404
341
|
fields(rules) {
|
|
405
342
|
if (this._type !== 'Object') {
|
|
406
343
|
throw new Error('fields() can only be called on an object type');
|
|
407
344
|
}
|
|
408
|
-
|
|
409
345
|
var rule = this.cloneWithRules([]);
|
|
410
346
|
rule._fieldRules = rules;
|
|
411
347
|
return rule;
|
|
412
348
|
}
|
|
413
|
-
|
|
414
349
|
assetRequired() {
|
|
415
350
|
var base = getBaseType(this._typeDef);
|
|
416
351
|
var assetType;
|
|
417
|
-
|
|
418
352
|
if (base && ['image', 'file'].includes(base.name)) {
|
|
419
353
|
assetType = base.name === 'image' ? 'Image' : 'File';
|
|
420
354
|
} else {
|
|
421
355
|
assetType = 'Asset';
|
|
422
356
|
}
|
|
423
|
-
|
|
424
357
|
return this.cloneWithRules([{
|
|
425
358
|
flag: 'assetRequired',
|
|
426
359
|
constraint: {
|
|
@@ -428,20 +361,19 @@ var Rule = (_class = class Rule {
|
|
|
428
361
|
}
|
|
429
362
|
}]);
|
|
430
363
|
}
|
|
431
|
-
|
|
432
364
|
validate(value) {
|
|
433
365
|
var _arguments = arguments,
|
|
434
|
-
|
|
435
|
-
|
|
366
|
+
_this = this;
|
|
436
367
|
return _asyncToGenerator(function* () {
|
|
437
368
|
var context = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : {};
|
|
438
|
-
var valueIsEmpty = value === null || value === undefined;
|
|
369
|
+
var valueIsEmpty = value === null || value === undefined;
|
|
439
370
|
|
|
371
|
+
// Short-circuit on optional, empty fields
|
|
440
372
|
if (valueIsEmpty && _this._required === 'optional') {
|
|
441
373
|
return EMPTY_ARRAY;
|
|
442
374
|
}
|
|
443
|
-
|
|
444
|
-
|
|
375
|
+
var rules =
|
|
376
|
+
// Run only the _custom_ functions if the rule is not set to required or optional
|
|
445
377
|
_this._required === undefined && valueIsEmpty ? _this._rules.filter(curr => curr.flag === 'custom') : _this._rules;
|
|
446
378
|
var validators = _this._type && typeValidators[_this._type] || _genericValidator.default;
|
|
447
379
|
var results = yield Promise.all(rules.map( /*#__PURE__*/function () {
|
|
@@ -449,32 +381,24 @@ var Rule = (_class = class Rule {
|
|
|
449
381
|
if (curr.flag === undefined) {
|
|
450
382
|
throw new Error('Invalid rule, did not contain "flag"-property');
|
|
451
383
|
}
|
|
452
|
-
|
|
453
384
|
var validator = validators[curr.flag];
|
|
454
|
-
|
|
455
385
|
if (!validator) {
|
|
456
386
|
var forType = _this._type ? "type \"".concat(_this._type, "\"") : 'rule without declared type';
|
|
457
387
|
throw new Error("Validator for flag \"".concat(curr.flag, "\" not found for ").concat(forType));
|
|
458
388
|
}
|
|
459
|
-
|
|
460
389
|
var specConstraint = 'constraint' in curr ? curr.constraint : null;
|
|
461
|
-
|
|
462
390
|
if (isFieldRef(specConstraint)) {
|
|
463
391
|
specConstraint = (0, _get2.default)(context.parent, specConstraint.path);
|
|
464
392
|
}
|
|
465
|
-
|
|
466
393
|
var result;
|
|
467
|
-
|
|
468
394
|
try {
|
|
469
395
|
result = yield validator(specConstraint, value, _this._message, context);
|
|
470
396
|
} catch (err) {
|
|
471
397
|
var errorFromException = new _ValidationError.default("".concat((0, _pathToString.default)(context.path), ": Exception occurred while validating value: ").concat(err.message));
|
|
472
398
|
return (0, _convertToValidationMarker.default)(errorFromException, 'error', context);
|
|
473
399
|
}
|
|
474
|
-
|
|
475
400
|
return (0, _convertToValidationMarker.default)(result, _this._level, context);
|
|
476
401
|
});
|
|
477
|
-
|
|
478
402
|
return function (_x) {
|
|
479
403
|
return _ref.apply(this, arguments);
|
|
480
404
|
};
|
|
@@ -482,7 +406,6 @@ var Rule = (_class = class Rule {
|
|
|
482
406
|
return results.flat();
|
|
483
407
|
})();
|
|
484
408
|
}
|
|
485
|
-
|
|
486
409
|
}, _defineProperty(_class, "FIELD_REF", FIELD_REF), _defineProperty(_class, "array", def => new _class(def).type('Array')), _defineProperty(_class, "object", def => new _class(def).type('Object')), _defineProperty(_class, "string", def => new _class(def).type('String')), _defineProperty(_class, "number", def => new _class(def).type('Number')), _defineProperty(_class, "boolean", def => new _class(def).type('Boolean')), _defineProperty(_class, "dateTime", def => new _class(def).type('Date')), _defineProperty(_class, "valueOfField", path => ({
|
|
487
410
|
type: FIELD_REF,
|
|
488
411
|
path
|
package/lib/ValidationError.js
CHANGED
|
@@ -4,28 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
9
|
-
|
|
10
8
|
// Follows the same pattern as Rule and RuleClass. @see Rule
|
|
11
9
|
var ValidationError = class ValidationError {
|
|
12
10
|
constructor(message) {
|
|
13
11
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
14
|
-
|
|
15
12
|
_defineProperty(this, "message", void 0);
|
|
16
|
-
|
|
17
13
|
_defineProperty(this, "paths", void 0);
|
|
18
|
-
|
|
19
14
|
_defineProperty(this, "children", void 0);
|
|
20
|
-
|
|
21
15
|
_defineProperty(this, "operation", void 0);
|
|
22
|
-
|
|
23
16
|
this.message = message;
|
|
24
17
|
this.paths = options.paths || [];
|
|
25
18
|
this.children = options.children;
|
|
26
19
|
this.operation = options.operation;
|
|
27
20
|
}
|
|
28
|
-
|
|
29
21
|
cloneWithMessage(msg) {
|
|
30
22
|
return new ValidationError(msg, {
|
|
31
23
|
paths: this.paths,
|
|
@@ -33,7 +25,6 @@ var ValidationError = class ValidationError {
|
|
|
33
25
|
operation: this.operation
|
|
34
26
|
});
|
|
35
27
|
}
|
|
36
|
-
|
|
37
28
|
};
|
|
38
29
|
var _default = ValidationError;
|
|
39
30
|
exports.default = _default;
|
package/lib/getClient.js
CHANGED
|
@@ -5,16 +5,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = getClient;
|
|
7
7
|
var client;
|
|
8
|
-
|
|
9
8
|
function getClient() {
|
|
10
9
|
if (!client) {
|
|
11
10
|
// Lazy-loading to not cause issues when loading schema in node environment
|
|
12
11
|
var sanityClient = require('part:@sanity/base/client');
|
|
13
|
-
|
|
14
12
|
client = sanityClient.withConfig({
|
|
15
13
|
apiVersion: '1'
|
|
16
14
|
});
|
|
17
15
|
}
|
|
18
|
-
|
|
19
16
|
return client;
|
|
20
17
|
}
|
package/lib/index.js
CHANGED
|
@@ -34,21 +34,13 @@ Object.defineProperty(exports, "validateDocumentObservable", {
|
|
|
34
34
|
return _validateDocument.validateDocumentObservable;
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
|
-
|
|
38
37
|
var _Rule = _interopRequireDefault(require("./Rule"));
|
|
39
|
-
|
|
40
38
|
var _validateDocument = _interopRequireWildcard(require("./validateDocument"));
|
|
41
|
-
|
|
42
39
|
var _inferFromSchema = _interopRequireDefault(require("./inferFromSchema"));
|
|
43
|
-
|
|
44
40
|
var _inferFromSchemaType = _interopRequireDefault(require("./inferFromSchemaType"));
|
|
45
|
-
|
|
46
41
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
47
|
-
|
|
48
42
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
49
|
-
|
|
50
43
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
51
|
-
|
|
52
44
|
var _default = {
|
|
53
45
|
Rule: _Rule.default,
|
|
54
46
|
validateDocument: _validateDocument.default,
|
package/lib/inferFromSchema.js
CHANGED
|
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _inferFromSchemaType = _interopRequireDefault(require("./inferFromSchemaType"));
|
|
9
|
-
|
|
10
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
9
|
// Note: Mutates schema. Refactor when @sanity/schema supports middlewares
|
|
13
10
|
function inferFromSchema(schema) {
|
|
14
11
|
var typeNames = schema.getTypeNames();
|
|
@@ -17,6 +14,5 @@ function inferFromSchema(schema) {
|
|
|
17
14
|
});
|
|
18
15
|
return schema;
|
|
19
16
|
}
|
|
20
|
-
|
|
21
17
|
var _default = inferFromSchema;
|
|
22
18
|
exports.default = _default;
|
|
@@ -4,29 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _normalizeValidationRules = _interopRequireDefault(require("./util/normalizeValidationRules"));
|
|
9
|
-
|
|
10
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
9
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
13
|
-
|
|
14
10
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
15
|
-
|
|
16
11
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
17
|
-
|
|
18
12
|
function traverse(typeDef, visited) {
|
|
19
13
|
if (visited.has(typeDef)) {
|
|
20
14
|
return;
|
|
21
15
|
}
|
|
22
|
-
|
|
23
16
|
visited.add(typeDef);
|
|
24
17
|
typeDef.validation = (0, _normalizeValidationRules.default)(typeDef);
|
|
25
|
-
|
|
26
18
|
if ('fields' in typeDef) {
|
|
27
19
|
var _iterator = _createForOfIteratorHelper(typeDef.fields),
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
_step;
|
|
30
21
|
try {
|
|
31
22
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
32
23
|
var field = _step.value;
|
|
@@ -38,11 +29,9 @@ function traverse(typeDef, visited) {
|
|
|
38
29
|
_iterator.f();
|
|
39
30
|
}
|
|
40
31
|
}
|
|
41
|
-
|
|
42
32
|
if ('of' in typeDef) {
|
|
43
33
|
var _iterator2 = _createForOfIteratorHelper(typeDef.of),
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
_step2;
|
|
46
35
|
try {
|
|
47
36
|
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
48
37
|
var candidate = _step2.value;
|
|
@@ -53,16 +42,15 @@ function traverse(typeDef, visited) {
|
|
|
53
42
|
} finally {
|
|
54
43
|
_iterator2.f();
|
|
55
44
|
}
|
|
56
|
-
}
|
|
57
|
-
// @ts-expect-error TODO (eventually): `annotations` does not exist on the SchemaType yet
|
|
58
|
-
|
|
45
|
+
}
|
|
59
46
|
|
|
47
|
+
// eslint-disable-next-line no-warning-comments
|
|
48
|
+
// @ts-expect-error TODO (eventually): `annotations` does not exist on the SchemaType yet
|
|
60
49
|
if (typeDef.annotations) {
|
|
61
50
|
// eslint-disable-next-line no-warning-comments
|
|
62
51
|
// @ts-expect-error TODO (eventually): `annotations` does not exist on the SchemaType yet
|
|
63
52
|
var _iterator3 = _createForOfIteratorHelper(typeDef.annotations),
|
|
64
|
-
|
|
65
|
-
|
|
53
|
+
_step3;
|
|
66
54
|
try {
|
|
67
55
|
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
68
56
|
var annotation = _step3.value;
|
|
@@ -74,13 +62,13 @@ function traverse(typeDef, visited) {
|
|
|
74
62
|
_iterator3.f();
|
|
75
63
|
}
|
|
76
64
|
}
|
|
77
|
-
}
|
|
65
|
+
}
|
|
78
66
|
|
|
67
|
+
// NOTE: this overload is for TS API compatibility with a previous implementation
|
|
79
68
|
|
|
80
69
|
function inferFromSchemaType(typeDef) {
|
|
81
70
|
traverse(typeDef, new Set());
|
|
82
71
|
return typeDef;
|
|
83
72
|
}
|
|
84
|
-
|
|
85
73
|
var _default = inferFromSchemaType;
|
|
86
74
|
exports.default = _default;
|