@sanity/validation 2.33.4-shopify.8 → 2.34.1-canary.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/dist/dts/index.d.ts +1 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/util/requestIdleCallback.d.ts +3 -0
- package/dist/dts/util/requestIdleCallback.d.ts.map +1 -0
- package/dist/dts/validateDocument.d.ts +3 -1
- package/dist/dts/validateDocument.d.ts.map +1 -1
- package/lib/Rule.js +23 -100
- package/lib/ValidationError.js +0 -9
- package/lib/getClient.js +0 -3
- package/lib/index.js +9 -7
- 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 +30 -0
- package/lib/util/typeString.js +2 -5
- package/lib/validateDocument.js +140 -168
- 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 +6 -5
- package/src/index.ts +2 -0
- package/src/util/requestIdleCallback.ts +29 -0
- package/src/validateDocument.test.ts +0 -77
- package/src/validateDocument.ts +97 -60
|
@@ -4,57 +4,41 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _typeString = _interopRequireDefault(require("../util/typeString"));
|
|
9
|
-
|
|
10
8
|
var _deepEquals = _interopRequireDefault(require("../util/deepEquals"));
|
|
11
|
-
|
|
12
9
|
var _pathToString = _interopRequireDefault(require("../util/pathToString"));
|
|
13
|
-
|
|
14
10
|
var _ValidationError = _interopRequireDefault(require("../ValidationError"));
|
|
15
|
-
|
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
12
|
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); } }
|
|
19
|
-
|
|
20
13
|
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); }); }; }
|
|
21
|
-
|
|
22
14
|
var SLOW_VALIDATOR_TIMEOUT = 5000;
|
|
23
|
-
|
|
24
15
|
var formatValidationErrors = options => {
|
|
25
16
|
var message;
|
|
26
|
-
|
|
27
17
|
if (options.message) {
|
|
28
18
|
message = options.message;
|
|
29
19
|
} else if (options.results.length === 1) {
|
|
30
20
|
var _options$results$;
|
|
31
|
-
|
|
32
21
|
message = (_options$results$ = options.results[0]) === null || _options$results$ === void 0 ? void 0 : _options$results$.item.message;
|
|
33
22
|
} else {
|
|
34
23
|
message = "[".concat(options.results.map(err => err.item.message).join(" - ".concat(options.operation, " - ")), "]");
|
|
35
24
|
}
|
|
36
|
-
|
|
37
25
|
return new _ValidationError.default(message, {
|
|
38
26
|
children: options.results.length > 1 ? options.results : undefined,
|
|
39
27
|
operation: options.operation
|
|
40
28
|
});
|
|
41
29
|
};
|
|
42
|
-
|
|
43
30
|
var genericValidators = {
|
|
44
31
|
type: (expected, value, message) => {
|
|
45
32
|
var actualType = (0, _typeString.default)(value);
|
|
46
|
-
|
|
47
33
|
if (actualType !== expected && actualType !== 'undefined') {
|
|
48
34
|
return message || "Expected type \"".concat(expected, "\", got \"").concat(actualType, "\"");
|
|
49
35
|
}
|
|
50
|
-
|
|
51
36
|
return true;
|
|
52
37
|
},
|
|
53
38
|
presence: (expected, value, message) => {
|
|
54
39
|
if (value === undefined && expected === 'required') {
|
|
55
40
|
return message || 'Value is required';
|
|
56
41
|
}
|
|
57
|
-
|
|
58
42
|
return true;
|
|
59
43
|
},
|
|
60
44
|
all: function () {
|
|
@@ -68,18 +52,17 @@ var genericValidators = {
|
|
|
68
52
|
operation: 'AND'
|
|
69
53
|
});
|
|
70
54
|
});
|
|
71
|
-
|
|
72
55
|
function all(_x, _x2, _x3, _x4) {
|
|
73
56
|
return _all.apply(this, arguments);
|
|
74
57
|
}
|
|
75
|
-
|
|
76
58
|
return all;
|
|
77
59
|
}(),
|
|
78
60
|
either: function () {
|
|
79
61
|
var _either = _asyncToGenerator(function* (children, value, message, context) {
|
|
80
62
|
var resolved = yield Promise.all(children.map(child => child.validate(value, context)));
|
|
81
|
-
var results = resolved.flat();
|
|
63
|
+
var results = resolved.flat();
|
|
82
64
|
|
|
65
|
+
// Read: There is at least one rule that matched
|
|
83
66
|
if (results.length < children.length) return true;
|
|
84
67
|
return formatValidationErrors({
|
|
85
68
|
message,
|
|
@@ -87,20 +70,16 @@ var genericValidators = {
|
|
|
87
70
|
operation: 'OR'
|
|
88
71
|
});
|
|
89
72
|
});
|
|
90
|
-
|
|
91
73
|
function either(_x5, _x6, _x7, _x8) {
|
|
92
74
|
return _either.apply(this, arguments);
|
|
93
75
|
}
|
|
94
|
-
|
|
95
76
|
return either;
|
|
96
77
|
}(),
|
|
97
78
|
valid: (allowedValues, actual, message) => {
|
|
98
79
|
var valueType = typeof actual;
|
|
99
|
-
|
|
100
80
|
if (valueType === 'undefined') {
|
|
101
81
|
return true;
|
|
102
82
|
}
|
|
103
|
-
|
|
104
83
|
var value = (valueType === 'number' || valueType === 'string') && "".concat(actual);
|
|
105
84
|
var strValue = value && value.length > 30 ? "".concat(value.slice(0, 30), "\u2026") : value;
|
|
106
85
|
var defaultMessage = value ? "Value \"".concat(strValue, "\" did not match any allowed values") : 'Value did not match any allowed values';
|
|
@@ -113,21 +92,17 @@ var genericValidators = {
|
|
|
113
92
|
console.warn("Custom validator at ".concat((0, _pathToString.default)(context.path), " has taken more than ").concat(SLOW_VALIDATOR_TIMEOUT, "ms to respond"));
|
|
114
93
|
}, SLOW_VALIDATOR_TIMEOUT);
|
|
115
94
|
var result;
|
|
116
|
-
|
|
117
95
|
try {
|
|
118
96
|
result = yield fn(value, context);
|
|
119
97
|
} finally {
|
|
120
98
|
clearTimeout(slowTimer);
|
|
121
99
|
}
|
|
122
|
-
|
|
123
100
|
if (typeof result === 'string') return message || result;
|
|
124
101
|
return result;
|
|
125
102
|
});
|
|
126
|
-
|
|
127
103
|
function custom(_x9, _x10, _x11, _x12) {
|
|
128
104
|
return _custom.apply(this, arguments);
|
|
129
105
|
}
|
|
130
|
-
|
|
131
106
|
return custom;
|
|
132
107
|
}()
|
|
133
108
|
};
|
|
@@ -4,67 +4,52 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _genericValidator = _interopRequireDefault(require("./genericValidator"));
|
|
9
|
-
|
|
10
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
9
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
10
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
-
|
|
16
11
|
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; }
|
|
17
|
-
|
|
18
12
|
var precisionRx = /(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/;
|
|
19
|
-
|
|
20
13
|
var numberValidators = _objectSpread(_objectSpread({}, _genericValidator.default), {}, {
|
|
21
14
|
integer: (_unused, value, message) => {
|
|
22
15
|
if (!Number.isInteger(value)) {
|
|
23
16
|
return message || 'Must be an integer';
|
|
24
17
|
}
|
|
25
|
-
|
|
26
18
|
return true;
|
|
27
19
|
},
|
|
28
20
|
precision: (limit, value, message) => {
|
|
29
21
|
if (value === undefined) return true;
|
|
30
22
|
var places = value.toString().match(precisionRx);
|
|
31
23
|
var decimals = Math.max((places[1] ? places[1].length : 0) - (places[2] ? parseInt(places[2], 10) : 0), 0);
|
|
32
|
-
|
|
33
24
|
if (decimals > limit) {
|
|
34
25
|
return message || "Max precision is ".concat(limit);
|
|
35
26
|
}
|
|
36
|
-
|
|
37
27
|
return true;
|
|
38
28
|
},
|
|
39
29
|
min: (minNum, value, message) => {
|
|
40
30
|
if (value >= minNum) {
|
|
41
31
|
return true;
|
|
42
32
|
}
|
|
43
|
-
|
|
44
33
|
return message || "Must be greater than or equal ".concat(minNum);
|
|
45
34
|
},
|
|
46
35
|
max: (maxNum, value, message) => {
|
|
47
36
|
if (value <= maxNum) {
|
|
48
37
|
return true;
|
|
49
38
|
}
|
|
50
|
-
|
|
51
39
|
return message || "Must be less than or equal ".concat(maxNum);
|
|
52
40
|
},
|
|
53
41
|
greaterThan: (num, value, message) => {
|
|
54
42
|
if (value > num) {
|
|
55
43
|
return true;
|
|
56
44
|
}
|
|
57
|
-
|
|
58
45
|
return message || "Must be greater than ".concat(num);
|
|
59
46
|
},
|
|
60
47
|
lessThan: (maxNum, value, message) => {
|
|
61
48
|
if (value < maxNum) {
|
|
62
49
|
return true;
|
|
63
50
|
}
|
|
64
|
-
|
|
65
51
|
return message || "Must be less than ".concat(maxNum);
|
|
66
52
|
}
|
|
67
53
|
});
|
|
68
|
-
|
|
69
54
|
var _default = numberValidators;
|
|
70
55
|
exports.default = _default;
|
|
@@ -4,37 +4,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _types = require("@sanity/types");
|
|
9
|
-
|
|
10
8
|
var _genericValidator = _interopRequireDefault(require("./genericValidator"));
|
|
11
|
-
|
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
10
|
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
11
|
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
|
-
|
|
18
12
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
19
|
-
|
|
20
13
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
21
|
-
|
|
22
14
|
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; }
|
|
23
|
-
|
|
24
15
|
var metaKeys = ['_key', '_type', '_weak'];
|
|
25
|
-
|
|
26
16
|
var objectValidators = _objectSpread(_objectSpread({}, _genericValidator.default), {}, {
|
|
27
17
|
presence: (expected, value, message) => {
|
|
28
18
|
if (expected !== 'required') {
|
|
29
19
|
return true;
|
|
30
20
|
}
|
|
31
|
-
|
|
32
21
|
var keys = value && Object.keys(value).filter(key => !metaKeys.includes(key));
|
|
33
|
-
|
|
34
22
|
if (value === undefined || keys && keys.length === 0) {
|
|
35
23
|
return message || 'Required';
|
|
36
24
|
}
|
|
37
|
-
|
|
38
25
|
return true;
|
|
39
26
|
},
|
|
40
27
|
reference: function () {
|
|
@@ -42,41 +29,31 @@ var objectValidators = _objectSpread(_objectSpread({}, _genericValidator.default
|
|
|
42
29
|
if (!value) {
|
|
43
30
|
return true;
|
|
44
31
|
}
|
|
45
|
-
|
|
46
32
|
if (!(0, _types.isReference)(value)) {
|
|
47
33
|
return message || true;
|
|
48
34
|
}
|
|
49
|
-
|
|
50
35
|
var type = context.type,
|
|
51
|
-
|
|
52
|
-
|
|
36
|
+
getDocumentExists = context.getDocumentExists;
|
|
53
37
|
if (!type) {
|
|
54
38
|
throw new Error("`type` was not provided in validation context");
|
|
55
39
|
}
|
|
56
|
-
|
|
57
40
|
if ('weak' in type && type.weak) {
|
|
58
41
|
return true;
|
|
59
42
|
}
|
|
60
|
-
|
|
61
43
|
if (!getDocumentExists) {
|
|
62
44
|
throw new Error("`getDocumentExists` was not provided in validation context");
|
|
63
45
|
}
|
|
64
|
-
|
|
65
46
|
var exists = yield getDocumentExists({
|
|
66
47
|
id: value._ref
|
|
67
48
|
});
|
|
68
|
-
|
|
69
49
|
if (!exists) {
|
|
70
50
|
return 'This reference must be published';
|
|
71
51
|
}
|
|
72
|
-
|
|
73
52
|
return true;
|
|
74
53
|
});
|
|
75
|
-
|
|
76
54
|
function reference(_x, _x2, _x3, _x4) {
|
|
77
55
|
return _reference.apply(this, arguments);
|
|
78
56
|
}
|
|
79
|
-
|
|
80
57
|
return reference;
|
|
81
58
|
}(),
|
|
82
59
|
assetRequired: (flag, value, message) => {
|
|
@@ -84,10 +61,8 @@ var objectValidators = _objectSpread(_objectSpread({}, _genericValidator.default
|
|
|
84
61
|
var assetType = flag.assetType || 'Asset';
|
|
85
62
|
return message || "".concat(assetType, " required");
|
|
86
63
|
}
|
|
87
|
-
|
|
88
64
|
return true;
|
|
89
65
|
}
|
|
90
66
|
});
|
|
91
|
-
|
|
92
67
|
var _default = objectValidators;
|
|
93
68
|
exports.default = _default;
|
|
@@ -4,27 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.slugValidator = void 0;
|
|
7
|
-
|
|
8
7
|
var _memoize2 = _interopRequireDefault(require("lodash/memoize"));
|
|
9
|
-
|
|
10
8
|
var _types = require("@sanity/types");
|
|
11
|
-
|
|
12
9
|
var _getClient = _interopRequireDefault(require("../getClient"));
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
11
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
-
|
|
18
12
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
|
-
|
|
20
13
|
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; }
|
|
21
|
-
|
|
22
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); } }
|
|
23
|
-
|
|
24
15
|
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); }); }; }
|
|
25
|
-
|
|
26
16
|
var memoizedWarnOnArraySlug = (0, _memoize2.default)(warnOnArraySlug);
|
|
27
|
-
|
|
28
17
|
function getDocumentIds(id) {
|
|
29
18
|
var isDraft = id.indexOf('drafts.') === 0;
|
|
30
19
|
return {
|
|
@@ -32,7 +21,6 @@ function getDocumentIds(id) {
|
|
|
32
21
|
draft: isDraft ? id : "drafts.".concat(id)
|
|
33
22
|
};
|
|
34
23
|
}
|
|
35
|
-
|
|
36
24
|
function serializePath(path) {
|
|
37
25
|
return path.reduce((target, part, i) => {
|
|
38
26
|
var isIndex = typeof part === 'number';
|
|
@@ -42,34 +30,26 @@ function serializePath(path) {
|
|
|
42
30
|
return "".concat(target).concat(add);
|
|
43
31
|
}, '');
|
|
44
32
|
}
|
|
45
|
-
|
|
46
33
|
var defaultIsUnique = (slug, context) => {
|
|
47
34
|
var document = context.document,
|
|
48
|
-
|
|
49
|
-
|
|
35
|
+
path = context.path,
|
|
36
|
+
type = context.type;
|
|
50
37
|
var schemaOptions = type === null || type === void 0 ? void 0 : type.options;
|
|
51
|
-
|
|
52
38
|
if (!document) {
|
|
53
39
|
throw new Error("`document` was not provided in validation context.");
|
|
54
40
|
}
|
|
55
|
-
|
|
56
41
|
if (!path) {
|
|
57
42
|
throw new Error("`path` was not provided in validation context.");
|
|
58
43
|
}
|
|
59
|
-
|
|
60
44
|
var disableArrayWarning = (schemaOptions === null || schemaOptions === void 0 ? void 0 : schemaOptions.disableArrayWarning) || false;
|
|
61
|
-
|
|
62
45
|
var _getDocumentIds = getDocumentIds(document._id),
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
46
|
+
published = _getDocumentIds.published,
|
|
47
|
+
draft = _getDocumentIds.draft;
|
|
66
48
|
var docType = document._type;
|
|
67
49
|
var atPath = serializePath(path.concat('current'));
|
|
68
|
-
|
|
69
50
|
if (!disableArrayWarning && atPath.includes('[]')) {
|
|
70
51
|
memoizedWarnOnArraySlug(serializePath(path));
|
|
71
52
|
}
|
|
72
|
-
|
|
73
53
|
var constraints = ['_type == $docType', "!(_id in [$draft, $published])", "".concat(atPath, " == $slug")].join(' && ');
|
|
74
54
|
return (0, _getClient.default)().fetch("!defined(*[".concat(constraints, "][0]._id)"), {
|
|
75
55
|
docType,
|
|
@@ -80,54 +60,43 @@ var defaultIsUnique = (slug, context) => {
|
|
|
80
60
|
tag: 'validation.slug-is-unique'
|
|
81
61
|
});
|
|
82
62
|
};
|
|
83
|
-
|
|
84
63
|
function warnOnArraySlug(serializedPath) {
|
|
85
64
|
/* eslint-disable no-console */
|
|
86
65
|
console.warn(["Slug field at path ".concat(serializedPath, " is within an array and cannot be automatically checked for uniqueness"), "If you need to check for uniqueness, provide your own \"isUnique\" method", "To disable this message, set `disableArrayWarning: true` on the slug `options` field"].join('\n'));
|
|
87
66
|
/* eslint-enable no-console */
|
|
88
67
|
}
|
|
68
|
+
|
|
89
69
|
/**
|
|
90
70
|
* Validates slugs values by querying for uniqueness from the client.
|
|
91
71
|
*
|
|
92
72
|
* This is a custom rule implementation (e.g. `Rule.custom(slugValidator)`)
|
|
93
73
|
* that's populated in `inferFromSchemaType` when the type name is `slug`
|
|
94
74
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
75
|
var slugValidator = /*#__PURE__*/function () {
|
|
98
76
|
var _ref = _asyncToGenerator(function* (value, context) {
|
|
99
77
|
var _context$type;
|
|
100
|
-
|
|
101
78
|
if (!value) {
|
|
102
79
|
return true;
|
|
103
80
|
}
|
|
104
|
-
|
|
105
81
|
if (typeof value !== 'object') {
|
|
106
82
|
return 'Slug must be an object';
|
|
107
83
|
}
|
|
108
|
-
|
|
109
84
|
var slugValue = value.current;
|
|
110
|
-
|
|
111
85
|
if (!slugValue) {
|
|
112
86
|
return 'Slug must have a value';
|
|
113
87
|
}
|
|
114
|
-
|
|
115
88
|
var options = context === null || context === void 0 ? void 0 : (_context$type = context.type) === null || _context$type === void 0 ? void 0 : _context$type.options;
|
|
116
89
|
var isUnique = (options === null || options === void 0 ? void 0 : options.isUnique) || defaultIsUnique;
|
|
117
90
|
var wasUnique = yield isUnique(slugValue, _objectSpread(_objectSpread({}, context), {}, {
|
|
118
91
|
defaultIsUnique
|
|
119
92
|
}));
|
|
120
|
-
|
|
121
93
|
if (wasUnique) {
|
|
122
94
|
return true;
|
|
123
95
|
}
|
|
124
|
-
|
|
125
96
|
return 'Slug is already in use';
|
|
126
97
|
});
|
|
127
|
-
|
|
128
98
|
return function slugValidator(_x, _x2) {
|
|
129
99
|
return _ref.apply(this, arguments);
|
|
130
100
|
};
|
|
131
101
|
}();
|
|
132
|
-
|
|
133
102
|
exports.slugValidator = slugValidator;
|
|
@@ -4,54 +4,41 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _genericValidator = _interopRequireDefault(require("./genericValidator"));
|
|
9
|
-
|
|
10
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
9
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
10
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
-
|
|
16
11
|
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; }
|
|
17
|
-
|
|
18
12
|
var DUMMY_ORIGIN = 'http://sanity';
|
|
19
13
|
var emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
20
|
-
|
|
21
14
|
var isRelativeUrl = url => /^\.*\//.test(url);
|
|
22
|
-
|
|
23
15
|
var stringValidators = _objectSpread(_objectSpread({}, _genericValidator.default), {}, {
|
|
24
16
|
min: (minLength, value, message) => {
|
|
25
17
|
if (!value || value.length >= minLength) {
|
|
26
18
|
return true;
|
|
27
19
|
}
|
|
28
|
-
|
|
29
20
|
return message || "Must be at least ".concat(minLength, " characters long");
|
|
30
21
|
},
|
|
31
22
|
max: (maxLength, value, message) => {
|
|
32
23
|
if (!value || value.length <= maxLength) {
|
|
33
24
|
return true;
|
|
34
25
|
}
|
|
35
|
-
|
|
36
26
|
return message || "Must be at most ".concat(maxLength, " characters long");
|
|
37
27
|
},
|
|
38
28
|
length: (wantedLength, value, message) => {
|
|
39
29
|
var strValue = value || '';
|
|
40
|
-
|
|
41
30
|
if (strValue.length === wantedLength) {
|
|
42
31
|
return true;
|
|
43
32
|
}
|
|
44
|
-
|
|
45
33
|
return message || "Must be exactly ".concat(wantedLength, " characters long");
|
|
46
34
|
},
|
|
47
35
|
uri: (constraints, value, message) => {
|
|
48
36
|
var strValue = value || '';
|
|
49
37
|
var options = constraints.options;
|
|
50
38
|
var allowCredentials = options.allowCredentials,
|
|
51
|
-
|
|
39
|
+
relativeOnly = options.relativeOnly;
|
|
52
40
|
var allowRelative = options.allowRelative || relativeOnly;
|
|
53
41
|
var url;
|
|
54
|
-
|
|
55
42
|
try {
|
|
56
43
|
// WARNING: Safari checks for a given `base` param by looking at the length of arguments passed
|
|
57
44
|
// to new URL(str, base), and will fail if invoked with new URL(strValue, undefined)
|
|
@@ -59,73 +46,58 @@ var stringValidators = _objectSpread(_objectSpread({}, _genericValidator.default
|
|
|
59
46
|
} catch (err) {
|
|
60
47
|
return message || 'Not a valid URL';
|
|
61
48
|
}
|
|
62
|
-
|
|
63
49
|
if (relativeOnly && url.origin !== DUMMY_ORIGIN) {
|
|
64
50
|
return message || 'Only relative URLs are allowed';
|
|
65
51
|
}
|
|
66
|
-
|
|
67
52
|
if (!allowRelative && url.origin === DUMMY_ORIGIN && isRelativeUrl(strValue)) {
|
|
68
53
|
return message || 'Relative URLs are not allowed';
|
|
69
54
|
}
|
|
70
|
-
|
|
71
55
|
if (!allowCredentials && (url.username || url.password)) {
|
|
72
56
|
return message || "Username/password not allowed";
|
|
73
57
|
}
|
|
74
|
-
|
|
75
58
|
var urlScheme = url.protocol.replace(/:$/, '');
|
|
76
59
|
var matchesAllowedScheme = options.scheme.some(scheme => scheme.test(urlScheme));
|
|
77
|
-
|
|
78
60
|
if (!matchesAllowedScheme) {
|
|
79
61
|
return message || 'Does not match allowed protocols/schemes';
|
|
80
62
|
}
|
|
81
|
-
|
|
82
63
|
return true;
|
|
83
64
|
},
|
|
84
65
|
stringCasing: (casing, value, message) => {
|
|
85
66
|
var strValue = value || '';
|
|
86
|
-
|
|
87
67
|
if (casing === 'uppercase' && strValue !== strValue.toLocaleUpperCase()) {
|
|
88
68
|
return message || "Must be all uppercase letters";
|
|
89
69
|
}
|
|
90
|
-
|
|
91
70
|
if (casing === 'lowercase' && strValue !== strValue.toLocaleLowerCase()) {
|
|
92
71
|
return message || "Must be all lowercase letters";
|
|
93
72
|
}
|
|
94
|
-
|
|
95
73
|
return true;
|
|
96
74
|
},
|
|
97
75
|
presence: (flag, value, message) => {
|
|
98
76
|
if (flag === 'required' && !value) {
|
|
99
77
|
return message || 'Required';
|
|
100
78
|
}
|
|
101
|
-
|
|
102
79
|
return true;
|
|
103
80
|
},
|
|
104
81
|
regex: (options, value, message) => {
|
|
105
82
|
var pattern = options.pattern,
|
|
106
|
-
|
|
107
|
-
|
|
83
|
+
name = options.name,
|
|
84
|
+
invert = options.invert;
|
|
108
85
|
var regName = name || "\"".concat(pattern.toString(), "\"");
|
|
109
86
|
var strValue = value || '';
|
|
110
87
|
var matches = pattern.test(strValue);
|
|
111
|
-
|
|
112
88
|
if (!invert && !matches || invert && matches) {
|
|
113
89
|
var defaultMessage = invert ? "Should not match ".concat(regName, "-pattern") : "Does not match ".concat(regName, "-pattern");
|
|
114
90
|
return message || defaultMessage;
|
|
115
91
|
}
|
|
116
|
-
|
|
117
92
|
return true;
|
|
118
93
|
},
|
|
119
94
|
email: (_unused, value, message) => {
|
|
120
95
|
var strValue = "".concat(value || '').trim();
|
|
121
|
-
|
|
122
96
|
if (!strValue || emailRegex.test(strValue)) {
|
|
123
97
|
return true;
|
|
124
98
|
}
|
|
125
|
-
|
|
126
99
|
return message || 'Must be a valid email address';
|
|
127
100
|
}
|
|
128
101
|
});
|
|
129
|
-
|
|
130
102
|
var _default = stringValidators;
|
|
131
103
|
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/validation",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.34.1-canary.0+b3e58b8893",
|
|
4
4
|
"description": "Validation and warning infrastructure for Sanity projects",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./dist/dts",
|
|
@@ -33,12 +33,13 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@sanity/client": "^3.3.3",
|
|
36
|
-
"@sanity/schema": "2.
|
|
36
|
+
"@sanity/schema": "2.34.1-canary.0+b3e58b8893"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@sanity/types": "2.
|
|
39
|
+
"@sanity/types": "2.34.1-canary.0+b3e58b8893",
|
|
40
40
|
"date-fns": "^2.16.1",
|
|
41
|
-
"lodash": "^4.17.15"
|
|
41
|
+
"lodash": "^4.17.15",
|
|
42
|
+
"rxjs": "^6.5.3"
|
|
42
43
|
},
|
|
43
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "b3e58b8893c9573844279488896df2313df914f1"
|
|
44
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,3 +5,5 @@ import inferFromSchemaType from './inferFromSchemaType'
|
|
|
5
5
|
|
|
6
6
|
export default {Rule: RuleClass, validateDocument, inferFromSchema, inferFromSchemaType}
|
|
7
7
|
export {RuleClass as Rule, validateDocument, inferFromSchema, inferFromSchemaType}
|
|
8
|
+
|
|
9
|
+
export {validateDocumentObservable} from './validateDocument'
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple requestIdleCallback polyfill
|
|
3
|
+
* Can be removed when all browsers support requestIdleCallback: https://caniuse.com/requestidlecallback
|
|
4
|
+
* @param callback
|
|
5
|
+
* @param options
|
|
6
|
+
*/
|
|
7
|
+
const requestIdleCallbackShim: typeof window.requestIdleCallback = function requestIdleCallbackShim(
|
|
8
|
+
callback,
|
|
9
|
+
options?
|
|
10
|
+
): number {
|
|
11
|
+
const start = Date.now()
|
|
12
|
+
return window.setTimeout(() => {
|
|
13
|
+
callback({
|
|
14
|
+
didTimeout: false,
|
|
15
|
+
timeRemaining() {
|
|
16
|
+
return Math.max(0, Date.now() - start)
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
}, 0)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const cancelIdleCallbackShim: typeof window.cancelIdleCallback = function cancelIdleCallbackShim(
|
|
23
|
+
handle: number
|
|
24
|
+
): void {
|
|
25
|
+
return window.clearTimeout(handle)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const requestIdleCallback = window.requestIdleCallback || requestIdleCallbackShim
|
|
29
|
+
export const cancelIdleCallback = window.cancelIdleCallback || cancelIdleCallbackShim
|