@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
|
@@ -5,45 +5,36 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = convertToValidationMarker;
|
|
7
7
|
exports.isNonNullable = isNonNullable;
|
|
8
|
-
|
|
9
8
|
var _ValidationError = _interopRequireDefault(require("../ValidationError"));
|
|
10
|
-
|
|
11
9
|
var _pathToString = _interopRequireDefault(require("../util/pathToString"));
|
|
12
|
-
|
|
13
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
-
|
|
15
11
|
function isNonNullable(t) {
|
|
16
12
|
return t !== null || t !== undefined;
|
|
17
13
|
}
|
|
18
|
-
|
|
19
14
|
function convertToValidationMarker(validatorResult, level, context) {
|
|
20
15
|
var _validatorResult$path;
|
|
21
|
-
|
|
22
16
|
if (validatorResult === true) return [];
|
|
23
|
-
|
|
24
17
|
if (Array.isArray(validatorResult)) {
|
|
25
18
|
return validatorResult.flatMap(child => convertToValidationMarker(child, level, context)).filter(isNonNullable);
|
|
26
19
|
}
|
|
27
|
-
|
|
28
20
|
if (typeof validatorResult === 'string') {
|
|
29
21
|
return convertToValidationMarker(new _ValidationError.default(validatorResult), level, context);
|
|
30
22
|
}
|
|
31
|
-
|
|
32
23
|
if (!(validatorResult instanceof _ValidationError.default)) {
|
|
33
24
|
// in order to accept the `ValidationErrorLike`, it at least needs to have
|
|
34
25
|
// a `message` in the object
|
|
35
26
|
if (typeof (validatorResult === null || validatorResult === void 0 ? void 0 : validatorResult.message) !== 'string') {
|
|
36
27
|
throw new Error("".concat((0, _pathToString.default)(context.path), ": Validator must return 'true' if valid or an error message as a string on errors"));
|
|
37
|
-
}
|
|
38
|
-
// `ValidationErrorClass`. in this case, we want to convert it to a class
|
|
39
|
-
|
|
28
|
+
}
|
|
40
29
|
|
|
30
|
+
// this is the occurs when an object is returned that wasn't created with the
|
|
31
|
+
// `ValidationErrorClass`. in this case, we want to convert it to a class
|
|
41
32
|
return convertToValidationMarker(new _ValidationError.default(validatorResult.message, validatorResult), level, context);
|
|
42
33
|
}
|
|
34
|
+
var results = [];
|
|
43
35
|
|
|
44
|
-
|
|
36
|
+
// the validator result does not include any item-level relative paths,
|
|
45
37
|
// then just return the top-level path with the validation result
|
|
46
|
-
|
|
47
38
|
if (!((_validatorResult$path = validatorResult.paths) !== null && _validatorResult$path !== void 0 && _validatorResult$path.length)) {
|
|
48
39
|
return [{
|
|
49
40
|
type: 'validation',
|
|
@@ -51,11 +42,11 @@ function convertToValidationMarker(validatorResult, level, context) {
|
|
|
51
42
|
item: validatorResult,
|
|
52
43
|
path: context.path || []
|
|
53
44
|
}];
|
|
54
|
-
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// if the validator result did include item-level relative paths, then for
|
|
55
48
|
// each item-level relative path, create a validation marker that concatenates
|
|
56
49
|
// the relative path with the path from the validation context
|
|
57
|
-
|
|
58
|
-
|
|
59
50
|
return results.concat(validatorResult.paths.map(path => ({
|
|
60
51
|
type: 'validation',
|
|
61
52
|
path: (context.path || []).concat(path),
|
package/lib/util/deepEquals.js
CHANGED
|
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = deepEquals;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Modified version of fast-deep-equal (https://github.com/epoberezkin/fast-deep-equal)
|
|
10
9
|
* MIT-licensed, copyright (c) 2017 Evgeny Poberezkin
|
|
11
10
|
**/
|
|
11
|
+
|
|
12
12
|
// NOTE: when converting to typescript, some of the checks were inlined (vs
|
|
13
13
|
// having them in a variable) because the type predicate type narrowing only
|
|
14
14
|
// works when type predicate is called inline in the condition that starts the
|
|
@@ -18,70 +18,53 @@ function deepEquals(a, b) {
|
|
|
18
18
|
if (a === b) {
|
|
19
19
|
return true;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
21
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
23
22
|
if (a.length != b.length) return false;
|
|
24
|
-
|
|
25
23
|
for (var i = 0; i < a.length; i++) {
|
|
26
24
|
if (!deepEquals(a[i], b[i])) {
|
|
27
25
|
return false;
|
|
28
26
|
}
|
|
29
27
|
}
|
|
30
|
-
|
|
31
28
|
return true;
|
|
32
29
|
}
|
|
33
|
-
|
|
34
30
|
if (Array.isArray(a) != Array.isArray(b)) {
|
|
35
31
|
return false;
|
|
36
32
|
}
|
|
37
|
-
|
|
38
33
|
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
39
34
|
var keys = Object.keys(a);
|
|
40
|
-
|
|
41
35
|
if (keys.length !== Object.keys(b).length) {
|
|
42
36
|
return false;
|
|
43
37
|
}
|
|
44
|
-
|
|
45
38
|
if (a instanceof Date && b instanceof Date) {
|
|
46
39
|
return a.getTime() === b.getTime();
|
|
47
40
|
}
|
|
48
|
-
|
|
49
41
|
if (a instanceof Date != b instanceof Date) {
|
|
50
42
|
return false;
|
|
51
43
|
}
|
|
52
|
-
|
|
53
44
|
if (a instanceof RegExp && b instanceof RegExp) {
|
|
54
45
|
return a.toString() == b.toString();
|
|
55
46
|
}
|
|
56
|
-
|
|
57
47
|
if (a instanceof RegExp != b instanceof RegExp) {
|
|
58
48
|
return false;
|
|
59
49
|
}
|
|
60
|
-
|
|
61
50
|
for (var _i = 0; _i < keys.length; _i++) {
|
|
62
51
|
if (keys[_i] === '_key') {
|
|
63
52
|
continue;
|
|
64
53
|
}
|
|
65
|
-
|
|
66
54
|
if (!Object.prototype.hasOwnProperty.call(b, keys[_i])) {
|
|
67
55
|
return false;
|
|
68
56
|
}
|
|
69
57
|
}
|
|
70
|
-
|
|
71
58
|
for (var _i2 = 0; _i2 < keys.length; _i2++) {
|
|
72
59
|
var key = keys[_i2];
|
|
73
|
-
|
|
74
60
|
if (key === '_key') {
|
|
75
61
|
continue;
|
|
76
62
|
}
|
|
77
|
-
|
|
78
63
|
if (!deepEquals(a[key], b[key])) {
|
|
79
64
|
return false;
|
|
80
65
|
}
|
|
81
66
|
}
|
|
82
|
-
|
|
83
67
|
return true;
|
|
84
68
|
}
|
|
85
|
-
|
|
86
69
|
return false;
|
|
87
70
|
}
|
package/lib/util/escapeRegex.js
CHANGED
|
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
/* eslint-disable no-useless-escape */
|
|
9
|
-
var _default = string => {
|
|
7
|
+
/* eslint-disable no-useless-escape */var _default = string => {
|
|
10
8
|
// Escape ^$.*+-?=!:|\/()[]{},
|
|
11
9
|
return string.replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g, '\\$&');
|
|
12
10
|
};
|
|
13
|
-
|
|
14
11
|
exports.default = _default;
|
|
@@ -4,19 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = normalizeValidationRules;
|
|
7
|
-
|
|
8
7
|
var _Rule = _interopRequireDefault(require("../Rule"));
|
|
9
|
-
|
|
10
8
|
var _slugValidator = require("../validators/slugValidator");
|
|
11
|
-
|
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
10
|
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; }
|
|
15
|
-
|
|
16
11
|
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; }
|
|
17
|
-
|
|
18
12
|
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; }
|
|
19
|
-
|
|
20
13
|
var ruleConstraintTypes = {
|
|
21
14
|
array: true,
|
|
22
15
|
boolean: true,
|
|
@@ -25,9 +18,7 @@ var ruleConstraintTypes = {
|
|
|
25
18
|
object: true,
|
|
26
19
|
string: true
|
|
27
20
|
};
|
|
28
|
-
|
|
29
21
|
var isRuleConstraint = typeString => typeString in ruleConstraintTypes;
|
|
30
|
-
|
|
31
22
|
function getTypeChain(type, visited) {
|
|
32
23
|
if (!type) return [];
|
|
33
24
|
if (visited.has(type)) return [];
|
|
@@ -35,24 +26,23 @@ function getTypeChain(type, visited) {
|
|
|
35
26
|
var next = type.type ? getTypeChain(type.type, visited) : [];
|
|
36
27
|
return [...next, type];
|
|
37
28
|
}
|
|
38
|
-
|
|
39
29
|
function baseRuleReducer(inputRule, type) {
|
|
40
30
|
var baseRule = inputRule;
|
|
41
|
-
|
|
42
31
|
if (isRuleConstraint(type.jsonType)) {
|
|
43
32
|
baseRule = baseRule.type(type.jsonType);
|
|
44
33
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(type === null || type === void 0 ? void 0 : type.options) &&
|
|
48
|
-
|
|
49
|
-
|
|
34
|
+
var typeOptionsList =
|
|
35
|
+
// if type.options is truthy
|
|
36
|
+
(type === null || type === void 0 ? void 0 : type.options) &&
|
|
37
|
+
// and type.options is an object (non-null from the previous)
|
|
38
|
+
typeof type.options === 'object' &&
|
|
39
|
+
// and if `list` is in options
|
|
40
|
+
'list' in type.options &&
|
|
41
|
+
// then finally access the list
|
|
50
42
|
type.options.list;
|
|
51
|
-
|
|
52
43
|
if (Array.isArray(typeOptionsList)) {
|
|
53
44
|
baseRule = baseRule.valid(typeOptionsList.map(option => extractValueFromListOption(option, type)));
|
|
54
45
|
}
|
|
55
|
-
|
|
56
46
|
if (type.name === 'datetime') return baseRule.type('Date');
|
|
57
47
|
if (type.name === 'date') return baseRule.type('Date');
|
|
58
48
|
if (type.name === 'url') return baseRule.uri();
|
|
@@ -61,7 +51,6 @@ function baseRuleReducer(inputRule, type) {
|
|
|
61
51
|
if (type.name === 'email') return baseRule.email();
|
|
62
52
|
return baseRule;
|
|
63
53
|
}
|
|
64
|
-
|
|
65
54
|
function hasValueField(typeDef) {
|
|
66
55
|
if (!typeDef) return false;
|
|
67
56
|
if (!('fields' in typeDef) && typeDef.type) return hasValueField(typeDef.type);
|
|
@@ -69,7 +58,6 @@ function hasValueField(typeDef) {
|
|
|
69
58
|
if (!Array.isArray(typeDef.fields)) return false;
|
|
70
59
|
return typeDef.fields.some(field => field.name === 'value');
|
|
71
60
|
}
|
|
72
|
-
|
|
73
61
|
function extractValueFromListOption(option, typeDef) {
|
|
74
62
|
// If you define a `list` option with object items, where the item has a `value` field,
|
|
75
63
|
// we don't want to treat that as the value but rather the surrounding object
|
|
@@ -77,38 +65,32 @@ function extractValueFromListOption(option, typeDef) {
|
|
|
77
65
|
if (typeDef.jsonType === 'object' && hasValueField(typeDef)) return option;
|
|
78
66
|
return option.value === undefined ? option : option.value;
|
|
79
67
|
}
|
|
68
|
+
|
|
80
69
|
/**
|
|
81
70
|
* Takes in `SchemaValidationValue` and returns an array of `Rule` instances.
|
|
82
71
|
*/
|
|
83
|
-
|
|
84
|
-
|
|
85
72
|
function normalizeValidationRules(typeDef) {
|
|
86
73
|
if (!typeDef) {
|
|
87
74
|
return [];
|
|
88
75
|
}
|
|
89
|
-
|
|
90
76
|
var validation = typeDef.validation;
|
|
91
|
-
|
|
92
77
|
if (Array.isArray(validation)) {
|
|
93
78
|
return validation.flatMap(i => normalizeValidationRules(_objectSpread(_objectSpread({}, typeDef), {}, {
|
|
94
79
|
validation: i
|
|
95
80
|
})));
|
|
96
81
|
}
|
|
97
|
-
|
|
98
82
|
if (validation instanceof _Rule.default) {
|
|
99
83
|
return [validation];
|
|
100
84
|
}
|
|
101
|
-
|
|
102
|
-
|
|
85
|
+
var baseRule =
|
|
86
|
+
// using an object + Object.values to de-dupe the type chain by type name
|
|
103
87
|
Object.values(getTypeChain(typeDef, new Set()).reduce((acc, type) => {
|
|
104
88
|
acc[type.name] = type;
|
|
105
89
|
return acc;
|
|
106
90
|
}, {})).reduce(baseRuleReducer, new _Rule.default(typeDef));
|
|
107
|
-
|
|
108
91
|
if (!validation) {
|
|
109
92
|
return [baseRule];
|
|
110
93
|
}
|
|
111
|
-
|
|
112
94
|
return normalizeValidationRules(_objectSpread(_objectSpread({}, typeDef), {}, {
|
|
113
95
|
validation: validation(baseRule)
|
|
114
96
|
}));
|
package/lib/util/pathToString.js
CHANGED
|
@@ -4,27 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = pathToString;
|
|
7
|
-
|
|
8
7
|
var _types = require("@sanity/types");
|
|
9
|
-
|
|
10
8
|
function pathToString() {
|
|
11
9
|
var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
12
10
|
return path.reduce((target, segment, i) => {
|
|
13
11
|
var segmentType = typeof segment;
|
|
14
|
-
|
|
15
12
|
if (segmentType === 'number') {
|
|
16
13
|
return "".concat(target, "[").concat(segment, "]");
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
if (segmentType === 'string') {
|
|
20
16
|
var separator = i === 0 ? '' : '.';
|
|
21
17
|
return "".concat(target).concat(separator).concat(segment);
|
|
22
18
|
}
|
|
23
|
-
|
|
24
19
|
if ((0, _types.isKeyedObject)(segment)) {
|
|
25
20
|
return "".concat(target, "[_key==\"").concat(segment._key, "\"]");
|
|
26
21
|
}
|
|
27
|
-
|
|
28
22
|
throw new Error("Unsupported path segment \"".concat(segment, "\""));
|
|
29
23
|
}, '');
|
|
30
24
|
}
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.requestIdleCallback = exports.cancelIdleCallback = void 0;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Simple requestIdleCallback polyfill
|
|
10
9
|
* Can be removed when all browsers support requestIdleCallback: https://caniuse.com/requestidlecallback
|
|
@@ -16,19 +15,15 @@ var requestIdleCallbackShim = function requestIdleCallbackShim(callback, options
|
|
|
16
15
|
return window.setTimeout(() => {
|
|
17
16
|
callback({
|
|
18
17
|
didTimeout: false,
|
|
19
|
-
|
|
20
18
|
timeRemaining() {
|
|
21
19
|
return Math.max(0, Date.now() - start);
|
|
22
20
|
}
|
|
23
|
-
|
|
24
21
|
});
|
|
25
22
|
}, 0);
|
|
26
23
|
};
|
|
27
|
-
|
|
28
24
|
var cancelIdleCallbackShim = function cancelIdleCallbackShim(handle) {
|
|
29
25
|
return window.clearTimeout(handle);
|
|
30
26
|
};
|
|
31
|
-
|
|
32
27
|
var requestIdleCallback = window.requestIdleCallback || requestIdleCallbackShim;
|
|
33
28
|
exports.requestIdleCallback = requestIdleCallback;
|
|
34
29
|
var cancelIdleCallback = window.cancelIdleCallback || cancelIdleCallbackShim;
|
package/lib/util/typeString.js
CHANGED
|
@@ -8,21 +8,18 @@ exports.default = typeString;
|
|
|
8
8
|
// https://github.com/stephenhandley/type-of-is/blob/7138a7e79f5af7c286bf8123f60843a91aaebf38/index.js
|
|
9
9
|
var _toString = {}.toString;
|
|
10
10
|
var builtIns = [Object, Function, Array, String, Boolean, Number, Date, RegExp, Error];
|
|
11
|
-
|
|
12
11
|
function isBuiltIn(_constructor) {
|
|
13
12
|
for (var i = 0; i < builtIns.length; i++) {
|
|
14
13
|
if (builtIns[i] === _constructor) return true;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
return false;
|
|
18
16
|
}
|
|
19
|
-
|
|
20
17
|
function typeString(obj) {
|
|
21
18
|
// [object Blah] -> Blah
|
|
22
19
|
var stringType = _toString.call(obj).slice(8, -1);
|
|
20
|
+
if (obj === null || obj === undefined) return stringType.toLowerCase();
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
26
23
|
var constructorType = obj.constructor;
|
|
27
24
|
if (constructorType && !isBuiltIn(constructorType)) return constructorType.name;
|
|
28
25
|
return stringType;
|
package/lib/validateDocument.js
CHANGED
|
@@ -7,92 +7,57 @@ exports.default = validateDocument;
|
|
|
7
7
|
exports.resolveTypeForArrayItem = resolveTypeForArrayItem;
|
|
8
8
|
exports.validateDocumentObservable = validateDocumentObservable;
|
|
9
9
|
exports.validateItem = validateItem;
|
|
10
|
-
|
|
11
10
|
var _uniqBy2 = _interopRequireDefault(require("lodash/uniqBy"));
|
|
12
|
-
|
|
13
11
|
var _flatten2 = _interopRequireDefault(require("lodash/flatten"));
|
|
14
|
-
|
|
15
12
|
var _types = require("@sanity/types");
|
|
16
|
-
|
|
17
13
|
var _rxjs = require("rxjs");
|
|
18
|
-
|
|
19
14
|
var _operators = require("rxjs/operators");
|
|
20
|
-
|
|
21
15
|
var _typeString = _interopRequireDefault(require("./util/typeString"));
|
|
22
|
-
|
|
23
16
|
var _requestIdleCallback = require("./util/requestIdleCallback");
|
|
24
|
-
|
|
25
17
|
var _ValidationError = _interopRequireDefault(require("./ValidationError"));
|
|
26
|
-
|
|
27
18
|
var _normalizeValidationRules = _interopRequireDefault(require("./util/normalizeValidationRules"));
|
|
28
|
-
|
|
29
19
|
var _excluded = ["value", "type", "path", "parent"];
|
|
30
|
-
|
|
31
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
32
|
-
|
|
33
21
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
34
|
-
|
|
35
22
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
36
|
-
|
|
37
23
|
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); }
|
|
38
|
-
|
|
39
24
|
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; }
|
|
40
|
-
|
|
41
25
|
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
42
|
-
|
|
43
26
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
44
|
-
|
|
45
27
|
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; }
|
|
46
|
-
|
|
47
28
|
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; }
|
|
48
|
-
|
|
49
29
|
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; }
|
|
50
|
-
|
|
51
30
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
52
|
-
|
|
53
31
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
54
|
-
|
|
55
32
|
var isRecord = maybeRecord => typeof maybeRecord === 'object' && maybeRecord !== null && !Array.isArray(maybeRecord);
|
|
56
|
-
|
|
57
33
|
var isNonNullable = value => value !== null && value !== undefined;
|
|
34
|
+
|
|
58
35
|
/**
|
|
59
36
|
* @internal
|
|
60
37
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
38
|
function resolveTypeForArrayItem(item, candidates) {
|
|
64
39
|
// if there is only one type available, assume that it's the correct one
|
|
65
40
|
if (candidates.length === 1) return candidates[0];
|
|
66
|
-
|
|
67
41
|
var itemType = (0, _types.isTypedObject)(item) && item._type;
|
|
68
|
-
|
|
69
42
|
var primitive = item === undefined || item === null || !itemType && (0, _typeString.default)(item).toLowerCase();
|
|
70
|
-
|
|
71
43
|
if (primitive && primitive !== 'object') {
|
|
72
44
|
return candidates.find(candidate => candidate.jsonType === primitive);
|
|
73
45
|
}
|
|
74
|
-
|
|
75
46
|
return candidates.find(candidate => {
|
|
76
47
|
var _candidate$type;
|
|
77
|
-
|
|
78
48
|
return ((_candidate$type = candidate.type) === null || _candidate$type === void 0 ? void 0 : _candidate$type.name) === itemType;
|
|
79
49
|
}) || candidates.find(candidate => candidate.name === itemType) || candidates.find(candidate => candidate.name === 'object' && primitive === 'object');
|
|
80
50
|
}
|
|
81
|
-
|
|
82
51
|
var EMPTY_MARKERS = [];
|
|
83
|
-
|
|
84
52
|
function validateDocument(doc, schema, context) {
|
|
85
53
|
return validateDocumentObservable(doc, schema, context).toPromise();
|
|
86
54
|
}
|
|
87
|
-
|
|
88
55
|
function validateDocumentObservable(doc, schema, context) {
|
|
89
56
|
var documentType = schema.get(doc._type);
|
|
90
|
-
|
|
91
57
|
if (!documentType) {
|
|
92
58
|
console.warn('Schema type for object type "%s" not found, skipping validation', doc._type);
|
|
93
59
|
return (0, _rxjs.of)(EMPTY_MARKERS);
|
|
94
60
|
}
|
|
95
|
-
|
|
96
61
|
return validateItemObservable({
|
|
97
62
|
parent: undefined,
|
|
98
63
|
value: doc,
|
|
@@ -110,6 +75,7 @@ function validateDocumentObservable(doc, schema, context) {
|
|
|
110
75
|
}]);
|
|
111
76
|
}));
|
|
112
77
|
}
|
|
78
|
+
|
|
113
79
|
/**
|
|
114
80
|
* this is used make optional properties required by replacing optionals with
|
|
115
81
|
* `T[P] | undefined`. this is used to prevent errors in `validateItem` where
|
|
@@ -118,46 +84,46 @@ function validateDocumentObservable(doc, schema, context) {
|
|
|
118
84
|
* https://medium.com/terria/typescript-transforming-optional-properties-to-required-properties-that-may-be-undefined-7482cb4e1585
|
|
119
85
|
*/
|
|
120
86
|
|
|
121
|
-
|
|
122
87
|
function validateItem(opts) {
|
|
123
88
|
return validateItemObservable(opts).toPromise();
|
|
124
89
|
}
|
|
125
|
-
|
|
126
90
|
function validateItemObservable(_ref) {
|
|
127
91
|
var value = _ref.value,
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
92
|
+
type = _ref.type,
|
|
93
|
+
_ref$path = _ref.path,
|
|
94
|
+
path = _ref$path === void 0 ? [] : _ref$path,
|
|
95
|
+
parent = _ref.parent,
|
|
96
|
+
restOfContext = _objectWithoutProperties(_ref, _excluded);
|
|
97
|
+
var rules = (0, _normalizeValidationRules.default)(type);
|
|
98
|
+
// run validation for the current value
|
|
136
99
|
var selfChecks = rules.map(rule => (0, _rxjs.defer)(() => rule.validate(value, _objectSpread(_objectSpread({}, restOfContext), {}, {
|
|
137
100
|
parent,
|
|
138
101
|
path,
|
|
139
102
|
type
|
|
140
|
-
}))));
|
|
103
|
+
}))));
|
|
141
104
|
|
|
105
|
+
// run validation for nested values (conditionally)
|
|
142
106
|
var fieldChecks = [];
|
|
143
107
|
var selfIsRequired = rules.some(rule => rule.isRequired());
|
|
144
|
-
var shouldRunNestedObjectValidation =
|
|
145
|
-
|
|
146
|
-
|
|
108
|
+
var shouldRunNestedObjectValidation =
|
|
109
|
+
// run nested validation for objects
|
|
110
|
+
(type === null || type === void 0 ? void 0 : type.jsonType) === 'object' && (
|
|
111
|
+
// if the value is truthy
|
|
112
|
+
!!value ||
|
|
113
|
+
// or
|
|
147
114
|
// (the value is null or undefined) and the top-level value is required
|
|
148
115
|
(value === null || value === undefined) && selfIsRequired);
|
|
149
|
-
|
|
150
116
|
if (shouldRunNestedObjectValidation) {
|
|
151
117
|
var fieldTypes = type.fields.reduce((acc, field) => {
|
|
152
118
|
acc[field.name] = field.type;
|
|
153
119
|
return acc;
|
|
154
|
-
}, {});
|
|
120
|
+
}, {});
|
|
155
121
|
|
|
122
|
+
// Validation for rules set at the object level with `Rule.fields({/* ... */})`
|
|
156
123
|
fieldChecks = fieldChecks.concat(rules.map(rule => rule._fieldRules).filter(isNonNullable).flatMap(fieldResults => Object.entries(fieldResults)).flatMap(_ref2 => {
|
|
157
124
|
var _ref3 = _slicedToArray(_ref2, 2),
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
125
|
+
name = _ref3[0],
|
|
126
|
+
validation = _ref3[1];
|
|
161
127
|
var fieldType = fieldTypes[name];
|
|
162
128
|
return (0, _normalizeValidationRules.default)(_objectSpread(_objectSpread({}, fieldType), {}, {
|
|
163
129
|
validation
|
|
@@ -169,22 +135,22 @@ function validateItemObservable(_ref) {
|
|
|
169
135
|
type: fieldType
|
|
170
136
|
})));
|
|
171
137
|
});
|
|
172
|
-
}));
|
|
138
|
+
}));
|
|
173
139
|
|
|
140
|
+
// Validation from each field's schema `validation: Rule => {/* ... */}` function
|
|
174
141
|
fieldChecks = fieldChecks.concat(type.fields.map(field => validateItemObservable(_objectSpread(_objectSpread({}, restOfContext), {}, {
|
|
175
142
|
parent: value,
|
|
176
143
|
value: isRecord(value) ? value[field.name] : undefined,
|
|
177
144
|
path: path.concat(field.name),
|
|
178
145
|
type: field.type
|
|
179
146
|
}))));
|
|
180
|
-
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// note: unlike objects, arrays should not run nested validation for undefined
|
|
181
150
|
// values because we won't have a valid path to put a marker (i.e. missing the
|
|
182
151
|
// key or index in the path) and the downstream form builder won't have a
|
|
183
152
|
// valid target component
|
|
184
|
-
|
|
185
|
-
|
|
186
153
|
var shouldRunNestedValidationForArrays = (type === null || type === void 0 ? void 0 : type.jsonType) === 'array' && Array.isArray(value);
|
|
187
|
-
|
|
188
154
|
if (shouldRunNestedValidationForArrays) {
|
|
189
155
|
fieldChecks = fieldChecks.concat(value.map(item => validateItemObservable(_objectSpread(_objectSpread({}, restOfContext), {}, {
|
|
190
156
|
parent: value,
|
|
@@ -194,16 +160,14 @@ function validateItemObservable(_ref) {
|
|
|
194
160
|
} : value.indexOf(item)),
|
|
195
161
|
type: resolveTypeForArrayItem(item, type.of)
|
|
196
162
|
}))));
|
|
197
|
-
}
|
|
198
|
-
// for a similar reason to arrays
|
|
199
|
-
|
|
163
|
+
}
|
|
200
164
|
|
|
165
|
+
// markDefs also do no run nested validation if the parent object is undefined
|
|
166
|
+
// for a similar reason to arrays
|
|
201
167
|
var shouldRunNestedValidationForMarkDefs = (0, _types.isBlock)(value) && value.markDefs.length && (0, _types.isBlockSchemaType)(type);
|
|
202
|
-
|
|
203
168
|
if (shouldRunNestedValidationForMarkDefs) {
|
|
204
169
|
var _type$fields = _slicedToArray(type.fields, 1),
|
|
205
|
-
|
|
206
|
-
|
|
170
|
+
spanChildrenField = _type$fields[0];
|
|
207
171
|
var spanType = spanChildrenField.type.of.find(_types.isSpanSchemaType);
|
|
208
172
|
var annotations = ((spanType === null || spanType === void 0 ? void 0 : spanType.annotations) || []).reduce((acc, annotationType) => {
|
|
209
173
|
acc.set(annotationType.name, annotationType);
|
|
@@ -218,18 +182,15 @@ function validateItemObservable(_ref) {
|
|
|
218
182
|
type: annotations.get(markDef._type)
|
|
219
183
|
}))));
|
|
220
184
|
}
|
|
221
|
-
|
|
222
185
|
return (0, _rxjs.defer)(() => (0, _rxjs.merge)([...selfChecks, ...fieldChecks])).pipe((0, _operators.mergeMap)(validateNode => (0, _rxjs.concat)(idle(), validateNode), 40), (0, _operators.mergeAll)(), (0, _operators.toArray)(), (0, _operators.map)(_flatten2.default), (0, _operators.map)(results => {
|
|
223
186
|
// run `uniqBy` if `_fieldRules` are present because they can
|
|
224
187
|
// cause repeat markers
|
|
225
188
|
if (rules.some(rule => rule._fieldRules)) {
|
|
226
189
|
return (0, _uniqBy2.default)(results, rule => JSON.stringify(rule));
|
|
227
190
|
}
|
|
228
|
-
|
|
229
191
|
return results;
|
|
230
192
|
}));
|
|
231
193
|
}
|
|
232
|
-
|
|
233
194
|
function idle(timeout) {
|
|
234
195
|
return new _rxjs.Observable(observer => {
|
|
235
196
|
var handle = (0, _requestIdleCallback.requestIdleCallback)(() => {
|