@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.
@@ -4,108 +4,82 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _deepEquals = _interopRequireDefault(require("../util/deepEquals"));
9
-
10
8
  var _ValidationError = _interopRequireDefault(require("../ValidationError"));
11
-
12
9
  var _genericValidator = _interopRequireDefault(require("./genericValidator"));
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
  var arrayValidators = _objectSpread(_objectSpread({}, _genericValidator.default), {}, {
23
15
  min: (minLength, value, message) => {
24
16
  if (!value || value.length >= minLength) {
25
17
  return true;
26
18
  }
27
-
28
19
  return message || "Must have at least ".concat(minLength, " items");
29
20
  },
30
21
  max: (maxLength, value, message) => {
31
22
  if (!value || value.length <= maxLength) {
32
23
  return true;
33
24
  }
34
-
35
25
  return message || "Must have at most ".concat(maxLength, " items");
36
26
  },
37
27
  length: (wantedLength, value, message) => {
38
28
  if (!value || value.length === wantedLength) {
39
29
  return true;
40
30
  }
41
-
42
31
  return message || "Must have exactly ".concat(wantedLength, " items");
43
32
  },
44
33
  presence: (flag, value, message) => {
45
34
  if (flag === 'required' && !value) {
46
35
  return message || 'Required';
47
36
  }
48
-
49
37
  return true;
50
38
  },
51
39
  valid: (allowedValues, values, message) => {
52
40
  var valueType = typeof values;
53
-
54
41
  if (valueType === 'undefined') {
55
42
  return true;
56
43
  }
57
-
58
44
  var paths = [];
59
-
60
45
  var _loop = function _loop(i) {
61
46
  var value = values[i];
62
-
63
47
  if (allowedValues.some(expected => (0, _deepEquals.default)(expected, value))) {
64
48
  return "continue";
65
49
  }
66
-
67
50
  var pathSegment = value && value._key ? {
68
51
  _key: value._key
69
52
  } : i;
70
53
  paths.push([pathSegment]);
71
54
  };
72
-
73
55
  for (var i = 0; i < values.length; i++) {
74
56
  var _ret = _loop(i);
75
-
76
57
  if (_ret === "continue") continue;
77
58
  }
78
-
79
59
  return paths.length === 0 ? true : new _ValidationError.default(message || 'Value did not match any allowed values', {
80
60
  paths
81
61
  });
82
62
  },
83
63
  unique: (_unused, value, message) => {
84
64
  var dupeIndices = [];
85
-
86
65
  if (!value) {
87
66
  return true;
88
67
  }
89
-
90
68
  for (var x = 0; x < value.length; x++) {
91
69
  for (var y = x + 1; y < value.length; y++) {
92
70
  var itemA = value[x];
93
71
  var itemB = value[y];
94
-
95
72
  if (!(0, _deepEquals.default)(itemA, itemB)) {
96
73
  continue;
97
74
  }
98
-
99
75
  if (dupeIndices.indexOf(x) === -1) {
100
76
  dupeIndices.push(x);
101
77
  }
102
-
103
78
  if (dupeIndices.indexOf(y) === -1) {
104
79
  dupeIndices.push(y);
105
80
  }
106
81
  }
107
82
  }
108
-
109
83
  var paths = dupeIndices.map(idx => {
110
84
  var item = value[idx];
111
85
  var pathSegment = item && item._key ? {
@@ -118,6 +92,5 @@ var arrayValidators = _objectSpread(_objectSpread({}, _genericValidator.default)
118
92
  }) : true;
119
93
  }
120
94
  });
121
-
122
95
  var _default = arrayValidators;
123
96
  exports.default = _default;
@@ -4,26 +4,18 @@ 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 booleanValidators = _objectSpread(_objectSpread({}, _genericValidator.default), {}, {
19
13
  presence: (flag, value, message) => {
20
14
  if (flag === 'required' && typeof value !== 'boolean') {
21
15
  return message || 'Required';
22
16
  }
23
-
24
17
  return true;
25
18
  }
26
19
  });
27
-
28
20
  var _default = booleanValidators;
29
21
  exports.default = _default;
@@ -5,25 +5,20 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  exports.isRecord = isRecord;
8
-
9
8
  var _format = _interopRequireDefault(require("date-fns/format"));
10
-
11
9
  var _genericValidator = _interopRequireDefault(require("./genericValidator"));
12
-
13
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
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; }
16
-
17
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; }
18
-
19
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; }
20
-
21
14
  function isRecord(obj) {
22
15
  return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
23
- } // eslint-disable-next-line no-useless-escape
16
+ }
24
17
 
