UNPKG

39.4 kBJavaScriptView Raw
1function _defineProperty(obj, key, value) {
2 if (key in obj) {
3 Object.defineProperty(obj, key, {
4 value: value,
5 enumerable: true,
6 configurable: true,
7 writable: true
8 });
9 } else {
10 obj[key] = value;
11 }
12
13 return obj;
14}
15
16function ownKeys(object, enumerableOnly) {
17 var keys = Object.keys(object);
18
19 if (Object.getOwnPropertySymbols) {
20 var symbols = Object.getOwnPropertySymbols(object);
21 if (enumerableOnly) symbols = symbols.filter(function (sym) {
22 return Object.getOwnPropertyDescriptor(object, sym).enumerable;
23 });
24 keys.push.apply(keys, symbols);
25 }
26
27 return keys;
28}
29
30function _objectSpread2(target) {
31 for (var i = 1; i < arguments.length; i++) {
32 var source = arguments[i] != null ? arguments[i] : {};
33
34 if (i % 2) {
35 ownKeys(Object(source), true).forEach(function (key) {
36 _defineProperty(target, key, source[key]);
37 });
38 } else if (Object.getOwnPropertyDescriptors) {
39 Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
40 } else {
41 ownKeys(Object(source)).forEach(function (key) {
42 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
43 });
44 }
45 }
46
47 return target;
48}
49
50function _inheritsLoose(subClass, superClass) {
51 subClass.prototype = Object.create(superClass.prototype);
52 subClass.prototype.constructor = subClass;
53 subClass.__proto__ = superClass;
54}
55
56function _getPrototypeOf(o) {
57 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
58 return o.__proto__ || Object.getPrototypeOf(o);
59 };
60 return _getPrototypeOf(o);
61}
62
63function _setPrototypeOf(o, p) {
64 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
65 o.__proto__ = p;
66 return o;
67 };
68
69 return _setPrototypeOf(o, p);
70}
71
72function isNativeReflectConstruct() {
73 if (typeof Reflect === "undefined" || !Reflect.construct) return false;
74 if (Reflect.construct.sham) return false;
75 if (typeof Proxy === "function") return true;
76
77 try {
78 Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
79 return true;
80 } catch (e) {
81 return false;
82 }
83}
84
85function _construct(Parent, args, Class) {
86 if (isNativeReflectConstruct()) {
87 _construct = Reflect.construct;
88 } else {
89 _construct = function _construct(Parent, args, Class) {
90 var a = [null];
91 a.push.apply(a, args);
92 var Constructor = Function.bind.apply(Parent, a);
93 var instance = new Constructor();
94 if (Class) _setPrototypeOf(instance, Class.prototype);
95 return instance;
96 };
97 }
98
99 return _construct.apply(null, arguments);
100}
101
102function _isNativeFunction(fn) {
103 return Function.toString.call(fn).indexOf("[native code]") !== -1;
104}
105
106function _wrapNativeSuper(Class) {
107 var _cache = typeof Map === "function" ? new Map() : undefined;
108
109 _wrapNativeSuper = function _wrapNativeSuper(Class) {
110 if (Class === null || !_isNativeFunction(Class)) return Class;
111
112 if (typeof Class !== "function") {
113 throw new TypeError("Super expression must either be null or a function");
114 }
115
116 if (typeof _cache !== "undefined") {
117 if (_cache.has(Class)) return _cache.get(Class);
118
119 _cache.set(Class, Wrapper);
120 }
121
122 function Wrapper() {
123 return _construct(Class, arguments, _getPrototypeOf(this).constructor);
124 }
125
126 Wrapper.prototype = Object.create(Class.prototype, {
127 constructor: {
128 value: Wrapper,
129 enumerable: false,
130 writable: true,
131 configurable: true
132 }
133 });
134 return _setPrototypeOf(Wrapper, Class);
135 };
136
137 return _wrapNativeSuper(Class);
138}
139
140function _objectWithoutPropertiesLoose(source, excluded) {
141 if (source == null) return {};
142 var target = {};
143 var sourceKeys = Object.keys(source);
144 var key, i;
145
146 for (i = 0; i < sourceKeys.length; i++) {
147 key = sourceKeys[i];
148 if (excluded.indexOf(key) >= 0) continue;
149 target[key] = source[key];
150 }
151
152 return target;
153}
154
155function _assertThisInitialized(self) {
156 if (self === void 0) {
157 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
158 }
159
160 return self;
161}
162
163var toString = Object.prototype.toString;
164
165var kindOf = function kindOf(val) {
166 if (val === void 0) return 'undefined';
167 if (val === null) return 'null';
168
169 var type = typeof val;
170 if (type === 'boolean') return 'boolean';
171 if (type === 'string') return 'string';
172 if (type === 'number') return 'number';
173 if (type === 'symbol') return 'symbol';
174 if (type === 'function') {
175 return isGeneratorFn(val) ? 'generatorfunction' : 'function';
176 }
177
178 if (isArray(val)) return 'array';
179 if (isBuffer(val)) return 'buffer';
180 if (isArguments(val)) return 'arguments';
181 if (isDate(val)) return 'date';
182 if (isError(val)) return 'error';
183 if (isRegexp(val)) return 'regexp';
184
185 switch (ctorName(val)) {
186 case 'Symbol': return 'symbol';
187 case 'Promise': return 'promise';
188
189 // Set, Map, WeakSet, WeakMap
190 case 'WeakMap': return 'weakmap';
191 case 'WeakSet': return 'weakset';
192 case 'Map': return 'map';
193 case 'Set': return 'set';
194
195 // 8-bit typed arrays
196 case 'Int8Array': return 'int8array';
197 case 'Uint8Array': return 'uint8array';
198 case 'Uint8ClampedArray': return 'uint8clampedarray';
199
200 // 16-bit typed arrays
201 case 'Int16Array': return 'int16array';
202 case 'Uint16Array': return 'uint16array';
203
204 // 32-bit typed arrays
205 case 'Int32Array': return 'int32array';
206 case 'Uint32Array': return 'uint32array';
207 case 'Float32Array': return 'float32array';
208 case 'Float64Array': return 'float64array';
209 }
210
211 if (isGeneratorObj(val)) {
212 return 'generator';
213 }
214
215 // Non-plain objects
216 type = toString.call(val);
217 switch (type) {
218 case '[object Object]': return 'object';
219 // iterators
220 case '[object Map Iterator]': return 'mapiterator';
221 case '[object Set Iterator]': return 'setiterator';
222 case '[object String Iterator]': return 'stringiterator';
223 case '[object Array Iterator]': return 'arrayiterator';
224 }
225
226 // other
227 return type.slice(8, -1).toLowerCase().replace(/\s/g, '');
228};
229
230function ctorName(val) {
231 return val.constructor ? val.constructor.name : null;
232}
233
234function isArray(val) {
235 if (Array.isArray) return Array.isArray(val);
236 return val instanceof Array;
237}
238
239function isError(val) {
240 return val instanceof Error || (typeof val.message === 'string' && val.constructor && typeof val.constructor.stackTraceLimit === 'number');
241}
242
243function isDate(val) {
244 if (val instanceof Date) return true;
245 return typeof val.toDateString === 'function'
246 && typeof val.getDate === 'function'
247 && typeof val.setDate === 'function';
248}
249
250function isRegexp(val) {
251 if (val instanceof RegExp) return true;
252 return typeof val.flags === 'string'
253 && typeof val.ignoreCase === 'boolean'
254 && typeof val.multiline === 'boolean'
255 && typeof val.global === 'boolean';
256}
257
258function isGeneratorFn(name, val) {
259 return ctorName(name) === 'GeneratorFunction';
260}
261
262function isGeneratorObj(val) {
263 return typeof val.throw === 'function'
264 && typeof val.return === 'function'
265 && typeof val.next === 'function';
266}
267
268function isArguments(val) {
269 try {
270 if (typeof val.length === 'number' && typeof val.callee === 'function') {
271 return true;
272 }
273 } catch (err) {
274 if (err.message.indexOf('callee') !== -1) {
275 return true;
276 }
277 }
278 return false;
279}
280
281/**
282 * If you need to support Safari 5-7 (8-10 yr-old browser),
283 * take a look at https://github.com/feross/is-buffer
284 */
285
286function isBuffer(val) {
287 if (val.constructor && typeof val.constructor.isBuffer === 'function') {
288 return val.constructor.isBuffer(val);
289 }
290 return false;
291}
292
293/**
294 * Superstruct ships by default with an unopinionated set of scalar types that
295 * express all of the data types that are built-in to JavaScript.
296 */
297
298var Types = {
299 /**
300 * Matches any value other than `undefined`.
301 *
302 * ```js
303 * 'anything'
304 * true
305 * ```
306 */
307 any: function any(value) {
308 return value !== undefined;
309 },
310
311 /**
312 * Matches an `arguments` object.
313 *
314 * ```js
315 * arguments
316 * ```
317 */
318 arguments: function _arguments(value) {
319 return kindOf(value) === 'arguments';
320 },
321
322 /**
323 * Matches an `Array`.
324 *
325 * ```js
326 * [1, 2, 3]
327 * ```
328 */
329 array: function array(value) {
330 return kindOf(value) === 'array';
331 },
332
333 /**
334 * Matches a boolean.
335 *
336 * ```js
337 * true
338 * false
339 * ```
340 */
341 boolean: function boolean(value) {
342 return kindOf(value) === 'boolean';
343 },
344
345 /**
346 * Matches a Node.js `Buffer`.
347 *
348 * ```js
349 * Buffer.from('string')
350 * ```
351 */
352 buffer: function buffer(value) {
353 return kindOf(value) === 'buffer';
354 },
355
356 /**
357 * Matches a **valid** `Date` object.
358 *
359 * ```js
360 * new Date()
361 * ```
362 *
363 * Note: Invalid `Date` objects that equal `NaN` are not matched.
364 */
365 date: function date(value) {
366 return kindOf(value) === 'date' && !isNaN(value);
367 },
368
369 /**
370 * Matches an error object.
371 *
372 * ```js
373 * new Error()
374 * ```
375 */
376 error: function error(value) {
377 return kindOf(value) === 'error';
378 },
379
380 /**
381 * Matches a `Float32Array` object.
382 */
383 float32array: function float32array(value) {
384 return kindOf(value) === 'float32array';
385 },
386
387 /**
388 * Matches a `Float64Array` object.
389 */
390 float64array: function float64array(value) {
391 return kindOf(value) === 'float64array';
392 },
393
394 /**
395 * Matches a function.
396 *
397 * ```js
398 * () => {}
399 * function () {}
400 * ```
401 */
402 function: function _function(value) {
403 return kindOf(value) === 'function';
404 },
405
406 /**
407 * Matches a generator function.
408 *
409 * ```js
410 * function* () {}
411 * ```
412 */
413 generatorfunction: function generatorfunction(value) {
414 return kindOf(value) === 'generatorfunction';
415 },
416
417 /**
418 * Matches a `Int16Array` object.
419 */
420 int16array: function int16array(value) {
421 return kindOf(value) === 'int16array';
422 },
423
424 /**
425 * Matches a `Int32Array` object.
426 */
427 int32array: function int32array(value) {
428 return kindOf(value) === 'int32array';
429 },
430
431 /**
432 * Matches a `Int8Array` object.
433 */
434 int8array: function int8array(value) {
435 return kindOf(value) === 'int8array';
436 },
437
438 /**
439 * Matches a `Map` object.
440 *
441 * ```js
442 * new Map()
443 * ```
444 */
445 map: function map(value) {
446 return kindOf(value) === 'map';
447 },
448
449 /**
450 * Matches the `null` literal value.
451 *
452 * ```js
453 * null
454 * ```
455 */
456 null: function _null(value) {
457 return kindOf(value) === 'null';
458 },
459
460 /**
461 * Matches a number.
462 *
463 * ```js
464 * 42
465 * ```
466 */
467 number: function number(value) {
468 return kindOf(value) === 'number';
469 },
470
471 /**
472 * Matches a plain object.
473 *
474 * ```js
475 * { key: 'value' }
476 * { something: true }
477 * ```
478 */
479 object: function object(value) {
480 return kindOf(value) === 'object';
481 },
482
483 /**
484 * Matches a `Promise` object.
485 *
486 * ```js
487 * Promise.resolve()
488 * ```
489 */
490 promise: function promise(value) {
491 return kindOf(value) === 'promise';
492 },
493
494 /**
495 * Matches a regular expression object.
496 *
497 * ```js
498 * /a-z/g
499 * ```
500 */
501 regexp: function regexp(value) {
502 return kindOf(value) === 'regexp';
503 },
504
505 /**
506 * Matches a `Set` object.
507 *
508 * ```js
509 * new Set()
510 * ```
511 */
512 set: function set(value) {
513 return kindOf(value) === 'set';
514 },
515
516 /**
517 * Matches a string.
518 *
519 * ```js
520 * 'text'
521 * ```
522 */
523 string: function string(value) {
524 return kindOf(value) === 'string';
525 },
526
527 /**
528 * Matches a `Symbol`.
529 *
530 * ```js
531 * Symbol()
532 * ```
533 */
534 symbol: function symbol(value) {
535 return kindOf(value) === 'symbol';
536 },
537
538 /**
539 * Matches a `Uint16Array` object.
540 */
541 uint16array: function uint16array(value) {
542 return kindOf(value) === 'uint16array';
543 },
544
545 /**
546 * Matches a `Uint32Array` object.
547 */
548 uint32array: function uint32array(value) {
549 return kindOf(value) === 'uint32array';
550 },
551
552 /**
553 * Matches a `Uint8Array` object.
554 */
555 uint8array: function uint8array(value) {
556 return kindOf(value) === 'uint8array';
557 },
558
559 /**
560 * Matches a `Uint8ClampedArray` object.
561 */
562 uint8clampedarray: function uint8clampedarray(value) {
563 return kindOf(value) === 'uint8clampedarray';
564 },
565
566 /**
567 * Matches the `undefined` literal value.
568 *
569 * ```js
570 * undefined
571 * ```
572 */
573 undefined: function undefined$1(value) {
574 return kindOf(value) === 'undefined';
575 },
576
577 /**
578 * Matches a `WeakMap` object.
579 *
580 * ```js
581 * new WeakMap()
582 * ```
583 */
584 weakmap: function weakmap(value) {
585 return kindOf(value) === 'weakmap';
586 },
587
588 /**
589 * Matches a `WeakSet` object.
590 *
591 * ```js
592 * new WeakSet()
593 * ```
594 */
595 weakset: function weakset(value) {
596 return kindOf(value) === 'weakset';
597 }
598};
599
600var isProduction = process.env.NODE_ENV === 'production';
601var prefix = 'Invariant failed';
602function invariant(condition, message) {
603 if (condition) {
604 return;
605 }
606
607 if (isProduction) {
608 throw new Error(prefix);
609 } else {
610 throw new Error(prefix + ": " + (message || ''));
611 }
612}
613
614/**
615 * `StructError` objects are thrown (or returned) by Superstruct when its
616 * validation fails. The error represents the first error encountered during
617 * validation. But they also have an `error.failures` property that holds
618 * information for all of the failures encountered.
619 */
620
621var StructError =
622/*#__PURE__*/
623function (_TypeError) {
624 _inheritsLoose(StructError, _TypeError);
625
626 function StructError(failures) {
627 var _this;
628
629 invariant(failures.length > 0, "StructError requires being passed a failure, but received: " + failures);
630 var first = failures[0];
631
632 var path = first.path,
633 value = first.value,
634 type = first.type,
635 branch = first.branch,
636 rest = _objectWithoutPropertiesLoose(first, ["path", "value", "type", "branch"]);
637
638 var message = "Expected a value of type `" + type + "`" + (path.length ? " for `" + path.join('.') + "`" : '') + " but received `" + JSON.stringify(value) + "`.";
639 _this = _TypeError.call(this, message) || this;
640 _this.type = type;
641 _this.value = value;
642 Object.assign(_assertThisInitialized(_this), rest);
643 _this.path = path;
644 _this.branch = branch;
645 _this.failures = failures;
646 _this.stack = new Error().stack;
647 _this.__proto__ = StructError.prototype;
648 return _this;
649 }
650
651 return StructError;
652}(_wrapNativeSuper(TypeError));
653
654/**
655 * A symbol to set on `Struct` objects to test them against later.
656 */
657var STRUCT = Symbol('STRUCT');
658/**
659 * Check if a value is a `Struct` object.
660 */
661
662var isStruct = function isStruct(value) {
663 return typeof value === 'function' && value[STRUCT];
664};
665/**
666 * This abstract `Struct` factory creates a generic struct that validates values
667 * against a `Validator` function.
668 */
669
670var createStruct = function createStruct(props) {
671 var struct = props.struct;
672 var Error = struct.Error;
673
674 var Struct = function Struct(value) {
675 return Struct.assert(value);
676 }; // Set a hidden symbol property so that we can check it later to see if an
677 // object is a struct object.
678
679
680 Object.defineProperty(Struct, STRUCT, {
681 value: true
682 });
683 Struct.kind = props.kind;
684 Struct.type = props.type;
685
686 Struct.default = function () {
687 return typeof props.defaults === 'function' ? props.defaults() : props.defaults;
688 };
689
690 Struct.test = function (value) {
691 var _Struct$check = Struct.check(value, [value], []),
692 failures = _Struct$check[0];
693
694 return !failures;
695 };
696
697 Struct.assert = function (value) {
698 var _Struct$check2 = Struct.check(value, [value], []),
699 failures = _Struct$check2[0],
700 result = _Struct$check2[1];
701
702 if (failures) {
703 throw new Error(failures);
704 } else {
705 return result;
706 }
707 };
708
709 Struct.validate = function (value) {
710 var _Struct$check3 = Struct.check(value, [value], []),
711 failures = _Struct$check3[0],
712 result = _Struct$check3[1];
713
714 if (failures) {
715 return [new Error(failures)];
716 } else {
717 return [undefined, result];
718 }
719 };
720
721 Struct.check = function (value, branch, path) {
722 if (value === void 0) {
723 value = Struct.default();
724 }
725
726 var failures = [Struct.fail({
727 value: value,
728 branch: branch,
729 path: path
730 })];
731 return [failures];
732 };
733
734 Struct.fail = function (obj) {
735 return _objectSpread2({}, obj, {
736 type: 'type' in obj ? obj.type : Struct.type
737 });
738 };
739
740 return Struct;
741};
742
743var createArray = function createArray(schema, defaults, struct) {
744 invariant(Array.isArray(schema) && schema.length === 1, "Array structs must be defined as an array with one element, but you passed: " + schema);
745 var Element = struct(schema[0], undefined);
746 var Struct = createStruct({
747 kind: 'array',
748 type: Element.type + "[]",
749 defaults: defaults,
750 struct: struct
751 });
752
753 Struct.check = function (value, branch, path) {
754 if (value === void 0) {
755 value = Struct.default();
756 }
757
758 if (!Array.isArray(value)) {
759 return [[Struct.fail({
760 value: value,
761 branch: branch,
762 path: path
763 })]];
764 }
765
766 var result = [];
767 var failures = [];
768
769 for (var i = 0; i < value.length; i++) {
770 var v = value[i];
771
772 var _Element$check = Element.check(v, branch.concat(v), path.concat(i)),
773 efs = _Element$check[0],
774 er = _Element$check[1];
775
776 if (efs) {
777 failures.push.apply(failures, efs);
778 continue;
779 }
780
781 result[i] = er;
782 }
783
784 return failures.length ? [failures] : [undefined, result];
785 };
786
787 return Struct;
788};
789
790var createDynamic = function createDynamic(schema, defaults, struct) {
791 invariant(typeof schema === 'function', "Dynamic structs must be defined as a function, but you passed: " + schema);
792 var Dynamic = createStruct({
793 kind: 'dynamic',
794 type: "dynamic<\u2026>",
795 defaults: defaults,
796 struct: struct
797 });
798
799 Dynamic.check = function (value, branch, path) {
800 if (value === void 0) {
801 value = Dynamic.default();
802 }
803
804 var Struct = schema(value, branch, path);
805 return Struct.check(value, branch, path);
806 };
807
808 return Dynamic;
809};
810
811var createEnum = function createEnum(schema, defaults, struct) {
812 invariant(Array.isArray(schema), "Enum structs must be defined as an array, but you passed: " + schema);
813
814 var validator = function validator(value) {
815 return schema.includes(value);
816 };
817
818 var Struct = struct(validator, defaults);
819 Struct.kind = 'enum';
820 Struct.type = schema.map(function (s) {
821 return typeof s === 'string' ? "\"" + s + "\"" : "" + s;
822 }).join(' | ');
823 return Struct;
824};
825
826var createFunction = function createFunction(schema, defaults, struct) {
827 var Struct = createStruct({
828 kind: 'function',
829 type: "function<\u2026>",
830 defaults: defaults,
831 struct: struct
832 });
833
834 Struct.check = function (value, branch, path) {
835 if (value === void 0) {
836 value = Struct.default();
837 }
838
839 var result = schema(value, branch, path);
840
841 if (result === true) {
842 return [undefined, value];
843 }
844
845 var failures = [];
846
847 if (result === false) {
848 failures.push(Struct.fail({
849 value: value,
850 branch: branch,
851 path: path
852 }));
853 } else if (Array.isArray(result) && result.length > 0) {
854 for (var _iterator = result, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
855 var _ref;
856
857 if (_isArray) {
858 if (_i >= _iterator.length) break;
859 _ref = _iterator[_i++];
860 } else {
861 _i = _iterator.next();
862 if (_i.done) break;
863 _ref = _i.value;
864 }
865
866 var r = _ref;
867 failures.push(Struct.fail(_objectSpread2({
868 value: value,
869 branch: branch,
870 path: path
871 }, r)));
872 }
873 } else if (typeof result === 'object') {
874 failures.push(Struct.fail(_objectSpread2({
875 value: value,
876 branch: branch,
877 path: path
878 }, result)));
879 } else {
880 invariant(false, "Validator functions must return a boolean, a failure object, or an array of failure objects, but you passed: " + result);
881 }
882
883 return [failures];
884 };
885
886 return Struct;
887};
888
889var createInstance = function createInstance(schema, defaults, struct) {
890 invariant(typeof schema === 'function', "Instance structs must be defined as a function, but you passed: " + schema);
891
892 var validator = function validator(value) {
893 return value instanceof schema;
894 };
895
896 var Struct = struct(validator, defaults);
897 Struct.kind = 'instance';
898 Struct.type = "instance<" + schema.name + ">";
899 return Struct;
900};
901
902var createInterface = function createInterface(schema, defaults, struct) {
903 invariant(typeof schema === 'object', "Interface structs must be defined as an object, but you passed: " + schema);
904 var Props = {};
905
906 for (var key in schema) {
907 Props[key] = struct(schema[key]);
908 }
909
910 var Struct = createStruct({
911 kind: 'interface',
912 type: "interface<{" + Object.keys(schema).join() + "}>",
913 defaults: defaults,
914 struct: struct
915 });
916
917 Struct.check = function (value, branch, path) {
918 if (value === void 0) {
919 value = Struct.default();
920 }
921
922 if (typeof value !== 'object' && typeof value !== 'function') {
923 return [[Struct.fail({
924 value: value,
925 branch: branch,
926 path: path
927 })]];
928 }
929
930 var failures = [];
931
932 for (var k in Props) {
933 var Prop = Props[k];
934 var v = value[k];
935
936 var _Prop$check = Prop.check(v, branch.concat(v), path.concat(k)),
937 pfs = _Prop$check[0];
938
939 if (pfs) {
940 failures.push.apply(failures, pfs);
941 }
942 }
943
944 return failures.length ? [failures] : [undefined, value];
945 };
946
947 return Struct;
948};
949
950var createIntersection = function createIntersection(schema, defaults, struct) {
951 invariant(Array.isArray(schema) && schema.length !== 0, "Intersection structs must be defined as a non-empty array, but you passed: " + schema);
952 var Structs = schema.map(function (sch) {
953 return struct(sch);
954 });
955 var type = Structs.map(function (s) {
956 return s.type;
957 }).join(' & ');
958 var Struct = createStruct({
959 kind: 'intersection',
960 type: type,
961 defaults: defaults,
962 struct: struct
963 });
964
965 Struct.check = function (value, branch, path) {
966 if (value === void 0) {
967 value = Struct.default();
968 }
969
970 var result = value;
971
972 for (var _iterator = Structs, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
973 var _ref;
974
975 if (_isArray) {
976 if (_i >= _iterator.length) break;
977 _ref = _iterator[_i++];
978 } else {
979 _i = _iterator.next();
980 if (_i.done) break;
981 _ref = _i.value;
982 }
983
984 var _struct = _ref;
985
986 var _struct$check = _struct.check(value, branch, path),
987 fs = _struct$check[0],
988 v = _struct$check[1];
989
990 if (fs) {
991 return [[Struct.fail({
992 value: value,
993 branch: branch,
994 path: path
995 })]];
996 } else {
997 result = v;
998 }
999 }
1000
1001 return [undefined, result];
1002 };
1003
1004 return Struct;
1005};
1006
1007var createLazy = function createLazy(schema, defaults, struct) {
1008 invariant(typeof schema === 'function', "Lazy structs must be defined as a function, but you passed: " + schema);
1009 var Lazy = createStruct({
1010 kind: 'lazy',
1011 type: "lazy<\u2026>",
1012 defaults: defaults,
1013 struct: struct
1014 });
1015
1016 Lazy.check = function () {
1017 Object.assign(Lazy, schema());
1018 return Lazy.check.apply(Lazy, arguments);
1019 };
1020
1021 return Lazy;
1022};
1023
1024var createSize = function createSize(schema, defaults, struct) {
1025 invariant(Array.isArray(schema) && schema.length === 2 && schema.every(function (n) {
1026 return typeof n === 'number';
1027 }), "Size structs must be defined as an array with two number elements, but you passed: " + schema);
1028 var min = schema[0],
1029 max = schema[1];
1030
1031 var validator = function validator(value) {
1032 return value != null && typeof value.length === 'number' && value.length >= min && value.length <= max;
1033 };
1034
1035 var Struct = struct(validator, defaults);
1036 Struct.kind = 'size';
1037 Struct.type = "size<" + min + "," + max + ">";
1038 return Struct;
1039};
1040
1041var createLiteral = function createLiteral(schema, defaults, struct) {
1042 var validator = function validator(value) {
1043 return value === schema;
1044 };
1045
1046 var Struct = struct(validator, defaults);
1047 Struct.kind = 'literal';
1048 Struct.type = typeof schema === 'string' ? "\"" + schema + "\"" : "" + schema;
1049 return Struct;
1050};
1051
1052var createObject = function createObject(schema, defaults, struct) {
1053 invariant(typeof schema === 'object', "Object structs must be defined as an object, but you passed: " + schema);
1054 var Props = {};
1055
1056 for (var key in schema) {
1057 Props[key] = struct(schema[key]);
1058 }
1059
1060 var Struct = createStruct({
1061 kind: 'object',
1062 type: "{" + Object.keys(schema).join() + "}",
1063 defaults: defaults,
1064 struct: struct
1065 });
1066
1067 Struct.check = function (value, branch, path) {
1068 if (value === void 0) {
1069 value = Struct.default();
1070 }
1071
1072 var d = Struct.default();
1073
1074 if (value === undefined) {
1075 value = d;
1076 }
1077
1078 if (kindOf(value) !== 'object') {
1079 return [[Struct.fail({
1080 value: value,
1081 branch: branch,
1082 path: path
1083 })]];
1084 }
1085
1086 var result = {};
1087 var failures = [];
1088 var keys = new Set(Object.keys(Props).concat(Object.keys(value)));
1089
1090 for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
1091 var _ref;
1092
1093 if (_isArray) {
1094 if (_i >= _iterator.length) break;
1095 _ref = _iterator[_i++];
1096 } else {
1097 _i = _iterator.next();
1098 if (_i.done) break;
1099 _ref = _i.value;
1100 }
1101
1102 var k = _ref;
1103 var v = value[k];
1104 var p = path.concat(k);
1105 var b = branch.concat(v);
1106 var Prop = Props[k];
1107
1108 if (v === undefined && d != null && k in d) {
1109 v = typeof d[k] === 'function' ? d[k](value, branch, path) : d[k];
1110 }
1111
1112 if (!(k in Props)) {
1113 failures.push(Struct.fail({
1114 type: undefined,
1115 value: v,
1116 path: p,
1117 branch: value
1118 }));
1119 continue;
1120 }
1121
1122 var _Prop$check = Prop.check(v, b, p),
1123 pfs = _Prop$check[0],
1124 pr = _Prop$check[1];
1125
1126 if (pfs) {
1127 failures.push.apply(failures, pfs);
1128 } else if (pr !== undefined && k in Props) {
1129 result[k] = pr;
1130 }
1131 }
1132
1133 return failures.length ? [failures] : [undefined, result];
1134 };
1135
1136 return Struct;
1137};
1138
1139var createPartial = function createPartial(schema, defaults, struct) {
1140 invariant(typeof schema === 'object', "Partial structs must be defined as an object, but you passed: " + schema);
1141 var Props = {};
1142
1143 for (var key in schema) {
1144 Props[key] = struct.union([schema[key], 'undefined']);
1145 }
1146
1147 var Struct = createStruct({
1148 kind: 'object',
1149 type: "{" + Object.keys(schema).join() + "}",
1150 defaults: defaults,
1151 struct: struct
1152 });
1153
1154 Struct.check = function (value, branch, path) {
1155 if (value === void 0) {
1156 value = Struct.default();
1157 }
1158
1159 var d = Struct.default();
1160
1161 if (value === undefined) {
1162 value = d;
1163 }
1164
1165 if (kindOf(value) !== 'object') {
1166 return [[Struct.fail({
1167 value: value,
1168 branch: branch,
1169 path: path
1170 })]];
1171 }
1172
1173 var result = {};
1174 var failures = [];
1175
1176 for (var _iterator = value, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
1177 var _ref;
1178
1179 if (_isArray) {
1180 if (_i >= _iterator.length) break;
1181 _ref = _iterator[_i++];
1182 } else {
1183 _i = _iterator.next();
1184 if (_i.done) break;
1185 _ref = _i.value;
1186 }
1187
1188 var k = _ref;
1189 var v = value[k];
1190 var p = path.concat(k);
1191 var b = branch.concat(v);
1192 var Prop = Props[k];
1193
1194 if (v === undefined && d != null && k in d) {
1195 v = typeof d[k] === 'function' ? d[k](value, branch, path) : d[k];
1196 }
1197
1198 if (!(k in Props)) {
1199 failures.push(Struct.fail({
1200 type: undefined,
1201 value: v,
1202 path: p,
1203 branch: value
1204 }));
1205 continue;
1206 }
1207
1208 var _Prop$check = Prop.check(v, b, p),
1209 pfs = _Prop$check[0],
1210 pr = _Prop$check[1];
1211
1212 if (pfs) {
1213 failures.push.apply(failures, pfs);
1214 } else if (pr !== undefined && k in Props) {
1215 result[k] = pr;
1216 }
1217 }
1218
1219 return failures.length ? [failures] : [undefined, result];
1220 };
1221
1222 return Struct;
1223};
1224
1225var createPick = function createPick(schema, defaults, struct) {
1226 invariant(typeof schema === 'object', "Pick structs must be defined as an object, but you passed: " + schema);
1227 var Props = {};
1228
1229 for (var key in schema) {
1230 Props[key] = struct(schema[key]);
1231 }
1232
1233 var Struct = createStruct({
1234 kind: 'pick',
1235 type: "pick<{" + Object.keys(schema).join() + "}>",
1236 defaults: defaults,
1237 struct: struct
1238 });
1239
1240 Struct.check = function (value, branch, path) {
1241 if (value === void 0) {
1242 value = Struct.default();
1243 }
1244
1245 var d = Struct.default();
1246
1247 if (value === undefined) {
1248 value = d;
1249 }
1250
1251 if (kindOf(value) !== 'object') {
1252 return [[Struct.fail({
1253 value: value,
1254 branch: branch,
1255 path: path
1256 })]];
1257 }
1258
1259 var result = {};
1260 var failures = [];
1261
1262 for (var k in Props) {
1263 var v = value[k];
1264 var p = path.concat(k);
1265 var b = branch.concat(v);
1266 var Prop = Props[k];
1267
1268 if (v === undefined && d != null && k in d) {
1269 v = typeof d[k] === 'function' ? d[k](value, branch, path) : d[k];
1270 }
1271
1272 var _Prop$check = Prop.check(v, b, p),
1273 pfs = _Prop$check[0],
1274 pr = _Prop$check[1];
1275
1276 if (pfs) {
1277 failures.push.apply(failures, pfs);
1278 } else if (pr !== undefined && k in Props) {
1279 result[k] = pr;
1280 }
1281 }
1282
1283 return failures.length ? [failures] : [undefined, result];
1284 };
1285
1286 return Struct;
1287};
1288
1289var createRecord = function createRecord(schema, defaults, struct) {
1290 invariant(Array.isArray(schema) && schema.length === 2, "Record structs must be defined as an array with two elements, but you passed: " + schema);
1291 var Key = struct(schema[0]);
1292 var Value = struct(schema[1]);
1293 var Struct = createStruct({
1294 kind: 'record',
1295 type: "record<" + Key.type + "," + Value.type + ">",
1296 defaults: defaults,
1297 struct: struct
1298 });
1299
1300 Struct.check = function (value, branch, path) {
1301 // Record structs have a special default handling behavior, where the defaults
1302 // are for the entries themselves, not for the entire value. So we can't use
1303 // JavaScript's built-in default handling here.
1304 var defs = Struct.default();
1305 value = defs ? _objectSpread2({}, defs, {}, value) : value;
1306
1307 if (kindOf(value) !== 'object') {
1308 return [[Struct.fail({
1309 value: value,
1310 branch: branch,
1311 path: path
1312 })]];
1313 }
1314
1315 var result = {};
1316 var failures = [];
1317
1318 for (var k in value) {
1319 var v = value[k];
1320 var p = path.concat(k);
1321 var b = branch.concat(v);
1322
1323 var _Key$check = Key.check(k, b, p),
1324 kfs = _Key$check[0],
1325 kr = _Key$check[1];
1326
1327 if (kfs) {
1328 failures.push.apply(failures, kfs);
1329 } else {
1330 var _Value$check = Value.check(v, b, p),
1331 vfs = _Value$check[0],
1332 vr = _Value$check[1];
1333
1334 if (vfs) {
1335 failures.push.apply(failures, vfs);
1336 } else {
1337 result[kr] = vr;
1338 }
1339 }
1340 }
1341
1342 return failures.length ? [failures] : [undefined, result];
1343 };
1344
1345 return Struct;
1346};
1347
1348var createScalar = function createScalar(schema, defaults, struct) {
1349 invariant(typeof schema === 'string', "Scalar structs must be defined as a string, but you passed: " + schema);
1350 var Types = struct.Types;
1351 invariant(schema in Types, "No struct validator function found for type \"" + schema + "\".");
1352 var Struct = struct(Types[schema], defaults);
1353 Struct.kind = 'scalar';
1354 Struct.type = schema;
1355 return Struct;
1356};
1357
1358var createShorthand = function createShorthand(schema, defaults, struct) {
1359 if (isStruct(schema)) {
1360 return schema;
1361 }
1362
1363 if (Array.isArray(schema)) {
1364 if (schema.length === 1) {
1365 var _schema = schema,
1366 first = _schema[0];
1367 return struct.array([first], defaults);
1368 } else if (schema.length > 1) {
1369 return struct.tuple(schema, defaults);
1370 }
1371 }
1372
1373 if (typeof schema === 'function') {
1374 return struct.function(schema, defaults);
1375 }
1376
1377 if (typeof schema === 'object') {
1378 return struct.object(schema, defaults);
1379 }
1380
1381 if (typeof schema === 'string') {
1382 var optional = false;
1383 var Struct;
1384
1385 if (schema.endsWith('?')) {
1386 optional = true;
1387 schema = schema.slice(0, -1);
1388 }
1389
1390 if (schema.includes('|')) {
1391 var scalars = schema.split(/\s*\|\s*/g);
1392 Struct = struct.union(scalars, defaults);
1393 } else if (schema.includes('&')) {
1394 var _scalars = schema.split(/\s*&\s*/g);
1395
1396 Struct = struct.intersection(_scalars, defaults);
1397 } else {
1398 Struct = struct.scalar(schema, defaults);
1399 }
1400
1401 if (optional) {
1402 Struct = struct.union([Struct, 'undefined'], undefined);
1403 }
1404
1405 return Struct;
1406 }
1407
1408 throw new Error("A schema definition must be an object, array, string or function, but you passed: " + schema);
1409};
1410
1411var createTuple = function createTuple(schema, defaults, struct) {
1412 invariant(Array.isArray(schema), "Tuple structs must be defined as an array, but you passed: " + schema);
1413 var Elements = schema.map(function (s) {
1414 return struct(s);
1415 });
1416 var Struct = createStruct({
1417 kind: 'tuple',
1418 type: "[" + Elements.map(function (S) {
1419 return S.type;
1420 }).join() + "]",
1421 defaults: defaults,
1422 struct: struct
1423 });
1424
1425 Struct.check = function (value, branch, path) {
1426 if (value === void 0) {
1427 value = Struct.default();
1428 }
1429
1430 if (!Array.isArray(value)) {
1431 return [[Struct.fail({
1432 value: value,
1433 branch: branch,
1434 path: path
1435 })]];
1436 }
1437
1438 var result = [];
1439 var failures = [];
1440 var length = Math.max(value.length, Elements.length);
1441
1442 for (var i = 0; i < length; i++) {
1443 var Element = Elements[i];
1444 var v = value[i];
1445 var p = path.concat(i);
1446 var b = branch.concat(v);
1447
1448 if (!Element) {
1449 failures.push(Struct.fail({
1450 type: undefined,
1451 value: v,
1452 path: p,
1453 branch: b
1454 }));
1455 } else {
1456 var _Element$check = Element.check(v, b, p),
1457 efs = _Element$check[0],
1458 er = _Element$check[1];
1459
1460 if (efs) {
1461 failures.push.apply(failures, efs);
1462 } else {
1463 result[i] = er;
1464 }
1465 }
1466 }
1467
1468 return failures.length ? [failures] : [undefined, result];
1469 };
1470
1471 return Struct;
1472};
1473
1474var createUnion = function createUnion(schema, defaults, struct) {
1475 invariant(Array.isArray(schema) && schema.length !== 0, "Union structs must be defined as a non-empty array, but you passed: " + schema);
1476 var Structs = schema.map(function (sch) {
1477 return struct(sch);
1478 });
1479 var type = Structs.map(function (s) {
1480 return s.type;
1481 }).join(' | ');
1482 var Struct = createStruct({
1483 kind: 'union',
1484 type: type,
1485 defaults: defaults,
1486 struct: struct
1487 });
1488
1489 Struct.check = function (value, branch, path) {
1490 if (value === void 0) {
1491 value = Struct.default();
1492 }
1493
1494 for (var _iterator = Structs, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
1495 var _ref;
1496
1497 if (_isArray) {
1498 if (_i >= _iterator.length) break;
1499 _ref = _iterator[_i++];
1500 } else {
1501 _i = _iterator.next();
1502 if (_i.done) break;
1503 _ref = _i.value;
1504 }
1505
1506 var _struct = _ref;
1507
1508 var _struct$check = _struct.check(value, branch, path),
1509 fs = _struct$check[0],
1510 v = _struct$check[1];
1511
1512 if (!fs) {
1513 return [undefined, v];
1514 }
1515 }
1516
1517 return [[Struct.fail({
1518 value: value,
1519 branch: branch,
1520 path: path
1521 })]];
1522 };
1523
1524 return Struct;
1525};
1526
1527/**
1528 * Create a struct singleton with settings that include your own domain-specific
1529 * data `types`, and an optional custom `Error` class.
1530 */
1531
1532var superstruct = function superstruct(settings) {
1533 if (settings === void 0) {
1534 settings = {};
1535 }
1536
1537 var struct = function struct(schema, defaults) {
1538 return createShorthand(schema, defaults, struct);
1539 };
1540
1541 struct.array = function (schema, defaults) {
1542 return createArray(schema, defaults, struct);
1543 };
1544
1545 struct.dynamic = function (schema, defaults) {
1546 return createDynamic(schema, defaults, struct);
1547 };
1548
1549 struct.enum = function (schema, defaults) {
1550 return createEnum(schema, defaults, struct);
1551 };
1552
1553 struct.function = function (schema, defaults) {
1554 return createFunction(schema, defaults, struct);
1555 };
1556
1557 struct.instance = function (schema, defaults) {
1558 return createInstance(schema, defaults, struct);
1559 };
1560
1561 struct.interface = function (schema, defaults) {
1562 return createInterface(schema, defaults, struct);
1563 };
1564
1565 struct.intersection = function (schema, defaults) {
1566 return createIntersection(schema, defaults, struct);
1567 };
1568
1569 struct.lazy = function (schema, defaults) {
1570 return createLazy(schema, defaults, struct);
1571 };
1572
1573 struct.literal = function (schema, defaults) {
1574 return createLiteral(schema, defaults, struct);
1575 };
1576
1577 struct.object = function (schema, defaults) {
1578 return createObject(schema, defaults, struct);
1579 };
1580
1581 struct.optional = function (schema, defaults) {
1582 return createUnion([schema, 'undefined'], defaults, struct);
1583 };
1584
1585 struct.partial = function (schema, defaults) {
1586 return createPartial(schema, defaults, struct);
1587 };
1588
1589 struct.pick = function (schema, defaults) {
1590 return createPick(schema, defaults, struct);
1591 };
1592
1593 struct.record = function (schema, defaults) {
1594 return createRecord(schema, defaults, struct);
1595 };
1596
1597 struct.scalar = function (schema, defaults) {
1598 return createScalar(schema, defaults, struct);
1599 };
1600
1601 struct.size = function (schema, defaults) {
1602 return createSize(schema, defaults, struct);
1603 };
1604
1605 struct.tuple = function (schema, defaults) {
1606 return createTuple(schema, defaults, struct);
1607 };
1608
1609 struct.union = function (schema, defaults) {
1610 return createUnion(schema, defaults, struct);
1611 };
1612
1613 struct.Error = settings.error || StructError;
1614 struct.Types = _objectSpread2({}, Types, {}, settings.types);
1615 return struct;
1616};
1617
1618/**
1619 * The singleton instance of Superstruct that is exported by default, configured
1620 * with types for all of the JavaScript built-in data types.
1621 *
1622 * You can use it if you don't need any custom types. However, if you do want to
1623 * define custom types, use the [[superstruct]] factory to configure your own
1624 * [[Superstruct]] instance.
1625 */
1626
1627var struct = superstruct();
1628
1629export { StructError, Types, isStruct, struct, superstruct };
1630//# sourceMappingURL=index.es.js.map