schemas.js 35 KB

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