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