@optique/core 1.0.0-dev.1244 → 1.0.0-dev.1251

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.
@@ -2647,8 +2647,30 @@ function cidr(options) {
2647
2647
  const maxPrefix = options?.maxPrefix;
2648
2648
  const errors = options?.errors;
2649
2649
  const metavar = options?.metavar ?? "CIDR";
2650
- const ipv4Parser = version === 4 || version === "both" ? ipv4(options?.ipv4) : null;
2651
- const ipv6Parser = version === 6 || version === "both" ? ipv6(options?.ipv6) : null;
2650
+ const genericIpSentinel = [];
2651
+ const ipv4Parser = version === 4 || version === "both" ? ipv4({
2652
+ ...options?.ipv4,
2653
+ errors: {
2654
+ invalidIpv4: genericIpSentinel,
2655
+ privateNotAllowed: errors?.privateNotAllowed,
2656
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2657
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2658
+ multicastNotAllowed: errors?.multicastNotAllowed,
2659
+ broadcastNotAllowed: errors?.broadcastNotAllowed,
2660
+ zeroNotAllowed: errors?.zeroNotAllowed
2661
+ }
2662
+ }) : null;
2663
+ const ipv6Parser = version === 6 || version === "both" ? ipv6({
2664
+ ...options?.ipv6,
2665
+ errors: {
2666
+ invalidIpv6: genericIpSentinel,
2667
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2668
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2669
+ multicastNotAllowed: errors?.multicastNotAllowed,
2670
+ zeroNotAllowed: errors?.zeroNotAllowed,
2671
+ uniqueLocalNotAllowed: errors?.uniqueLocalNotAllowed
2672
+ }
2673
+ }) : null;
2652
2674
  return {
2653
2675
  $mode: "sync",
2654
2676
  metavar,
@@ -2709,6 +2731,8 @@ function cidr(options) {
2709
2731
  }
2710
2732
  let ipVersion = null;
2711
2733
  let normalizedIp = null;
2734
+ let ipv4Error = null;
2735
+ let ipv6Error = null;
2712
2736
  if (ipv4Parser !== null) {
2713
2737
  const result = ipv4Parser.parse(ipPart);
2714
2738
  if (result.success) {
@@ -2747,7 +2771,7 @@ function cidr(options) {
2747
2771
  error: msg
2748
2772
  };
2749
2773
  }
2750
- }
2774
+ } else ipv4Error = result;
2751
2775
  }
2752
2776
  if (ipVersion === null && ipv6Parser !== null) {
2753
2777
  const result = ipv6Parser.parse(ipPart);
@@ -2787,9 +2811,120 @@ function cidr(options) {
2787
2811
  error: msg
2788
2812
  };
2789
2813
  }
2790
- }
2814
+ } else ipv6Error = result;
2791
2815
  }
