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