ascertain 0.9.7 → 0.9.8
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 +0 -49
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -4,103 +4,76 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
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
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, descriptor.key, descriptor); } }
|
|
13
|
-
|
|
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
|
-
|
|
16
11
|
var AssertError = /*#__PURE__*/function () {
|
|
17
12
|
function AssertError(value, expected, path) {
|
|
18
13
|
var subject = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'value';
|
|
19
|
-
|
|
20
14
|
_classCallCheck(this, AssertError);
|
|
21
|
-
|
|
22
15
|
this.value = value;
|
|
23
16
|
this.expected = expected;
|
|
24
17
|
this.path = path;
|
|
25
18
|
this.subject = subject;
|
|
26
19
|
}
|
|
27
|
-
|
|
28
20
|
_createClass(AssertError, [{
|
|
29
21
|
key: "toString",
|
|
30
22
|
value: function toString() {
|
|
31
23
|
return "Invalid ".concat(this.subject, " ").concat(JSON.stringify(this.value), " specified by path ").concat(this.path, " expected ").concat(this.expected);
|
|
32
24
|
}
|
|
33
25
|
}]);
|
|
34
|
-
|
|
35
26
|
return AssertError;
|
|
36
27
|
}();
|
|
37
|
-
|
|
38
28
|
function findFirstError(array, map) {
|
|
39
29
|
for (var i = 0; i < array.length; i++) {
|
|
40
30
|
var error = map(array[i], i);
|
|
41
|
-
|
|
42
31
|
if (error) {
|
|
43
32
|
return error;
|
|
44
33
|
}
|
|
45
34
|
}
|
|
46
|
-
|
|
47
35
|
return false;
|
|
48
36
|
}
|
|
49
|
-
|
|
50
37
|
function findNotError(array, map) {
|
|
51
38
|
var errors = [];
|
|
52
|
-
|
|
53
39
|
for (var i = 0; i < array.length; i++) {
|
|
54
40
|
var error = map(array[i], i);
|
|
55
|
-
|
|
56
41
|
if (!error) {
|
|
57
42
|
return false;
|
|
58
43
|
} else {
|
|
59
44
|
errors.push(error);
|
|
60
45
|
}
|
|
61
46
|
}
|
|
62
|
-
|
|
63
47
|
return errors.reduce(function (result, error) {
|
|
64
48
|
return new AssertError(result.value, [result.expected, error.expected].join(' or '), result.path, result.string);
|
|
65
49
|
});
|
|
66
50
|
}
|
|
67
|
-
|
|
68
51
|
function Optional(schema) {
|
|
69
52
|
this.schema = schema;
|
|
70
53
|
}
|
|
71
|
-
|
|
72
54
|
function And(schema) {
|
|
73
55
|
this.schema = schema;
|
|
74
56
|
}
|
|
75
|
-
|
|
76
57
|
function Or(schema) {
|
|
77
58
|
this.schema = schema;
|
|
78
59
|
}
|
|
79
|
-
|
|
80
60
|
var optional = function optional(schema) {
|
|
81
61
|
return new Optional(schema);
|
|
82
62
|
};
|
|
83
|
-
|
|
84
63
|
exports.optional = optional;
|
|
85
|
-
|
|
86
64
|
var and = function and() {
|
|
87
65
|
for (var _len = arguments.length, schema = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
88
66
|
schema[_key] = arguments[_key];
|
|
89
67
|
}
|
|
90
|
-
|
|
91
68
|
return new And(schema);
|
|
92
69
|
};
|
|
93
|
-
|
|
94
70
|
exports.and = and;
|
|
95
|
-
|
|
96
71
|
var or = function or() {
|
|
97
72
|
for (var _len2 = arguments.length, schema = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
98
73
|
schema[_key2] = arguments[_key2];
|
|
99
74
|
}
|
|
100
|
-
|
|
101
75
|
return new Or(schema);
|
|
102
76
|
};
|
|
103
|
-
|
|
104
77
|
exports.or = or;
|
|
105
78
|
var $keys = Symbol.for('@@keys');
|
|
106
79
|
exports.$keys = $keys;
|
|
@@ -126,51 +99,40 @@ var as = {
|
|
|
126
99
|
}
|
|
127
100
|
};
|
|
128
101
|
exports.as = as;
|
|
129
|
-
|
|
130
102
|
function certain(target, schema, path, optional) {
|
|
131
103
|
if (schema === null || typeof schema === 'undefined') {
|
|
132
104
|
return new AssertError(schema, 'any value', path, 'schema value');
|
|
133
105
|
}
|
|
134
|
-
|
|
135
106
|
if (schema instanceof Optional) {
|
|
136
107
|
return certain(target, schema.schema, path, true);
|
|
137
108
|
}
|
|
138
|
-
|
|
139
109
|
var isValue = target !== null && typeof target !== 'undefined';
|
|
140
|
-
|
|
141
110
|
if (!isValue && optional) {
|
|
142
111
|
return;
|
|
143
112
|
}
|
|
144
|
-
|
|
145
113
|
if (schema instanceof Or) {
|
|
146
114
|
if (!schema.schema.length) {
|
|
147
115
|
return new AssertError(target, 'values', path, 'OR schema');
|
|
148
116
|
}
|
|
149
|
-
|
|
150
117
|
return findNotError(schema.schema, function (schema) {
|
|
151
118
|
return certain(target, schema, path);
|
|
152
119
|
});
|
|
153
120
|
}
|
|
154
|
-
|
|
155
121
|
if (schema instanceof And) {
|
|
156
122
|
if (!schema.schema.length) {
|
|
157
123
|
return new AssertError(target, 'values', path, 'AND schema');
|
|
158
124
|
}
|
|
159
|
-
|
|
160
125
|
return findFirstError(schema.schema, function (schema) {
|
|
161
126
|
return certain(target, schema, path);
|
|
162
127
|
});
|
|
163
128
|
}
|
|
164
|
-
|
|
165
129
|
if (typeof schema === 'function') {
|
|
166
130
|
if (!isValue) {
|
|
167
131
|
return new AssertError(target, schema.name, path);
|
|
168
132
|
}
|
|
169
|
-
|
|
170
133
|
if (_typeof(target) === 'object' && !(target instanceof schema)) {
|
|
171
134
|
return new AssertError(target, schema.name, path);
|
|
172
135
|
}
|
|
173
|
-
|
|
174
136
|
if (_typeof(target) !== 'object' && target.constructor !== schema) {
|
|
175
137
|
return new AssertError(target, schema.name, path);
|
|
176
138
|
}
|
|
@@ -178,7 +140,6 @@ function certain(target, schema, path, optional) {
|
|
|
178
140
|
if (!Array.isArray(target)) {
|
|
179
141
|
return new AssertError(target, schema.constructor.name, path);
|
|
180
142
|
}
|
|
181
|
-
|
|
182
143
|
return findFirstError(target, function (value, idx) {
|
|
183
144
|
return findNotError(schema, function (itemSchemaType) {
|
|
184
145
|
return certain(value, itemSchemaType, "".concat(path, ".").concat(idx));
|
|
@@ -193,34 +154,27 @@ function certain(target, schema, path, optional) {
|
|
|
193
154
|
if (_typeof(target) !== 'object') {
|
|
194
155
|
return new AssertError(target, schema.constructor.name, path);
|
|
195
156
|
}
|
|
196
|
-
|
|
197
157
|
if (target === null) {
|
|
198
158
|
return new AssertError(target, 'an object', path);
|
|
199
159
|
}
|
|
200
|
-
|
|
201
160
|
if ($keys in schema) {
|
|
202
161
|
var targetKeys = Object.keys(target);
|
|
203
162
|
var assertError = findFirstError(targetKeys, function (targetKey) {
|
|
204
163
|
return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
|
|
205
164
|
});
|
|
206
|
-
|
|
207
165
|
if (assertError) {
|
|
208
166
|
return assertError;
|
|
209
167
|
}
|
|
210
168
|
}
|
|
211
|
-
|
|
212
169
|
if ($values in schema) {
|
|
213
170
|
var _targetKeys = Object.keys(target);
|
|
214
|
-
|
|
215
171
|
var _assertError = findFirstError(_targetKeys, function (targetKey) {
|
|
216
172
|
return certain(target[targetKey], schema[$values], "".concat(path, ".").concat(targetKey));
|
|
217
173
|
});
|
|
218
|
-
|
|
219
174
|
if (_assertError) {
|
|
220
175
|
return _assertError;
|
|
221
176
|
}
|
|
222
177
|
}
|
|
223
|
-
|
|
224
178
|
return findFirstError(Object.keys(schema), function (key) {
|
|
225
179
|
return certain(target[key], schema[key], "".concat(path, ".").concat(key));
|
|
226
180
|
});
|
|
@@ -229,16 +183,13 @@ function certain(target, schema, path, optional) {
|
|
|
229
183
|
return new AssertError(target, schema, path);
|
|
230
184
|
}
|
|
231
185
|
}
|
|
232
|
-
|
|
233
186
|
var ascertain = function ascertain(schema, data) {
|
|
234
187
|
var rootName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '[root]';
|
|
235
188
|
var result = certain(data, schema, rootName);
|
|
236
|
-
|
|
237
189
|
if (result instanceof AssertError) {
|
|
238
190
|
throw new TypeError(result.toString());
|
|
239
191
|
}
|
|
240
192
|
};
|
|
241
|
-
|
|
242
193
|
exports.ascertain = ascertain;
|
|
243
194
|
var _default = ascertain;
|
|
244
195
|
exports.default = _default;
|