@thirstie/thirstievalidators 1.0.0 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,44 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.1.0](https://github.com/ThirstieAdmin/thirstiejs-monorepo/compare/@thirstie/thirstievalidators@0.0.3...@thirstie/thirstievalidators@1.1.0) (2025-04-01)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * age gate validation ([6f88a6f](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/6f88a6fb88d4a2b02807eb457efbf28e95d2b232))
12
+ * SUPPORT-851 update email validator ([d883a16](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/d883a1653b0da2d7431d3237fc4f44d9e2fc9e66))
13
+
14
+
15
+ ### Features
16
+
17
+ * add state code, email domain checkers ([e5590ba](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/e5590ba6f3e2dd5b6f0952a39ca0b595aa08ed21))
18
+ * trusted email domain ([bd36a3c](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/bd36a3c6b2044d0acfa1d3c0250c68d3b46975fa))
19
+ * validator package ([7cc609a](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/7cc609a78cd030c866542a629d6bf307ad9b08c8))
20
+
21
+
22
+
23
+
24
+
25
+ ## [1.0.1](https://github.com/ThirstieAdmin/thirstiejs-monorepo/compare/@thirstie/thirstievalidators@0.0.3...@thirstie/thirstievalidators@1.0.1) (2025-03-17)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * age gate validation ([6f88a6f](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/6f88a6fb88d4a2b02807eb457efbf28e95d2b232))
31
+ * SUPPORT-851 update email validator ([d883a16](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/d883a1653b0da2d7431d3237fc4f44d9e2fc9e66))
32
+
33
+
34
+ ### Features
35
+
36
+ * add state code, email domain checkers ([e5590ba](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/e5590ba6f3e2dd5b6f0952a39ca0b595aa08ed21))
37
+ * trusted email domain ([bd36a3c](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/bd36a3c6b2044d0acfa1d3c0250c68d3b46975fa))
38
+ * validator package ([7cc609a](https://github.com/ThirstieAdmin/thirstiejs-monorepo/commit/7cc609a78cd030c866542a629d6bf307ad9b08c8))
39
+
40
+
41
+
42
+
43
+
6
44
  # [1.0.0](https://github.com/ThirstieAdmin/thirstiejs-monorepo/compare/@thirstie/thirstievalidators@0.0.3...@thirstie/thirstievalidators@1.0.0) (2025-03-15)
7
45
 
8
46
 
@@ -1,6 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
- <coverage generated="1742220734966" clover="3.2.0">
3
- <project timestamp="1742220734966" name="All files">
2
+ <coverage generated="1743534405831" clover="3.2.0">
3
+ <project timestamp="1743534405831" name="All files">
4
4
  <metrics statements="93" coveredstatements="77" conditionals="69" coveredconditionals="46" methods="16" coveredmethods="13" elements="178" coveredelements="136" complexity="0" loc="93" ncloc="93" packages="1" files="5" classes="5"/>
5
5
  <file name="agegate.js" path="/home/circleci/thirstiejs-monorepo/packages/thirstievalidators/src/agegate.js">
6
6
  <metrics statements="20" coveredstatements="19" conditionals="11" coveredconditionals="9" methods="1" coveredmethods="1"/>
@@ -163,7 +163,7 @@ export const ageGate = (ageGateType, ageGateValue, legalAge = null) =&gt; {
163
163
  <div class='footer quiet pad2 space-top1 center small'>
164
164
  Code coverage generated by
165
165
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
166
- at 2025-03-17T14:12:14.953Z
166
+ at 2025-04-01T19:06:45.815Z
167
167
  </div>
168
168
  <script src="prettify.js"></script>
169
169
  <script>
@@ -244,7 +244,7 @@ export const trustedEmailDomains = [
244
244
  <div class='footer quiet pad2 space-top1 center small'>
245
245
  Code coverage generated by
246
246
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
247
- at 2025-03-17T14:12:14.953Z
247
+ at 2025-04-01T19:06:45.815Z
248
248
  </div>
249
249
  <script src="prettify.js"></script>
250
250
  <script>
@@ -161,7 +161,7 @@
161
161
  <div class='footer quiet pad2 space-top1 center small'>
162
162
  Code coverage generated by
163
163
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
164
- at 2025-03-17T14:12:14.953Z
164
+ at 2025-04-01T19:06:45.815Z
165
165
  </div>
166
166
  <script src="prettify.js"></script>
167
167
  <script>
@@ -328,7 +328,7 @@ export { Validators, Checkers };
328
328
  <div class='footer quiet pad2 space-top1 center small'>
329
329
  Code coverage generated by
330
330
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
331
- at 2025-03-17T14:12:14.953Z
331
+ at 2025-04-01T19:06:45.815Z
332
332
  </div>
333
333
  <script src="prettify.js"></script>
334
334
  <script>
@@ -316,7 +316,7 @@ export const getAdministrativeArea = (value, countryCode = <span class="branch-0
316
316
  <div class='footer quiet pad2 space-top1 center small'>
317
317
  Code coverage generated by
318
318
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
319
- at 2025-03-17T14:12:14.953Z
319
+ at 2025-04-01T19:06:45.815Z
320
320
  </div>
321
321
  <script src="prettify.js"></script>
322
322
  <script>
@@ -163,7 +163,7 @@ export default passwordcheck;
163
163
  <div class='footer quiet pad2 space-top1 center small'>
164
164
  Code coverage generated by
165
165
  <a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
166
- at 2025-03-17T14:12:14.953Z
166
+ at 2025-04-01T19:06:45.815Z
167
167
  </div>
168
168
  <script src="prettify.js"></script>
169
169
  <script>
package/dist/bundle.cjs CHANGED
@@ -334,6 +334,9 @@ const quotelessJson = (obj) => {
334
334
  return json.replace(/"([^"]+)":/g, "$1:");
335
335
  };
336
336
  class ZodError extends Error {
337
+ get errors() {
338
+ return this.issues;
339
+ }
337
340
  constructor(issues) {
338
341
  super();
339
342
  this.issues = [];
@@ -354,9 +357,6 @@ class ZodError extends Error {
354
357
  this.name = "ZodError";
355
358
  this.issues = issues;
356
359
  }
357
- get errors() {
358
- return this.issues;
359
- }
360
360
  format(_mapper) {
361
361
  const mapper = _mapper ||
362
362
  function (issue) {
@@ -612,9 +612,9 @@ function addIssueToContext(ctx, issueData) {
612
612
  data: ctx.data,
613
613
  path: ctx.path,
614
614
  errorMaps: [
615
- ctx.common.contextualErrorMap,
616
- ctx.schemaErrorMap,
617
- overrideMap,
615
+ ctx.common.contextualErrorMap, // contextual error map is first priority
616
+ ctx.schemaErrorMap, // then schema-bound map if available
617
+ overrideMap, // then global override map
618
618
  overrideMap === errorMap ? undefined : errorMap, // then global default map
619
619
  ].filter((x) => !!x),
620
620
  });
@@ -701,12 +701,12 @@ PERFORMANCE OF THIS SOFTWARE.
701
701
  ***************************************************************************** */
702
702
 
703
703
  function __classPrivateFieldGet(receiver, state, kind, f) {
704
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
704
+ if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
705
705
  return state.get(receiver);
706
706
  }
707
707
 
708
708
  function __classPrivateFieldSet(receiver, state, value, kind, f) {
709
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
709
+ if (typeof state === "function" ? receiver !== state || true : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
710
710
  return (state.set(receiver, value)), value;
711
711
  }
712
712
 
@@ -787,35 +787,6 @@ function processCreateParams(params) {
787
787
  return { errorMap: customMap, description };
788
788
  }
789
789
  class ZodType {
790
- constructor(def) {
791
- /** Alias of safeParseAsync */
792
- this.spa = this.safeParseAsync;
793
- this._def = def;
794
- this.parse = this.parse.bind(this);
795
- this.safeParse = this.safeParse.bind(this);
796
- this.parseAsync = this.parseAsync.bind(this);
797
- this.safeParseAsync = this.safeParseAsync.bind(this);
798
- this.spa = this.spa.bind(this);
799
- this.refine = this.refine.bind(this);
800
- this.refinement = this.refinement.bind(this);
801
- this.superRefine = this.superRefine.bind(this);
802
- this.optional = this.optional.bind(this);
803
- this.nullable = this.nullable.bind(this);
804
- this.nullish = this.nullish.bind(this);
805
- this.array = this.array.bind(this);
806
- this.promise = this.promise.bind(this);
807
- this.or = this.or.bind(this);
808
- this.and = this.and.bind(this);
809
- this.transform = this.transform.bind(this);
810
- this.brand = this.brand.bind(this);
811
- this.default = this.default.bind(this);
812
- this.catch = this.catch.bind(this);
813
- this.describe = this.describe.bind(this);
814
- this.pipe = this.pipe.bind(this);
815
- this.readonly = this.readonly.bind(this);
816
- this.isNullable = this.isNullable.bind(this);
817
- this.isOptional = this.isOptional.bind(this);
818
- }
819
790
  get description() {
820
791
  return this._def.description;
821
792
  }
@@ -879,6 +850,48 @@ class ZodType {
879
850
  const result = this._parseSync({ data, path: ctx.path, parent: ctx });
880
851
  return handleResult(ctx, result);
881
852
  }
853
+ "~validate"(data) {
854
+ var _a, _b;
855
+ const ctx = {
856
+ common: {
857
+ issues: [],
858
+ async: !!this["~standard"].async,
859
+ },
860
+ path: [],
861
+ schemaErrorMap: this._def.errorMap,
862
+ parent: null,
863
+ data,
864
+ parsedType: getParsedType(data),
865
+ };
866
+ if (!this["~standard"].async) {
867
+ try {
868
+ const result = this._parseSync({ data, path: [], parent: ctx });
869
+ return isValid(result)
870
+ ? {
871
+ value: result.value,
872
+ }
873
+ : {
874
+ issues: ctx.common.issues,
875
+ };
876
+ }
877
+ catch (err) {
878
+ if ((_b = (_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes("encountered")) {
879
+ this["~standard"].async = true;
880
+ }
881
+ ctx.common = {
882
+ issues: [],
883
+ async: true,
884
+ };
885
+ }
886
+ }
887
+ return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result)
888
+ ? {
889
+ value: result.value,
890
+ }
891
+ : {
892
+ issues: ctx.common.issues,
893
+ });
894
+ }
882
895
  async parseAsync(data, params) {
883
896
  const result = await this.safeParseAsync(data, params);
884
897
  if (result.success)
@@ -965,6 +978,40 @@ class ZodType {
965
978
  superRefine(refinement) {
966
979
  return this._refinement(refinement);
967
980
  }
981
+ constructor(def) {
982
+ /** Alias of safeParseAsync */
983
+ this.spa = this.safeParseAsync;
984
+ this._def = def;
985
+ this.parse = this.parse.bind(this);
986
+ this.safeParse = this.safeParse.bind(this);
987
+ this.parseAsync = this.parseAsync.bind(this);
988
+ this.safeParseAsync = this.safeParseAsync.bind(this);
989
+ this.spa = this.spa.bind(this);
990
+ this.refine = this.refine.bind(this);
991
+ this.refinement = this.refinement.bind(this);
992
+ this.superRefine = this.superRefine.bind(this);
993
+ this.optional = this.optional.bind(this);
994
+ this.nullable = this.nullable.bind(this);
995
+ this.nullish = this.nullish.bind(this);
996
+ this.array = this.array.bind(this);
997
+ this.promise = this.promise.bind(this);
998
+ this.or = this.or.bind(this);
999
+ this.and = this.and.bind(this);
1000
+ this.transform = this.transform.bind(this);
1001
+ this.brand = this.brand.bind(this);
1002
+ this.default = this.default.bind(this);
1003
+ this.catch = this.catch.bind(this);
1004
+ this.describe = this.describe.bind(this);
1005
+ this.pipe = this.pipe.bind(this);
1006
+ this.readonly = this.readonly.bind(this);
1007
+ this.isNullable = this.isNullable.bind(this);
1008
+ this.isOptional = this.isOptional.bind(this);
1009
+ this["~standard"] = {
1010
+ version: 1,
1011
+ vendor: "zod",
1012
+ validate: (data) => this["~validate"](data),
1013
+ };
1014
+ }
968
1015
  optional() {
969
1016
  return ZodOptional.create(this, this._def);
970
1017
  }
@@ -975,7 +1022,7 @@ class ZodType {
975
1022
  return this.nullable().optional();
976
1023
  }
977
1024
  array() {
978
- return ZodArray.create(this, this._def);
1025
+ return ZodArray.create(this);
979
1026
  }
980
1027
  promise() {
981
1028
  return ZodPromise.create(this, this._def);
@@ -1041,11 +1088,12 @@ class ZodType {
1041
1088
  }
1042
1089
  const cuidRegex = /^c[^\s-]{8,}$/i;
1043
1090
  const cuid2Regex = /^[0-9a-z]+$/;
1044
- const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
1091
+ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
1045
1092
  // const uuidRegex =
1046
1093
  // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
1047
1094
  const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
1048
1095
  const nanoidRegex = /^[a-z0-9_-]{21}$/i;
1096
+ const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
1049
1097
  const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
1050
1098
  // from https://stackoverflow.com/a/46181/1550155
1051
1099
  // old version: too slow, didn't support unicode
@@ -1067,9 +1115,15 @@ const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
1067
1115
  let emojiRegex;
1068
1116
  // faster, simpler, safer
1069
1117
  const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
1070
- const ipv6Regex = /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
1118
+ const ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
1119
+ // const ipv6Regex =
1120
+ // /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
1121
+ const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
1122
+ const ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
1071
1123
  // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
1072
1124
  const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
1125
+ // https://base64.guru/standards/base64url
1126
+ const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
1073
1127
  // simple
1074
1128
  // const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
1075
1129
  // no leap year validation
@@ -1110,6 +1164,38 @@ function isValidIP(ip, version) {
1110
1164
  }
1111
1165
  return false;
1112
1166
  }
1167
+ function isValidJWT(jwt, alg) {
1168
+ if (!jwtRegex.test(jwt))
1169
+ return false;
1170
+ try {
1171
+ const [header] = jwt.split(".");
1172
+ // Convert base64url to base64
1173
+ const base64 = header
1174
+ .replace(/-/g, "+")
1175
+ .replace(/_/g, "/")
1176
+ .padEnd(header.length + ((4 - (header.length % 4)) % 4), "=");
1177
+ const decoded = JSON.parse(atob(base64));
1178
+ if (typeof decoded !== "object" || decoded === null)
1179
+ return false;
1180
+ if (!decoded.typ || !decoded.alg)
1181
+ return false;
1182
+ if (alg && decoded.alg !== alg)
1183
+ return false;
1184
+ return true;
1185
+ }
1186
+ catch (_a) {
1187
+ return false;
1188
+ }
1189
+ }
1190
+ function isValidCidr(ip, version) {
1191
+ if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
1192
+ return true;
1193
+ }
1194
+ if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
1195
+ return true;
1196
+ }
1197
+ return false;
1198
+ }
1113
1199
  class ZodString extends ZodType {
1114
1200
  _parse(input) {
1115
1201
  if (this._def.coerce) {
@@ -1391,6 +1477,28 @@ class ZodString extends ZodType {
1391
1477
  status.dirty();
1392
1478
  }
1393
1479
  }
1480
+ else if (check.kind === "jwt") {
1481
+ if (!isValidJWT(input.data, check.alg)) {
1482
+ ctx = this._getOrReturnCtx(input, ctx);
1483
+ addIssueToContext(ctx, {
1484
+ validation: "jwt",
1485
+ code: ZodIssueCode.invalid_string,
1486
+ message: check.message,
1487
+ });
1488
+ status.dirty();
1489
+ }
1490
+ }
1491
+ else if (check.kind === "cidr") {
1492
+ if (!isValidCidr(input.data, check.version)) {
1493
+ ctx = this._getOrReturnCtx(input, ctx);
1494
+ addIssueToContext(ctx, {
1495
+ validation: "cidr",
1496
+ code: ZodIssueCode.invalid_string,
1497
+ message: check.message,
1498
+ });
1499
+ status.dirty();
1500
+ }
1501
+ }
1394
1502
  else if (check.kind === "base64") {
1395
1503
  if (!base64Regex.test(input.data)) {
1396
1504
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1402,6 +1510,17 @@ class ZodString extends ZodType {
1402
1510
  status.dirty();
1403
1511
  }
1404
1512
  }
1513
+ else if (check.kind === "base64url") {
1514
+ if (!base64urlRegex.test(input.data)) {
1515
+ ctx = this._getOrReturnCtx(input, ctx);
1516
+ addIssueToContext(ctx, {
1517
+ validation: "base64url",
1518
+ code: ZodIssueCode.invalid_string,
1519
+ message: check.message,
1520
+ });
1521
+ status.dirty();
1522
+ }
1523
+ }
1405
1524
  else {
1406
1525
  util.assertNever(check);
1407
1526
  }
@@ -1448,9 +1567,22 @@ class ZodString extends ZodType {
1448
1567
  base64(message) {
1449
1568
  return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
1450
1569
  }
1570
+ base64url(message) {
1571
+ // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1572
+ return this._addCheck({
1573
+ kind: "base64url",
1574
+ ...errorUtil.errToObj(message),
1575
+ });
1576
+ }
1577
+ jwt(options) {
1578
+ return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
1579
+ }
1451
1580
  ip(options) {
1452
1581
  return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
1453
1582
  }
1583
+ cidr(options) {
1584
+ return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
1585
+ }
1454
1586
  datetime(options) {
1455
1587
  var _a, _b;
1456
1588
  if (typeof options === "string") {
@@ -1541,8 +1673,7 @@ class ZodString extends ZodType {
1541
1673
  });
1542
1674
  }
1543
1675
  /**
1544
- * @deprecated Use z.string().min(1) instead.
1545
- * @see {@link ZodString.min}
1676
+ * Equivalent to `.min(1)`
1546
1677
  */
1547
1678
  nonempty(message) {
1548
1679
  return this.min(1, errorUtil.errToObj(message));
@@ -1604,9 +1735,16 @@ class ZodString extends ZodType {
1604
1735
  get isIP() {
1605
1736
  return !!this._def.checks.find((ch) => ch.kind === "ip");
1606
1737
  }
1738
+ get isCIDR() {
1739
+ return !!this._def.checks.find((ch) => ch.kind === "cidr");
1740
+ }
1607
1741
  get isBase64() {
1608
1742
  return !!this._def.checks.find((ch) => ch.kind === "base64");
1609
1743
  }
1744
+ get isBase64url() {
1745
+ // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1746
+ return !!this._def.checks.find((ch) => ch.kind === "base64url");
1747
+ }
1610
1748
  get minLength() {
1611
1749
  let min = null;
1612
1750
  for (const ch of this._def.checks) {
@@ -1899,17 +2037,16 @@ class ZodBigInt extends ZodType {
1899
2037
  }
1900
2038
  _parse(input) {
1901
2039
  if (this._def.coerce) {
1902
- input.data = BigInt(input.data);
2040
+ try {
2041
+ input.data = BigInt(input.data);
2042
+ }
2043
+ catch (_a) {
2044
+ return this._getInvalidInput(input);
2045
+ }
1903
2046
  }
1904
2047
  const parsedType = this._getType(input);
1905
2048
  if (parsedType !== ZodParsedType.bigint) {
1906
- const ctx = this._getOrReturnCtx(input);
1907
- addIssueToContext(ctx, {
1908
- code: ZodIssueCode.invalid_type,
1909
- expected: ZodParsedType.bigint,
1910
- received: ctx.parsedType,
1911
- });
1912
- return INVALID;
2049
+ return this._getInvalidInput(input);
1913
2050
  }
1914
2051
  let ctx = undefined;
1915
2052
  const status = new ParseStatus();
@@ -1963,6 +2100,15 @@ class ZodBigInt extends ZodType {
1963
2100
  }
1964
2101
  return { status: status.value, value: input.data };
1965
2102
  }
2103
+ _getInvalidInput(input) {
2104
+ const ctx = this._getOrReturnCtx(input);
2105
+ addIssueToContext(ctx, {
2106
+ code: ZodIssueCode.invalid_type,
2107
+ expected: ZodParsedType.bigint,
2108
+ received: ctx.parsedType,
2109
+ });
2110
+ return INVALID;
2111
+ }
1966
2112
  gte(value, message) {
1967
2113
  return this.setLimit("min", value, true, errorUtil.toString(message));
1968
2114
  }
@@ -4169,7 +4315,23 @@ ZodReadonly.create = (type, params) => {
4169
4315
  ...processCreateParams(params),
4170
4316
  });
4171
4317
  };
4172
- function custom(check, params = {},
4318
+ ////////////////////////////////////////
4319
+ ////////////////////////////////////////
4320
+ ////////// //////////
4321
+ ////////// z.custom //////////
4322
+ ////////// //////////
4323
+ ////////////////////////////////////////
4324
+ ////////////////////////////////////////
4325
+ function cleanParams(params, data) {
4326
+ const p = typeof params === "function"
4327
+ ? params(data)
4328
+ : typeof params === "string"
4329
+ ? { message: params }
4330
+ : params;
4331
+ const p2 = typeof p === "string" ? { message: p } : p;
4332
+ return p2;
4333
+ }
4334
+ function custom(check, _params = {},
4173
4335
  /**
4174
4336
  * @deprecated
4175
4337
  *
@@ -4184,16 +4346,23 @@ fatal) {
4184
4346
  if (check)
4185
4347
  return ZodAny.create().superRefine((data, ctx) => {
4186
4348
  var _a, _b;
4187
- if (!check(data)) {
4188
- const p = typeof params === "function"
4189
- ? params(data)
4190
- : typeof params === "string"
4191
- ? { message: params }
4192
- : params;
4193
- const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4194
- const p2 = typeof p === "string" ? { message: p } : p;
4195
- ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
4349
+ const r = check(data);
4350
+ if (r instanceof Promise) {
4351
+ return r.then((r) => {
4352
+ var _a, _b;
4353
+ if (!r) {
4354
+ const params = cleanParams(_params, data);
4355
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4356
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4357
+ }
4358
+ });
4359
+ }
4360
+ if (!r) {
4361
+ const params = cleanParams(_params, data);
4362
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4363
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4196
4364
  }
4365
+ return;
4197
4366
  });
4198
4367
  return ZodAny.create();
4199
4368
  }