decoders 2.12.1 → 2.12.2

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/dist/index.cjs CHANGED
@@ -670,18 +670,6 @@ function tuple(...decoders) {
670
670
  });
671
671
  }
672
672
 
673
- // src/lib/set-methods.ts
674
- // @__NO_SIDE_EFFECTS__
675
- function difference(xs, ys) {
676
- const result = /* @__PURE__ */ new Set();
677
- for (const x of xs) {
678
- if (!ys.has(x)) {
679
- result.add(x);
680
- }
681
- }
682
- return result;
683
- }
684
-
685
673
  // src/objects.ts
686
674
  // @__NO_SIDE_EFFECTS__
687
675
  function rejectUnsafeKey(decoders) {
@@ -703,13 +691,12 @@ var pojo = define(
703
691
  );
704
692
  // @__NO_SIDE_EFFECTS__
705
693
  function buildObject(decoders) {
706
- const knownKeys = new Set(Object.keys(decoders));
694
+ const knownKeys = Object.keys(decoders);
707
695
  return pojo.chain((plainObj, ok2, err2) => {
708
- const actualKeys = new Set(Object.keys(plainObj));
709
- const missingKeys = difference(knownKeys, actualKeys);
710
696
  const record2 = {};
711
697
  let errors = null;
712
- for (const key of Object.keys(decoders)) {
698
+ let missingKeys = null;
699
+ for (const key of knownKeys) {
713
700
  const decoder = decoders[key];
714
701
  const rawValue = plainObj[key];
715
702
  const result = decoder.decode(rawValue);
@@ -718,25 +705,23 @@ function buildObject(decoders) {
718
705
  if (value !== void 0) {
719
706
  record2[key] = value;
720
707
  }
721
- missingKeys.delete(key);
722
708
  } else {
723
709
  const ann = result.error;
724
710
  if (rawValue === void 0) {
725
- missingKeys.add(key);
711
+ (missingKeys ??= []).push(key);
726
712
  } else {
727
- errors ??= /* @__PURE__ */ new Map();
728
- errors.set(key, ann);
713
+ (errors ??= /* @__PURE__ */ new Map()).set(key, ann);
729
714
  }
730
715
  }
731
716
  }
732
- if (errors || missingKeys.size > 0) {
717
+ if (errors || missingKeys) {
733
718
  let objAnn = public_annotateObject(plainObj);
734
719
  if (errors) {
735
720
  objAnn = merge(objAnn, errors);
736
721
  }
737
- if (missingKeys.size > 0) {
738
- const errMsg = Array.from(missingKeys).map(quote).join(", ");
739
- const pluralized = missingKeys.size > 1 ? "keys" : "key";
722
+ if (missingKeys) {
723
+ const errMsg = missingKeys.map(quote).join(", ");
724
+ const pluralized = missingKeys.length > 1 ? "keys" : "key";
740
725
  objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
741
726
  }
742
727
  return err2(objAnn);
@@ -753,41 +738,50 @@ function object(decoders) {
753
738
  function exact(decoders) {
754
739
  /* @__PURE__ */ rejectUnsafeKey(decoders);
755
740
  const allowedKeys = new Set(Object.keys(decoders));
756
- const checked = pojo.chain((plainObj, ok2, err2) => {
757
- const actualKeys = new Set(Object.keys(plainObj));
758
- if (actualKeys.has("__proto__")) {
759
- return err2(/* @__PURE__ */ annotateUnsafeKey(plainObj));
741
+ const objDecoder = /* @__PURE__ */ buildObject(decoders);
742
+ return pojo.chain((plainObj, _ok, err2) => {
743
+ let extraKeys = null;
744
+ for (const key of Object.keys(plainObj)) {
745
+ if (key === "__proto__") {
746
+ return err2(/* @__PURE__ */ annotateUnsafeKey(plainObj));
747
+ }
748
+ if (!allowedKeys.has(key)) {
749
+ (extraKeys ??= []).push(key);
750
+ }
751
+ }
752
+ if (extraKeys) {
753
+ return err2(`Unexpected extra keys: ${extraKeys.map(quote).join(", ")}`);
760
754
  }
761
- const extraKeys = difference(actualKeys, allowedKeys);
762
- return extraKeys.size > 0 ? err2(`Unexpected extra keys: ${Array.from(extraKeys).map(quote).join(", ")}`) : ok2(plainObj);
755
+ return objDecoder.decode(plainObj);
763
756
  });
764
- return checked.pipe(/* @__PURE__ */ buildObject(decoders));
765
757
  }
766
758
  // @__NO_SIDE_EFFECTS__
767
759
  function inexact(decoders) {
768
760
  /* @__PURE__ */ rejectUnsafeKey(decoders);
761
+ const safekeys = new Set(Object.keys(decoders));
762
+ const safeDecoder = /* @__PURE__ */ buildObject(decoders);
769
763
  return pojo.chain(
770
- (plainObj, _ok, err2) => {
764
+ (plainObj, ok2, err2) => {
771
765
  const allkeys = new Set(Object.keys(plainObj));
772
766
  if (allkeys.has("__proto__")) {
773
767
  return err2(/* @__PURE__ */ annotateUnsafeKey(plainObj));
774
768
  }
775
- return (/* @__PURE__ */ buildObject(decoders)).transform((safepart) => {
776
- const safekeys = new Set(Object.keys(decoders));
777
- for (const k of safekeys) allkeys.add(k);
778
- const rv = {};
779
- for (const k of allkeys) {
780
- if (safekeys.has(k)) {
781
- const value = safepart[k];
782
- if (value !== void 0) {
783
- rv[k] = value;
784
- }
785
- } else {
786
- rv[k] = plainObj[k];
769
+ const result = safeDecoder.decode(plainObj);
770
+ if (!result.ok) return result;
771
+ const safepart = result.value;
772
+ for (const k of safekeys) allkeys.add(k);
773
+ const rv = {};
774
+ for (const k of allkeys) {
775
+ if (safekeys.has(k)) {
776
+ const value = safepart[k];
777
+ if (value !== void 0) {
778
+ rv[k] = value;
787
779
  }
780
+ } else {
781
+ rv[k] = plainObj[k];
788
782
  }
789
- return rv;
790
- });
783
+ }
784
+ return ok2(rv);
791
785
  }
792
786
  );
793
787
  }
package/dist/index.js CHANGED
@@ -670,18 +670,6 @@ function tuple(...decoders) {
670
670
  });
671
671
  }
672
672
 
673
- // src/lib/set-methods.ts
674
- // @__NO_SIDE_EFFECTS__
675
- function difference(xs, ys) {
676
- const result = /* @__PURE__ */ new Set();
677
- for (const x of xs) {
678
- if (!ys.has(x)) {
679
- result.add(x);
680
- }
681
- }
682
- return result;
683
- }
684
-
685
673
  // src/objects.ts
686
674
  // @__NO_SIDE_EFFECTS__
687
675
  function rejectUnsafeKey(decoders) {
@@ -703,13 +691,12 @@ var pojo = define(
703
691
  );
704
692
  // @__NO_SIDE_EFFECTS__
705
693
  function buildObject(decoders) {
706
- const knownKeys = new Set(Object.keys(decoders));
694
+ const knownKeys = Object.keys(decoders);
707
695
  return pojo.chain((plainObj, ok2, err2) => {
708
- const actualKeys = new Set(Object.keys(plainObj));
709
- const missingKeys = difference(knownKeys, actualKeys);
710
696
  const record2 = {};
711
697
  let errors = null;
712
- for (const key of Object.keys(decoders)) {
698
+ let missingKeys = null;
699
+ for (const key of knownKeys) {
713
700
  const decoder = decoders[key];
714
701
  const rawValue = plainObj[key];
715
702
  const result = decoder.decode(rawValue);
@@ -718,25 +705,23 @@ function buildObject(decoders) {
718
705
  if (value !== void 0) {
719
706
  record2[key] = value;
720
707
  }
721
- missingKeys.delete(key);
722
708
  } else {
723
709
  const ann = result.error;
724
710
  if (rawValue === void 0) {
725
- missingKeys.add(key);
711
+ (missingKeys ??= []).push(key);
726
712
  } else {
727
- errors ??= /* @__PURE__ */ new Map();
728
- errors.set(key, ann);
713
+ (errors ??= /* @__PURE__ */ new Map()).set(key, ann);
729
714
  }
730
715
  }
731
716
  }
732
- if (errors || missingKeys.size > 0) {
717
+ if (errors || missingKeys) {
733
718
  let objAnn = public_annotateObject(plainObj);
734
719
  if (errors) {
735
720
  objAnn = merge(objAnn, errors);
736
721
  }
737
- if (missingKeys.size > 0) {
738
- const errMsg = Array.from(missingKeys).map(quote).join(", ");
739
- const pluralized = missingKeys.size > 1 ? "keys" : "key";
722
+ if (missingKeys) {
723
+ const errMsg = missingKeys.map(quote).join(", ");
724
+ const pluralized = missingKeys.length > 1 ? "keys" : "key";
740
725
  objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
741
726
  }
742
727
  return err2(objAnn);
@@ -753,41 +738,50 @@ function object(decoders) {
753
738
  function exact(decoders) {
754
739
  /* @__PURE__ */ rejectUnsafeKey(decoders);
755
740
  const allowedKeys = new Set(Object.keys(decoders));
756
- const checked = pojo.chain((plainObj, ok2, err2) => {
757
- const actualKeys = new Set(Object.keys(plainObj));
758
- if (actualKeys.has("__proto__")) {
759
- return err2(/* @__PURE__ */ annotateUnsafeKey(plainObj));
741
+ const objDecoder = /* @__PURE__ */ buildObject(decoders);
742
+ return pojo.chain((plainObj, _ok, err2) => {
743
+ let extraKeys = null;
744
+ for (const key of Object.keys(plainObj)) {
745
+ if (key === "__proto__") {
746
+ return err2(/* @__PURE__ */ annotateUnsafeKey(plainObj));
747
+ }
748
+ if (!allowedKeys.has(key)) {
749
+ (extraKeys ??= []).push(key);
750
+ }
751
+ }
752
+ if (extraKeys) {
753
+ return err2(`Unexpected extra keys: ${extraKeys.map(quote).join(", ")}`);
760
754
  }
761
- const extraKeys = difference(actualKeys, allowedKeys);
762
- return extraKeys.size > 0 ? err2(`Unexpected extra keys: ${Array.from(extraKeys).map(quote).join(", ")}`) : ok2(plainObj);
755
+ return objDecoder.decode(plainObj);
763
756
  });
764
- return checked.pipe(/* @__PURE__ */ buildObject(decoders));
765
757
  }
766
758
  // @__NO_SIDE_EFFECTS__
767
759
  function inexact(decoders) {
768
760
  /* @__PURE__ */ rejectUnsafeKey(decoders);
761
+ const safekeys = new Set(Object.keys(decoders));
762
+ const safeDecoder = /* @__PURE__ */ buildObject(decoders);
769
763
  return pojo.chain(
770
- (plainObj, _ok, err2) => {
764
+ (plainObj, ok2, err2) => {
771
765
  const allkeys = new Set(Object.keys(plainObj));
772
766
  if (allkeys.has("__proto__")) {
773
767
  return err2(/* @__PURE__ */ annotateUnsafeKey(plainObj));
774
768
  }
775
- return (/* @__PURE__ */ buildObject(decoders)).transform((safepart) => {
776
- const safekeys = new Set(Object.keys(decoders));
777
- for (const k of safekeys) allkeys.add(k);
778
- const rv = {};
779
- for (const k of allkeys) {
780
- if (safekeys.has(k)) {
781
- const value = safepart[k];
782
- if (value !== void 0) {
783
- rv[k] = value;
784
- }
785
- } else {
786
- rv[k] = plainObj[k];
769
+ const result = safeDecoder.decode(plainObj);
770
+ if (!result.ok) return result;
771
+ const safepart = result.value;
772
+ for (const k of safekeys) allkeys.add(k);
773
+ const rv = {};
774
+ for (const k of allkeys) {
775
+ if (safekeys.has(k)) {
776
+ const value = safepart[k];
777
+ if (value !== void 0) {
778
+ rv[k] = value;
787
779
  }
780
+ } else {
781
+ rv[k] = plainObj[k];
788
782
  }
789
- return rv;
790
- });
783
+ }
784
+ return ok2(rv);
791
785
  }
792
786
  );
793
787
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "2.12.1",
3
+ "version": "2.12.2",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data for TypeScript",
5
5
  "license": "MIT",
6
6
  "repository": {