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