ascertain 0.11.0 → 0.12.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/build/index.js +3 -54
- package/package.json +6 -6
package/build/index.js
CHANGED
|
@@ -4,103 +4,78 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.toggle = exports.or = exports.optional = exports.default = exports.ascertain = exports.as = exports.and = exports.$values = exports.$keys = void 0;
|
|
7
|
-
|
|
8
7
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
9
|
-
|
|
10
8
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
11
|
-
|
|
12
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
13
|
-
|
|
9
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
14
10
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
15
|
-
|
|
11
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
12
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
16
13
|
var AssertError = /*#__PURE__*/function () {
|
|
17
14
|
function AssertError(value, expected, path) {
|
|
18
15
|
var subject = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'value';
|
|
19
|
-
|
|
20
16
|
_classCallCheck(this, AssertError);
|
|
21
|
-
|
|
22
17
|
this.value = value;
|
|
23
18
|
this.expected = expected;
|
|
24
19
|
this.path = path;
|
|
25
20
|
this.subject = subject;
|
|
26
21
|
}
|
|
27
|
-
|
|
28
22
|
_createClass(AssertError, [{
|
|
29
23
|
key: "toString",
|
|
30
24
|
value: function toString() {
|
|
31
25
|
return "Invalid ".concat(this.subject, " ").concat(JSON.stringify(this.value), " specified by path ").concat(this.path, " expected ").concat(this.expected);
|
|
32
26
|
}
|
|
33
27
|
}]);
|
|
34
|
-
|
|
35
28
|
return AssertError;
|
|
36
29
|
}();
|
|
37
|
-
|
|
38
30
|
function findFirstError(array, map) {
|
|
39
31
|
for (var i = 0; i < array.length; i++) {
|
|
40
32
|
var error = map(array[i], i);
|
|
41
|
-
|
|
42
33
|
if (error) {
|
|
43
34
|
return error;
|
|
44
35
|
}
|
|
45
36
|
}
|
|
46
|
-
|
|
47
37
|
return false;
|
|
48
38
|
}
|
|
49
|
-
|
|
50
39
|
function findNotError(array, map) {
|
|
51
40
|
var errors = [];
|
|
52
|
-
|
|
53
41
|
for (var i = 0; i < array.length; i++) {
|
|
54
42
|
var error = map(array[i], i);
|
|
55
|
-
|
|
56
43
|
if (!error) {
|
|
57
44
|
return false;
|
|
58
45
|
} else {
|
|
59
46
|
errors.push(error);
|
|
60
47
|
}
|
|
61
48
|
}
|
|
62
|
-
|
|
63
49
|
return errors.reduce(function (result, error) {
|
|
64
50
|
return new AssertError(result.value, [result.expected, error.expected].join(' or '), result.path, result.string);
|
|
65
51
|
});
|
|
66
52
|
}
|
|
67
|
-
|
|
68
53
|
function Optional(schema) {
|
|
69
54
|
this.schema = schema;
|
|
70
55
|
}
|
|
71
|
-
|
|
72
56
|
function And(schema) {
|
|
73
57
|
this.schema = schema;
|
|
74
58
|
}
|
|
75
|
-
|
|
76
59
|
function Or(schema) {
|
|
77
60
|
this.schema = schema;
|
|
78
61
|
}
|
|
79
|
-
|
|
80
62
|
var optional = function optional(schema) {
|
|
81
63
|
return new Optional(schema);
|
|
82
64
|
};
|
|
83
|
-
|
|
84
65
|
exports.optional = optional;
|
|
85
|
-
|
|
86
66
|
var and = function and() {
|
|
87
67
|
for (var _len = arguments.length, schema = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
88
68
|
schema[_key] = arguments[_key];
|
|
89
69
|
}
|
|
90
|
-
|
|
91
70
|
return new And(schema);
|
|
92
71
|
};
|
|
93
|
-
|
|
94
72
|
exports.and = and;
|
|
95
|
-
|
|
96
73
|
var or = function or() {
|
|
97
74
|
for (var _len2 = arguments.length, schema = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
98
75
|
schema[_key2] = arguments[_key2];
|
|
99
76
|
}
|
|
100
|
-
|
|
101
77
|
return new Or(schema);
|
|
102
78
|
};
|
|
103
|
-
|
|
104
79
|
exports.or = or;
|
|
105
80
|
var $keys = Symbol.for('@@keys');
|
|
106
81
|
exports.$keys = $keys;
|
|
@@ -124,7 +99,6 @@ var as = {
|
|
|
124
99
|
},
|
|
125
100
|
array: function array(value, delimiter) {
|
|
126
101
|
var _value$split;
|
|
127
|
-
|
|
128
102
|
return (_value$split = value === null || value === void 0 ? void 0 : value.split(delimiter)) !== null && _value$split !== void 0 ? _value$split : undefined;
|
|
129
103
|
},
|
|
130
104
|
json: function json(value) {
|
|
@@ -143,59 +117,45 @@ var as = {
|
|
|
143
117
|
}
|
|
144
118
|
};
|
|
145
119
|
exports.as = as;
|
|
146
|
-
|
|
147
120
|
var toggle = function toggle(key, cases, defaultKey) {
|
|
148
121
|
var _ref, _cases$key;
|
|
149
|
-
|
|
150
122
|
return (_ref = (_cases$key = cases[key]) !== null && _cases$key !== void 0 ? _cases$key : cases[defaultKey]) !== null && _ref !== void 0 ? _ref : undefined;
|
|
151
123
|
};
|
|
152
|
-
|
|
153
124
|
exports.toggle = toggle;
|
|
154
|
-
|
|
155
125
|
function certain(target, schema, path, optional) {
|
|
156
126
|
if (schema === null || typeof schema === 'undefined') {
|
|
157
127
|
return new AssertError(schema, 'any value', path, 'schema value');
|
|
158
128
|
}
|
|
159
|
-
|
|
160
129
|
if (schema instanceof Optional) {
|
|
161
130
|
return certain(target, schema.schema, path, true);
|
|
162
131
|
}
|
|
163
|
-
|
|
164
132
|
var isValue = target !== null && typeof target !== 'undefined';
|
|
165
|
-
|
|
166
133
|
if (!isValue && optional) {
|
|
167
134
|
return;
|
|
168
135
|
}
|
|
169
|
-
|
|
170
136
|
if (schema instanceof Or) {
|
|
171
137
|
if (!schema.schema.length) {
|
|
172
138
|
return new AssertError(target, 'values', path, 'OR schema');
|
|
173
139
|
}
|
|
174
|
-
|
|
175
140
|
return findNotError(schema.schema, function (schema) {
|
|
176
141
|
return certain(target, schema, path);
|
|
177
142
|
});
|
|
178
143
|
}
|
|
179
|
-
|
|
180
144
|
if (schema instanceof And) {
|
|
181
145
|
if (!schema.schema.length) {
|
|
182
146
|
return new AssertError(target, 'values', path, 'AND schema');
|
|
183
147
|
}
|
|
184
|
-
|
|
185
148
|
return findFirstError(schema.schema, function (schema) {
|
|
186
149
|
return certain(target, schema, path);
|
|
187
150
|
});
|
|
188
151
|
}
|
|
189
|
-
|
|
190
152
|
if (typeof schema === 'function') {
|
|
191
153
|
if (!isValue) {
|
|
192
154
|
return new AssertError(target, schema.name, path);
|
|
193
155
|
}
|
|
194
|
-
|
|
195
156
|
if (_typeof(target) === 'object' && !(target instanceof schema)) {
|
|
196
157
|
return new AssertError(target, schema.name, path);
|
|
197
158
|
}
|
|
198
|
-
|
|
199
159
|
if (_typeof(target) !== 'object' && target.constructor !== schema) {
|
|
200
160
|
return new AssertError(target, schema.name, path);
|
|
201
161
|
}
|
|
@@ -203,7 +163,6 @@ function certain(target, schema, path, optional) {
|
|
|
203
163
|
if (!Array.isArray(target)) {
|
|
204
164
|
return new AssertError(target, schema.constructor.name, path);
|
|
205
165
|
}
|
|
206
|
-
|
|
207
166
|
return findFirstError(target, function (value, idx) {
|
|
208
167
|
return findNotError(schema, function (itemSchemaType) {
|
|
209
168
|
return certain(value, itemSchemaType, "".concat(path, ".").concat(idx));
|
|
@@ -218,34 +177,27 @@ function certain(target, schema, path, optional) {
|
|
|
218
177
|
if (_typeof(target) !== 'object') {
|
|
219
178
|
return new AssertError(target, schema.constructor.name, path);
|
|
220
179
|
}
|
|
221
|
-
|
|
222
180
|
if (target === null) {
|
|
223
181
|
return new AssertError(target, 'an object', path);
|
|
224
182
|
}
|
|
225
|
-
|
|
226
183
|
if ($keys in schema) {
|
|
227
184
|
var targetKeys = Object.keys(target);
|
|
228
185
|
var assertError = findFirstError(targetKeys, function (targetKey) {
|
|
229
186
|
return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
|
|
230
187
|
});
|
|
231
|
-
|
|
232
188
|
if (assertError) {
|
|
233
189
|
return assertError;
|
|
234
190
|
}
|
|
235
191
|
}
|
|
236
|
-
|
|
237
192
|
if ($values in schema) {
|
|
238
193
|
var _targetKeys = Object.keys(target);
|
|
239
|
-
|
|
240
194
|
var _assertError = findFirstError(_targetKeys, function (targetKey) {
|
|
241
195
|
return certain(target[targetKey], schema[$values], "".concat(path, ".").concat(targetKey));
|
|
242
196
|
});
|
|
243
|
-
|
|
244
197
|
if (_assertError) {
|
|
245
198
|
return _assertError;
|
|
246
199
|
}
|
|
247
200
|
}
|
|
248
|
-
|
|
249
201
|
return findFirstError(Object.keys(schema), function (key) {
|
|
250
202
|
return certain(target[key], schema[key], "".concat(path, ".").concat(key));
|
|
251
203
|
});
|
|
@@ -254,16 +206,13 @@ function certain(target, schema, path, optional) {
|
|
|
254
206
|
return new AssertError(target, schema, path);
|
|
255
207
|
}
|
|
256
208
|
}
|
|
257
|
-
|
|
258
209
|
var ascertain = function ascertain(schema, data) {
|
|
259
210
|
var rootName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '[root]';
|
|
260
211
|
var result = certain(data, schema, rootName);
|
|
261
|
-
|
|
262
212
|
if (result instanceof AssertError) {
|
|
263
213
|
throw new TypeError(result.toString());
|
|
264
214
|
}
|
|
265
215
|
};
|
|
266
|
-
|
|
267
216
|
exports.ascertain = ascertain;
|
|
268
217
|
var _default = ascertain;
|
|
269
218
|
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ascertain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "0-Deps, simple, fast, for browser and node js object schema validator",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/3axap4eHko/ascertain#readme",
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@babel/cli": "^7.
|
|
37
|
-
"@babel/core": "^7.
|
|
38
|
-
"@babel/preset-env": "^7.
|
|
39
|
-
"babel-jest": "^29.0
|
|
40
|
-
"jest": "^29.0
|
|
36
|
+
"@babel/cli": "^7.21.0",
|
|
37
|
+
"@babel/core": "^7.21.4",
|
|
38
|
+
"@babel/preset-env": "^7.21.4",
|
|
39
|
+
"babel-jest": "^29.5.0",
|
|
40
|
+
"jest": "^29.5.0"
|
|
41
41
|
}
|
|
42
42
|
}
|