ascertain 0.9.7 → 0.9.9

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,109 +3,87 @@
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;
7
-
6
+ exports.toggle = exports.or = exports.optional = exports.default = exports.ascertain = exports.as = exports.and = exports.$values = exports.$keys = void 0;
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;
107
80
  var $values = Symbol.for('@@values');
108
81
  exports.$values = $values;
82
+ var fromBase64 = typeof Buffer === 'undefined' ? function (value) {
83
+ return atob(value);
84
+ } : function (value) {
85
+ return Buffer.from(value, 'base64').toString('utf-8');
86
+ };
109
87
  var as = {
110
88
  number: function number(value) {
111
89
  var result = parseFloat(value);
@@ -123,54 +101,55 @@ var as = {
123
101
  } catch (e) {
124
102
  return undefined;
125
103
  }
104
+ },
105
+ base64: function base64(value) {
106
+ try {
107
+ return fromBase64(value);
108
+ } catch (e) {
109
+ return undefined;
110
+ }
126
111
  }
127
112
  };
128
113
  exports.as = as;
129
-
114
+ var toggle = function toggle(key, cases, defaultKey) {
115
+ var _ref, _cases$key;
116
+ return (_ref = (_cases$key = cases[key]) !== null && _cases$key !== void 0 ? _cases$key : cases[defaultKey]) !== null && _ref !== void 0 ? _ref : undefined;
117
+ };
118
+ exports.toggle = toggle;
130
119
  function certain(target, schema, path, optional) {
131
120
  if (schema === null || typeof schema === 'undefined') {
132
121
  return new AssertError(schema, 'any value', path, 'schema value');
133
122
  }
134
-
135
123
  if (schema instanceof Optional) {
136
124
  return certain(target, schema.schema, path, true);
137
125
  }
138
-
139
126
  var isValue = target !== null && typeof target !== 'undefined';
140
-
141
127
  if (!isValue && optional) {
142
128
  return;
143
129
  }
144
-
145
130
  if (schema instanceof Or) {
146
131
  if (!schema.schema.length) {
147
132
  return new AssertError(target, 'values', path, 'OR schema');
148
133
  }
149
-
150
134
  return findNotError(schema.schema, function (schema) {
151
135
  return certain(target, schema, path);
152
136
  });
153
137
  }
154
-
155
138
  if (schema instanceof And) {
156
139
  if (!schema.schema.length) {
157
140
  return new AssertError(target, 'values', path, 'AND schema');
158
141
  }
159
-
160
142
  return findFirstError(schema.schema, function (schema) {
161
143
  return certain(target, schema, path);
162
144
  });
163
145
  }
164
-
165
146
  if (typeof schema === 'function') {
166
147
  if (!isValue) {
167
148
  return new AssertError(target, schema.name, path);
168
149
  }
169
-
170
150
  if (_typeof(target) === 'object' && !(target instanceof schema)) {
171
151
  return new AssertError(target, schema.name, path);
172
152
  }
173
-
174
153
  if (_typeof(target) !== 'object' && target.constructor !== schema) {
175
154
  return new AssertError(target, schema.name, path);
176
155
  }
@@ -178,7 +157,6 @@ function certain(target, schema, path, optional) {
178
157
  if (!Array.isArray(target)) {
179
158
  return new AssertError(target, schema.constructor.name, path);
180
159
  }
181
-
182
160
  return findFirstError(target, function (value, idx) {
183
161
  return findNotError(schema, function (itemSchemaType) {
184
162
  return certain(value, itemSchemaType, "".concat(path, ".").concat(idx));
@@ -193,34 +171,27 @@ function certain(target, schema, path, optional) {
193
171
  if (_typeof(target) !== 'object') {
194
172
  return new AssertError(target, schema.constructor.name, path);
195
173
  }
196
-
197
174
  if (target === null) {
198
175
  return new AssertError(target, 'an object', path);
199
176
  }
200
-
201
177
  if ($keys in schema) {
202
178
  var targetKeys = Object.keys(target);
203
179
  var assertError = findFirstError(targetKeys, function (targetKey) {
204
180
  return certain(targetKey, schema[$keys], "".concat(path, ".").concat(targetKey));
205
181
  });
206
-
207
182
  if (assertError) {
208
183
  return assertError;
209
184
  }
210
185
  }
211
-
212
186
  if ($values in schema) {
213
187
  var _targetKeys = Object.keys(target);
214
-
215
188
  var _assertError = findFirstError(_targetKeys, function (targetKey) {
216
189
  return certain(target[targetKey], schema[$values], "".concat(path, ".").concat(targetKey));
217
190
  });
218
-
219
191
  if (_assertError) {
220
192
  return _assertError;
221
193
  }
222
194
  }
223
-
224
195
  return findFirstError(Object.keys(schema), function (key) {
225
196
  return certain(target[key], schema[key], "".concat(path, ".").concat(key));
226
197
  });
@@ -229,16 +200,13 @@ function certain(target, schema, path, optional) {
229
200
  return new AssertError(target, schema, path);
230
201
  }
231
202
  }
232
-
233
203
  var ascertain = function ascertain(schema, data) {
234
204
  var rootName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '[root]';
235
205
  var result = certain(data, schema, rootName);
236
-
237
206
  if (result instanceof AssertError) {
238
207
  throw new TypeError(result.toString());
239
208
  }
240
209
  };
241
-
242
210
  exports.ascertain = ascertain;
243
211
  var _default = ascertain;
244
212
  exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ascertain",
3
- "version": "0.9.7",
3
+ "version": "0.9.9",
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",