18
+ // eslint-disable-next-line no-useless-escape
19
+ var isoDate = /^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/;
25
20
 
26
- var isoDate = /^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/; // eslint-disable-next-line no-warning-comments
21
+ // eslint-disable-next-line no-warning-comments
27
22
  // TODO (eventually): move these to schema type package
28
23
 
29
24
  var getFormattedDate = function getFormattedDate() {
@@ -31,54 +26,45 @@ var getFormattedDate = function getFormattedDate() {
31
26
  var value = arguments.length > 1 ? arguments[1] : undefined;
32
27
  var options = arguments.length > 2 ? arguments[2] : undefined;
33
28
  var format = 'yyyy-MM-dd';
34
-
35
29
  if (options && options.dateFormat) {
36
30
  format = options.dateFormat;
37
31
  }
38
-
39
32
  if (type === 'date') {
40
33
  // If the type is date only
41
34
  return (0, _format.default)(new Date(value), format);
42
- } // If the type is datetime
43
-
35
+ }
44
36
 
37
+ // If the type is datetime
45
38
  if (options && options.timeFormat) {
46
39
  format += " ".concat(options.timeFormat);
47
40
  } else {
48
41
  format += ' HH:mm';
49
42
  }
50
-
51
43
  return (0, _format.default)(new Date(value), format);
52
44
  };
53
-
54
45
  function parseDate(date) {
55
46
  var throwOnError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
56
47
  if (!date) return null;
57
- if (date === 'now') return new Date(); // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ if (date === 'now') return new Date();
58
49
 
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
51
  var parsed = new Date(date);
60
52
  var isInvalid = isNaN(parsed.getTime());
61
-
62
53
  if (isInvalid && throwOnError) {
63
54
  throw new Error("Unable to parse \"".concat(date, "\" to a date"));
64
55
  }
65
-
66
56
  return isInvalid ? null : parsed;
67
57
  }
68
-
69
58
  var dateValidators = _objectSpread(_objectSpread({}, _genericValidator.default), {}, {
70
59
  type: (_unused, value, message) => {
71
60
  var strVal = "".concat(value);
72
-
73
61
  if (!strVal || isoDate.test(value)) {
74
62
  return true;
75
63
  }
76
-
77
64
  return message || 'Must be a valid ISO-8601 formatted date string';
78
65
  },
79
66
  min: (minDate, value, message, context) => {
80
67
  var dateVal = parseDate(value);
81
-
82
68
  if (!dateVal) {
83
69
  return true; // `type()` should catch parse errors
84
70
  }
@@ -86,18 +72,15 @@ var dateValidators = _objectSpread(_objectSpread({}, _genericValidator.default),
86
72
  if (!value || dateVal >= parseDate(minDate, true)) {
87
73
  return true;
88
74
  }
89
-
90
75
  if (!context.type) {
91
76
  throw new Error("`type` was not provided in validation context.");
92
77
  }
93
-
94
78
  var dateTimeOptions = isRecord(context.type.options) ? context.type.options : {};
95
79
  var date = getFormattedDate(context.type.name, minDate, dateTimeOptions);
96
80
  return message || "Must be at or after ".concat(date);
97
81
  },
98
82
  max: (maxDate, value, message, context) => {
99
83
  var dateVal = parseDate(value);
100
-
101
84
  if (!dateVal) {
102
85
  return true; // `type()` should catch parse errors
103
86
  }
@@ -105,16 +88,13 @@ var dateValidators = _objectSpread(_objectSpread({}, _genericValidator.default),
105
88
  if (!value || dateVal <= parseDate(maxDate, true)) {
106
89
  return true;
107
90
  }
108
-
109
91
  if (!context.type) {
110
92
  throw new Error("`type` was not provided in validation context.");
111
93
  }
112
-
113
94
  var dateTimeOptions = isRecord(context.type.options) ? context.type.options : {};
114
95
  var date = getFormattedDate(context.type.name, maxDate, dateTimeOptions);
115
96
  return message || "Must be at or before ".concat(date);
116
97
  }
117
98
  });
118
-
119
99
  var _default = dateValidators;
120
100
  exports.default = _default;
@@ -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(); // Read: There is at least one rule that matched
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
- getDocumentExists = context.getDocumentExists;
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
- path = context.path,
49
- type = context.type;
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
- published = _getDocumentIds.published,
64
- draft = _getDocumentIds.draft;
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;