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