mol_schema 0.0.23 → 0.0.24

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/node.mjs CHANGED
@@ -81,34 +81,36 @@ var $;
81
81
  static toString() {
82
82
  return $$.$mol_func_name(this);
83
83
  }
84
- /** Type-guard that checks value by schema. */
85
- static check(val) {
84
+ /** Type-predicate that checks value by schema. */
85
+ static check(value) {
86
86
  try {
87
- this.guard(val);
87
+ this.guard(value);
88
88
  return true;
89
89
  }
90
90
  catch (error) {
91
91
  return false;
92
92
  }
93
93
  }
94
- static [Symbol.hasInstance](val) {
95
- return this.check(val);
94
+ /** `instanceof` support */
95
+ static [Symbol.hasInstance](value) {
96
+ return this.check(value);
96
97
  }
97
- /** Strict parse. Fails of wrong values. */
98
+ /** Type-parser that fails of wrong values. */
98
99
  static guard(value) {
99
100
  return value;
100
101
  }
101
- /** Relaxed cast. Normalizes wrong values. */
102
+ /** Type-caster that normalizes wrong values. */
102
103
  static cast(value) {
103
104
  try {
104
- return this.guard(value);
105
+ this.guard(value);
106
+ return value;
105
107
  }
106
108
  catch (error) {
107
109
  return this.default;
108
110
  }
109
111
  }
110
112
  /** Default value which conforms schema. */
111
- static default = undefined;
113
+ static default = null;
112
114
  }
113
115
  $.$mol_schema_any = $mol_schema_any;
114
116
  })($ || ($ = {}));
@@ -568,7 +570,7 @@ var $;
568
570
  "use strict";
569
571
  var $;
