check-types-mini 6.1.0 → 7.0.3
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/CHANGELOG.md +30 -0
- package/README.md +4 -0
- package/dist/check-types-mini.esm.js +5 -5
- package/dist/check-types-mini.umd.js +7 -7
- package/package.json +54 -46
- package/dist/check-types-mini.cjs.js +0 -203
- package/dist/check-types-mini.dev.umd.js +0 -4432
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name check-types-mini
|
|
3
|
-
* @fileoverview Validate options object
|
|
4
|
-
* @version 6.1.0
|
|
5
|
-
* @author Roy Revelt, Codsen Ltd
|
|
6
|
-
* @license MIT
|
|
7
|
-
* {@link https://codsen.com/os/check-types-mini/}
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
13
|
-
|
|
14
|
-
var _typeof = require('@babel/runtime/helpers/typeof');
|
|
15
|
-
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
16
|
-
var typ = require('type-detect');
|
|
17
|
-
var pullAll = require('lodash.pullall');
|
|
18
|
-
var astMonkeyTraverse = require('ast-monkey-traverse');
|
|
19
|
-
var intersection = require('lodash.intersection');
|
|
20
|
-
var arrayiffyIfString = require('arrayiffy-if-string');
|
|
21
|
-
var objectPath = require('object-path');
|
|
22
|
-
var matcher = require('matcher');
|
|
23
|
-
|
|
24
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
25
|
-
|
|
26
|
-
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
|
|
27
|
-
var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
|
|
28
|
-
var typ__default = /*#__PURE__*/_interopDefaultLegacy(typ);
|
|
29
|
-
var pullAll__default = /*#__PURE__*/_interopDefaultLegacy(pullAll);
|
|
30
|
-
var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection);
|
|
31
|
-
var objectPath__default = /*#__PURE__*/_interopDefaultLegacy(objectPath);
|
|
32
|
-
var matcher__default = /*#__PURE__*/_interopDefaultLegacy(matcher);
|
|
33
|
-
|
|
34
|
-
var defaults = {
|
|
35
|
-
ignoreKeys: [],
|
|
36
|
-
ignorePaths: [],
|
|
37
|
-
acceptArrays: false,
|
|
38
|
-
acceptArraysIgnore: [],
|
|
39
|
-
enforceStrictKeyset: true,
|
|
40
|
-
schema: {},
|
|
41
|
-
msg: "check-types-mini",
|
|
42
|
-
optsVarName: "opts"
|
|
43
|
-
};
|
|
44
|
-
function internalApi(obj, ref, originalOptions) {
|
|
45
|
-
function existy(something) {
|
|
46
|
-
return something != null;
|
|
47
|
-
}
|
|
48
|
-
function isObj(something) {
|
|
49
|
-
return typ__default['default'](something) === "Object";
|
|
50
|
-
}
|
|
51
|
-
function pullAllWithGlob(originalInput, toBeRemoved) {
|
|
52
|
-
if (typeof toBeRemoved === "string") {
|
|
53
|
-
toBeRemoved = arrayiffyIfString.arrayiffy(toBeRemoved);
|
|
54
|
-
}
|
|
55
|
-
return Array.from(originalInput).filter(function (originalVal) {
|
|
56
|
-
return !toBeRemoved.some(function (remVal) {
|
|
57
|
-
return matcher__default['default'].isMatch(originalVal, remVal, {
|
|
58
|
-
caseSensitive: true
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
var hasKey = Object.prototype.hasOwnProperty;
|
|
64
|
-
var NAMESFORANYTYPE = ["any", "anything", "every", "everything", "all", "whatever", "whatevs"];
|
|
65
|
-
if (!existy(obj)) {
|
|
66
|
-
throw new Error("check-types-mini: [THROW_ID_01] First argument is missing!");
|
|
67
|
-
}
|
|
68
|
-
var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults), originalOptions);
|
|
69
|
-
if (typeof opts.ignoreKeys === "string") {
|
|
70
|
-
opts.ignoreKeys = [opts.ignoreKeys];
|
|
71
|
-
}
|
|
72
|
-
if (typeof opts.ignorePaths === "string") {
|
|
73
|
-
opts.ignorePaths = [opts.ignorePaths];
|
|
74
|
-
}
|
|
75
|
-
if (typeof opts.acceptArraysIgnore === "string") {
|
|
76
|
-
opts.acceptArraysIgnore = [opts.acceptArraysIgnore];
|
|
77
|
-
}
|
|
78
|
-
opts.msg = "".concat(opts.msg).trim();
|
|
79
|
-
if (opts.msg[opts.msg.length - 1] === ":") {
|
|
80
|
-
opts.msg = opts.msg.slice(0, opts.msg.length - 1).trim();
|
|
81
|
-
}
|
|
82
|
-
if (isObj(opts.schema)) {
|
|
83
|
-
Object.keys(opts.schema).forEach(function (oneKey) {
|
|
84
|
-
if (isObj(opts.schema[oneKey])) {
|
|
85
|
-
var tempObj = {};
|
|
86
|
-
astMonkeyTraverse.traverse(opts.schema[oneKey], function (key, val, innerObj) {
|
|
87
|
-
var current = val !== undefined ? val : key;
|
|
88
|
-
if (!Array.isArray(current) && !isObj(current)) {
|
|
89
|
-
tempObj["".concat(oneKey, ".").concat(innerObj.path)] = current;
|
|
90
|
-
}
|
|
91
|
-
return current;
|
|
92
|
-
});
|
|
93
|
-
delete opts.schema[oneKey];
|
|
94
|
-
opts.schema = _objectSpread__default['default'](_objectSpread__default['default']({}, opts.schema), tempObj);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
Object.keys(opts.schema).forEach(function (oneKey) {
|
|
98
|
-
if (!Array.isArray(opts.schema[oneKey])) {
|
|
99
|
-
opts.schema[oneKey] = [opts.schema[oneKey]];
|
|
100
|
-
}
|
|
101
|
-
opts.schema[oneKey] = opts.schema[oneKey].map(function (el) {
|
|
102
|
-
return "".concat(el).toLowerCase().trim();
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
} else if (opts.schema != null) {
|
|
106
|
-
throw new Error("check-types-mini: opts.schema was customised to ".concat(JSON.stringify(opts.schema, null, 0), " which is not object but ").concat(_typeof__default['default'](opts.schema)));
|
|
107
|
-
}
|
|
108
|
-
if (!existy(ref)) {
|
|
109
|
-
ref = {};
|
|
110
|
-
}
|
|
111
|
-
if (opts.enforceStrictKeyset) {
|
|
112
|
-
if (existy(opts.schema) && Object.keys(opts.schema).length > 0) {
|
|
113
|
-
if (ref && pullAllWithGlob(pullAll__default['default'](Object.keys(obj), Object.keys(ref).concat(Object.keys(opts.schema))), opts.ignoreKeys).length) {
|
|
114
|
-
var keys = pullAll__default['default'](Object.keys(obj), Object.keys(ref).concat(Object.keys(opts.schema)));
|
|
115
|
-
throw new TypeError("".concat(opts.msg, ": ").concat(opts.optsVarName, ".enforceStrictKeyset is on and the following key").concat(keys.length > 1 ? "s" : "", " ").concat(keys.length > 1 ? "are" : "is", " not covered by schema and/or reference objects: ").concat(keys.join(", ")));
|
|
116
|
-
}
|
|
117
|
-
} else if (isObj(ref) && Object.keys(ref).length > 0) {
|
|
118
|
-
if (pullAllWithGlob(pullAll__default['default'](Object.keys(obj), Object.keys(ref)), opts.ignoreKeys).length !== 0) {
|
|
119
|
-
var _keys = pullAll__default['default'](Object.keys(obj), Object.keys(ref));
|
|
120
|
-
throw new TypeError("".concat(opts.msg, ": The input object has key").concat(_keys.length > 1 ? "s" : "", " which ").concat(_keys.length > 1 ? "are" : "is", " not covered by the reference object: ").concat(_keys.join(", ")));
|
|
121
|
-
} else if (pullAllWithGlob(pullAll__default['default'](Object.keys(ref), Object.keys(obj)), opts.ignoreKeys).length !== 0) {
|
|
122
|
-
var _keys2 = pullAll__default['default'](Object.keys(ref), Object.keys(obj));
|
|
123
|
-
throw new TypeError("".concat(opts.msg, ": The reference object has key").concat(_keys2.length > 1 ? "s" : "", " which ").concat(_keys2.length > 1 ? "are" : "is", " not present in the input object: ").concat(_keys2.join(", ")));
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
throw new TypeError("".concat(opts.msg, ": Both ").concat(opts.optsVarName, ".schema and reference objects are missing! We don't have anything to match the keys as you requested via opts.enforceStrictKeyset!"));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
var ignoredPathsArr = [];
|
|
130
|
-
astMonkeyTraverse.traverse(obj, function (key, val, innerObj) {
|
|
131
|
-
var current = val;
|
|
132
|
-
var objKey = key;
|
|
133
|
-
if (innerObj.parentType === "array") {
|
|
134
|
-
objKey = undefined;
|
|
135
|
-
current = key;
|
|
136
|
-
}
|
|
137
|
-
if (Array.isArray(ignoredPathsArr) && ignoredPathsArr.length && ignoredPathsArr.some(function (path) {
|
|
138
|
-
return innerObj.path.startsWith(path);
|
|
139
|
-
})) {
|
|
140
|
-
return current;
|
|
141
|
-
}
|
|
142
|
-
if (objKey && opts.ignoreKeys.some(function (oneOfKeysToIgnore) {
|
|
143
|
-
return matcher__default['default'].isMatch(objKey, oneOfKeysToIgnore);
|
|
144
|
-
})) {
|
|
145
|
-
return current;
|
|
146
|
-
}
|
|
147
|
-
if (opts.ignorePaths.some(function (oneOfPathsToIgnore) {
|
|
148
|
-
return matcher__default['default'].isMatch(innerObj.path, oneOfPathsToIgnore);
|
|
149
|
-
})) {
|
|
150
|
-
return current;
|
|
151
|
-
}
|
|
152
|
-
var isNotAnArrayChild = !(!isObj(current) && !Array.isArray(current) && Array.isArray(innerObj.parent));
|
|
153
|
-
var optsSchemaHasThisPathDefined = false;
|
|
154
|
-
if (isObj(opts.schema) && hasKey.call(opts.schema, innerObj.path)) {
|
|
155
|
-
optsSchemaHasThisPathDefined = true;
|
|
156
|
-
}
|
|
157
|
-
var refHasThisPathDefined = false;
|
|
158
|
-
if (isObj(ref) && objectPath__default['default'].has(ref, innerObj.path)) {
|
|
159
|
-
refHasThisPathDefined = true;
|
|
160
|
-
}
|
|
161
|
-
if (opts.enforceStrictKeyset && isNotAnArrayChild && !optsSchemaHasThisPathDefined && !refHasThisPathDefined) {
|
|
162
|
-
throw new TypeError("".concat(opts.msg, ": ").concat(opts.optsVarName, ".").concat(innerObj.path, " is neither covered by reference object (second input argument), nor ").concat(opts.optsVarName, ".schema! To stop this error, turn off ").concat(opts.optsVarName, ".enforceStrictKeyset or provide some type reference (2nd argument or ").concat(opts.optsVarName, ".schema).\n\nDebug info:\n\nobj = ").concat(JSON.stringify(obj, null, 4), "\n\nref = ").concat(JSON.stringify(ref, null, 4), "\n\ninnerObj = ").concat(JSON.stringify(innerObj, null, 4), "\n\nopts = ").concat(JSON.stringify(opts, null, 4), "\n\ncurrent = ").concat(JSON.stringify(current, null, 4), "\n\n"));
|
|
163
|
-
} else if (optsSchemaHasThisPathDefined) {
|
|
164
|
-
var currentKeysSchema = arrayiffyIfString.arrayiffy(opts.schema[innerObj.path]).map(function (el) {
|
|
165
|
-
return "".concat(el).toLowerCase();
|
|
166
|
-
});
|
|
167
|
-
objectPath__default['default'].set(opts.schema, innerObj.path, currentKeysSchema);
|
|
168
|
-
if (!intersection__default['default'](currentKeysSchema, NAMESFORANYTYPE).length) {
|
|
169
|
-
if (current !== true && current !== false && !currentKeysSchema.includes(typ__default['default'](current).toLowerCase()) || (current === true || current === false) && !currentKeysSchema.includes(String(current)) && !currentKeysSchema.includes("boolean")) {
|
|
170
|
-
if (Array.isArray(current) && opts.acceptArrays) {
|
|
171
|
-
for (var i = 0, len = current.length; i < len; i++) {
|
|
172
|
-
if (!currentKeysSchema.includes(typ__default['default'](current[i]).toLowerCase())) {
|
|
173
|
-
throw new TypeError("".concat(opts.msg, ": ").concat(opts.optsVarName, ".").concat(innerObj.path, ".").concat(i, ", the ").concat(i, "th element (equal to ").concat(JSON.stringify(current[i], null, 0), ") is of a type ").concat(typ__default['default'](current[i]).toLowerCase(), ", but only the following are allowed by the ").concat(opts.optsVarName, ".schema: ").concat(currentKeysSchema.join(", ")));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
} else {
|
|
177
|
-
throw new TypeError("".concat(opts.msg, ": ").concat(opts.optsVarName, ".").concat(innerObj.path, " was customised to ").concat(typ__default['default'](current) !== "string" ? '"' : "").concat(JSON.stringify(current, null, 0)).concat(typ__default['default'](current) !== "string" ? '"' : "", " (type: ").concat(typ__default['default'](current).toLowerCase(), ") which is not among the allowed types in schema (which is equal to ").concat(JSON.stringify(currentKeysSchema, null, 0), ")"));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
} else {
|
|
181
|
-
ignoredPathsArr.push(innerObj.path);
|
|
182
|
-
}
|
|
183
|
-
} else if (ref && isObj(ref) && refHasThisPathDefined) {
|
|
184
|
-
var compareTo = objectPath__default['default'].get(ref, innerObj.path);
|
|
185
|
-
if (opts.acceptArrays && Array.isArray(current) && !opts.acceptArraysIgnore.includes(key)) {
|
|
186
|
-
var allMatch = current.every(function (el) {
|
|
187
|
-
return typ__default['default'](el).toLowerCase() === typ__default['default'](ref[key]).toLowerCase();
|
|
188
|
-
});
|
|
189
|
-
if (!allMatch) {
|
|
190
|
-
throw new TypeError("".concat(opts.msg, ": ").concat(opts.optsVarName, ".").concat(innerObj.path, " was customised to be array, but not all of its elements are ").concat(typ__default['default'](ref[key]).toLowerCase(), "-type"));
|
|
191
|
-
}
|
|
192
|
-
} else if (typ__default['default'](current) !== typ__default['default'](compareTo)) {
|
|
193
|
-
throw new TypeError("".concat(opts.msg, ": ").concat(opts.optsVarName, ".").concat(innerObj.path, " was customised to ").concat(typ__default['default'](current).toLowerCase() === "string" ? "" : '"').concat(JSON.stringify(current, null, 0)).concat(typ__default['default'](current).toLowerCase() === "string" ? "" : '"', " which is not ").concat(typ__default['default'](compareTo).toLowerCase(), " but ").concat(typ__default['default'](current).toLowerCase()));
|
|
194
|
-
}
|
|
195
|
-
} else ;
|
|
196
|
-
return current;
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
function checkTypesMini(obj, ref, originalOptions) {
|
|
200
|
-
return internalApi(obj, ref, originalOptions);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
exports.checkTypesMini = checkTypesMini;
|