File size: 38,632 Bytes
31e8457 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 | import * as core from "../core/index.js";
import { util } from "../core/index.js";
import * as checks from "./checks.js";
import * as iso from "./iso.js";
import * as parse from "./parse.js";
export const ZodType = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) => {
core.$ZodType.init(inst, def);
inst.def = def;
inst.type = def.type;
Object.defineProperty(inst, "_def", { value: def });
// base methods
inst.check = (...checks) => {
return inst.clone({
...def,
checks: [
...(def.checks ?? []),
...checks.map((ch) => typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch),
],
}
// { parent: true }
);
};
inst.clone = (def, params) => core.clone(inst, def, params);
inst.brand = () => inst;
inst.register = ((reg, meta) => {
reg.add(inst, meta);
return inst;
});
// parsing
inst.parse = (data, params) => parse.parse(inst, data, params, { callee: inst.parse });
inst.safeParse = (data, params) => parse.safeParse(inst, data, params);
inst.parseAsync = async (data, params) => parse.parseAsync(inst, data, params, { callee: inst.parseAsync });
inst.safeParseAsync = async (data, params) => parse.safeParseAsync(inst, data, params);
inst.spa = inst.safeParseAsync;
// encoding/decoding
inst.encode = (data, params) => parse.encode(inst, data, params);
inst.decode = (data, params) => parse.decode(inst, data, params);
inst.encodeAsync = async (data, params) => parse.encodeAsync(inst, data, params);
inst.decodeAsync = async (data, params) => parse.decodeAsync(inst, data, params);
inst.safeEncode = (data, params) => parse.safeEncode(inst, data, params);
inst.safeDecode = (data, params) => parse.safeDecode(inst, data, params);
inst.safeEncodeAsync = async (data, params) => parse.safeEncodeAsync(inst, data, params);
inst.safeDecodeAsync = async (data, params) => parse.safeDecodeAsync(inst, data, params);
// refinements
inst.refine = (check, params) => inst.check(refine(check, params));
inst.superRefine = (refinement) => inst.check(superRefine(refinement));
inst.overwrite = (fn) => inst.check(checks.overwrite(fn));
// wrappers
inst.optional = () => optional(inst);
inst.nullable = () => nullable(inst);
inst.nullish = () => optional(nullable(inst));
inst.nonoptional = (params) => nonoptional(inst, params);
inst.array = () => array(inst);
inst.or = (arg) => union([inst, arg]);
inst.and = (arg) => intersection(inst, arg);
inst.transform = (tx) => pipe(inst, transform(tx));
inst.default = (def) => _default(inst, def);
inst.prefault = (def) => prefault(inst, def);
// inst.coalesce = (def, params) => coalesce(inst, def, params);
inst.catch = (params) => _catch(inst, params);
inst.pipe = (target) => pipe(inst, target);
inst.readonly = () => readonly(inst);
// meta
inst.describe = (description) => {
const cl = inst.clone();
core.globalRegistry.add(cl, { description });
return cl;
};
Object.defineProperty(inst, "description", {
get() {
return core.globalRegistry.get(inst)?.description;
},
configurable: true,
});
inst.meta = (...args) => {
if (args.length === 0) {
return core.globalRegistry.get(inst);
}
const cl = inst.clone();
core.globalRegistry.add(cl, args[0]);
return cl;
};
// helpers
inst.isOptional = () => inst.safeParse(undefined).success;
inst.isNullable = () => inst.safeParse(null).success;
return inst;
});
/** @internal */
export const _ZodString = /*@__PURE__*/ core.$constructor("_ZodString", (inst, def) => {
core.$ZodString.init(inst, def);
ZodType.init(inst, def);
const bag = inst._zod.bag;
inst.format = bag.format ?? null;
inst.minLength = bag.minimum ?? null;
inst.maxLength = bag.maximum ?? null;
// validations
inst.regex = (...args) => inst.check(checks.regex(...args));
inst.includes = (...args) => inst.check(checks.includes(...args));
inst.startsWith = (...args) => inst.check(checks.startsWith(...args));
inst.endsWith = (...args) => inst.check(checks.endsWith(...args));
inst.min = (...args) => inst.check(checks.minLength(...args));
inst.max = (...args) => inst.check(checks.maxLength(...args));
inst.length = (...args) => inst.check(checks.length(...args));
inst.nonempty = (...args) => inst.check(checks.minLength(1, ...args));
inst.lowercase = (params) => inst.check(checks.lowercase(params));
inst.uppercase = (params) => inst.check(checks.uppercase(params));
// transforms
inst.trim = () => inst.check(checks.trim());
inst.normalize = (...args) => inst.check(checks.normalize(...args));
inst.toLowerCase = () => inst.check(checks.toLowerCase());
inst.toUpperCase = () => inst.check(checks.toUpperCase());
});
export const ZodString = /*@__PURE__*/ core.$constructor("ZodString", (inst, def) => {
core.$ZodString.init(inst, def);
_ZodString.init(inst, def);
inst.email = (params) => inst.check(core._email(ZodEmail, params));
inst.url = (params) => inst.check(core._url(ZodURL, params));
inst.jwt = (params) => inst.check(core._jwt(ZodJWT, params));
inst.emoji = (params) => inst.check(core._emoji(ZodEmoji, params));
inst.guid = (params) => inst.check(core._guid(ZodGUID, params));
inst.uuid = (params) => inst.check(core._uuid(ZodUUID, params));
inst.uuidv4 = (params) => inst.check(core._uuidv4(ZodUUID, params));
inst.uuidv6 = (params) => inst.check(core._uuidv6(ZodUUID, params));
inst.uuidv7 = (params) => inst.check(core._uuidv7(ZodUUID, params));
inst.nanoid = (params) => inst.check(core._nanoid(ZodNanoID, params));
inst.guid = (params) => inst.check(core._guid(ZodGUID, params));
inst.cuid = (params) => inst.check(core._cuid(ZodCUID, params));
inst.cuid2 = (params) => inst.check(core._cuid2(ZodCUID2, params));
inst.ulid = (params) => inst.check(core._ulid(ZodULID, params));
inst.base64 = (params) => inst.check(core._base64(ZodBase64, params));
inst.base64url = (params) => inst.check(core._base64url(ZodBase64URL, params));
inst.xid = (params) => inst.check(core._xid(ZodXID, params));
inst.ksuid = (params) => inst.check(core._ksuid(ZodKSUID, params));
inst.ipv4 = (params) => inst.check(core._ipv4(ZodIPv4, params));
inst.ipv6 = (params) => inst.check(core._ipv6(ZodIPv6, params));
inst.cidrv4 = (params) => inst.check(core._cidrv4(ZodCIDRv4, params));
inst.cidrv6 = (params) => inst.check(core._cidrv6(ZodCIDRv6, params));
inst.e164 = (params) => inst.check(core._e164(ZodE164, params));
// iso
inst.datetime = (params) => inst.check(iso.datetime(params));
inst.date = (params) => inst.check(iso.date(params));
inst.time = (params) => inst.check(iso.time(params));
inst.duration = (params) => inst.check(iso.duration(params));
});
export function string(params) {
return core._string(ZodString, params);
}
export const ZodStringFormat = /*@__PURE__*/ core.$constructor("ZodStringFormat", (inst, def) => {
core.$ZodStringFormat.init(inst, def);
_ZodString.init(inst, def);
});
export const ZodEmail = /*@__PURE__*/ core.$constructor("ZodEmail", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodEmail.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function email(params) {
return core._email(ZodEmail, params);
}
export const ZodGUID = /*@__PURE__*/ core.$constructor("ZodGUID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodGUID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function guid(params) {
return core._guid(ZodGUID, params);
}
export const ZodUUID = /*@__PURE__*/ core.$constructor("ZodUUID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodUUID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function uuid(params) {
return core._uuid(ZodUUID, params);
}
export function uuidv4(params) {
return core._uuidv4(ZodUUID, params);
}
// ZodUUIDv6
export function uuidv6(params) {
return core._uuidv6(ZodUUID, params);
}
// ZodUUIDv7
export function uuidv7(params) {
return core._uuidv7(ZodUUID, params);
}
export const ZodURL = /*@__PURE__*/ core.$constructor("ZodURL", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodURL.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function url(params) {
return core._url(ZodURL, params);
}
export function httpUrl(params) {
return core._url(ZodURL, {
protocol: /^https?$/,
hostname: core.regexes.domain,
...util.normalizeParams(params),
});
}
export const ZodEmoji = /*@__PURE__*/ core.$constructor("ZodEmoji", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodEmoji.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function emoji(params) {
return core._emoji(ZodEmoji, params);
}
export const ZodNanoID = /*@__PURE__*/ core.$constructor("ZodNanoID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodNanoID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function nanoid(params) {
return core._nanoid(ZodNanoID, params);
}
export const ZodCUID = /*@__PURE__*/ core.$constructor("ZodCUID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodCUID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function cuid(params) {
return core._cuid(ZodCUID, params);
}
export const ZodCUID2 = /*@__PURE__*/ core.$constructor("ZodCUID2", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodCUID2.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function cuid2(params) {
return core._cuid2(ZodCUID2, params);
}
export const ZodULID = /*@__PURE__*/ core.$constructor("ZodULID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodULID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function ulid(params) {
return core._ulid(ZodULID, params);
}
export const ZodXID = /*@__PURE__*/ core.$constructor("ZodXID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodXID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function xid(params) {
return core._xid(ZodXID, params);
}
export const ZodKSUID = /*@__PURE__*/ core.$constructor("ZodKSUID", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodKSUID.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function ksuid(params) {
return core._ksuid(ZodKSUID, params);
}
export const ZodIPv4 = /*@__PURE__*/ core.$constructor("ZodIPv4", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodIPv4.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function ipv4(params) {
return core._ipv4(ZodIPv4, params);
}
export const ZodIPv6 = /*@__PURE__*/ core.$constructor("ZodIPv6", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodIPv6.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function ipv6(params) {
return core._ipv6(ZodIPv6, params);
}
export const ZodCIDRv4 = /*@__PURE__*/ core.$constructor("ZodCIDRv4", (inst, def) => {
core.$ZodCIDRv4.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function cidrv4(params) {
return core._cidrv4(ZodCIDRv4, params);
}
export const ZodCIDRv6 = /*@__PURE__*/ core.$constructor("ZodCIDRv6", (inst, def) => {
core.$ZodCIDRv6.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function cidrv6(params) {
return core._cidrv6(ZodCIDRv6, params);
}
export const ZodBase64 = /*@__PURE__*/ core.$constructor("ZodBase64", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodBase64.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function base64(params) {
return core._base64(ZodBase64, params);
}
export const ZodBase64URL = /*@__PURE__*/ core.$constructor("ZodBase64URL", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodBase64URL.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function base64url(params) {
return core._base64url(ZodBase64URL, params);
}
export const ZodE164 = /*@__PURE__*/ core.$constructor("ZodE164", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodE164.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function e164(params) {
return core._e164(ZodE164, params);
}
export const ZodJWT = /*@__PURE__*/ core.$constructor("ZodJWT", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodJWT.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function jwt(params) {
return core._jwt(ZodJWT, params);
}
export const ZodCustomStringFormat = /*@__PURE__*/ core.$constructor("ZodCustomStringFormat", (inst, def) => {
// ZodStringFormat.init(inst, def);
core.$ZodCustomStringFormat.init(inst, def);
ZodStringFormat.init(inst, def);
});
export function stringFormat(format, fnOrRegex, _params = {}) {
return core._stringFormat(ZodCustomStringFormat, format, fnOrRegex, _params);
}
export function hostname(_params) {
return core._stringFormat(ZodCustomStringFormat, "hostname", core.regexes.hostname, _params);
}
export function hex(_params) {
return core._stringFormat(ZodCustomStringFormat, "hex", core.regexes.hex, _params);
}
export function hash(alg, params) {
const enc = params?.enc ?? "hex";
const format = `${alg}_${enc}`;
const regex = core.regexes[format];
if (!regex)
throw new Error(`Unrecognized hash format: ${format}`);
return core._stringFormat(ZodCustomStringFormat, format, regex, params);
}
export const ZodNumber = /*@__PURE__*/ core.$constructor("ZodNumber", (inst, def) => {
core.$ZodNumber.init(inst, def);
ZodType.init(inst, def);
inst.gt = (value, params) => inst.check(checks.gt(value, params));
inst.gte = (value, params) => inst.check(checks.gte(value, params));
inst.min = (value, params) => inst.check(checks.gte(value, params));
inst.lt = (value, params) => inst.check(checks.lt(value, params));
inst.lte = (value, params) => inst.check(checks.lte(value, params));
inst.max = (value, params) => inst.check(checks.lte(value, params));
inst.int = (params) => inst.check(int(params));
inst.safe = (params) => inst.check(int(params));
inst.positive = (params) => inst.check(checks.gt(0, params));
inst.nonnegative = (params) => inst.check(checks.gte(0, params));
inst.negative = (params) => inst.check(checks.lt(0, params));
inst.nonpositive = (params) => inst.check(checks.lte(0, params));
inst.multipleOf = (value, params) => inst.check(checks.multipleOf(value, params));
inst.step = (value, params) => inst.check(checks.multipleOf(value, params));
// inst.finite = (params) => inst.check(core.finite(params));
inst.finite = () => inst;
const bag = inst._zod.bag;
inst.minValue =
Math.max(bag.minimum ?? Number.NEGATIVE_INFINITY, bag.exclusiveMinimum ?? Number.NEGATIVE_INFINITY) ?? null;
inst.maxValue =
Math.min(bag.maximum ?? Number.POSITIVE_INFINITY, bag.exclusiveMaximum ?? Number.POSITIVE_INFINITY) ?? null;
inst.isInt = (bag.format ?? "").includes("int") || Number.isSafeInteger(bag.multipleOf ?? 0.5);
inst.isFinite = true;
inst.format = bag.format ?? null;
});
export function number(params) {
return core._number(ZodNumber, params);
}
export const ZodNumberFormat = /*@__PURE__*/ core.$constructor("ZodNumberFormat", (inst, def) => {
core.$ZodNumberFormat.init(inst, def);
ZodNumber.init(inst, def);
});
export function int(params) {
return core._int(ZodNumberFormat, params);
}
export function float32(params) {
return core._float32(ZodNumberFormat, params);
}
export function float64(params) {
return core._float64(ZodNumberFormat, params);
}
export function int32(params) {
return core._int32(ZodNumberFormat, params);
}
export function uint32(params) {
return core._uint32(ZodNumberFormat, params);
}
export const ZodBoolean = /*@__PURE__*/ core.$constructor("ZodBoolean", (inst, def) => {
core.$ZodBoolean.init(inst, def);
ZodType.init(inst, def);
});
export function boolean(params) {
return core._boolean(ZodBoolean, params);
}
export const ZodBigInt = /*@__PURE__*/ core.$constructor("ZodBigInt", (inst, def) => {
core.$ZodBigInt.init(inst, def);
ZodType.init(inst, def);
inst.gte = (value, params) => inst.check(checks.gte(value, params));
inst.min = (value, params) => inst.check(checks.gte(value, params));
inst.gt = (value, params) => inst.check(checks.gt(value, params));
inst.gte = (value, params) => inst.check(checks.gte(value, params));
inst.min = (value, params) => inst.check(checks.gte(value, params));
inst.lt = (value, params) => inst.check(checks.lt(value, params));
inst.lte = (value, params) => inst.check(checks.lte(value, params));
inst.max = (value, params) => inst.check(checks.lte(value, params));
inst.positive = (params) => inst.check(checks.gt(BigInt(0), params));
inst.negative = (params) => inst.check(checks.lt(BigInt(0), params));
inst.nonpositive = (params) => inst.check(checks.lte(BigInt(0), params));
inst.nonnegative = (params) => inst.check(checks.gte(BigInt(0), params));
inst.multipleOf = (value, params) => inst.check(checks.multipleOf(value, params));
const bag = inst._zod.bag;
inst.minValue = bag.minimum ?? null;
inst.maxValue = bag.maximum ?? null;
inst.format = bag.format ?? null;
});
export function bigint(params) {
return core._bigint(ZodBigInt, params);
}
export const ZodBigIntFormat = /*@__PURE__*/ core.$constructor("ZodBigIntFormat", (inst, def) => {
core.$ZodBigIntFormat.init(inst, def);
ZodBigInt.init(inst, def);
});
// int64
export function int64(params) {
return core._int64(ZodBigIntFormat, params);
}
// uint64
export function uint64(params) {
return core._uint64(ZodBigIntFormat, params);
}
export const ZodSymbol = /*@__PURE__*/ core.$constructor("ZodSymbol", (inst, def) => {
core.$ZodSymbol.init(inst, def);
ZodType.init(inst, def);
});
export function symbol(params) {
return core._symbol(ZodSymbol, params);
}
export const ZodUndefined = /*@__PURE__*/ core.$constructor("ZodUndefined", (inst, def) => {
core.$ZodUndefined.init(inst, def);
ZodType.init(inst, def);
});
function _undefined(params) {
return core._undefined(ZodUndefined, params);
}
export { _undefined as undefined };
export const ZodNull = /*@__PURE__*/ core.$constructor("ZodNull", (inst, def) => {
core.$ZodNull.init(inst, def);
ZodType.init(inst, def);
});
function _null(params) {
return core._null(ZodNull, params);
}
export { _null as null };
export const ZodAny = /*@__PURE__*/ core.$constructor("ZodAny", (inst, def) => {
core.$ZodAny.init(inst, def);
ZodType.init(inst, def);
});
export function any() {
return core._any(ZodAny);
}
export const ZodUnknown = /*@__PURE__*/ core.$constructor("ZodUnknown", (inst, def) => {
core.$ZodUnknown.init(inst, def);
ZodType.init(inst, def);
});
export function unknown() {
return core._unknown(ZodUnknown);
}
export const ZodNever = /*@__PURE__*/ core.$constructor("ZodNever", (inst, def) => {
core.$ZodNever.init(inst, def);
ZodType.init(inst, def);
});
export function never(params) {
return core._never(ZodNever, params);
}
export const ZodVoid = /*@__PURE__*/ core.$constructor("ZodVoid", (inst, def) => {
core.$ZodVoid.init(inst, def);
ZodType.init(inst, def);
});
function _void(params) {
return core._void(ZodVoid, params);
}
export { _void as void };
export const ZodDate = /*@__PURE__*/ core.$constructor("ZodDate", (inst, def) => {
core.$ZodDate.init(inst, def);
ZodType.init(inst, def);
inst.min = (value, params) => inst.check(checks.gte(value, params));
inst.max = (value, params) => inst.check(checks.lte(value, params));
const c = inst._zod.bag;
inst.minDate = c.minimum ? new Date(c.minimum) : null;
inst.maxDate = c.maximum ? new Date(c.maximum) : null;
});
export function date(params) {
return core._date(ZodDate, params);
}
export const ZodArray = /*@__PURE__*/ core.$constructor("ZodArray", (inst, def) => {
core.$ZodArray.init(inst, def);
ZodType.init(inst, def);
inst.element = def.element;
inst.min = (minLength, params) => inst.check(checks.minLength(minLength, params));
inst.nonempty = (params) => inst.check(checks.minLength(1, params));
inst.max = (maxLength, params) => inst.check(checks.maxLength(maxLength, params));
inst.length = (len, params) => inst.check(checks.length(len, params));
inst.unwrap = () => inst.element;
});
export function array(element, params) {
return core._array(ZodArray, element, params);
}
// .keyof
export function keyof(schema) {
const shape = schema._zod.def.shape;
return _enum(Object.keys(shape));
}
export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
core.$ZodObjectJIT.init(inst, def);
ZodType.init(inst, def);
util.defineLazy(inst, "shape", () => def.shape);
inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall });
inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
inst.loose = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
inst.strict = () => inst.clone({ ...inst._zod.def, catchall: never() });
inst.strip = () => inst.clone({ ...inst._zod.def, catchall: undefined });
inst.extend = (incoming) => {
return util.extend(inst, incoming);
};
inst.safeExtend = (incoming) => {
return util.safeExtend(inst, incoming);
};
inst.merge = (other) => util.merge(inst, other);
inst.pick = (mask) => util.pick(inst, mask);
inst.omit = (mask) => util.omit(inst, mask);
inst.partial = (...args) => util.partial(ZodOptional, inst, args[0]);
inst.required = (...args) => util.required(ZodNonOptional, inst, args[0]);
});
export function object(shape, params) {
const def = {
type: "object",
get shape() {
util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
return this.shape;
},
...util.normalizeParams(params),
};
return new ZodObject(def);
}
// strictObject
export function strictObject(shape, params) {
return new ZodObject({
type: "object",
get shape() {
util.assignProp(this, "shape", util.objectClone(shape));
return this.shape;
},
catchall: never(),
...util.normalizeParams(params),
});
}
// looseObject
export function looseObject(shape, params) {
return new ZodObject({
type: "object",
get shape() {
util.assignProp(this, "shape", util.objectClone(shape));
return this.shape;
},
catchall: unknown(),
...util.normalizeParams(params),
});
}
export const ZodUnion = /*@__PURE__*/ core.$constructor("ZodUnion", (inst, def) => {
core.$ZodUnion.init(inst, def);
ZodType.init(inst, def);
inst.options = def.options;
});
export function union(options, params) {
return new ZodUnion({
type: "union",
options: options,
...util.normalizeParams(params),
});
}
export const ZodDiscriminatedUnion = /*@__PURE__*/ core.$constructor("ZodDiscriminatedUnion", (inst, def) => {
ZodUnion.init(inst, def);
core.$ZodDiscriminatedUnion.init(inst, def);
});
export function discriminatedUnion(discriminator, options, params) {
// const [options, params] = args;
return new ZodDiscriminatedUnion({
type: "union",
options,
discriminator,
...util.normalizeParams(params),
});
}
export const ZodIntersection = /*@__PURE__*/ core.$constructor("ZodIntersection", (inst, def) => {
core.$ZodIntersection.init(inst, def);
ZodType.init(inst, def);
});
export function intersection(left, right) {
return new ZodIntersection({
type: "intersection",
left: left,
right: right,
});
}
export const ZodTuple = /*@__PURE__*/ core.$constructor("ZodTuple", (inst, def) => {
core.$ZodTuple.init(inst, def);
ZodType.init(inst, def);
inst.rest = (rest) => inst.clone({
...inst._zod.def,
rest: rest,
});
});
export function tuple(items, _paramsOrRest, _params) {
const hasRest = _paramsOrRest instanceof core.$ZodType;
const params = hasRest ? _params : _paramsOrRest;
const rest = hasRest ? _paramsOrRest : null;
return new ZodTuple({
type: "tuple",
items: items,
rest,
...util.normalizeParams(params),
});
}
export const ZodRecord = /*@__PURE__*/ core.$constructor("ZodRecord", (inst, def) => {
core.$ZodRecord.init(inst, def);
ZodType.init(inst, def);
inst.keyType = def.keyType;
inst.valueType = def.valueType;
});
export function record(keyType, valueType, params) {
return new ZodRecord({
type: "record",
keyType,
valueType: valueType,
...util.normalizeParams(params),
});
}
// type alksjf = core.output<core.$ZodRecordKey>;
export function partialRecord(keyType, valueType, params) {
const k = core.clone(keyType);
k._zod.values = undefined;
return new ZodRecord({
type: "record",
keyType: k,
valueType: valueType,
...util.normalizeParams(params),
});
}
export const ZodMap = /*@__PURE__*/ core.$constructor("ZodMap", (inst, def) => {
core.$ZodMap.init(inst, def);
ZodType.init(inst, def);
inst.keyType = def.keyType;
inst.valueType = def.valueType;
});
export function map(keyType, valueType, params) {
return new ZodMap({
type: "map",
keyType: keyType,
valueType: valueType,
...util.normalizeParams(params),
});
}
export const ZodSet = /*@__PURE__*/ core.$constructor("ZodSet", (inst, def) => {
core.$ZodSet.init(inst, def);
ZodType.init(inst, def);
inst.min = (...args) => inst.check(core._minSize(...args));
inst.nonempty = (params) => inst.check(core._minSize(1, params));
inst.max = (...args) => inst.check(core._maxSize(...args));
inst.size = (...args) => inst.check(core._size(...args));
});
export function set(valueType, params) {
return new ZodSet({
type: "set",
valueType: valueType,
...util.normalizeParams(params),
});
}
export const ZodEnum = /*@__PURE__*/ core.$constructor("ZodEnum", (inst, def) => {
core.$ZodEnum.init(inst, def);
ZodType.init(inst, def);
inst.enum = def.entries;
inst.options = Object.values(def.entries);
const keys = new Set(Object.keys(def.entries));
inst.extract = (values, params) => {
const newEntries = {};
for (const value of values) {
if (keys.has(value)) {
newEntries[value] = def.entries[value];
}
else
throw new Error(`Key ${value} not found in enum`);
}
return new ZodEnum({
...def,
checks: [],
...util.normalizeParams(params),
entries: newEntries,
});
};
inst.exclude = (values, params) => {
const newEntries = { ...def.entries };
for (const value of values) {
if (keys.has(value)) {
delete newEntries[value];
}
else
throw new Error(`Key ${value} not found in enum`);
}
return new ZodEnum({
...def,
checks: [],
...util.normalizeParams(params),
entries: newEntries,
});
};
});
function _enum(values, params) {
const entries = Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values;
return new ZodEnum({
type: "enum",
entries,
...util.normalizeParams(params),
});
}
export { _enum as enum };
/** @deprecated This API has been merged into `z.enum()`. Use `z.enum()` instead.
*
* ```ts
* enum Colors { red, green, blue }
* z.enum(Colors);
* ```
*/
export function nativeEnum(entries, params) {
return new ZodEnum({
type: "enum",
entries,
...util.normalizeParams(params),
});
}
export const ZodLiteral = /*@__PURE__*/ core.$constructor("ZodLiteral", (inst, def) => {
core.$ZodLiteral.init(inst, def);
ZodType.init(inst, def);
inst.values = new Set(def.values);
Object.defineProperty(inst, "value", {
get() {
if (def.values.length > 1) {
throw new Error("This schema contains multiple valid literal values. Use `.values` instead.");
}
return def.values[0];
},
});
});
export function literal(value, params) {
return new ZodLiteral({
type: "literal",
values: Array.isArray(value) ? value : [value],
...util.normalizeParams(params),
});
}
export const ZodFile = /*@__PURE__*/ core.$constructor("ZodFile", (inst, def) => {
core.$ZodFile.init(inst, def);
ZodType.init(inst, def);
inst.min = (size, params) => inst.check(core._minSize(size, params));
inst.max = (size, params) => inst.check(core._maxSize(size, params));
inst.mime = (types, params) => inst.check(core._mime(Array.isArray(types) ? types : [types], params));
});
export function file(params) {
return core._file(ZodFile, params);
}
export const ZodTransform = /*@__PURE__*/ core.$constructor("ZodTransform", (inst, def) => {
core.$ZodTransform.init(inst, def);
ZodType.init(inst, def);
inst._zod.parse = (payload, _ctx) => {
if (_ctx.direction === "backward") {
throw new core.$ZodEncodeError(inst.constructor.name);
}
payload.addIssue = (issue) => {
if (typeof issue === "string") {
payload.issues.push(util.issue(issue, payload.value, def));
}
else {
// for Zod 3 backwards compatibility
const _issue = issue;
if (_issue.fatal)
_issue.continue = false;
_issue.code ?? (_issue.code = "custom");
_issue.input ?? (_issue.input = payload.value);
_issue.inst ?? (_issue.inst = inst);
// _issue.continue ??= true;
payload.issues.push(util.issue(_issue));
}
};
const output = def.transform(payload.value, payload);
if (output instanceof Promise) {
return output.then((output) => {
payload.value = output;
return payload;
});
}
payload.value = output;
return payload;
};
});
export function transform(fn) {
return new ZodTransform({
type: "transform",
transform: fn,
});
}
export const ZodOptional = /*@__PURE__*/ core.$constructor("ZodOptional", (inst, def) => {
core.$ZodOptional.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function optional(innerType) {
return new ZodOptional({
type: "optional",
innerType: innerType,
});
}
export const ZodNullable = /*@__PURE__*/ core.$constructor("ZodNullable", (inst, def) => {
core.$ZodNullable.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function nullable(innerType) {
return new ZodNullable({
type: "nullable",
innerType: innerType,
});
}
// nullish
export function nullish(innerType) {
return optional(nullable(innerType));
}
export const ZodDefault = /*@__PURE__*/ core.$constructor("ZodDefault", (inst, def) => {
core.$ZodDefault.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
inst.removeDefault = inst.unwrap;
});
export function _default(innerType, defaultValue) {
return new ZodDefault({
type: "default",
innerType: innerType,
get defaultValue() {
return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
},
});
}
export const ZodPrefault = /*@__PURE__*/ core.$constructor("ZodPrefault", (inst, def) => {
core.$ZodPrefault.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function prefault(innerType, defaultValue) {
return new ZodPrefault({
type: "prefault",
innerType: innerType,
get defaultValue() {
return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
},
});
}
export const ZodNonOptional = /*@__PURE__*/ core.$constructor("ZodNonOptional", (inst, def) => {
core.$ZodNonOptional.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function nonoptional(innerType, params) {
return new ZodNonOptional({
type: "nonoptional",
innerType: innerType,
...util.normalizeParams(params),
});
}
export const ZodSuccess = /*@__PURE__*/ core.$constructor("ZodSuccess", (inst, def) => {
core.$ZodSuccess.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function success(innerType) {
return new ZodSuccess({
type: "success",
innerType: innerType,
});
}
export const ZodCatch = /*@__PURE__*/ core.$constructor("ZodCatch", (inst, def) => {
core.$ZodCatch.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
inst.removeCatch = inst.unwrap;
});
function _catch(innerType, catchValue) {
return new ZodCatch({
type: "catch",
innerType: innerType,
catchValue: (typeof catchValue === "function" ? catchValue : () => catchValue),
});
}
export { _catch as catch };
export const ZodNaN = /*@__PURE__*/ core.$constructor("ZodNaN", (inst, def) => {
core.$ZodNaN.init(inst, def);
ZodType.init(inst, def);
});
export function nan(params) {
return core._nan(ZodNaN, params);
}
export const ZodPipe = /*@__PURE__*/ core.$constructor("ZodPipe", (inst, def) => {
core.$ZodPipe.init(inst, def);
ZodType.init(inst, def);
inst.in = def.in;
inst.out = def.out;
});
export function pipe(in_, out) {
return new ZodPipe({
type: "pipe",
in: in_,
out: out,
// ...util.normalizeParams(params),
});
}
export const ZodCodec = /*@__PURE__*/ core.$constructor("ZodCodec", (inst, def) => {
ZodPipe.init(inst, def);
core.$ZodCodec.init(inst, def);
});
export function codec(in_, out, params) {
return new ZodCodec({
type: "pipe",
in: in_,
out: out,
transform: params.decode,
reverseTransform: params.encode,
});
}
export const ZodReadonly = /*@__PURE__*/ core.$constructor("ZodReadonly", (inst, def) => {
core.$ZodReadonly.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function readonly(innerType) {
return new ZodReadonly({
type: "readonly",
innerType: innerType,
});
}
export const ZodTemplateLiteral = /*@__PURE__*/ core.$constructor("ZodTemplateLiteral", (inst, def) => {
core.$ZodTemplateLiteral.init(inst, def);
ZodType.init(inst, def);
});
export function templateLiteral(parts, params) {
return new ZodTemplateLiteral({
type: "template_literal",
parts,
...util.normalizeParams(params),
});
}
export const ZodLazy = /*@__PURE__*/ core.$constructor("ZodLazy", (inst, def) => {
core.$ZodLazy.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.getter();
});
export function lazy(getter) {
return new ZodLazy({
type: "lazy",
getter: getter,
});
}
export const ZodPromise = /*@__PURE__*/ core.$constructor("ZodPromise", (inst, def) => {
core.$ZodPromise.init(inst, def);
ZodType.init(inst, def);
inst.unwrap = () => inst._zod.def.innerType;
});
export function promise(innerType) {
return new ZodPromise({
type: "promise",
innerType: innerType,
});
}
export const ZodFunction = /*@__PURE__*/ core.$constructor("ZodFunction", (inst, def) => {
core.$ZodFunction.init(inst, def);
ZodType.init(inst, def);
});
export function _function(params) {
return new ZodFunction({
type: "function",
input: Array.isArray(params?.input) ? tuple(params?.input) : (params?.input ?? array(unknown())),
output: params?.output ?? unknown(),
});
}
export { _function as function };
export const ZodCustom = /*@__PURE__*/ core.$constructor("ZodCustom", (inst, def) => {
core.$ZodCustom.init(inst, def);
ZodType.init(inst, def);
});
// custom checks
export function check(fn) {
const ch = new core.$ZodCheck({
check: "custom",
// ...util.normalizeParams(params),
});
ch._zod.check = fn;
return ch;
}
export function custom(fn, _params) {
return core._custom(ZodCustom, fn ?? (() => true), _params);
}
export function refine(fn, _params = {}) {
return core._refine(ZodCustom, fn, _params);
}
// superRefine
export function superRefine(fn) {
return core._superRefine(fn);
}
function _instanceof(cls, params = {
error: `Input not instance of ${cls.name}`,
}) {
const inst = new ZodCustom({
type: "custom",
check: "custom",
fn: (data) => data instanceof cls,
abort: true,
...util.normalizeParams(params),
});
inst._zod.bag.Class = cls;
return inst;
}
export { _instanceof as instanceof };
// stringbool
export const stringbool = (...args) => core._stringbool({
Codec: ZodCodec,
Boolean: ZodBoolean,
String: ZodString,
}, ...args);
export function json(params) {
const jsonSchema = lazy(() => {
return union([string(params), number(), boolean(), _null(), array(jsonSchema), record(string(), jsonSchema)]);
});
return jsonSchema;
}
// preprocess
// /** @deprecated Use `z.pipe()` and `z.transform()` instead. */
export function preprocess(fn, schema) {
return pipe(transform(fn), schema);
}
|