ascertain 0.10.0 → 0.10.2
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/README.md +2 -0
- package/build/index.d.ts +2 -1
- package/build/index.js +10 -56
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,7 @@ const data = {
|
|
|
49
49
|
},
|
|
50
50
|
// fault tolernat type casting
|
|
51
51
|
parsedNumber: as.number('1'),
|
|
52
|
+
parsedString: as.number('string'),
|
|
52
53
|
parsedBoolean: as.boolean('false'),
|
|
53
54
|
parsedArray: as.array('1,2,3,4,5', ','),
|
|
54
55
|
parsedJSON: as.json('{ "number": 1 }'),
|
|
@@ -78,6 +79,7 @@ const schema: Schema<typeof data> = {
|
|
|
78
79
|
[$values]: Number
|
|
79
80
|
},
|
|
80
81
|
parsedNumber: Number,
|
|
82
|
+
parsedString: String,
|
|
81
83
|
parsedBoolean: Boolean,
|
|
82
84
|
parsedArray: [String],
|
|
83
85
|
parsedJSON: {
|
package/build/index.d.ts
CHANGED
|
@@ -14,11 +14,12 @@ export function and<T = any>(...schema: Schema<T>[]): Schema<any>;
|
|
|
14
14
|
export function or<T = any>(...schemas: Schema<T>[]): Schema<any>;
|
|
15
15
|
|
|
16
16
|
interface As {
|
|
17
|
+
string: (value: string | undefined) => string,
|
|
17
18
|
number: (value: string | undefined) => number,
|
|
18
19
|
boolean: (value: string | undefined) => boolean,
|
|
19
20
|
array: (value: string | undefined, delimiter: string | RegExp) => string[];
|
|
20
21
|
json: <T>(value: string | undefined) => T;
|
|
21
|
-
base64: (value: string | undefined) => string
|
|
22
|
+
base64: (value: string | undefined) => string;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
export const as: As;
|
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;
|
|
@@ -112,15 +87,19 @@ var fromBase64 = typeof Buffer === 'undefined' ? function (value) {
|
|
|
112
87
|
return Buffer.from(value, 'base64').toString('utf-8');
|
|
113
88
|
};
|
|
114
89
|
var as = {
|
|
90
|
+
string: function string(value) {
|
|
91
|
+
return typeof value === "string" ? value : undefined;
|
|
92
|
+
},
|
|
115
93
|
number: function number(value) {
|
|
116
94
|
var result = parseFloat(value);
|
|
117
|
-
return Number.
|
|
95
|
+
return Number.isFinite(result) ? result : undefined;
|
|
118
96
|
},
|
|
119
97
|
boolean: function boolean(value) {
|
|
120
|
-
return value ?
|
|
98
|
+
return /^(0|1|true|false|enabled|disabled)$/i.test(value) ? /^(1|true|enabled)$/i.test(value) : undefined;
|
|
121
99
|
},
|
|
122
100
|
array: function array(value, delimiter) {
|
|
123
|
-
|
|
101
|
+
var _value$split;
|
|
102
|
+
return (_value$split = value === null || value === void 0 ? void 0 : value.split(delimiter)) !== null && _value$split !== void 0 ? _value$split : undefined;
|
|
124
103
|
},
|
|
125
104
|
json: function json(value) {
|
|
126
105
|
try {
|
|
@@ -138,59 +117,45 @@ var as = {
|
|
|
138
117
|
}
|
|
139
118
|
};
|
|
140
119
|
exports.as = as;
|
|
141
|
-
|
|
142
120
|
var toggle = function toggle(key, cases, defaultKey) {
|
|
143
121
|
var _ref, _cases$key;
|
|
144
|
-
|
|
145
122
|
return (_ref = (_cases$key = cases[key]) !== null && _cases$key !== void 0 ? _cases$key : cases[defaultKey]) !== null && _ref !== void 0 ? _ref : undefined;
|
|
146
123
|
};
|
|
147
|
-
|
|
148
124
|
exports.toggle = toggle;
|
|
149
|
-
|
|
150
125
|
function certain(target, schema, path, optional) {
|
|
151
126
|
if (schema === null || typeof schema === 'undefined') {
|
|
152
127
|
return new AssertError(schema, 'any value', path, 'schema value');
|
|
153
128
|
}
|
|
154
|
-
|
|
155
129
|
if (schema instanceof Optional) {
|
|
156
130
|
return certain(target, schema.schema, path, true);
|
|
157
131
|
}
|
|
158
|
-
|
|
159
132
|
var isValue = target !== null && typeof target !== 'undefined';
|
|
160
|
-
|
|
161
133
|
if (!isValue && optional) {
|
|
162
134
|
return;
|
|
163
135
|
}
|
|
164
|
-
|
|
165
136
|
if (schema instanceof Or) {
|
|
166
137
|
if (!schema.schema.length) {
|
|
167
138
|
return new AssertError(target, 'values', path, 'OR schema');
|
|
168
139
|
}
|
|
169
|
-
|
|
170
140
|
return findNotError(schema.schema, function (schema) {
|
|
171
141
|
return certain(target, schema, path);
|
|
172
142
|
});
|
|
173
143
|
}
|
|
174
|
-
|
|
175
144
|
if (schema instanceof And) {
|
|
176
145
|
if (!schema.schema.length) {
|
|
177
146
|
return new AssertError(target, 'values', path, 'AND schema');
|
|
178
147
|
}
|
|
179
|
-
|
|
180
148
|
return findFirstError(schema.schema, function (schema) {
|
|
181
149
|
return certain(target, schema, path);
|
|
182
150
|
});
|
|
183
151
|
}
|
|
184
|
-
|
|
185
152
|
if (typeof schema === 'function') {
|
|
186
153
|
if (!isValue) {
|
|
187
154
|
return new AssertError(target, schema.name, path);
|
|
188
155
|
}
|
|
189
|
-
|
|
190
156
|
if (_typeof(target) === 'object' && !(target instanceof schema)) {
|
|
191
157
|
return new AssertError(target, schema.name, path);
|
|
192
158
|
}
|
|
193
|
-
|
|
194
159
|
if (_typeof(target) !== 'object' && target.constructor !== schema) {
|
|
195
160
|
return new AssertError(target, schema.name, path);
|
|
196
161
|
}
|
|
@@ -198,7 +163,6 @@ function certain(target, schema, path, optional) {
|
|
|
198
163
|
if (!Array.isArray(target)) {
|
|
199
164
|
return new AssertError(target, schema.constructor.name, path);
|
|
200
165
|
}
|
|
201
|
-
|
|
202
166
|
return findFirstError(target, function (value, idx) {
|
|
203
167
|
return findNotError(schema, function (itemSchemaType) {
|
|
204
168
|
return certain(value, itemSchemaType, "".concat(path, ".").concat(idx));
|
|
@@ -213,34 +177,27 @@ function certain(target, schema, path, optional) {
|
|
|
213
177
|
if (_typeof(target) !== 'object') {
|
|
214
178
|
return new AssertError(target, schema.constructor.name, path);
|
|
215
179
|
}
|
|
216
|
-
|
|
217
180
|
if (target === null) {
|
|
218
181
|
return new AssertError(target, 'an object', path);
|
|
219
182
|
}
|
|
220
|
-
|
|
221
183
|
if ($keys in schema) {
|
|
222
184
|
var targetKeys = Object.keys(target);
|
|
223
185
|
var assertError = findFirstError(targetKeys, function (targetKey) {
|
|
224
186
|
return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
|
|
225
187
|
});
|
|
226
|
-
|
|
227
188
|
if (assertError) {
|
|
228
189
|
return assertError;
|
|
229
190
|
}
|
|
230
191
|
}
|
|
231
|
-
|
|
232
192
|
if ($values in schema) {
|
|
233
193
|
var _targetKeys = Object.keys(target);
|
|
234
|
-
|
|
235
194
|
var _assertError = findFirstError(_targetKeys, function (targetKey) {
|
|
236
195
|
return certain(target[targetKey], schema[$values], "".concat(path, ".").concat(targetKey));
|
|
237
196
|
});
|
|
238
|
-
|
|
239
197
|
if (_assertError) {
|
|
240
198
|
return _assertError;
|
|
241
199
|
}
|
|
242
200
|
}
|
|
243
|
-
|
|
244
201
|
return findFirstError(Object.keys(schema), function (key) {
|
|
245
202
|
return certain(target[key], schema[key], "".concat(path, ".").concat(key));
|
|
246
203
|
});
|
|
@@ -249,16 +206,13 @@ function certain(target, schema, path, optional) {
|
|
|
249
206
|
return new AssertError(target, schema, path);
|
|
250
207
|
}
|
|
251
208
|
}
|
|
252
|
-
|
|
253
209
|
var ascertain = function ascertain(schema, data) {
|
|
254
210
|
var rootName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '[root]';
|
|
255
211
|
var result = certain(data, schema, rootName);
|
|
256
|
-
|
|
257
212
|
if (result instanceof AssertError) {
|
|
258
213
|
throw new TypeError(result.toString());
|
|
259
214
|
}
|
|
260
215
|
};
|
|
261
|
-
|
|
262
216
|
exports.ascertain = ascertain;
|
|
263
217
|
var _default = ascertain;
|
|
264
218
|
exports.default = _default;
|