2792
2816
  if (ipVersion === null || normalizedIp === null) {
2817
+ const candidates = [[
2818
+ ipv4Error,
2819
+ 4,
2820
+ 32
2821
+ ], [
2822
+ ipv6Error,
2823
+ 6,
2824
+ 128
2825
+ ]];
2826
+ for (const [err, ver, maxPfx] of candidates) if (err !== null && !err.success && err.error !== genericIpSentinel) {
2827
+ if (prefix > maxPfx) {
2828
+ const errorMsg$1 = errors?.invalidPrefix;
2829
+ if (typeof errorMsg$1 === "function") return {
2830
+ success: false,
2831
+ error: errorMsg$1(prefix, ver)
2832
+ };
2833
+ const msg$1 = errorMsg$1 ?? [
2834
+ {
2835
+ type: "text",
2836
+ text: "Expected a prefix length between 0 and "
2837
+ },
2838
+ {
2839
+ type: "text",
2840
+ text: maxPfx.toString()
2841
+ },
2842
+ {
2843
+ type: "text",
2844
+ text: ` for IPv${ver}, but got `
2845
+ },
2846
+ {
2847
+ type: "text",
2848
+ text: prefix.toString()
2849
+ },
2850
+ {
2851
+ type: "text",
2852
+ text: "."
2853
+ }
2854
+ ];
2855
+ return {
2856
+ success: false,
2857
+ error: msg$1
2858
+ };
2859
+ }
2860
+ if (minPrefix !== void 0 && prefix < minPrefix) {
2861
+ const errorMsg$1 = errors?.prefixBelowMinimum;
2862
+ if (typeof errorMsg$1 === "function") return {
2863
+ success: false,
2864
+ error: errorMsg$1(prefix, minPrefix)
2865
+ };
2866
+ const msg$1 = errorMsg$1 ?? [
2867
+ {
2868
+ type: "text",
2869
+ text: "Expected a prefix length greater than or equal to "
2870
+ },
2871
+ {
2872
+ type: "text",
2873
+ text: minPrefix.toString()
2874
+ },
2875
+ {
2876
+ type: "text",
2877
+ text: ", but got "
2878
+ },
2879
+ {
2880
+ type: "text",
2881
+ text: prefix.toString()
2882
+ },
2883
+ {
2884
+ type: "text",
2885
+ text: "."
2886
+ }
2887
+ ];
2888
+ return {
2889
+ success: false,
2890
+ error: msg$1
2891
+ };
2892
+ }
2893
+ if (maxPrefix !== void 0 && prefix > maxPrefix) {
2894
+ const errorMsg$1 = errors?.prefixAboveMaximum;
2895
+ if (typeof errorMsg$1 === "function") return {
2896
+ success: false,
2897
+ error: errorMsg$1(prefix, maxPrefix)
2898
+ };
2899
+ const msg$1 = errorMsg$1 ?? [
2900
+ {
2901
+ type: "text",
2902
+ text: "Expected a prefix length less than or equal to "
2903
+ },
2904
+ {
2905
+ type: "text",
2906
+ text: maxPrefix.toString()
2907
+ },
2908
+ {
2909
+ type: "text",
2910
+ text: ", but got "
2911
+ },
2912
+ {
2913
+ type: "text",
2914
+ text: prefix.toString()
2915
+ },
2916
+ {
2917
+ type: "text",
2918
+ text: "."
2919
+ }
2920
+ ];
2921
+ return {
2922
+ success: false,
2923
+ error: msg$1
2924
+ };
2925
+ }
2926
+ return err;
2927
+ }
2793
2928
  const errorMsg = errors?.invalidCidr;
2794
2929
  if (typeof errorMsg === "function") return {
2795
2930
  success: false,
@@ -1922,6 +1922,50 @@ interface CidrOptions {
1922
1922
  * Can be a static message or a function that receives the prefix and maximum.
1923
1923
  */
1924
1924
  prefixAboveMaximum?: Message | ((prefix: number, max: number) => Message);
1925
+ /**
1926
+ * Custom error message when a private IPv4 address is used but disallowed.
1927
+ * Can be a static message or a function that receives the IP.
1928
+ * @since 1.0.0
1929
+ */
1930
+ privateNotAllowed?: Message | ((ip: string) => Message);
1931
+ /**
1932
+ * Custom error message when a loopback address is used but disallowed.
1933
+ * Can be a static message or a function that receives the IP.
1934
+ * @since 1.0.0
1935
+ */
1936
+ loopbackNotAllowed?: Message | ((ip: string) => Message);
1937
+ /**
1938
+ * Custom error message when a link-local address is used but disallowed.
1939
+ * Can be a static message or a function that receives the IP.
1940
+ * @since 1.0.0
1941
+ */
1942
+ linkLocalNotAllowed?: Message | ((ip: string) => Message);
1943
+ /**
1944
+ * Custom error message when a multicast address is used but disallowed.
1945
+ * Can be a static message or a function that receives the IP.
1946
+ * @since 1.0.0
1947
+ */
1948
+ multicastNotAllowed?: Message | ((ip: string) => Message);
1949
+ /**
1950
+ * Custom error message when the broadcast address is used but disallowed
1951
+ * (IPv4 only).
1952
+ * Can be a static message or a function that receives the IP.
1953
+ * @since 1.0.0
1954
+ */
1955
+ broadcastNotAllowed?: Message | ((ip: string) => Message);
1956
+ /**
1957
+ * Custom error message when the zero address is used but disallowed.
1958
+ * Can be a static message or a function that receives the IP.
1959
+ * @since 1.0.0
1960
+ */
1961
+ zeroNotAllowed?: Message | ((ip: string) => Message);
1962
+ /**
1963
+ * Custom error message when a unique local address is used but disallowed
1964
+ * (IPv6 only).
1965
+ * Can be a static message or a function that receives the IP.
1966
+ * @since 1.0.0
1967
+ */
1968
+ uniqueLocalNotAllowed?: Message | ((ip: string) => Message);
1925
1969
  };
1926
1970
  }
1927
1971
  /**
@@ -1922,6 +1922,50 @@ interface CidrOptions {
1922
1922
  * Can be a static message or a function that receives the prefix and maximum.
1923
1923
  */
1924
1924
  prefixAboveMaximum?: Message | ((prefix: number, max: number) => Message);
1925
+ /**
1926
+ * Custom error message when a private IPv4 address is used but disallowed.
1927
+ * Can be a static message or a function that receives the IP.
1928
+ * @since 1.0.0
1929
+ */
1930
+ privateNotAllowed?: Message | ((ip: string) => Message);
1931
+ /**
1932
+ * Custom error message when a loopback address is used but disallowed.
1933
+ * Can be a static message or a function that receives the IP.
1934
+ * @since 1.0.0
1935
+ */
1936
+ loopbackNotAllowed?: Message | ((ip: string) => Message);
1937
+ /**
1938
+ * Custom error message when a link-local address is used but disallowed.
1939
+ * Can be a static message or a function that receives the IP.
1940
+ * @since 1.0.0
1941
+ */
1942
+ linkLocalNotAllowed?: Message | ((ip: string) => Message);
1943
+ /**
1944
+ * Custom error message when a multicast address is used but disallowed.
1945
+ * Can be a static message or a function that receives the IP.
1946
+ * @since 1.0.0
1947
+ */
1948
+ multicastNotAllowed?: Message | ((ip: string) => Message);
1949
+ /**
1950
+ * Custom error message when the broadcast address is used but disallowed
1951
+ * (IPv4 only).
1952
+ * Can be a static message or a function that receives the IP.
1953
+ * @since 1.0.0
1954
+ */
1955
+ broadcastNotAllowed?: Message | ((ip: string) => Message);
1956
+ /**
1957
+ * Custom error message when the zero address is used but disallowed.
1958
+ * Can be a static message or a function that receives the IP.
1959
+ * @since 1.0.0
1960
+ */
1961
+ zeroNotAllowed?: Message | ((ip: string) => Message);
1962
+ /**
1963
+ * Custom error message when a unique local address is used but disallowed
1964
+ * (IPv6 only).
1965
+ * Can be a static message or a function that receives the IP.
1966
+ * @since 1.0.0
1967
+ */
1968
+ uniqueLocalNotAllowed?: Message | ((ip: string) => Message);
1925
1969
  };