570
572
  (function ($) {
571
- $.$mol_schema_instance = $mol_memo_key.func(function $mol_schema_instance(Class, ...args) {
573
+ $.$mol_schema_instance = $mol_memo_key.func(function $mol_schema_instance(Class) {
572
574
  class $mol_schema_instance_ extends $mol_schema_any {
573
575
  static Class = Class;
574
576
  static toString() {
@@ -581,9 +583,12 @@ var $;
581
583
  return value;
582
584
  return $mol_fail(new TypeError('Wrong class', { cause: { value, schema: this } }));
583
585
  }
584
- static default = new Class(...args);
586
+ static cast(value) {
587
+ return this.guard(value);
588
+ }
589
+ static default;
585
590
  }
586
- return ((Class[Symbol.hasInstance] === $mol_schema_any[Symbol.hasInstance])
591
+ return ((Class?.[Symbol.hasInstance] === $mol_schema_any[Symbol.hasInstance])
587
592
  ? Class
588
593
  : $mol_schema_instance_);
589
594
  });
@@ -591,31 +596,59 @@ var $;
591
596
 
592
597
  ;
593
598
  "use strict";
599
+ var $;
600
+ (function ($) {
601
+ function $mol_schema_lazy(Schema) {
602
+ // return $mol_delegate( $mol_schema_any, Schema )
603
+ return class $mol_schema_lazy_ extends $mol_schema_any {
604
+ static Schema = $mol_memo.func(Schema);
605
+ static guard(value) {
606
+ return this.Schema().guard(value);
607
+ }
608
+ static cast(value) {
609
+ return this.Schema().cast(value);
610
+ }
611
+ static get default() {
612
+ return this.Schema().default;
613
+ }
614
+ };
615
+ }
616
+ $.$mol_schema_lazy = $mol_schema_lazy;
617
+ })($ || ($ = {}));
594
618
 
595
619
  ;
596
620
  "use strict";
597
621
  var $;
598
622
  (function ($) {
599
- $.$mol_schema_every = $mol_memo_key.func(function $mol_schema_every(Schemas) {
600
- return class $mol_schema_every_ extends $mol_schema_any {
601
- static Schemas = Schemas;
623
+ $.$mol_schema_some = $mol_memo_key.func(function $mol_schema_some(Variants) {
624
+ return class $mol_schema_some_ extends $mol_schema_any {
625
+ static Variants = Variants;
602
626
  static toString() {
603
- if (this !== $mol_schema_every_)
627
+ if (this !== $mol_schema_some_)
604
628
  return super.toString();
605
- return '$mol_schema_every<' + $mol_key(Schemas) + '>';
629
+ return '$mol_schema_some<' + $mol_key(Variants) + '>';
606
630
  }
607
631
  static guard(value) {
608
- for (const Schema of Schemas) {
609
- Schema.guard(value);
632
+ const errors = [];
633
+ for (const Variant of Variants) {
634
+ try {
635
+ return Variant.guard(value);
636
+ }
637
+ catch (error) {
638
+ errors.push(error);
639
+ }
610
640
  }
611
- return value;
641
+ return $mol_fail(new AggregateError(errors, 'Wrong variant', { cause: { value, schema: this } }));
612
642
  }
613
643
  static cast(value) {
614
- for (const Scheme of Schemas)
615
- value = Scheme.cast(value);
616
- return value;
644
+ try {
645
+ return this.guard(value);
646
+ }
647
+ catch (error) {
648
+ return Variants[0].cast(value);
649
+ }
617
650
  }
618
- static default = Schemas.find(Scheme => this.check(Scheme.default));
651
+ static default = Variants[0].default;
619
652
  };
620
653
  });
621
654
  })($ || ($ = {}));
@@ -624,56 +657,50 @@ var $;
624
657
  "use strict";
625
658
  var $;
626
659
  (function ($) {
627
- $.$mol_schema_range = $mol_memo_key.func(function $mol_schema_range(Range) {
628
- return class $mol_schema_range_ extends $mol_schema_any {
629
- static Range = Range;
660
+ $.$mol_schema_dict = $mol_memo_key.func(function $mol_schema_dict(Pair) {
661
+ return class $mol_schema_dict_ extends $mol_schema_any {
662
+ static Pair = Pair;
630
663
  static toString() {
631
- if (this !== $mol_schema_range_)
664
+ if (this !== $mol_schema_dict_)
632
665
  return super.toString();
633
- return '$mol_schema_range<' + $mol_key(Range) + '>';
666
+ return '$mol_schema_dict<' + $mol_key(Pair) + '>';
634
667
  }
635
668
  static guard(value) {
636
- if (typeof value !== 'number' && typeof value !== 'bigint')
637
- return $mol_fail(new TypeError('Uncomparable type', { cause: { value, schema: this } }));
638
- if (!(value <= Range[1]))
639
- return $mol_fail(new TypeError('Too large', { cause: { value, schema: this } }));
640
- if (!(value >= Range[0]))
641
- return $mol_fail(new TypeError('Too small', { cause: { value, schema: this } }));
669
+ if (Object.getPrototypeOf(Object.getPrototypeOf(value))) {
670
+ return $mol_fail(new TypeError('Non dictionary', { cause: { value, schema: this } }));
671
+ }
672
+ for (const key in value) {
673
+ try {
674
+ Pair[0].guard(key);
675
+ }
676
+ catch (error) {
677
+ return $mol_fail(new TypeError('Wrong key', { cause: { key, error, value, schema: this } }));
678
+ }
679
+ try {
680
+ Pair[1].guard(value[key]);
681
+ }
682
+ catch (error) {
683
+ return $mol_fail(new TypeError('Wrong val', { cause: { key, error, value, schema: this } }));
684
+ }
685
+ }
642
686
  return value;
643
687
  }
644
- static cast(val) {
645
- if (val > Range[1])
646
- return Range[1];
647
- if (val >= Range[0])
648
- return val;
649
- return Range[0];
688
+ static cast(value) {
689
+ if (Object.getPrototypeOf(Object.getPrototypeOf(value)))
690
+ return this.default;
691
+ const res = {};
692
+ for (const key in value) {
693
+ if (!Pair[0].check(key))
694
+ continue;
695
+ res[key] = Pair[1].cast(value[key]);
696
+ }
697
+ return res;
650
698
  }
651
- static default = Range[0];
699
+ static default = {};
652
700
  };
653
701
  });
654
702
  })($ || ($ = {}));
655
703
 
656
- ;
657
- "use strict";
658
- var $;
659
- (function ($) {
660
- class $mol_schema_positive extends $mol_schema_range([0, Number.POSITIVE_INFINITY]) {
661
- }
662
- $.$mol_schema_positive = $mol_schema_positive;
663
- })($ || ($ = {}));
664
-
665
- ;
666
- "use strict";
667
- var $;
668
- (function ($) {
669
- class $mol_schema_natural extends $mol_schema_every([
670
- $mol_schema_integer,
671
- $mol_schema_positive,
672
- ]) {
673
- }
674
- $.$mol_schema_natural = $mol_schema_natural;
675
- })($ || ($ = {}));
676
-
677
704
  ;
678
705
  "use strict";
679
706
  var $;
@@ -689,11 +716,11 @@ var $;
689
716
  static guard(value) {
690
717
  if (Options.some(Option => Object.is(Option, value)))
691
718
  return value;
692
- return $mol_fail(new TypeError('No one option', { cause: { value, schema: this } }));
719
+ return $mol_fail(new TypeError('Wrong option', { cause: { value, schema: this } }));
693
720
  }
694
- static cast(val) {
695
- if (this.check(val))
696
- return val;
721
+ static cast(value) {
722
+ if (this.check(value))
723
+ return value;
697
724
  return Options[0];
698
725
  }
699
726
  static default = Options[0];
@@ -705,44 +732,33 @@ var $;
705
732
  "use strict";
706
733
  var $;
707
734
  (function ($) {
708
- class $mol_schema_negative extends $mol_schema_range([Number.NEGATIVE_INFINITY, 0]) {
709
- }
710
- $.$mol_schema_negative = $mol_schema_negative;
711
- })($ || ($ = {}));
712
-
713
- ;
714
- "use strict";
715
- var $;
716
- (function ($) {
717
- $.$mol_schema_some = $mol_memo_key.func(function $mol_schema_some(Variants) {
718
- return class $mol_schema_some_ extends $mol_schema_any {
719
- static Variants = Variants;
735
+ $.$mol_schema_list = $mol_memo_key.func(function $mol_schema_list(Item) {
736
+ return class $mol_schema_list_ extends $mol_schema_any {
737
+ static Item = Item;
720
738
  static toString() {
721
- if (this !== $mol_schema_some_)
739
+ if (this !== $mol_schema_list_)
722
740
  return super.toString();
723
- return '$mol_schema_some<' + $mol_key(Variants) + '>';
741
+ return '$mol_schema_list<' + $mol_key(Item) + '>';
724
742
  }
725
743
  static guard(value) {
726
- const errors = [];
727
- for (const Variant of Variants) {
744
+ if (!Array.isArray(value))
745
+ return $mol_fail(new TypeError('Non array', { cause: { value, schema: this } }));
746
+ for (const [index, item] of super.guard(value).entries()) {
728
747
  try {
729
- return Variant.guard(value);
748
+ Item.guard(item);
730
749
  }
731
750
  catch (error) {
732
- errors.push(error);
751
+ return $mol_fail(new TypeError('Wrong item', { cause: { index, error, value, schema: this } }));
733
752
  }
734
753
  }
735
- return $mol_fail(new AggregateError(errors, 'No one variant', { cause: { value, schema: this } }));
754
+ return value;
736
755
  }
737
756
  static cast(value) {
738
- try {
739
- return this.guard(value);
740
- }
741
- catch (error) {
742
- return Variants[0].cast(value);
743
- }
757
+ if (!Array.isArray(value))
758
+ return this.default;
759
+ return value.map(item => Item.cast(item));
744
760
  }
745
- static default = Variants[0].default;
761
+ static default = [];
746
762
  };
747
763
  });
748
764
  })($ || ($ = {}));
@@ -751,14 +767,43 @@ var $;
751
767
  "use strict";
752
768
  var $;
753
769
  (function ($) {
754
- $.$mol_schema_maybe = $mol_memo_key.func(function $mol_schema_maybe(Some) {
755
- return class $mol_schema_maybe_ extends $mol_schema_some([$mol_schema_enum([undefined, null]), Some]) {
756
- static Some = Some;
770
+ class $mol_schema_json extends $mol_schema_lazy(() => $mol_schema_some([
771
+ $mol_schema_dict([$mol_schema_string, $mol_schema_json]),
772
+ $mol_schema_enum([null]),
773
+ $mol_schema_boolean, $mol_schema_float, $mol_schema_string,
774
+ $mol_schema_list($mol_schema_json),
775
+ ])) {
776
+ }
777
+ $.$mol_schema_json = $mol_schema_json;
778
+ })($ || ($ = {}));
779
+
780
+ ;
781
+ "use strict";
782
+
783
+ ;
784
+ "use strict";
785
+ var $;
786
+ (function ($) {
787
+ $.$mol_schema_every = $mol_memo_key.func(function $mol_schema_every(Schemas) {
788
+ return class $mol_schema_every_ extends $mol_schema_any {
789
+ static Schemas = Schemas;
757
790
  static toString() {
758
- if (this !== $mol_schema_maybe_)
791
+ if (this !== $mol_schema_every_)
759
792
  return super.toString();
760
- return '$mol_schema_maybe<' + $mol_key(Some) + '>';
793
+ return '$mol_schema_every<' + $mol_key(Schemas) + '>';
761
794
  }
795
+ static guard(value) {
796
+ for (const Schema of Schemas) {
797
+ Schema.guard(value);
798
+ }
799
+ return value;
800
+ }
801
+ static cast(value) {
802
+ for (const Scheme of Schemas)
803
+ value = Scheme.cast(value);
804
+ return value;
805
+ }
806
+ static default = Schemas.find(Scheme => this.check(Scheme.default));
762
807
  };
763
808
  });
764
809
  })($ || ($ = {}));
@@ -767,33 +812,31 @@ var $;
767
812
  "use strict";
768
813
  var $;
769
814
  (function ($) {
770
- $.$mol_schema_list = $mol_memo_key.func(function $mol_schema_list(Item) {
771
- return class $mol_schema_list_ extends $mol_schema_any {
772
- static Item = Item;
815
+ $.$mol_schema_range = $mol_memo_key.func(function $mol_schema_range(Range) {
816
+ return class $mol_schema_range_ extends $mol_schema_any {
817
+ static Range = Range;
773
818
  static toString() {
774
- if (this !== $mol_schema_list_)
819
+ if (this !== $mol_schema_range_)
775
820
  return super.toString();
776
- return '$mol_schema_list<' + $mol_key(Item) + '>';
821
+ return '$mol_schema_range<' + $mol_key(Range) + '>';
777
822
  }
778
823
  static guard(value) {
779
- if (!Array.isArray(value))
780
- return $mol_fail(new TypeError('Non array', { cause: { value, schema: this } }));
781
- for (const [index, item] of super.guard(value).entries()) {
782
- try {
783
- Item.guard(item);
784
- }
785
- catch (error) {
786
- return $mol_fail(new TypeError('Wrong item', { cause: { index, error, value, schema: this } }));
787
- }
788
- }
824
+ if (typeof value !== 'number' && typeof value !== 'bigint')
825
+ return $mol_fail(new TypeError('Uncomparable type', { cause: { value, schema: this } }));
826
+ if (!(value <= Range[1]))
827
+ return $mol_fail(new TypeError('Too large', { cause: { value, schema: this } }));
828
+ if (!(value >= Range[0]))
829
+ return $mol_fail(new TypeError('Too small', { cause: { value, schema: this } }));
789
830
  return value;
790
831
  }
791
832
  static cast(value) {
792
- if (!Array.isArray(value))
793
- return this.default;
794
- return value.map(item => Item.cast(item));
833
+ if (value > Range[1])
834
+ return Range[1];
835
+ if (value >= Range[0])
836
+ return value;
837
+ return Range[0];
795
838
  }
796
- static default = [];
839
+ static default = Range[0];
797
840
  };
798
841
  });
799
842
  })($ || ($ = {}));
@@ -802,46 +845,50 @@ var $;
802
845
  "use strict";
803
846
  var $;
804
847
  (function ($) {
805
- $.$mol_schema_dict = $mol_memo_key.func(function $mol_schema_dict(Pair) {
806
- return class $mol_schema_dict_ extends $mol_schema_any {
807
- static Pair = Pair;
848
+ class $mol_schema_positive extends $mol_schema_range([0, Number.POSITIVE_INFINITY]) {
849
+ }
850
+ $.$mol_schema_positive = $mol_schema_positive;
851
+ })($ || ($ = {}));
852
+
853
+ ;
854
+ "use strict";
855
+ var $;
856
+ (function ($) {
857
+ class $mol_schema_natural extends $mol_schema_every([
858
+ $mol_schema_integer,
859
+ $mol_schema_positive,
860
+ ]) {
861
+ }
862
+ $.$mol_schema_natural = $mol_schema_natural;
863
+ })($ || ($ = {}));
864
+
865
+ ;
866
+ "use strict";
867
+ var $;
868
+ (function ($) {
869
+ class $mol_schema_negative extends $mol_schema_range([Number.NEGATIVE_INFINITY, 0]) {
870
+ }
871
+ $.$mol_schema_negative = $mol_schema_negative;
872
+ })($ || ($ = {}));
873
+
874
+ ;
875
+ "use strict";
876
+ var $;
877
+ (function ($) {
878
+ $.$mol_schema_maybe = $mol_memo_key.func(function $mol_schema_maybe(Some) {
879
+ return class $mol_schema_maybe_ extends $mol_schema_any {
880
+ static Some = Some;
808
881
  static toString() {
809
- if (this !== $mol_schema_dict_)
882
+ if (this !== $mol_schema_maybe_)
810
883
  return super.toString();
811
- return '$mol_schema_dict<' + $mol_key(Pair) + '>';
884
+ return '$mol_schema_maybe<' + $mol_key(Some) + '>';
812
885
  }
813
886
  static guard(value) {
814
- if (Object.getPrototypeOf(Object.getPrototypeOf(value))) {
815
- return $mol_fail(new TypeError('Non dictionary', { cause: { value, schema: this } }));
816
- }
817
- for (const key in value) {
818
- try {
819
- Pair[0].guard(key);
820
- }
821
- catch (error) {
822
- return $mol_fail(new TypeError('Wrong key', { cause: { key, error, value, schema: this } }));
823
- }
824
- try {
825
- Pair[1].guard(value[key]);
826
- }
827
- catch (error) {
828
- return $mol_fail(new TypeError('Wrong val', { cause: { key, error, value, schema: this } }));
829
- }
830
- }
831
- return value;
832
- }
833
- static cast(value) {
834
- if (Object.getPrototypeOf(Object.getPrototypeOf(value)))
835
- return this.default;
836
- const res = {};
837
- for (const key in value) {
838
- if (!Pair[0].check(key))
839
- continue;
840
- res[key] = Pair[1].cast(value[key]);
841
- }
842
- return res;
887
+ if (value == null)
888
+ return value;
889
+ return Some.guard(value);
843
890
  }
844
- static default = {};
891
+ static default = null;
845
892
  };
846
893
  });
847
894
  })($ || ($ = {}));