1926
1970
  }
1927
1971
  /**
@@ -2647,8 +2647,30 @@ function cidr(options) {
2647
2647
  const maxPrefix = options?.maxPrefix;
2648
2648
  const errors = options?.errors;
2649
2649
  const metavar = options?.metavar ?? "CIDR";
2650
- const ipv4Parser = version === 4 || version === "both" ? ipv4(options?.ipv4) : null;
2651
- const ipv6Parser = version === 6 || version === "both" ? ipv6(options?.ipv6) : null;
2650
+ const genericIpSentinel = [];
2651
+ const ipv4Parser = version === 4 || version === "both" ? ipv4({
2652
+ ...options?.ipv4,
2653
+ errors: {
2654
+ invalidIpv4: genericIpSentinel,
2655
+ privateNotAllowed: errors?.privateNotAllowed,
2656
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2657
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2658
+ multicastNotAllowed: errors?.multicastNotAllowed,
2659
+ broadcastNotAllowed: errors?.broadcastNotAllowed,
2660
+ zeroNotAllowed: errors?.zeroNotAllowed
2661
+ }
2662
+ }) : null;
2663
+ const ipv6Parser = version === 6 || version === "both" ? ipv6({
2664
+ ...options?.ipv6,
2665
+ errors: {
2666
+ invalidIpv6: genericIpSentinel,
2667
+ loopbackNotAllowed: errors?.loopbackNotAllowed,
2668
+ linkLocalNotAllowed: errors?.linkLocalNotAllowed,
2669
+ multicastNotAllowed: errors?.multicastNotAllowed,
2670
+ zeroNotAllowed: errors?.zeroNotAllowed,
2671
+ uniqueLocalNotAllowed: errors?.uniqueLocalNotAllowed
2672
+ }
2673
+ }) : null;
2652
2674
  return {
2653
2675
  $mode: "sync",
2654
2676
  metavar,
@@ -2709,6 +2731,8 @@ function cidr(options) {
2709
2731
  }
2710
2732
  let ipVersion = null;
2711
2733
  let normalizedIp = null;
2734
+ let ipv4Error = null;
2735
+ let ipv6Error = null;
2712
2736
  if (ipv4Parser !== null) {
2713
2737
  const result = ipv4Parser.parse(ipPart);
2714
2738
  if (result.success) {
@@ -2747,7 +2771,7 @@ function cidr(options) {
2747
2771
  error: msg
2748
2772
  };
2749
2773
  }
2750
- }
2774
+ } else ipv4Error = result;
2751
2775
  }
2752
2776
  if (ipVersion === null && ipv6Parser !== null) {
2753
2777
  const result = ipv6Parser.parse(ipPart);
@@ -2787,9 +2811,120 @@ function cidr(options) {
2787
2811
  error: msg
2788
2812
  };
2789
2813
  }
2790
- }
2814
+ } else ipv6Error = result;
2791
2815
  }
2792
2816
  if (ipVersion === null || normalizedIp === null) {
2817
+ const candidates = [[
2818
+ ipv4Error,
2819
+ 4,
2820
+ 32
2821
+ ], [
2822
+ ipv6Error,
2823
+ 6,
2824
+ 128
2825
+ ]];
2826
+ for (const [err, ver, maxPfx] of candidates) if (err !== null && !err.success && err.error !== genericIpSentinel) {
2827
+ if (prefix > maxPfx) {
2828
+ const errorMsg$1 = errors?.invalidPrefix;
2829
+ if (typeof errorMsg$1 === "function") return {
2830
+ success: false,
2831
+ error: errorMsg$1(prefix, ver)
2832
+ };
2833
+ const msg$1 = errorMsg$1 ?? [
2834
+ {
2835
+ type: "text",
2836
+ text: "Expected a prefix length between 0 and "
2837
+ },
2838
+ {
2839
+ type: "text",
2840
+ text: maxPfx.toString()
2841
+ },
2842
+ {
2843
+ type: "text",
2844
+ text: ` for IPv${ver}, but got `
2845
+ },
2846
+ {
2847
+ type: "text",
2848
+ text: prefix.toString()
2849
+ },
2850
+ {
2851
+ type: "text",
2852
+ text: "."
2853
+ }
2854
+ ];
2855
+ return {
2856
+ success: false,
2857
+ error: msg$1
2858
+ };
2859
+ }
2860
+ if (minPrefix !== void 0 && prefix < minPrefix) {
2861
+ const errorMsg$1 = errors?.prefixBelowMinimum;
2862
+ if (typeof errorMsg$1 === "function") return {
2863
+ success: false,
2864
+ error: errorMsg$1(prefix, minPrefix)
2865
+ };
2866
+ const msg$1 = errorMsg$1 ?? [
2867
+ {
2868
+ type: "text",
2869
+ text: "Expected a prefix length greater than or equal to "
2870
+ },
2871
+ {
2872
+ type: "text",
2873
+ text: minPrefix.toString()
2874
+ },
2875
+ {
2876
+ type: "text",
2877
+ text: ", but got "
2878
+ },
2879
+ {
2880
+ type: "text",
2881
+ text: prefix.toString()
2882
+ },
2883
+ {
2884
+ type: "text",
2885
+ text: "."
2886
+ }
2887
+ ];
2888
+ return {
2889
+ success: false,
2890
+ error: msg$1
2891
+ };
2892
+ }
2893
+ if (maxPrefix !== void 0 && prefix > maxPrefix) {
2894
+ const errorMsg$1 = errors?.prefixAboveMaximum;
2895
+ if (typeof errorMsg$1 === "function") return {
2896
+ success: false,
2897
+ error: errorMsg$1(prefix, maxPrefix)
2898
+ };
2899
+ const msg$1 = errorMsg$1 ?? [
2900
+ {
2901
+ type: "text",
2902
+ text: "Expected a prefix length less than or equal to "
2903
+ },
2904
+ {
2905
+ type: "text",
2906
+ text: maxPrefix.toString()
2907
+ },
2908
+ {
2909
+ type: "text",
2910
+ text: ", but got "
2911
+ },
2912
+ {
2913
+ type: "text",
2914
+ text: prefix.toString()
2915
+ },
2916
+ {
2917
+ type: "text",
2918
+ text: "."
2919
+ }
2920
+ ];
2921
+ return {
2922
+ success: false,
2923
+ error: msg$1
2924
+ };
2925
+ }
2926
+ return err;
2927
+ }
2793
2928
  const errorMsg = errors?.invalidCidr;
2794
2929
  if (typeof errorMsg === "function") return {
2795
2930
  success: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1244+338f4cf1",
3
+ "version": "1.0.0-dev.1251+2501c981",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",